| 1 |
/** @file patest_multi_sine.c |
| 2 |
@brief Play a different sine wave on each channel. |
| 3 |
@author Phil Burk http://www.softsynth.com |
| 4 |
*/ |
| 5 |
/* |
| 6 |
* $Id: patest_multi_sine.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 7 |
* |
| 8 |
* This program uses the PortAudio Portable Audio Library. |
| 9 |
* For more information see: http://www.portaudio.com |
| 10 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 11 |
* |
| 12 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 13 |
* a copy of this software and associated documentation files |
| 14 |
* (the "Software"), to deal in the Software without restriction, |
| 15 |
* including without limitation the rights to use, copy, modify, merge, |
| 16 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 17 |
* and to permit persons to whom the Software is furnished to do so, |
| 18 |
* subject to the following conditions: |
| 19 |
* |
| 20 |
* The above copyright notice and this permission notice shall be |
| 21 |
* included in all copies or substantial portions of the Software. |
| 22 |
* |
| 23 |
* Any person wishing to distribute modifications to the Software is |
| 24 |
* requested to send the modifications to the original developer so that |
| 25 |
* they can be incorporated into the canonical version. |
| 26 |
* |
| 27 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 30 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 31 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 32 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 33 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <math.h> |
| 39 |
|
| 40 |
#include "portaudio.h" |
| 41 |
|
| 42 |
#define SAMPLE_RATE (44100) |
| 43 |
#define FRAMES_PER_BUFFER (256) |
| 44 |
#define FREQ_INCR (300.0 / SAMPLE_RATE) |
| 45 |
#define MAX_CHANNELS (64) |
| 46 |
|
| 47 |
#ifndef M_PI |
| 48 |
#define M_PI (3.14159265) |
| 49 |
#endif |
| 50 |
|
| 51 |
typedef struct |
| 52 |
{ |
| 53 |
short interleaved; /* Nonzero for interleaved / zero for non-interleaved. */ |
| 54 |
int numChannels; /* Actually used. */ |
| 55 |
double phases[MAX_CHANNELS]; /* Each channel gets its' own frequency. */ |
| 56 |
} |
| 57 |
paTestData; |
| 58 |
|
| 59 |
/* This routine will be called by the PortAudio engine when audio is needed. |
| 60 |
** It may called at interrupt level on some machines so don't do anything |
| 61 |
** that could mess up the system like calling malloc() or free(). |
| 62 |
*/ |
| 63 |
static int patestCallback(const void* inputBuffer, |
| 64 |
void* outputBuffer, |
| 65 |
unsigned long framesPerBuffer, |
| 66 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 67 |
PaStreamCallbackFlags statusFlags, |
| 68 |
void* userData) |
| 69 |
{ |
| 70 |
int frameIndex, channelIndex; |
| 71 |
float** outputs = (float**)outputBuffer; |
| 72 |
paTestData* data = (paTestData*)userData; |
| 73 |
|
| 74 |
(void) inputBuffer; /* Prevent unused arg warning. */ |
| 75 |
if (data->interleaved) |
| 76 |
{ |
| 77 |
float *out = (float*)outputBuffer; /* interleaved version */ |
| 78 |
for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ ) |
| 79 |
{ |
| 80 |
for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ ) |
| 81 |
{ |
| 82 |
/* Output sine wave on every channel. */ |
| 83 |
*out++ = (float) sin(data->phases[channelIndex]); |
| 84 |
|
| 85 |
/* Play each channel at a higher frequency. */ |
| 86 |
data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex); |
| 87 |
if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ ) |
| 94 |
{ |
| 95 |
for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ ) |
| 96 |
{ |
| 97 |
/* Output sine wave on every channel. */ |
| 98 |
outputs[channelIndex][frameIndex] = (float) sin(data->phases[channelIndex]); |
| 99 |
|
| 100 |
/* Play each channel at a higher frequency. */ |
| 101 |
data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex); |
| 102 |
if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
/*******************************************************************/ |
| 110 |
int test(short interleaved) |
| 111 |
{ |
| 112 |
PaStream* stream; |
| 113 |
PaStreamParameters outputParameters; |
| 114 |
PaError err; |
| 115 |
const PaDeviceInfo* pdi; |
| 116 |
paTestData data; |
| 117 |
short n; |
| 118 |
|
| 119 |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device, max channels. */ |
| 120 |
pdi = Pa_GetDeviceInfo(outputParameters.device); |
| 121 |
outputParameters.channelCount = pdi->maxOutputChannels; |
| 122 |
if (outputParameters.channelCount > MAX_CHANNELS) |
| 123 |
outputParameters.channelCount = MAX_CHANNELS; |
| 124 |
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ |
| 125 |
outputParameters.suggestedLatency = pdi->defaultLowOutputLatency; |
| 126 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 127 |
|
| 128 |
data.interleaved = interleaved; |
| 129 |
data.numChannels = outputParameters.channelCount; |
| 130 |
for (n = 0; n < data.numChannels; n++) |
| 131 |
data.phases[n] = 0.0; /* Phases wrap and maybe don't need initialisation. */ |
| 132 |
printf("%d ", data.numChannels); |
| 133 |
if (interleaved) |
| 134 |
printf("interleaved "); |
| 135 |
else |
| 136 |
{ |
| 137 |
printf(" non-interleaved "); |
| 138 |
outputParameters.sampleFormat |= paNonInterleaved; |
| 139 |
} |
| 140 |
printf("channels.\n"); |
| 141 |
|
| 142 |
err = Pa_OpenStream(&stream, |
| 143 |
NULL, /* No input. */ |
| 144 |
&outputParameters, |
| 145 |
SAMPLE_RATE, /* Sample rate. */ |
| 146 |
FRAMES_PER_BUFFER, /* Frames per buffer. */ |
| 147 |
paClipOff, /* Samples never out of range, no clipping. */ |
| 148 |
patestCallback, |
| 149 |
&data); |
| 150 |
if (err == paNoError) |
| 151 |
{ |
| 152 |
err = Pa_StartStream(stream); |
| 153 |
if (err == paNoError) |
| 154 |
{ |
| 155 |
printf("Hit ENTER to stop this test.\n"); |
| 156 |
getchar(); |
| 157 |
err = Pa_StopStream(stream); |
| 158 |
} |
| 159 |
Pa_CloseStream( stream ); |
| 160 |
} |
| 161 |
return err; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/*******************************************************************/ |
| 166 |
int main(void) |
| 167 |
{ |
| 168 |
PaError err; |
| 169 |
|
| 170 |
printf("PortAudio Test: output sine wave on each channel.\n" ); |
| 171 |
|
| 172 |
err = Pa_Initialize(); |
| 173 |
if (err != paNoError) |
| 174 |
goto done; |
| 175 |
|
| 176 |
err = test(1); /* 1 means interleaved. */ |
| 177 |
if (err != paNoError) |
| 178 |
goto done; |
| 179 |
|
| 180 |
err = test(0); /* 0 means not interleaved. */ |
| 181 |
if (err != paNoError) |
| 182 |
goto done; |
| 183 |
|
| 184 |
printf("Test finished.\n"); |
| 185 |
done: |
| 186 |
if (err) |
| 187 |
{ |
| 188 |
fprintf(stderr, "An error occured while using the portaudio stream\n"); |
| 189 |
fprintf(stderr, "Error number: %d\n", err ); |
| 190 |
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err)); |
| 191 |
} |
| 192 |
Pa_Terminate(); |
| 193 |
return 0; |
| 194 |
} |