saving audio file to disk?

Hi all,

I’d like to save (stream) an array of floats to an audio file. I’ve been poking about but I’m not sure how to go about it - via Quicktime, or FMOD? Quicktime is giving me a bit of a headache. Any advice would be much appreciated.

Thanks
pixelpusher

I think binary file IO could be simple enough to use and not involve other libraries. If you have the data already (ie, 4000 floats) it’s pretty easy…

be sure to put mode as write binary.

http://www.codersource.net/cpp-file-io-binary.html
http://www.angelfire.com/country/aldev0-…-ileIO.html

take care!
zach

I see - thanks for the link.

I wound up using the libsndfile C++ library (installed via MacPorts) - it has some very easy to use, cross-platform sound writing/reading support:

in setup():

  
  
		int		format;  
		int		channels;  
		int		sampleRate;  
		int		sndBuffSize;  
		int		sndBuffPos;  
		  
		char	* outfilename;  
  
		SNDFILE * outfile;  
  
	SF_INFO 	info;  
	  
	info.format=SF_FORMAT_WAV | SF_FORMAT_PCM_16;  
	info.frames = sampleRate*60;  
	info.samplerate = sampleRate;  
	info.channels = 2;  
	  
	outfile = sf_open (outfilename, SFM_WRITE, &info) ;  
  
	if (!outfile)  
	{  
		cerr<<"Error opening ["<<outfilename<<"] : "<<sf_strerror (outfile)<<endl;  
	}  
  

Then in audioRequested():

  
  
sf_write_float(outfile, output, bufferSize*2);  
  

cheers
pixelpusher

nice !!

libsndfile is really good stuff and nice to see it was useful… thanks for posting the code!

a link for those interested:
http://www.mega-nerd.com/libsndfile/

they’ve made also a very good one for sample rate conversion – libsamplerate:
http://www.mega-nerd.com/SRC/

take care
zach

Hi pixelpusher, i’m trying to get started with libsndfile. I also installed the package using macports but i’m a bit unsure of the correct way to associate sndfile.hh with my xcode project. Especially with respect to using it as a dynamically loaded library:

Have you set it up in this way? if so would you mind saying a few words about how you achieved this? Thanks!