pythonaudiochunkssegment

Make chunks in Audio files with overlap in python


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


Solution

  • 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:

    1. 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.
    2. 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