Skip to content

Commit

Permalink
simplify creating a new audio segment from samples
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaaro committed Jan 6, 2017
1 parent 8d5e594 commit 5e02b57
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
4 changes: 4 additions & 0 deletions API.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,10 @@ from pydub import AudioSegment
sound = AudioSegment.from_file(“sound1.wav”)

samples = sound.get_array_of_samples()

# then modify samples...

new_sound = sound._spawn(samples)
```

## Effects
Expand Down
6 changes: 6 additions & 0 deletions pydub/audio_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ def _spawn(self, data, overrides={}):
if isinstance(data, list):
data = b''.join(data)

if isinstance(data, array.array):
try:
data = data.tobytes()
except:
data = data.tostring()

# accept file-like objects
if hasattr(data, 'read'):
if hasattr(data, 'seek'):
Expand Down
20 changes: 7 additions & 13 deletions pydub/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ def apply_mono_filter_to_each_channel(seg, filter_fn):
for sample_i, sample in enumerate(channel_seg.get_array_of_samples()):
index = (sample_i * n_channels) + channel_i
out_data[index] = sample
try:
return seg._spawn(out_data.tobytes())
except:
return seg._spawn(out_data.tostring())

return seg._spawn(out_data)


@register_pydub_effect
Expand Down Expand Up @@ -225,10 +223,8 @@ def low_pass_filter(seg, cutoff):
offset = (i * seg.channels) + j
last_val[j] = last_val[j] + (alpha * (original[offset] - last_val[j]))
filteredArray[offset] = int(last_val[j])
try:
return seg._spawn(data=filteredArray.tobytes())
except:
return seg._spawn(data=filteredArray.tostring())

return seg._spawn(data=filteredArray)


@register_pydub_effect
Expand Down Expand Up @@ -260,12 +256,10 @@ def high_pass_filter(seg, cutoff):

last_val[j] = alpha * (last_val[j] + original[offset] - original[offset_minus_1])
filteredArray[offset] = int(min(max(last_val[j], minval), maxval))
try:
return seg._spawn(data=filteredArray.tobytes())
except:
return seg._spawn(data=filteredArray.tostring())


return seg._spawn(data=filteredArray)


@register_pydub_effect
def pan(seg, pan_amount):
"""
Expand Down
11 changes: 4 additions & 7 deletions pydub/scipy_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Of course, you will need to install scipy for these to work.
When this module is imported the high and low pass filters from this module
will be used when calling audio_segment.high_pass_filter() and
audio_segment.high_pass_filter() instead of the slower, less powerful versions
will be used when calling audio_segment.high_pass_filter() and
audio_segment.high_pass_filter() instead of the slower, less powerful versions
provided by pydub.effects.
"""
from scipy.signal import butter, sosfilt
Expand Down Expand Up @@ -41,10 +41,7 @@ def filter_fn(seg):
sos = butter(order, freqs, btype=type, output='sos')
y = sosfilt(sos, seg.get_array_of_samples())

try:
return seg._spawn(y.astype(seg.array_type).tobytes())
except:
return seg._spawn(y.astype(seg.array_type).tostring())
return seg._spawn(y.astype(seg.array_type))

return filter_fn

Expand All @@ -64,4 +61,4 @@ def high_pass_filter(seg, cutoff_freq, order=5):
@register_pydub_effect
def low_pass_filter(seg, cutoff_freq, order=5):
filter_fn = _mk_butter_filter(cutoff_freq, 'lowpass', order=order)
return seg.apply_mono_filter_to_each_channel(filter_fn)
return seg.apply_mono_filter_to_each_channel(filter_fn)

0 comments on commit 5e02b57

Please sign in to comment.