PortAudio.jl/deps/src/shim.c

111 lines
2.9 KiB
C
Raw Normal View History

2013-12-12 02:18:36 +01:00
#include <portaudio.h>
#include <semaphore.h>
2013-12-12 02:18:36 +01:00
#include <stdio.h>
#include <unistd.h>
2013-12-12 02:18:36 +01:00
static int paCallback(const void *inputBuffer, void *outputBuffer,
2013-12-12 02:18:36 +01:00
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;
int make_pipe(void)
{
int pipefd[2];
pipe(pipefd);
JuliaPipeReadFD = pipefd[0];
JuliaPipeWriteFD = pipefd[1];
sem_init(&CSemaphore, 0, 0);
return JuliaPipeReadFD;
}
2013-12-12 02:18:36 +01:00
void wake_callback_thread(void *outData, unsigned int outFrames)
{
OutData = outData;
OutFrames = outFrames;
sem_post(&CSemaphore);
}
PaError open_stream(unsigned int sampleRate, unsigned int bufSize)
2013-12-12 02:18:36 +01:00
{
PaError err;
err = Pa_OpenDefaultStream(&AudioStream,
2013-12-12 02:18:36 +01:00
0, /* no input channels */
1, /* mono output */
2013-12-12 02:18:36 +01:00
paFloat32, /* 32 bit floating point output */
sampleRate,
bufSize, /* frames per buffer, i.e. the number of sample frames
2013-12-12 02:18:36 +01:00
that PortAudio will request from the callback. Many
apps may want to use paFramesPerBufferUnspecified,
which tells PortAudio to pick the best, possibly
changing, buffer size.*/
paCallback, /* this is your callback function */
2013-12-12 02:18:36 +01:00
NULL); /*This is a pointer that will be passed to your callback*/
if(err != paNoError)
{
return err;
}
err = Pa_StartStream(AudioStream);
2013-12-12 02:18:36 +01:00
if(err != paNoError)
{
return err;
}
return paNoError;
}
//PaError stop_sin(void)
//{
// PaError err;
// err = Pa_StopStream(sin_stream);
// if(err != paNoError)
// {
// return err;
// }
//
// err = Pa_CloseStream(sin_stream);
// if( err != paNoError )
// {
// return err;
// }
// return paNoError;
//}
2013-12-12 02:18:36 +01:00
/*
* 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,
2013-12-12 02:18:36 +01:00
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
unsigned int i;
unsigned char fd_data = 0;
2013-12-12 02:18:36 +01:00
sem_wait(&CSemaphore);
2013-12-12 02:18:36 +01:00
for(i=0; i<framesPerBuffer; i++)
{
((float *)outputBuffer)[i] = ((float *)OutData)[i];
2013-12-12 02:18:36 +01:00
}
// TODO: copy the input data somewhere
write(JuliaPipeWriteFD, &fd_data, 1);
2013-12-12 02:18:36 +01:00
return 0;
}