Quantcast
Viewing latest article 13
Browse Latest Browse All 51

Answer by Louis Maddox for Reading only a part of a large wav file with Python

There are a few packages you may want to look into for handling audio: commonly soundfile is used for I/O, as is librosa. The 'sampling rate' AKA 'frame rate' is the number of audio samples per second, commonly written in kHz, but in software just given in Hz.

There's also a dedicated Sound Design StackExchange which you may find more fruitful to search.

Taking a section of a file is known as 'seeking', and the soundfile.SoundFile class supports it.

The idea is you move the position of the 'cursor' to a particular frame, SoundFile.seek(pos), then read in some frames, SoundFile.read(n_frames), after which the position of the cursor will be moved along by that many frames, which you can obtain with SoundFile.tell().

Here's an example of accessing a part of a wav file:

import soundfile as sfdef read_audio_section(filename, start_time, stop_time):    track = sf.SoundFile(filename)    can_seek = track.seekable() # True    if not can_seek:        raise ValueError("Not compatible with seeking")    sr = track.samplerate    start_frame = sr * start_time    frames_to_read = sr * (stop_time - start_time)    track.seek(start_frame)    audio_section = track.read(frames_to_read)    return audio_section, sr

...and to write that to file you just use soundfile.write (note: a function in the package, not a method of the soundfile.SoundFile class)

def extract_as_clip(input_filename, output_filename, start_time, stop_time):    audio_extract, sr = read_audio_section(input_filename, start_time, stop_time)    sf.write(output_filename, audio_extract, sr)    return

Viewing latest article 13
Browse Latest Browse All 51

Trending Articles