]> git.parisson.com Git - timeside.git/commitdiff
trying to get appsink and filesink orking together with no luck...
authoryomguy <yomguy@parisson.com>
Tue, 15 Mar 2011 15:12:48 +0000 (15:12 +0000)
committeryomguy <yomguy@parisson.com>
Tue, 15 Mar 2011 15:12:48 +0000 (15:12 +0000)
timeside/encoder/flac.py
timeside/encoder/mp3.py
timeside/encoder/ogg.py
timeside/encoder/wav.py
timeside/tests/api/test_lolevel_streaming.py

index 632e5457043df4a8b1d70eca3e4255c84ba85221..dce1caa702c118c64037bcd319357ba627ea5a6f 100644 (file)
@@ -48,27 +48,25 @@ class FlacEncoder(Processor):
         super(FlacEncoder, self).setup(channels, samplerate, nframes)
         # TODO open file for writing
         # the output data format we want        
-        pipe = ''' appsrc name=src max-bytes=32768 block=true
-                  ! audioconvert 
-                  ! flacenc
-                  '''
-        if self.filename and self.streaming:
-            pipe += '''
-            ! queue2 name=q0 ! tee name=tee
-            tee. ! queue name=q1 ! appsink name=app sync=false
-            tee. ! queue name=q2 ! filesink location=%s
-            ''' % self.filename
+        self.pipe = ''' appsrc name=src ! audioconvert 
+                        ! flacenc '''
+                        
+        if self.filename and self.streaming:            
+            self.pipe += '''
+            ! tee name=t
+            ! queue ! appsink name=sink sync=False
+            t. ! queue ! filesink location=%s ''' % self.filename
             
         elif self.filename :
-            pipe += '! filesink location=%s ' % self.filename
+            self.pipe += '! filesink location=%s ' % self.filename
         else:
-            pipe += '! appsink name=app sync=false '
+            self.pipe += '! appsink name=sink blocksize=16384 sync=False '
             
-        self.pipeline = gst.parse_launch(pipe)
+        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')
+        self.sink = self.pipeline.get_by_name('sink')
         
         srccaps = gst.Caps("""audio/x-raw-float,
             endianness=(int)1234,
@@ -115,8 +113,8 @@ class FlacEncoder(Processor):
         buf = self.numpy_array_to_gst_buffer(frames)
         self.src.emit('push-buffer', buf)
         if self.streaming:
-            pull = self.app.emit('pull-buffer')
-        if eod: self.src.emit('end-of-stream')
+            pull = self.sink.emit('pull-buffer')
+#        if eod: self.src.emit('end-of-stream')
         if not self.streaming:
             return frames, eod
         else:
index 78e0b6b1a9382d237ad22baad9cf7b42cc697187..bbe97a6fe7b4af7e58c5d4909fa3f1aa1b884ff7 100644 (file)
@@ -24,7 +24,9 @@
 from timeside.core import Processor, implements, interfacedoc
 from timeside.api import IEncoder
 from numpy import array, frombuffer, getbuffer, float32
+from timeside.core import FixedSizeInputAdapter
 
+import numpy
 import pygst
 pygst.require('0.10')
 import gst
@@ -36,35 +38,39 @@ class Mp3Encoder(Processor):
     """ gstreamer-based mp3 encoder """
     implements(IEncoder)
 
-    def __init__(self, output,  streaming=False):
+    def __init__(self, output, streaming=False):
         if isinstance(output, basestring):
             self.filename = output
         else:
             self.filename = None
+
         self.streaming = streaming
         if not self.filename and not self.streaming:
             raise Exception('Must give an output')
-
+        
+        self.max_bytes = 65536
+        self.size_adapter = self.max_bytes
+        
     @interfacedoc
     def setup(self, channels=None, samplerate=None, nframes=None):
+        self.channels = channels
         super(Mp3Encoder, self).setup(channels, samplerate, nframes)
         #TODO: open file for writing
         # the output data format we want        
-        self.pipe = ''' appsrc name=src max-bytes=32768 block=true
+        self.pipe = '''appsrc name=src max-bytes=%s
                   ! audioconvert 
-                  ! lamemp3enc bitrate=192 quality=2 ! id3v2mux
-                  '''
+                  ! lamemp3enc bitrate=256 ! id3v2mux
+                  ''' % self.max_bytes
         if self.filename and self.streaming:
-            self.pipe += '''
-            ! queue ! tee name=t 
-            t. ! queue ! appsink name=app sync=false
-            t. ! queue ! filesink sync=false location=%s
+            self.pipe += ''' ! queue ! 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 '
+            self.pipe += '! queue ! appsink name=app sync=False '
             
         self.pipeline = gst.parse_launch(self.pipe)
         # store a pointer to appsrc in our encoder object
@@ -80,8 +86,11 @@ class Mp3Encoder(Processor):
         self.src.set_property("caps", srccaps)
 
         # start pipeline
+        if self.streaming:
+            self.app.set_property('emit-signals', True)
         self.pipeline.set_state(gst.STATE_PLAYING)
-
+        self.adapter = FixedSizeInputAdapter(self.size_adapter, channels, pad=True)
+        
     @staticmethod
     @interfacedoc
     def id():
@@ -114,13 +123,20 @@ class Mp3Encoder(Processor):
 
     @interfacedoc
     def process(self, frames, eod=False):
-        buf = self.numpy_array_to_gst_buffer(frames)
-        self.src.emit('push-buffer', buf)
+        print frames.shape
+        emit = 0
+        for samples, end in self.adapter.process(frames, eod):
+            print 'push: ', samples.shape
+            buf = self.numpy_array_to_gst_buffer(samples)
+            self.src.emit('push-buffer', buf)
+            if self.streaming:
+                pull = self.app.emit('pull-buffer')
+                emit = 1
         if self.streaming:
-            pull = self.app.emit('pull-buffer')
-#        if eod: self.app.emit('end-of-stream')
-        if self.streaming:
-            return pull, eod
+            if emit:
+                return pull, eod
+            else:
+                return None, eod
         else:
             return frames, eod
         
index fe84fe36eff079fa2d44d72a5c9434af372c116b..981bbcb82151879b124e1df6ef7289ddc35a6372 100644 (file)
@@ -49,22 +49,22 @@ class VorbisEncoder(Processor):
         super(VorbisEncoder, self).setup(channels, samplerate, nframes)
         # TODO open file for writing
         # the output data format we want        
-        pipe = ''' appsrc name=src max-bytes=32768 block=true
+        pipe = ''' appsrc name=src
                   ! audioconvert 
                   ! vorbisenc
                   ! oggmux
                   '''
         if self.filename and self.streaming:
             pipe += '''
-            ! queue2 name=q0 ! tee name=tee
-            tee. ! queue name=q1 ! appsink name=app sync=false
-            tee. ! queue name=q2 ! filesink location=%s 
+            ! tee name=t
+            ! queue ! appsink name=app sync=False
+            t. ! queue ! filesink location=%s 
             ''' % self.filename
             
         elif self.filename :
             pipe += '! filesink location=%s ' % self.filename
         else:
-            pipe += '! appsink name=app sync=false '
+            pipe += '! appsink name=app sync=False '
             
         self.pipeline = gst.parse_launch(pipe)
         # store a pointer to appsrc in our encoder object
index c60521b6e62a7ba790d664e2fec635f8a6425774..f40c203dfbd3d5fcfab45c1d5799babb0d5fc7c6 100644 (file)
@@ -50,23 +50,23 @@ class WavEncoder(Processor):
         super(WavEncoder, self).setup(channels, samplerate, nframes)
         # TODO open file for writing
         # the output data format we want
-        pipe = ''' appsrc name=src
+        self.pipe = ''' appsrc name=src
                   ! audioconvert 
                   ! wavenc
                   '''
         if self.filename and self.streaming:
-            pipe += '''
-            ! queue2 name=q0 ! tee name=tee
-            tee. ! queue name=q1 ! appsink name=app sync=false
-            tee. ! queue name=q2 ! filesink location=%s
+            self.pipe += '''
+            ! tee name=t 
+            t. ! queue ! appsink name=app sync=false
+            t. ! queue ! filesink sync=false location=%s
             ''' % self.filename
             
         elif self.filename :
-            pipe += '! filesink location=%s ' % self.filename
+            self.pipe += '! filesink location=%s ' % self.filename
         else:
-            pipe += '! appsink name=app sync=false '
+            self.pipe += '! appsink name=app sync=false'
             
-        self.pipeline = gst.parse_launch(pipe)
+        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
index cdf2754db3271bf85fb21d3e6a0a830d313d7649..8bc444fed90d531fa512062699be520b8cd6a1b5 100644 (file)
@@ -11,13 +11,13 @@ if len(sys.argv) > 1:
     source = sys.argv[1]
 else:
     import os.path
-    source= os.path.join (os.path.dirname(__file__),  "../samples/guitar.wav")
+    source= os.path.join (os.path.dirname(__file__),  "../samples/sweep.wav")
 
 decoder = FileDecoder(source)
 print "Creating decoder with id=%s for: %s" % (decoder.id(), source)
 decoder.setup()
 channels  = decoder.channels()
-print channels
+print 'channels :', channels
 samplerate = decoder.samplerate()
 nframes = decoder.nframes()
 
@@ -25,16 +25,16 @@ dest1 = "/tmp/test.mp3"
 dest2 = "/tmp/test2.mp3"
 f = open(dest2,'w')
 
-encoder = WavEncoder(dest1, streaming=True)
+streaming=True
+encoder = Mp3Encoder(dest1, streaming=streaming)
 encoder.setup(channels=channels, samplerate=samplerate)
 
-print encoder.pipe
-
 while True:
     buf, eod = encoder.process(*decoder.process())
-    f.write(buf)
+    if streaming and buf:
+        f.write(buf)
     if eod :
         break
 
 f.close()
-
+print encoder.pipe