class ArrayDecoder(Decoder):
+
""" Decoder taking Numpy array as input"""
implements(IDecoder)
- output_blocksize = 8*1024
+ output_blocksize = 8 * 1024
# IProcessor methods
for index in xrange(0,
nb_frames * self.output_blocksize,
self.output_blocksize):
- yield (self.samples[index:index+self.output_blocksize], False)
+ yield (self.samples[index:index + self.output_blocksize], False)
yield (self.samples[nb_frames * self.output_blocksize:], True)
def process(self):
return self.frames.next()
- ## IDecoder methods
+ # IDecoder methods
@interfacedoc
def format(self):
import re
base_type = re.search('^[a-z]*', self.samples.dtype.name).group(0)
- return 'audio/x-raw-'+base_type
+ return 'audio/x-raw-' + base_type
@interfacedoc
def metadata(self):
class Decoder(Processor):
+
"""General abstract base class for Decoder
"""
implements(IDecoder)
def stop(self):
self.src.send_event(gst.event_new_eos())
-
from timeside.tools.gstutils import MainloopThread
import threading
+
class FileDecoder(Decoder):
+
""" gstreamer-based decoder """
implements(IDecoder)
- output_blocksize = 8*1024
+ output_blocksize = 8 * 1024
pipeline = None
mainloopthread = None
return "gst_dec"
def __init__(self, uri, start=0, duration=None, stack=False):
-
"""
Construct a new FileDecoder
! audioconvert name=audioconvert
! audioresample
! appsink name=sink sync=False async=True
- '''.format(uri = self.uri,
- uri_start = np.uint64(round(self.uri_start * gst.SECOND)),
- uri_duration = np.int64(round(self.uri_duration * gst.SECOND)))
- # convert uri_start and uri_duration to nanoseconds
+ '''.format(uri=self.uri,
+ uri_start=np.uint64(
+ round(self.uri_start * gst.SECOND)),
+ uri_duration=np.int64(round(self.uri_duration * gst.SECOND)))
+ # convert uri_start and uri_duration to
+ # nanoseconds
else:
# Create the pipe with standard Gstreamer uridecodebin
self.pipe = ''' uridecodebin name=src uri={uri}
! audioconvert name=audioconvert
! audioresample
! appsink name=sink sync=False async=True
- '''.format(uri = self.uri)
+ '''.format(uri=self.uri)
self.pipeline = gst.parse_launch(self.pipe)
self.discovered_cond.acquire()
while not self.discovered:
- #print 'waiting'
+ # print 'waiting'
self.discovered_cond.wait()
self.discovered_cond.release()
self.output_channels = self.input_channels
self.input_duration = length / 1.e9
- self.input_totalframes = int(self.input_duration * self.input_samplerate)
+ self.input_totalframes = int(
+ self.input_duration * self.input_samplerate)
if "x-raw-float" in caps.to_string():
self.input_width = caps[0]["width"]
else:
def _on_new_buffer_cb(self, sink):
buf = sink.emit('pull-buffer')
new_array = gst_buffer_to_numpy_array(buf, self.output_channels)
- #print 'processing new buffer', new_array.shape
+ # print 'processing new buffer', new_array.shape
if self.last_buffer is None:
self.last_buffer = new_array
else:
- self.last_buffer = np.concatenate((self.last_buffer, new_array), axis=0)
+ self.last_buffer = np.concatenate(
+ (self.last_buffer, new_array), axis=0)
while self.last_buffer.shape[0] >= self.output_blocksize:
new_block = self.last_buffer[:self.output_blocksize]
self.last_buffer = self.last_buffer[self.output_blocksize:]
- #print 'queueing', new_block.shape, 'remaining', self.last_buffer.shape
+ # print 'queueing', new_block.shape, 'remaining',
+ # self.last_buffer.shape
self.queue.put([new_block, False])
@interfacedoc
self.stack = False
self.from_stack = True
- ## IDecoder methods
+ # IDecoder methods
@interfacedoc
def format(self):
class LiveDecoder(Decoder):
+
""" gstreamer-based decoder from live source"""
implements(IDecoder)
- output_blocksize = 8*1024
+ output_blocksize = 8 * 1024
pipeline = None
mainloopthread = None
return "gst_live_dec"
def __init__(self, num_buffers=-1, input_src='autoaudiosrc'):
-
"""
Construct a new LiveDecoder capturing audio from alsasrc
self.discovered_cond.acquire()
while not self.discovered:
- #print 'waiting'
+ # print 'waiting'
self.discovered_cond.wait()
self.discovered_cond.release()
self.output_channels = self.input_channels
self.input_duration = length / 1.e9
- self.input_totalframes = int(self.input_duration * self.input_samplerate)
+ self.input_totalframes = int(
+ self.input_duration * self.input_samplerate)
if "x-raw-float" in caps.to_string():
self.input_width = caps[0]["width"]
else:
buf = sink.emit('pull-buffer')
new_array = gst_buffer_to_numpy_array(buf, self.output_channels)
- #print 'processing new buffer', new_array.shape
+ # print 'processing new buffer', new_array.shape
if self.last_buffer is None:
self.last_buffer = new_array
else:
- self.last_buffer = np.concatenate((self.last_buffer, new_array), axis=0)
+ self.last_buffer = np.concatenate(
+ (self.last_buffer, new_array), axis=0)
while self.last_buffer.shape[0] >= self.output_blocksize:
new_block = self.last_buffer[:self.output_blocksize]
self.last_buffer = self.last_buffer[self.output_blocksize:]
- #print 'queueing', new_block.shape, 'remaining', self.last_buffer.shape
+ # print 'queueing', new_block.shape, 'remaining',
+ # self.last_buffer.shape
self.queue.put([new_block, False])
@interfacedoc
self.from_stack = True
pass
- ## IDecoder methods
+ # IDecoder methods
@interfacedoc
def format(self):
def metadata(self):
# TODO check
return self.tags
-
import numpy as np
+
class Noise(object):
+
"""A class that mimics audiolab.sndfile but generates noise instead of reading
a wave file. Additionally it can be told to have a "broken" header and thus crashing
in the middle of the file. Also useful for testing ultra-short files of 20 samples."""
else:
will_read = frames_to_read
self.seekpoint += will_read
- return np.random.random(will_read)*2 - 1
+ return np.random.random(will_read) * 2 - 1
def path2uri(path):
>>> path2uri('C:\Windows\my_file.wav')
'file:///C%3A%5CWindows%5Cmy_file.wav'
"""
- import urlparse, urllib
+ import urlparse
+ import urllib
return urlparse.urljoin('file:', urllib.pathname2url(path))
uri_discoverer = Discoverer(GST_DISCOVER_TIMEOUT)
try:
uri_info = uri_discoverer.discover_uri(uri)
- except GError as e:
+ except GError as e:
raise IOError(e)
info = dict()
audio_streams = uri_info.get_audio_streams()
info['streams'] = []
for stream in audio_streams:
- stream_info = {'bitrate': stream.get_bitrate (),
- 'channels': stream.get_channels (),
- 'depth': stream.get_depth (),
+ stream_info = {'bitrate': stream.get_bitrate(),
+ 'channels': stream.get_channels(),
+ 'depth': stream.get_depth(),
'max_bitrate': stream.get_max_bitrate(),
'samplerate': stream.get_sample_rate()
}
sha1 = hashlib.sha1()
chunk_size = sha1.block_size * 8192
- max_file_size = 10*1024*1024 # 10Mo limit in case of very large file
+ max_file_size = 10 * 1024 * 1024 # 10Mo limit in case of very large file
total_read = 0
with closing(urllib.urlopen(url)) as url_obj: