Load wav file into ofSoundBuffer


#1

I was presuming there’d be a methods like:

  • ofSoundBuffer::load(...)
  • ofSoundBuffer::save(...)

How do you do this instead?
Or do we presume always that an ofSoundBuffer represents a small sound buffer which is aprt of a large sound stream (i.e. not very complete by itself).


Display/Read a wav file as... text of bits
Limits with setting the speed of playback with ofSoundPlayer
#2

There is a copyFrom() method which takes a raw float * and asks you to set the channel count & sample rate metadata:

void copyFrom(const float * floatBuffer, std::size_t numFrames, std::size_t numChannels, unsigned int sampleRate)

…Or you could access the underlying buffer if you’d like to use std::vector methods like assign() or begin() / end()

std::vector<float>& getBuffer()

If you get the raw buffer like this, you should set the sample rate and channel count later, or things like resampling or playback won’t work correctly.

For reading the buffer, you can use operator[] to access the underlying std::vector<float> directly, or use getSample() to handle the (frameIndex * channels) + channel calculation for you.

ofSoundBuffer doesn’t include any file I/O at the moment


#3

thanks for the reply!

also adding your note from the email which helps answer the original question:

Personally I’d use libsndfile [ http://www.mega-nerd.com/ ] to handle the WAV reading part, I’m pretty sure arturo has an addon around it too. There was intended to be an ofSoundFile to accompany ofSoundBuffer, but it get shelved (hence why ofSoundBuffer pretends files don’t exist).


#4

There is also https://github.com/roymacdonald/ofxSoundObjects. It includes a class ofxSoundFile.


#5

ofxSoundObjects looks great!
I wonder why it’s so obscure(?)


#6

the api had some problems still so we decided not to include it in the release yet


#7

Hey Elliot! what do you mean with obscure?
best!


#8

by obscure i mean it’s only got a small handful of stars/forks on GitHub
(which suggests not a lot of people are using it yet compared to how handy and well worked out it is)


#9

Does this still hold? the only way to get an mp3 file loaded into an ofSoundBuffer is through addons?
ofxSoundObjects by @roymacdonald looks nice and it has a lot of examples but I am a bit skeptical regarding the dependency to ofxFft, because the last time it was updated was 3 years ago. I have always used ofxMaxim but since a while it crashes when closing the app (on mac osx at least).


#10

The crash does not happen anymore.


#11

Hi Davide,
the dependancy on ofxFft is just for the fftObject (example) and not actually for the addon itself. You can use ofxSoundObjects without using ofxFft with no problems.
A while ago I made ofxSoundObjects dependent on ofxAudioFile, if you dont want to depend on ofxSoundObjects you can just use ofxAudioFile which gives you simple access to most audio types.