Skip to content

Instantly share code, notes, and snippets.

@jacobjoaquin
Created December 2, 2010 21:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobjoaquin/726087 to your computer and use it in GitHub Desktop.
Save jacobjoaquin/726087 to your computer and use it in GitHub Desktop.
A bare minimum demonstration of writing audio to an AIFF file using libsndfile. (error checking not included)
/* simple_aiff.c
* Jacob Joaquin <jacobjoaquin@gmail.com>
* csoundblog.com
*
* A bare minimum demonstration of writing audio to an AIFF file using
* libsndfile. (error checking not included)
*
* License:
* GNU Lesser General Public License
* http://www.gnu.org/copyleft/lesser.html
*
* libsndfile by Erik de Castro Lopo
* http://www.mega-nerd.com/libsndfile/
*/
#include <sndfile.h>
#include <stdlib.h>
int main()
{
SNDFILE* sndfile;
SF_INFO* sf_info;
float* frame_buffer;
int samplerate = 44100;
int frames = 44100 * 4;
float saw = 0.0;
/* Set sound file attributes */
sf_info = (SF_INFO *) malloc(sizeof(SF_INFO));
sf_info->samplerate = samplerate;
sf_info->channels = 1;
sf_info->format = SF_FORMAT_AIFF | SF_FORMAT_PCM_16;
/* Open sound file for writing */
sndfile = sf_open("./simple_aiff.aif", SFM_WRITE, sf_info);
/* Intialize frame_buffer and point to saw */
frame_buffer = (float*) malloc(sf_info->channels * sizeof(float));
frame_buffer = &saw;
/* Write generated saw wave to file */
do
{
sf_writef_float(sndfile, frame_buffer, 1);
saw += 0.0025 - (saw >= 1.0) * 2.0;
} while(--frames);
/* Close sound file and return */
sf_close(sndfile);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment