Created
September 27, 2018 23:38
-
-
Save joshskeen/5bb4bacb1ccb6b875cbca288ebc09620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdio.h> | |
#include <soundio/soundio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <portaudio.h> | |
#define NUM_SECONDS (5) | |
#define SAMPLE_RATE (44100) | |
#define FRAMES_PER_BUFFER (64) | |
#ifndef M_PI | |
#define M_PI (3.14159265) | |
#endif | |
#define TABLE_SIZE (200) | |
typedef struct { | |
int left_phase; | |
int right_phase; | |
char message[20]; | |
float *sine; | |
} | |
paTestData; | |
static int patestCallback(const void *inputBuffer, void *outputBuffer, | |
unsigned long framesPerBuffer, | |
const PaStreamCallbackTimeInfo *timeInfo, | |
PaStreamCallbackFlags statusFlags, | |
void *userData) { | |
paTestData *data = (paTestData *) userData; | |
float *out = (float *) outputBuffer; | |
unsigned long i; | |
(void) timeInfo; /* Prevent unused variable warnings. */ | |
(void) statusFlags; | |
(void) inputBuffer; | |
for (i = 0; i < framesPerBuffer; i++) { | |
*out++ = data->sine[data->left_phase]; /* left */ | |
*out++ = data->sine[data->right_phase]; /* right */ | |
data->left_phase += 1; | |
if (data->left_phase >= TABLE_SIZE) data->left_phase -= TABLE_SIZE; | |
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ | |
if (data->right_phase >= TABLE_SIZE) data->right_phase -= TABLE_SIZE; | |
} | |
return paContinue; | |
} | |
/* | |
* This routine is called by portaudio when playback is done. | |
*/ | |
static void StreamFinished(void *userData) { | |
paTestData *data = (paTestData *) userData; | |
printf("Stream Completed: %s\n", data->message); | |
} | |
/*******************************************************************/ | |
int main(void); | |
PaError play(int tableSize) { | |
PaStreamParameters outputParameters; | |
PaStream *stream; | |
PaError err; | |
paTestData data; | |
int i; | |
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); | |
data.sine = malloc(sizeof(float) * tableSize); | |
for (i = 0; i < tableSize; i++) { | |
data.sine[i] = (float) sin(((double) i / (double) tableSize) * M_PI * 3.); | |
} | |
data.left_phase = data.right_phase = 0; | |
err = Pa_Initialize(); | |
if (err != paNoError) goto error; | |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ | |
if (outputParameters.device == paNoDevice) { | |
fprintf(stderr, "Error: No default output device.\n"); | |
goto error; | |
} | |
outputParameters.channelCount = 2; /* stereo output */ | |
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ | |
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency; | |
outputParameters.hostApiSpecificStreamInfo = NULL; | |
err = Pa_OpenStream( | |
&stream, | |
NULL, /* no input */ | |
&outputParameters, | |
SAMPLE_RATE, | |
FRAMES_PER_BUFFER, | |
paClipOff, /* we won't output out of range samples so don't bother clipping them */ | |
patestCallback, | |
&data); | |
if (err != paNoError) goto error; | |
sprintf(data.message, "No Message"); | |
err = Pa_SetStreamFinishedCallback(stream, &StreamFinished); | |
if (err != paNoError) goto error; | |
err = Pa_StartStream(stream); | |
if (err != paNoError) goto error; | |
printf("Play for %d seconds.\n", NUM_SECONDS); | |
Pa_Sleep(NUM_SECONDS * 1000); | |
err = Pa_StopStream(stream); | |
if (err != paNoError) goto error; | |
err = Pa_CloseStream(stream); | |
if (err != paNoError) goto error; | |
Pa_Terminate(); | |
printf("Test finished.\n"); | |
free(data.sine); | |
return err; | |
error: | |
Pa_Terminate(); | |
fprintf(stderr, "An error occured while using the portaudio stream\n"); | |
fprintf(stderr, "Error number: %d\n", err); | |
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err)); | |
return err; | |
} | |
int main(void) { | |
play(200); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment