Commit 2768c76c authored by Oliver Kirsebom's avatar Oliver Kirsebom
Browse files

audio_signal.py - ENH - crop method modifies instance instead of returning new instance

parent 74fee9de
1 merge request!42audio_signal.py - ENH - crop method modifies instance instead of returning new instance
Pipeline #373 passed with stage
in 4 minutes and 26 seconds
Showing with 9 additions and 12 deletions
+9 -12
......@@ -53,9 +53,7 @@ class AudioSignal:
return np.array(cropped_data)
def crop(self, begin=None, end=None):
cropped_data = self._cropped_data(begin,end)
cropped_signal = self.__class__(rate=self.rate, data=cropped_data)
return cropped_signal
self.data = self._cropped_data(begin,end)
def append(self, signal, overlap_sec=0):
assert self.rate == signal.rate, "Cannot merge audio signals with different sampling rates."
......@@ -137,15 +135,13 @@ class TimeStampedAudioSignal(AudioSignal):
def crop(self, begin=None, end=None):
begin_sec, end_sec = None, None
if begin is not None:
begin_sec = (begin - self.begin()).total_seconds()
if end is not None:
end_sec = (end - self.begin()).total_seconds()
cropped_data = self._cropped_data(begin_sec, end_sec)
self.data = self._cropped_data(begin_sec, end_sec)
time_stamp = self.time_stamp
if begin_sec > 0 and len(cropped_data) > 0:
time_stamp += datetime.timedelta(seconds=begin_sec) # update time stamp
cropped_signal = self.__class__(rate=self.rate, data=cropped_data, time_stamp=time_stamp, tag=self.tag)
return cropped_signal
\ No newline at end of file
if begin_sec > 0 and len(self.data) > 0:
self.time_stamp += datetime.timedelta(seconds=begin_sec) # update time stamp
......@@ -43,7 +43,8 @@ def test_crop_audio_signal(audio):
seconds = len(audio.data) / audio.rate
crop_begin = audio.begin() + datetime.timedelta(seconds=seconds/10.)
crop_end = audio.end() - datetime.timedelta(seconds=seconds/10.)
audio_cropped = audio.crop(begin=crop_begin, end=crop_end)
audio_cropped = audio
audio_cropped.crop(begin=crop_begin, end=crop_end)
seconds_cropped = len(audio_cropped.data) / audio_cropped.rate
assert seconds_cropped/seconds == pytest.approx(8./10., rel=1./audio.rate)
assert audio_cropped.begin() == crop_begin
......@@ -61,4 +62,4 @@ def test_append_audio_signal_without_time_stamp(audio, audio_without_time_stamp)
def test_append_audio_signal_with_smoothing(audio):
t = audio.seconds()
audio.append(signal=audio, overlap_sec=0.2)
assert audio.seconds() == pytest.approx(2.*t - 0.2, rel=1./audio.rate)
\ No newline at end of file
assert audio.seconds() == pytest.approx(2.*t - 0.2, rel=1./audio.rate)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment