Mercurial > hg > easaier-soundaccess
changeset 243:e977d4abea8a
Sample code to record audio
author | lbarthelemy |
---|---|
date | Fri, 28 Mar 2008 11:15:59 +0000 |
parents | 60e84bb658bc |
children | ec2ca3fbd957 |
files | sv/audioio/AudioRecording.cpp sv/audioio/AudioRecording.h |
diffstat | 2 files changed, 67 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sv/audioio/AudioRecording.cpp Fri Mar 28 11:15:59 2008 +0000 @@ -0,0 +1,61 @@ + +#include "audioio/AudioRecording.h" + +#ifdef HAVE_PORTAUDIO + +#include <portaudio.h> +#include <sndfile.h> +#endif + +static int myCallback( void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData ) +{ + //float *in = (float *) inputBuffer; + SNDFILE* file = (SNDFILE*)userData; + + if( inputBuffer == NULL ) return 0; + + // + // sf_writef_float(file, (float*)inputBuffer, framesPerBuffer); + //else + sf_writef_short(file, (short*)inputBuffer, framesPerBuffer); + + + return 0; +} + + +void recordAudio() //QString& fileName) +{ + PortAudioStream *stream; + + Pa_Initialize(); + + SF_INFO fileInfo; + fileInfo.samplerate = 44100; + fileInfo.channels = 1; + fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; //SF_FORMAT_FLOAT; + + + SNDFILE* file = sf_open("d:/sample.wav", SFM_WRITE, &fileInfo); + if (file != NULL) { + Pa_OpenDefaultStream(&stream,fileInfo.channels, 0, /* mon input, no output */ + paInt16/*paFloat32*/, fileInfo.samplerate, + 64, 0, /* 64 frames per buffer, let PA determine numBuffers */ + myCallback, (void*)file); + + Pa_StartStream( stream ); + Pa_Sleep( 5000 ); /* Sleep for 10 seconds while processing. */ + Pa_StopStream( stream ); + Pa_CloseStream( stream ); + + sf_close(file); + } + + + + Pa_Terminate(); + + + + +} \ No newline at end of file