I want to make chunks from my audio files in order to overlap between chunks. For example if each chunks has 4 second length and first chunk start from 0 to 4 and step for overlapping is 1 second, second chunk should be start from 3 to 7.According to this How to splice an audio file (wav format) into 1 sec splices in python?
,I used pydub
module for doing this and make_chunks(your_audio_file_object, chunk_length_ms)
method, but it doesn't have overlap between chunks and just slice an audio file into fix length chunks. Anyone has an idea for this purpose ? Thank you
Asked
Active
Viewed 3,450 times
0

hdiz
- 1,141
- 2
- 13
- 27
-
Did my answer work for you? – Lukasz Tracewski Feb 05 '19 at 16:59
-
sorry, i was late. yes it worked – hdiz Feb 10 '19 at 18:02
1 Answers
1
Here's one way:
import numpy as np
from scipy.io import wavfile
frequency, signal = wavfile.read(path)
slice_length = 4 # in seconds
overlap = 1 # in seconds
slices = np.arange(0, len(signal)/frequency, slice_length-overlap, dtype=np.int)
for start, end in zip(slices[:-1], slices[1:]):
start_audio = start * frequency
end_audio = (end + overlap)* frequency
audio_slice = signal[int(start_audio): int(end_audio)]
In essence, we do the following:
- Load file and it's corresponding frequency. For the sake of example I assume its single channel, with multi-channel it would work all the same, just more code.
- Define desired slice length and overlap. The array will give us start of every audio piece. By zipping it a step further and adding overlap we get desired chunks.
To convince yourself that slicing works, check this snippet:
slice_length = 4 # in seconds
overlap = 1 # in seconds
slices = np.arange(0, 26, slice_length-overlap, dtype=np.int) # 26 is arbitrary
frequency = 1
for start, end in zip(slices[:-1], slices[1:]):
start_audio = start * frequency
end_audio = (end + overlap) * frequency
print(start_audio, end_audio)
Output:
0 4
3 7
6 10
9 13
12 16
15 19
18 22
21 25

A_Sib
- 5
- 2

Lukasz Tracewski
- 10,794
- 3
- 34
- 53
-
1
-
How is this supposed to change when the frequency is not 1? print(start_audio, end_audio) – Kurt Apr 15 '21 at 11:32