| 1 |
/** @file patest_saw.c |
| 2 |
@brief Play a simple (aliasing) sawtooth wave. |
| 3 |
@author Phil Burk http://www.softsynth.com |
| 4 |
*/ |
| 5 |
/* |
| 6 |
* $Id: patest_saw.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 |
#include <stdio.h> |
| 37 |
#include <math.h> |
| 38 |
#include "portaudio.h" |
| 39 |
#define NUM_SECONDS (4) |
| 40 |
#define SAMPLE_RATE (44100) |
| 41 |
|
| 42 |
typedef struct |
| 43 |
{ |
| 44 |
float left_phase; |
| 45 |
float right_phase; |
| 46 |
} |
| 47 |
paTestData; |
| 48 |
|
| 49 |
/* This routine will be called by the PortAudio engine when audio is needed. |
| 50 |
** It may called at interrupt level on some machines so don't do anything |
| 51 |
** that could mess up the system like calling malloc() or free(). |
| 52 |
*/ |
| 53 |
static int patestCallback( const void *inputBuffer, void *outputBuffer, |
| 54 |
unsigned long framesPerBuffer, |
| 55 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 56 |
PaStreamCallbackFlags statusFlags, |
| 57 |
void *userData ) |
| 58 |
{ |
| 59 |
/* Cast data passed through stream to our structure. */ |
| 60 |
paTestData *data = (paTestData*)userData; |
| 61 |
float *out = (float*)outputBuffer; |
| 62 |
unsigned int i; |
| 63 |
(void) inputBuffer; /* Prevent unused variable warning. */ |
| 64 |
|
| 65 |
for( i=0; i<framesPerBuffer; i++ ) |
| 66 |
{ |
| 67 |
*out++ = data->left_phase; /* left */ |
| 68 |
*out++ = data->right_phase; /* right */ |
| 69 |
/* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */ |
| 70 |
data->left_phase += 0.01f; |
| 71 |
/* When signal reaches top, drop back down. */ |
| 72 |
if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f; |
| 73 |
/* higher pitch so we can distinguish left and right. */ |
| 74 |
data->right_phase += 0.03f; |
| 75 |
if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f; |
| 76 |
} |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
/*******************************************************************/ |
| 81 |
static paTestData data; |
| 82 |
int main(void); |
| 83 |
int main(void) |
| 84 |
{ |
| 85 |
PaStream *stream; |
| 86 |
PaError err; |
| 87 |
|
| 88 |
printf("PortAudio Test: output sawtooth wave.\n"); |
| 89 |
/* Initialize our data for use by callback. */ |
| 90 |
data.left_phase = data.right_phase = 0.0; |
| 91 |
/* Initialize library before making any other calls. */ |
| 92 |
err = Pa_Initialize(); |
| 93 |
if( err != paNoError ) goto error; |
| 94 |
|
| 95 |
/* Open an audio I/O stream. */ |
| 96 |
err = Pa_OpenDefaultStream( &stream, |
| 97 |
0, /* no input channels */ |
| 98 |
2, /* stereo output */ |
| 99 |
paFloat32, /* 32 bit floating point output */ |
| 100 |
SAMPLE_RATE, |
| 101 |
256, /* frames per buffer */ |
| 102 |
patestCallback, |
| 103 |
&data ); |
| 104 |
if( err != paNoError ) goto error; |
| 105 |
|
| 106 |
err = Pa_StartStream( stream ); |
| 107 |
if( err != paNoError ) goto error; |
| 108 |
|
| 109 |
/* Sleep for several seconds. */ |
| 110 |
Pa_Sleep(NUM_SECONDS*1000); |
| 111 |
|
| 112 |
err = Pa_StopStream( stream ); |
| 113 |
if( err != paNoError ) goto error; |
| 114 |
err = Pa_CloseStream( stream ); |
| 115 |
if( err != paNoError ) goto error; |
| 116 |
Pa_Terminate(); |
| 117 |
printf("Test finished.\n"); |
| 118 |
return err; |
| 119 |
error: |
| 120 |
Pa_Terminate(); |
| 121 |
fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
| 122 |
fprintf( stderr, "Error number: %d\n", err ); |
| 123 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 124 |
return err; |
| 125 |
} |