From 78315789551b48ad8a10227e60d1c1d8c0aa6a2a Mon Sep 17 00:00:00 2001 From: Joris Kraak Date: Tue, 25 Mar 2014 12:35:04 +0100 Subject: [PATCH] Add mono audio input to PortAudio shim The way the PortAudio callback works it is possible to reuse the buffer used for sharing output audio data between Julia and the C-library. The output data can be pushed from the shared buffer to the PortAudio's output buffer, after which the same location in the buffer can be used for storing the data read from the input buffer. This does assume equal lengths for the in- and output buffers. --- deps/src/shim.c | 37 +++++++++++++++---------------------- src/portaudio.jl | 15 ++++++--------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/deps/src/shim.c b/deps/src/shim.c index dc92d57..335e3a8 100644 --- a/deps/src/shim.c +++ b/deps/src/shim.c @@ -4,18 +4,16 @@ #include static int paCallback(const void *inputBuffer, void *outputBuffer, - unsigned long framesPerBuffer, - const PaStreamCallbackTimeInfo* timeInfo, - PaStreamCallbackFlags statusFlags, - void *userData); + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void *userData); static PaStream *AudioStream; static int JuliaPipeReadFD = 0; static int JuliaPipeWriteFD = 0; static sem_t CSemaphore; -static void *OutData = NULL; -static unsigned long OutFrames = 0; - +static void *Buffer = NULL; int make_pipe(void) { @@ -27,11 +25,9 @@ int make_pipe(void) return JuliaPipeReadFD; } - -void wake_callback_thread(void *outData, unsigned int outFrames) +void synchronize_buffer(void *buffer) { - OutData = outData; - OutFrames = outFrames; + Buffer = buffer; sem_post(&CSemaphore); } @@ -39,13 +35,12 @@ PaError open_stream(unsigned int sampleRate, unsigned int bufSize) { PaError err; - err = Pa_OpenDefaultStream(&AudioStream, - 0, /* no input channels */ + 1, /* single input channel */ 1, /* mono output */ paFloat32, /* 32 bit floating point output */ sampleRate, - bufSize, /* frames per buffer, i.e. the number of sample frames + bufSize, /* frames per buffer, i.e. the number of sample frames that PortAudio will request from the callback. Many apps may want to use paFramesPerBufferUnspecified, which tells PortAudio to pick the best, possibly @@ -66,7 +61,6 @@ PaError open_stream(unsigned int sampleRate, unsigned int bufSize) return paNoError; } - //PaError stop_sin(void) //{ // PaError err; @@ -84,17 +78,16 @@ PaError open_stream(unsigned int sampleRate, unsigned int bufSize) // return paNoError; //} - /* * This routine will be called by the PortAudio engine when audio is needed. * It may called at interrupt level on some machines so don't do anything that * could mess up the system like calling malloc() or free(). */ static int paCallback(const void *inputBuffer, void *outputBuffer, - unsigned long framesPerBuffer, - const PaStreamCallbackTimeInfo* timeInfo, - PaStreamCallbackFlags statusFlags, - void *userData) + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void *userData) { unsigned int i; unsigned char fd_data = 0; @@ -102,9 +95,9 @@ static int paCallback(const void *inputBuffer, void *outputBuffer, sem_wait(&CSemaphore); for(i=0; i