| 1 |
/* |
| 2 |
* $Id: debug_sine_amp.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 3 |
* Play a different sine wave on each channels, |
| 4 |
* using the Portable Audio api. |
| 5 |
* Allos amplitude to be set interactively. |
| 6 |
* |
| 7 |
* Author: Phil Burk http://www.softsynth.com |
| 8 |
* |
| 9 |
* This program uses the PortAudio Portable Audio Library. |
| 10 |
* For more information see: http://www.portaudio.com |
| 11 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 12 |
* |
| 13 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 14 |
* a copy of this software and associated documentation files |
| 15 |
* (the "Software"), to deal in the Software without restriction, |
| 16 |
* including without limitation the rights to use, copy, modify, merge, |
| 17 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 18 |
* and to permit persons to whom the Software is furnished to do so, |
| 19 |
* subject to the following conditions: |
| 20 |
* |
| 21 |
* The above copyright notice and this permission notice shall be |
| 22 |
* included in all copies or substantial portions of the Software. |
| 23 |
* |
| 24 |
* Any person wishing to distribute modifications to the Software is |
| 25 |
* requested to send the modifications to the original developer so that |
| 26 |
* they can be incorporated into the canonical version. |
| 27 |
* |
| 28 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 29 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 30 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 31 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 32 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 33 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 34 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 35 |
* |
| 36 |
*/ |
| 37 |
#include <stdio.h> |
| 38 |
#include <math.h> |
| 39 |
#include "portaudio.h" |
| 40 |
|
| 41 |
#define OUTPUT_DEVICE (Pa_GetDefaultOutputDeviceID()) |
| 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 |
int numChannels; |
| 54 |
double phases[MAX_CHANNELS]; |
| 55 |
float amplitude; |
| 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( void *inputBuffer, void *outputBuffer, |
| 64 |
unsigned long framesPerBuffer, |
| 65 |
PaTimestamp outTime, void *userData ) |
| 66 |
{ |
| 67 |
paTestData *data = (paTestData*)userData; |
| 68 |
float *out = (float*)outputBuffer; |
| 69 |
int frameIndex, channelIndex; |
| 70 |
(void) outTime; /* Prevent unused variable warnings. */ |
| 71 |
(void) inputBuffer; |
| 72 |
|
| 73 |
for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ ) |
| 74 |
{ |
| 75 |
for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ ) |
| 76 |
{ |
| 77 |
/* Output sine wave on every channel. */ |
| 78 |
*out++ = (float) ( data->amplitude * sin(data->phases[channelIndex]) ); |
| 79 |
|
| 80 |
/* Play each channel at a higher frequency. */ |
| 81 |
data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex); |
| 82 |
if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
/*******************************************************************/ |
| 89 |
int main(void); |
| 90 |
int main(void) |
| 91 |
{ |
| 92 |
char pad[256]; |
| 93 |
PortAudioStream *stream; |
| 94 |
PaError err; |
| 95 |
const PaDeviceInfo *pdi; |
| 96 |
paTestData data = {0}; |
| 97 |
printf("PortAudio Test: output sine wave on each channel.\n" ); |
| 98 |
|
| 99 |
err = Pa_Initialize(); |
| 100 |
if( err != paNoError ) goto error; |
| 101 |
|
| 102 |
pdi = Pa_GetDeviceInfo( OUTPUT_DEVICE ); |
| 103 |
data.numChannels = pdi->maxOutputChannels; |
| 104 |
if( data.numChannels > MAX_CHANNELS ) data.numChannels = MAX_CHANNELS; |
| 105 |
printf("Number of Channels = %d\n", data.numChannels ); |
| 106 |
data.amplitude = 1.0; |
| 107 |
|
| 108 |
err = Pa_OpenStream( |
| 109 |
&stream, |
| 110 |
paNoDevice, /* default input device */ |
| 111 |
0, /* no input */ |
| 112 |
paFloat32, /* 32 bit floating point input */ |
| 113 |
NULL, |
| 114 |
OUTPUT_DEVICE, |
| 115 |
data.numChannels, |
| 116 |
paFloat32, /* 32 bit floating point output */ |
| 117 |
NULL, |
| 118 |
SAMPLE_RATE, |
| 119 |
FRAMES_PER_BUFFER, /* frames per buffer */ |
| 120 |
0, /* number of buffers, if zero then use default minimum */ |
| 121 |
paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
| 122 |
patestCallback, |
| 123 |
&data ); |
| 124 |
if( err != paNoError ) goto error; |
| 125 |
|
| 126 |
err = Pa_StartStream( stream ); |
| 127 |
if( err != paNoError ) goto error; |
| 128 |
|
| 129 |
do |
| 130 |
{ |
| 131 |
printf("Current amplitude = %f\n", data.amplitude ); |
| 132 |
printf("Enter new amplitude or 'q' to quit.\n"); |
| 133 |
fflush(stdout); |
| 134 |
gets( pad ); |
| 135 |
if( pad[0] != 'q' ) |
| 136 |
{ |
| 137 |
// I tried to use atof but it seems to be broken on Mac OS X 10.1 |
| 138 |
float amp; |
| 139 |
sscanf( pad, "%f", & ); |
| 140 |
data.amplitude = amp; |
| 141 |
} |
| 142 |
} while( pad[0] != 'q' ); |
| 143 |
|
| 144 |
err = Pa_StopStream( stream ); |
| 145 |
if( err != paNoError ) goto error; |
| 146 |
|
| 147 |
Pa_CloseStream( stream ); |
| 148 |
Pa_Terminate(); |
| 149 |
printf("Test finished.\n"); |
| 150 |
return err; |
| 151 |
error: |
| 152 |
Pa_Terminate(); |
| 153 |
fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
| 154 |
fprintf( stderr, "Error number: %d\n", err ); |
| 155 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 156 |
return err; |
| 157 |
} |