I have built something that reads in audio files and processes the information for features. Sometimes the audio is rather long, and obviously I cannot keep all of this information in RAM. How can I process audio as efficiently as possible without keeping it all in RAM but still being able to refer back to slices when I need them?
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "General programming questions are off-topic here, but can be asked on Stack Overflow." – MBaz, Peter K.
If you use
libsndfile, you can jump to a wanted position in the audio data using
sf_seek, which accepts as an argument the number of sample frames to seek. If your processing is local in the sense that each output sample only depends on input samples close in time, then you can seek to the first input sample that affects your first output sample, and read from there until the last sample that affects your last output sample.
Or you could preprocess the audio piece-wise and write the output to a file as you go, and then seek in the processed audio when the output is needed.
Some file formats such as MP3 are difficult to seek, and might not be seekable by
libsndfile. WAV should be easy.