#! /usr/bin/env python
+from __future__ import division
+
from timeside.decoder.core import FileDecoder
from unit_timeside import *
self.expected_samplerate = 44100
self.expected_channels = 2
self.expected_totalframes = 352800
+ self.test_exact_duration = True
def testWav(self):
"Test wav decoding"
"samples/sweep_32000.wav")
expected_samplerate = 32000
- ratio = expected_samplerate/float(self.expected_samplerate)
+ ratio = expected_samplerate/self.expected_samplerate
self.expected_totalframes = int(self.expected_totalframes * ratio)
self.expected_samplerate = expected_samplerate
"samples/sweep.ogg")
self.expected_totalframes = 352832
+ self.test_exact_duration = False
+
def testMp3(self):
"Test mp3 decoding"
"samples/sweep.mp3")
self.expected_totalframes = 353664
+ self.test_exact_duration = False
+
def tearDown(self):
decoder = FileDecoder(uri=self.source,
totalframes += frames.shape[0]
if eod or decoder.eod:
break
- self.assertEquals(frames.shape[0], decoder.blocksize())
- self.assertEquals(frames.shape[1], decoder.channels())
+ self.assertEqual(frames.shape[0], decoder.blocksize())
+ self.assertEqual(frames.shape[1], decoder.channels())
- ratio = decoder.output_samplerate / float(decoder.input_samplerate)
+ ratio = decoder.output_samplerate / decoder.input_samplerate
if 0:
print "input / output_samplerate:", decoder.input_samplerate, '/', decoder.output_samplerate,
print "ratio:", ratio
if self.channels:
# when specified, check that the channels are the ones requested
- self.assertEquals(self.channels, decoder.output_channels)
+ self.assertEqual(self.channels, decoder.output_channels)
else:
# otherwise check that the channels are preserved, if not specified
- self.assertEquals(decoder.input_channels, decoder.output_channels)
+ self.assertEqual(decoder.input_channels, decoder.output_channels)
# and if we know the expected channels, check the output match
if self.expected_channels:
- self.assertEquals(self.expected_channels, decoder.output_channels)
+ self.assertEqual(self.expected_channels, decoder.output_channels)
# do the same with the sampling rate
if self.samplerate:
- self.assertEquals(self.samplerate, decoder.output_samplerate)
+ self.assertEqual(self.samplerate, decoder.output_samplerate)
else:
- self.assertEquals(decoder.input_samplerate, decoder.output_samplerate)
+ self.assertEqual(decoder.input_samplerate, decoder.output_samplerate)
if self.expected_samplerate:
- self.assertEquals(self.expected_samplerate, decoder.output_samplerate)
+ self.assertEqual(self.expected_samplerate, decoder.output_samplerate)
- self.assertEquals(totalframes, self.expected_totalframes)
+ self.assertEqual(totalframes, self.expected_totalframes)
+ if self.test_exact_duration:
+ self.assertEqual(totalframes/decoder.output_samplerate,
+ decoder.totalframes()/decoder.output_samplerate)
+ else:
+ self.assertAlmostEqual(totalframes/decoder.output_samplerate,
+ decoder.totalframes()/decoder.output_samplerate,
+ places=1)
class TestDecodingSegment(TestDecoding):
def __init__(self, blocksize, stepsize):
self.blocksize = blocksize
self.stepsize = stepsize
- self.stack = None
+ self.buffer = None
def frames(self, frames, eod):
- if self.stack is not None:
- stack = np.concatenate([self.stack, frames])
+ if self.buffer is not None:
+ stack = np.concatenate([self.buffer, frames])
else:
stack = frames.copy()
dtype=frames.dtype)])
nb_frames += 1
- self.stack = stack[nb_frames * self.stepsize:]
+ self.buffer = stack[nb_frames * self.stepsize:]
eod_list = np.repeat(False, nb_frames)
if eod and len(eod_list):
eod_list[-1] = eod
- for n in xrange(nb_frames):
- yield (
- stack[
- n * self.stepsize:n * self.stepsize + self.blocksize],
- eod_list[n])
+ for index, eod in zip(xrange(0, nb_frames*self.stepsize, self.stepsize), eod_list):
+ yield (stack[index:index + self.blocksize],eod)
@functools.wraps(process_func)
def wrapper(analyzer, frames, eod):
if __name__ == "__main__":
- # Run doctest
- import doctest
- doctest.testmod()
-
- # Run unittest from test_analyzer_preprocessors
+ # Run doctest from __main__ and unittest from test_analyzer_preprocessors
from tests import test_analyzer_preprocessors
from tests.unit_timeside import runTestModule
- runTestModule(test_analyzer_preprocessors)
+ runTestModule('__main__', test_analyzer_preprocessors)
return "gst_dec"
def __init__(self, uri, start = 0, duration = None):
- '''
- Construct a new FileDecoder
- Parameters
- ----------
- uri: uri of the media
- start : float
- start time of the segment in seconds
- duration : float
- duration of the segment in seconds
- '''
+ """
+ Construct a new FileDecoder
+
+ Parameters
+ ----------
+ uri : str
+ uri of the media
+ start : float
+ start time of the segment in seconds
+ duration : float
+ duration of the segment in seconds
+ """
+
super(FileDecoder, self).__init__()
# is this a file?
@interfacedoc
def metadata(self):
return None
+
+
+if __name__ == "__main__":
+ # Run doctest from __main__ and unittest from tests
+ from tests.unit_timeside import runTestModule
+ # load corresponding tests
+ from tests import test_decoding, test_array_decoding
+
+ runTestModule('__main__', test_decoding, test_array_decoding)
+