from timeside.core import Processor, implements, interfacedoc
from timeside.api import IDecoder
-from timeside.encoder.gstutils import *
+from timeside.gstutils import *
import Queue
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
-# Copyright (c) 2007-2009 Guillaume Pellerin <yomguy@parisson.com>
+# Copyright (c) 2012 Paul Brossier <piem@piem.org>
# This file is part of TimeSide.
# You should have received a copy of the GNU General Public License
# along with TimeSide. If not, see <http://www.gnu.org/licenses/>.
-# Author: Guillaume Pellerin <yomguy@parisson.com>
+
+from timeside.core import Processor, implements, interfacedoc
+from timeside.api import IEncoder
+from timeside.gstutils import *
+
+
+class GstEncoder(Processor):
+
+ def release(self):
+ while self.bus.have_pending():
+ self.bus.pop()
+
+ def __del__(self):
+ self.release()
+
+ def start_pipeline(self, channels, samplerate):
+ self.pipeline = gst.parse_launch(self.pipe)
+ # store a pointer to appsrc in our encoder object
+ self.src = self.pipeline.get_by_name('src')
+ # store a pointer to appsink in our encoder object
+ self.app = self.pipeline.get_by_name('app')
+
+ srccaps = gst.Caps("""audio/x-raw-float,
+ endianness=(int)1234,
+ channels=(int)%s,
+ width=(int)32,
+ rate=(int)%d""" % (int(channels), int(samplerate)))
+ self.src.set_property("caps", srccaps)
+ self.src.set_property('emit-signals', True)
+
+ self.bus = self.pipeline.get_bus()
+ self.bus.add_signal_watch()
+ self.bus.connect("message", self.on_message)
+
+ # start pipeline
+ self.pipeline.set_state(gst.STATE_PLAYING)
+
+ def on_message(self, bus, message):
+ t = message.type
+ if t == gst.MESSAGE_EOS:
+ self.pipeline.set_state(gst.STATE_NULL)
+ elif t == gst.MESSAGE_ERROR:
+ self.pipeline.set_state(gst.STATE_NULL)
+ err, debug = message.parse_error()
+ print "Error: %s" % err, debug
+
+ def process(self, frames, eod=False):
+ self.eod = eod
+ buf = numpy_array_to_gst_buffer(frames)
+ self.src.emit('push-buffer', buf)
+ if self.eod:
+ self.src.emit('end-of-stream')
+ if self.streaming:
+ self.chunk = self.app.emit('pull-buffer')
+ return frames, eod
from timeside.core import *
import subprocess
from timeside.core import Processor, implements, interfacedoc
from timeside.api import IEncoder
-from timeside.encoder.gstutils import *
+from timeside.gstutils import *
+
class FlacEncoder(GstEncoder):
""" gstreamer-based FLAC encoder """
else:
self.filename = None
self.streaming = streaming
-
+
if not self.filename and not self.streaming:
raise Exception('Must give an output')
-
+
self.eod = False
@interfacedoc
def setup(self, channels=None, samplerate=None, nframes=None):
super(FlacEncoder, self).setup(channels, samplerate, nframes)
# TODO open file for writing
- # the output data format we want
- self.pipe = ''' appsrc name=src ! audioconvert
+ # the output data format we want
+ self.pipe = ''' appsrc name=src ! audioconvert
! flacenc '''
-
- if self.filename and self.streaming:
+
+ if self.filename and self.streaming:
self.pipe += ''' ! tee name=t
! queue ! filesink location=%s
t. ! queue ! appsink name=app sync=False
''' % self.filename
-
+
elif self.filename :
self.pipe += '! filesink location=%s ' % self.filename
else:
self.pipe += '! appsink name=app sync=False '
-
+
self.start_pipeline(channels, samplerate)
@staticmethod
+++ /dev/null
-from timeside.core import Processor, implements, interfacedoc
-from timeside.api import IEncoder
-
-from numpy import array, getbuffer, frombuffer
-
-import pygst
-pygst.require('0.10')
-import gst
-import gobject
-
-gobject.threads_init()
-
-def numpy_array_to_gst_buffer(frames):
- """ gstreamer buffer to numpy array conversion """
- buf = gst.Buffer(getbuffer(frames.astype("float32")))
- return buf
-
-def gst_buffer_to_numpy_array(buf, chan):
- """ gstreamer buffer to numpy array conversion """
- samples = frombuffer(buf.data, dtype='float32')
- samples.resize([len(samples)/chan, chan])
- return samples
-
-class GstEncoder(Processor):
-
- def release(self):
- while self.bus.have_pending():
- self.bus.pop()
-
- def __del__(self):
- self.release()
-
- def start_pipeline(self, channels, samplerate):
- self.pipeline = gst.parse_launch(self.pipe)
- # store a pointer to appsrc in our encoder object
- self.src = self.pipeline.get_by_name('src')
- # store a pointer to appsink in our encoder object
- self.app = self.pipeline.get_by_name('app')
-
- srccaps = gst.Caps("""audio/x-raw-float,
- endianness=(int)1234,
- channels=(int)%s,
- width=(int)32,
- rate=(int)%d""" % (int(channels), int(samplerate)))
- self.src.set_property("caps", srccaps)
- self.src.set_property('emit-signals', True)
-
- self.bus = self.pipeline.get_bus()
- self.bus.add_signal_watch()
- self.bus.connect("message", self.on_message)
-
- # start pipeline
- self.pipeline.set_state(gst.STATE_PLAYING)
-
- def on_message(self, bus, message):
- t = message.type
- if t == gst.MESSAGE_EOS:
- self.pipeline.set_state(gst.STATE_NULL)
- elif t == gst.MESSAGE_ERROR:
- self.pipeline.set_state(gst.STATE_NULL)
- err, debug = message.parse_error()
- print "Error: %s" % err, debug
-
- def process(self, frames, eod=False):
- self.eod = eod
- buf = numpy_array_to_gst_buffer(frames)
- self.src.emit('push-buffer', buf)
- if self.eod:
- self.src.emit('end-of-stream')
- if self.streaming:
- self.chunk = self.app.emit('pull-buffer')
- return frames, eod
from timeside.core import Processor, implements, interfacedoc
from timeside.api import IEncoder
-from timeside.encoder.gstutils import *
+from timeside.gstutils import *
+
class AacEncoder(GstEncoder):
""" gstreamer-based AAC encoder """
from timeside.core import Processor, implements, interfacedoc
from timeside.api import IEncoder
-from timeside.encoder.gstutils import *
+from timeside.gstutils import *
import mutagen
self.pipe += '! queue ! appsink name=app sync=False '
self.start_pipeline(channels, samplerate)
-
+
@staticmethod
@interfacedoc
def id():
from timeside.core import Processor, implements, interfacedoc
from timeside.api import IEncoder
-from timeside.encoder.gstutils import *
+from timeside.gstutils import *
class VorbisEncoder(GstEncoder):
""" gstreamer-based vorbis encoder """
else:
self.filename = None
self.streaming = streaming
-
+
if not self.filename and not self.streaming:
raise Exception('Must give an output')
-
+
self.eod = False
@interfacedoc
def setup(self, channels=None, samplerate=None, nframes=None):
super(VorbisEncoder, self).setup(channels, samplerate, nframes)
# TODO open file for writing
- # the output data format we want
+ # the output data format we want
self.pipe = ''' appsrc name=src
- ! audioconvert
+ ! audioconvert
! vorbisenc
! oggmux
'''
! queue ! filesink location=%s
t. ! queue ! appsink name=app sync=False
''' % self.filename
-
+
elif self.filename :
self.pipe += '! filesink async=True location=%s ' % self.filename
else:
self.pipe += '! appsink name=app sync=False '
-
+
# start pipeline
self.start_pipeline(channels, samplerate)
from timeside.core import Processor, implements, interfacedoc
from timeside.api import IEncoder
-from timeside.encoder.gstutils import *
+from timeside.gstutils import *
+
class WavEncoder(GstEncoder):
""" gstreamer-based encoder """
else:
self.filename = None
self.streaming = streaming
-
+
if not self.filename and not self.streaming:
raise Exception('Must give an output')
self.eod = False
-
+
@interfacedoc
def setup(self, channels=None, samplerate=None, nframes=None):
super(WavEncoder, self).setup(channels, samplerate, nframes)
# TODO open file for writing
# the output data format we want
self.pipe = ''' appsrc name=src
- ! audioconvert
+ ! audioconvert
! wavenc
'''
if self.filename and self.streaming:
! queue ! filesink location=%s
t. ! queue ! appsink name=app sync=False
''' % self.filename
-
+
elif self.filename :
self.pipe += '! filesink location=%s ' % self.filename
else:
self.pipe += '! appsink name=app sync=False'
-
+
# start pipeline
self.start_pipeline(channels, samplerate)
-
+
@staticmethod
@interfacedoc
def id():
from timeside.core import Processor, implements, interfacedoc
from timeside.api import IEncoder
-from timeside.encoder.gstutils import *
+from timeside.gstutils import *
+
class WebMEncoder(GstEncoder):
""" gstreamer-based webm encoder and muxer """
--- /dev/null
+from timeside.core import Processor, implements, interfacedoc
+from timeside.api import IEncoder
+
+from numpy import array, getbuffer, frombuffer
+
+import pygst
+pygst.require('0.10')
+import gst
+import gobject
+
+gobject.threads_init()
+
+def numpy_array_to_gst_buffer(frames):
+ """ gstreamer buffer to numpy array conversion """
+ buf = gst.Buffer(getbuffer(frames.astype("float32")))
+ return buf
+
+def gst_buffer_to_numpy_array(buf, chan):
+ """ gstreamer buffer to numpy array conversion """
+ samples = frombuffer(buf.data, dtype='float32')
+ samples.resize([len(samples)/chan, chan])
+ return samples
+
+class GstEncoder(Processor):
+
+ def release(self):
+ while self.bus.have_pending():
+ self.bus.pop()
+
+ def __del__(self):
+ self.release()
+
+ def start_pipeline(self, channels, samplerate):
+ self.pipeline = gst.parse_launch(self.pipe)
+ # store a pointer to appsrc in our encoder object
+ self.src = self.pipeline.get_by_name('src')
+ # store a pointer to appsink in our encoder object
+ self.app = self.pipeline.get_by_name('app')
+
+ srccaps = gst.Caps("""audio/x-raw-float,
+ endianness=(int)1234,
+ channels=(int)%s,
+ width=(int)32,
+ rate=(int)%d""" % (int(channels), int(samplerate)))
+ self.src.set_property("caps", srccaps)
+ self.src.set_property('emit-signals', True)
+
+ self.bus = self.pipeline.get_bus()
+ self.bus.add_signal_watch()
+ self.bus.connect("message", self.on_message)
+
+ # start pipeline
+ self.pipeline.set_state(gst.STATE_PLAYING)
+
+ def on_message(self, bus, message):
+ t = message.type
+ if t == gst.MESSAGE_EOS:
+ self.pipeline.set_state(gst.STATE_NULL)
+ elif t == gst.MESSAGE_ERROR:
+ self.pipeline.set_state(gst.STATE_NULL)
+ err, debug = message.parse_error()
+ print "Error: %s" % err, debug
+
+ def process(self, frames, eod=False):
+ self.eod = eod
+ buf = numpy_array_to_gst_buffer(frames)
+ self.src.emit('push-buffer', buf)
+ if self.eod:
+ self.src.emit('end-of-stream')
+ if self.streaming:
+ self.chunk = self.app.emit('pull-buffer')
+ return frames, eod