fenrir/src/fenrir-package/sound/gstreamer.py

102 lines
3.2 KiB
Python
Raw Normal View History

2016-07-22 11:02:34 -04:00
import gi
2016-07-28 17:52:20 -04:00
import time
2016-07-22 11:02:34 -04:00
from gi.repository import GLib
try:
gi.require_version('Gst', '1.0')
from gi.repository import Gst
except:
_gstreamerAvailable = False
else:
_gstreamerAvailable, args = Gst.init_check(None)
class sound:
def __init__(self):
self._initialized = False
self._source = None
self._sink = None
if not _gstreamerAvailable:
return
self.init()
2016-07-28 17:52:20 -04:00
2016-07-22 11:02:34 -04:00
def _onPlayerMessage(self, bus, message):
if message.type == Gst.MessageType.EOS:
self._player.set_state(Gst.State.NULL)
elif message.type == Gst.MessageType.ERROR:
self._player.set_state(Gst.State.NULL)
error, info = message.parse_error()
2016-07-28 17:52:20 -04:00
print(error, info)
2016-08-06 08:12:29 -04:00
print('_onPlayerMessage')
2016-07-22 11:02:34 -04:00
def _onPipelineMessage(self, bus, message):
if message.type == Gst.MessageType.EOS:
self._pipeline.set_state(Gst.State.NULL)
elif message.type == Gst.MessageType.ERROR:
self._pipeline.set_state(Gst.State.NULL)
error, info = message.parse_error()
2016-07-28 17:52:20 -04:00
print(error, info)
2016-08-06 08:12:29 -04:00
print('_onPipelineMessage')
2016-07-28 17:52:20 -04:00
2016-07-22 11:02:34 -04:00
def _onTimeout(self, element):
element.set_state(Gst.State.NULL)
return False
2016-07-26 10:06:34 -04:00
def playSoundFile(self, fileName, interrupt=True):
2016-07-22 11:02:34 -04:00
if interrupt:
2016-07-28 17:52:20 -04:00
self.cancel()
2016-07-26 10:06:34 -04:00
self._player.set_property('uri', 'file://%s' % fileName)
2016-07-22 11:02:34 -04:00
self._player.set_state(Gst.State.PLAYING)
2016-08-06 08:12:29 -04:00
print('playSoundFile')
2016-07-26 10:06:34 -04:00
def playFrequence(self, frequence, duration, adjustVolume, interrupt=True):
2016-07-22 11:02:34 -04:00
if interrupt:
2016-07-28 17:52:20 -04:00
self.cancel()
2016-07-22 11:02:34 -04:00
self._source.set_property('volume', tone.volume)
self._source.set_property('freq', tone.frequency)
self._source.set_property('wave', tone.wave)
self._pipeline.set_state(Gst.State.PLAYING)
duration = int(1000 * tone.duration)
GLib.timeout_add(duration, self._onTimeout, self._pipeline)
def init(self):
if self._initialized:
return
if not _gstreamerAvailable:
return
self._player = Gst.ElementFactory.make('playbin', 'player')
bus = self._player.get_bus()
bus.add_signal_watch()
bus.connect("message", self._onPlayerMessage)
2016-07-26 10:06:34 -04:00
self._pipeline = Gst.Pipeline(name='fenrir-pipeline')
2016-07-22 11:02:34 -04:00
bus = self._pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", self._onPipelineMessage)
self._source = Gst.ElementFactory.make('audiotestsrc', 'src')
self._sink = Gst.ElementFactory.make('autoaudiosink', 'output')
self._pipeline.add(self._source)
self._pipeline.add(self._sink)
self._source.link(self._sink)
self._initialized = True
2016-07-26 10:15:39 -04:00
def cancel(self, element=None):
2016-07-22 11:02:34 -04:00
if not _gstreamerAvailable:
return
if element:
element.set_state(Gst.State.NULL)
return
self._player.set_state(Gst.State.NULL)
self._pipeline.set_state(Gst.State.NULL)
def shutdown(self):
global _gstreamerAvailable
if not _gstreamerAvailable:
return
2016-07-28 17:52:20 -04:00
self.cancel()
2016-07-22 11:02:34 -04:00
self._initialized = False
_gstreamerAvailable = False