paex_write_sine.c (5772B)
1 /** @file paex_write_sine.c 2 @ingroup examples_src 3 @brief Play a sine wave for several seconds using the blocking API (Pa_WriteStream()) 4 @author Ross Bencina <rossb@audiomulch.com> 5 @author Phil Burk <philburk@softsynth.com> 6 */ 7 /* 8 * $Id$ 9 * 10 * This program uses the PortAudio Portable Audio Library. 11 * For more information see: http://www.portaudio.com/ 12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining 15 * a copy of this software and associated documentation files 16 * (the "Software"), to deal in the Software without restriction, 17 * including without limitation the rights to use, copy, modify, merge, 18 * publish, distribute, sublicense, and/or sell copies of the Software, 19 * and to permit persons to whom the Software is furnished to do so, 20 * subject to the following conditions: 21 * 22 * The above copyright notice and this permission notice shall be 23 * included in all copies or substantial portions of the Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 */ 33 34 /* 35 * The text above constitutes the entire PortAudio license; however, 36 * the PortAudio community also makes the following non-binding requests: 37 * 38 * Any person wishing to distribute modifications to the Software is 39 * requested to send the modifications to the original developer so that 40 * they can be incorporated into the canonical version. It is also 41 * requested that these non-binding requests be included along with the 42 * license above. 43 */ 44 45 #include <stdio.h> 46 #include <math.h> 47 #include "portaudio.h" 48 49 #define NUM_SECONDS (5) 50 #define SAMPLE_RATE (44100) 51 #define FRAMES_PER_BUFFER (1024) 52 53 #ifndef M_PI 54 #define M_PI (3.14159265) 55 #endif 56 57 #define TABLE_SIZE (200) 58 59 60 int main(void); 61 int main(void) 62 { 63 PaStreamParameters outputParameters; 64 PaStream *stream; 65 PaError err; 66 float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */ 67 float sine[TABLE_SIZE]; /* sine wavetable */ 68 int left_phase = 0; 69 int right_phase = 0; 70 int left_inc = 1; 71 int right_inc = 3; /* higher pitch so we can distinguish left and right. */ 72 int i, j, k; 73 int bufferCount; 74 75 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); 76 77 /* initialise sinusoidal wavetable */ 78 for( i=0; i<TABLE_SIZE; i++ ) 79 { 80 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); 81 } 82 83 84 err = Pa_Initialize(); 85 if( err != paNoError ) goto error; 86 87 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 88 if (outputParameters.device == paNoDevice) { 89 fprintf(stderr,"Error: No default output device.\n"); 90 goto error; 91 } 92 outputParameters.channelCount = 2; /* stereo output */ 93 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ 94 outputParameters.suggestedLatency = 0.050; // Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 95 outputParameters.hostApiSpecificStreamInfo = NULL; 96 97 err = Pa_OpenStream( 98 &stream, 99 NULL, /* no input */ 100 &outputParameters, 101 SAMPLE_RATE, 102 FRAMES_PER_BUFFER, 103 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 104 NULL, /* no callback, use blocking API */ 105 NULL ); /* no callback, so no callback userData */ 106 if( err != paNoError ) goto error; 107 108 109 printf( "Play 3 times, higher each time.\n" ); 110 111 for( k=0; k < 3; ++k ) 112 { 113 err = Pa_StartStream( stream ); 114 if( err != paNoError ) goto error; 115 116 printf("Play for %d seconds.\n", NUM_SECONDS ); 117 118 bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER); 119 120 for( i=0; i < bufferCount; i++ ) 121 { 122 for( j=0; j < FRAMES_PER_BUFFER; j++ ) 123 { 124 buffer[j][0] = sine[left_phase]; /* left */ 125 buffer[j][1] = sine[right_phase]; /* right */ 126 left_phase += left_inc; 127 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE; 128 right_phase += right_inc; 129 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE; 130 } 131 132 err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER ); 133 if( err != paNoError ) goto error; 134 } 135 136 err = Pa_StopStream( stream ); 137 if( err != paNoError ) goto error; 138 139 ++left_inc; 140 ++right_inc; 141 142 Pa_Sleep( 1000 ); 143 } 144 145 err = Pa_CloseStream( stream ); 146 if( err != paNoError ) goto error; 147 148 Pa_Terminate(); 149 printf("Test finished.\n"); 150 151 return err; 152 153 error: 154 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 155 fprintf( stderr, "Error number: %d\n", err ); 156 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 157 // Print more information about the error. 158 if( err == paUnanticipatedHostError ) 159 { 160 const PaHostErrorInfo *hostErrorInfo = Pa_GetLastHostErrorInfo(); 161 fprintf( stderr, "Host API error = #%ld, hostApiType = %d\n", hostErrorInfo->errorCode, hostErrorInfo->hostApiType ); 162 fprintf( stderr, "Host API error = %s\n", hostErrorInfo->errorText ); 163 } 164 Pa_Terminate(); 165 return err; 166 }