Ejemplo n.º 1
0
 def test_can_get_closed_time_slice(self):
     rr = RangeRequest('seconds=10.5-100.5')
     sl = rr.range()
     self.assertIsInstance(sl, TimeSlice)
     expected_start = Picoseconds(int(10.5 * 1e12))
     expected_duration = Picoseconds(int(90 * 1e12))
     self.assertEqual(
         TimeSlice(start=expected_start, duration=expected_duration), sl)
Ejemplo n.º 2
0
 def time_slice(self, start, stop):
     start = float(start)
     try:
         stop = float(stop)
     except ValueError:
         stop = None
     duration = \
         None if stop is None else Picoseconds(int(1e12 * (stop - start)))
     start = Picoseconds(int(1e12 * start))
     return TimeSlice(duration, start=start)
Ejemplo n.º 3
0
    def square(self, n_coeffs, do_overlap_add=False):
        """
        Compute a "square" view of the frequency adaptive transform, by
        resampling each frequency band such that they all contain the same
        number of samples, and performing an overlap-add procedure in the
        case where the sample frequency and duration differ
        :param n_coeffs: The common size to which each frequency band should
        be resampled
        """
        resampled_bands = [
            self._resample(band, n_coeffs) for band in self.iter_bands()
        ]

        stacked = np.vstack(resampled_bands).T

        fdim = FrequencyDimension(self.scale)

        # TODO: This feels like it could be wrapped up nicely elsewhere
        chunk_frequency = Picoseconds(
            int(
                np.round(self.time_dimension.duration / Picoseconds(1) /
                         n_coeffs)))

        td = TimeDimension(frequency=chunk_frequency)

        arr = ConstantRateTimeSeries(
            ArrayWithUnits(stacked.reshape(-1, n_coeffs, self.n_bands),
                           dimensions=[self.time_dimension, td, fdim]))

        if not do_overlap_add:
            return arr

        # Begin the overlap add procedure
        overlap_ratio = self.time_dimension.overlap_ratio

        if overlap_ratio == 0:
            # no overlap add is necessary
            return ArrayWithUnits(stacked, [td, fdim])

        step_size_samples = int(n_coeffs * overlap_ratio)

        first_dim = int(
            np.round((stacked.shape[0] * overlap_ratio) +
                     (n_coeffs * overlap_ratio)))

        output = ArrayWithUnits(np.zeros((first_dim, self.n_bands)),
                                dimensions=[td, fdim])

        for i, chunk in enumerate(arr):
            start = step_size_samples * i
            stop = start + n_coeffs
            output[start:stop] += chunk.reshape((-1, self.n_bands))

        return output
Ejemplo n.º 4
0
 def test_from_timeslice_closed(self):
     ts = TimeSlice(
             start=Picoseconds(int(1e12)) * 2.5,
             duration=Milliseconds(2000))
     self.assertEqual(
             'seconds 2.5-4.5/100.0',
             str(ContentRange.from_timeslice(ts, Seconds(100))))
Ejemplo n.º 5
0
    def setUp(self):
        self.samplerate = SR22050()
        rs = resampled(resample_to=self.samplerate)

        window_size = Picoseconds(int(1e12))
        wscheme = SampleRate(window_size, window_size)

        @simple_in_memory_settings
        class Document(rs):
            windowed = ArrayWithUnitsFeature(
                SlidingWindow,
                wscheme=wscheme,
                needs=rs.resampled,
                store=False)

            dct = ArrayWithUnitsFeature(
                DCTIV,
                needs=windowed,
                store=True)

        ss = SineSynthesizer(self.samplerate)
        self.audio = ss.synthesize(Seconds(5), [440., 660., 880.])

        _id = Document.process(meta=self.audio.encode())
        self.doc = Document(_id)
Ejemplo n.º 6
0
    def test_has_correct_duration(self):
        samplerate = SR22050()
        rs = resampled(resample_to=samplerate)

        @simple_in_memory_settings
        class Document(rs):
            windowed = ArrayWithUnitsFeature(SlidingWindow,
                                             wscheme=HalfLapped(),
                                             needs=rs.resampled,
                                             store=True)

        synth = NoiseSynthesizer(samplerate)
        audio = synth.synthesize(Milliseconds(5500))

        _id = Document.process(meta=audio.encode())
        doc = Document(_id)

        orig_seconds = audio.dimensions[0].end / Picoseconds(int(1e12))
        new_seconds = doc.windowed.dimensions[0].end / Picoseconds(int(1e12))
        self.assertAlmostEqual(orig_seconds, new_seconds, delta=0.01)
Ejemplo n.º 7
0
 def serialize(self, context):
     feature = context.feature
     document = context.document
     slce = context.slce
     wrapper = feature(_id=document._id, persistence=document)
     samples = wrapper[slce]
     bio = BytesIO()
     with SoundFile(bio,
                    mode='w',
                    samplerate=wrapper.samplerate,
                    channels=wrapper.channels,
                    format='OGG',
                    subtype='VORBIS') as sf:
         sf.write(samples)
     bio.seek(0)
     content_range = ContentRange.from_timeslice(
         slce, Picoseconds(int(1e12 * wrapper.duration_seconds)))
     return TempResult(bio.read(),
                       'audio/ogg',
                       is_partial=slce != TimeSlice(),
                       content_range=content_range)
Ejemplo n.º 8
0
 def test_from_timeslice_open_ended(self):
     ts = TimeSlice(start=Picoseconds(int(1e12)) * 2.5)
     self.assertEqual('seconds 2.5-100.0/100.0',
                      str(ContentRange.from_timeslice(ts, Seconds(100))))
Ejemplo n.º 9
0
 def __init__(self, flo):
     self._flo = flo
     self._sf = SoundFile(self._flo)
     self._freq = Picoseconds(int(1e12)) / self._sf.samplerate
Ejemplo n.º 10
0
 def __init__(self, needs=None):
     super(TimestampEmitter, self).__init__(needs=needs)
     self.pos = Picoseconds(0)
Ejemplo n.º 11
0
 def __init__(self, needs=None):
     super(BasePeakPicker, self).__init__(needs=needs)
     self._pos = Picoseconds(0)
     self._leftover_timestamp = self._pos