How to change AudioInput example to get input from a mp3 file?

If I need to modify the AudioInput to read from music files (mp3, wav,…) instread of mic input? Could I use ofSoundPlayer to read local music files?
I’m new to audio processing and confused about ofSoundGetSpectrum and audioIn functions.

To play audio from a file, you don’t need AudioInput/AudioOuput. Look at the SoundPlayer examples. AudioInout is specifically for real-time input from a microphone.

Basically it’s:

ofSoundPlayer player;  
player.load("sound.wav"); //load the file you want to play; //plays file  

My question is: how to if I use ofSoundPlayer to read from music files, then what functions could I use to do the same job as ofsoundStreamsetup and audioIn? I mean parameters like the buffersize and the * input. I don’t know how to get these use ofSoundPlayer.

You need to read the sound file in blocks of samples at the necessary rate.

I attach a couple of source files with the functions to read a .wav file (I have them from a long time and I don’t know the original author)

In your setup method you can do something like this to switch between input capture streaming and file streaming:

#ifdef SAMPLE  
sampleRate = sample.getSampleRate();  
ofSoundStreamSetup(1, 0, this, sampleRate, BUFFER_SIZE, 4);	  
sampleRate = 44100;  
ofSoundStreamSetup(1, 1, this, sampleRate, BUFFER_SIZE, 4);  

If you use a input capture stream you can copy the input samples in a buffer:

for (int i = 0; i < bufferSize; i++) sampleBuffer[i] = input[i];  

And then in the audio requested callback function you can make your processing and write to the output. And in the case of reading from a file, you fill the buffer reading the required number of samples from the file:

#ifdef SAMPLE  
for (int i = 0; i < bufferSize; i++)  
// mono  
if(sample.getChannels() == 1) {  
sampleBuffer[i] = sample.update();  
// stereo  
else if (sample.getChannels() == 2) {  
sampleBuffer[i] = sample.update();  

Of course, if you want to do an offline processing of the sound file you don’t need to setup an output audio stream, then you can just get the samples as you need them from the file.




I am wondering did you figure out how to do it? I am trying to do the same thing and get stucked here too


The current audio implementation works differently for audio input or output and for audio file read and playback.
There is this addon that standardizes the behavior of audio, regardless of its source.
It has several examples you can use to achieve what’s described here.


1 Like

Thanks for letting me know this addon!

I successfully got the time domain (which I’ve been trying for a long time)(instead of freq domain)!
And did some experiment like this

Thank you! :heart_eyes:

hey that looks great!

1 Like