def _prepare_data(self, data): output = np.recarray(len(data), dtype=[ ('start', np.int64), ('duration', np.int64), ('slicedata', data.slicedata.dtype, data.slicedata.shape[1:]) ]) ps = Picoseconds(1) output.slicedata[:] = data.slicedata output.start[:] = [ts.start / ps for ts in data.slices] output.duration[:] = [ts.duration / ps for ts in data.slices] return output
def __init__(self, duration=None, start=None): super(TimeSlice, self).__init__() if duration is not None and not isinstance(duration, np.timedelta64): raise ValueError( 'duration must be of type {t} but was {t2}'.format( t=np.timedelta64, t2=duration.__class__)) if start is not None and not isinstance(start, np.timedelta64): raise ValueError('start must be of type {t} but was {t2}'.format( t=np.timedelta64, t2=start.__class__)) self.duration = duration self.start = start or Picoseconds(0)
def __and__(self, other): delta = max(Picoseconds(0), min(self.end, other.end) - max(self.start, other.start)) return TimeSlice(delta)
def end_seconds(self): return self.end / Picoseconds(int(1e12))
def samples_per_second(self): return int(Picoseconds(int(1e12)) / self.frequency)
def duration_in_seconds(self): return self.duration / Picoseconds(int(1e12))
def __str__(self): fs = self.frequency / Picoseconds(int(1e12)) ds = self.duration / Picoseconds(int(1e12)) return 'TimeDimension(f={fs}, d={ds})'.format(**locals())
def __hash__(self): start = self.start / Picoseconds(1) duration = \ None if self.duration is None else (self.duration / Picoseconds(1)) return (start, duration).__hash__()
def __init__(self, window_at_44100=2048, hop_at_44100=1024): one_sample_at_44100 = Picoseconds(int(1e12)) / 44100. window = one_sample_at_44100 * window_at_44100 step = one_sample_at_44100 * hop_at_44100 super(HalfLapped, self).__init__(step, window)
def __init__(self, samples_per_second, suggested_window, suggested_hop): self.suggested_hop = suggested_hop self.suggested_window = suggested_window self.one_sample = Picoseconds(int(1e12)) // samples_per_second super(AudioSampleRate, self).__init__(self.one_sample, self.one_sample)
def resample(self, ratio): orig_freq = Picoseconds(int(self.frequency / Picoseconds(1))) orig_duration = Picoseconds(int(self.duration / Picoseconds(1))) f = orig_freq * ratio d = orig_duration * ratio return SampleRate(f, d)
def _gen(self, raw): for r in raw: ts = TimeSlice( start=Picoseconds(r.start), duration=Picoseconds(r.duration)) yield (ts, r.slicedata)
def test_samplerate_audio(self): arr = np.arange(10) freq = Picoseconds(int(1e12)) / 44100. ts = ArrayWithUnits(arr, [TimeDimension(freq)]) self.assertEqual(44100, int(ts.dimensions[0].samples_per_second))