| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * patest_buffer.c |
|---|
| 4 | * Test opening streams with different buffer sizes. |
|---|
| 5 | * |
|---|
| 6 | * Author: Phil Burk http://www.softsynth.com |
|---|
| 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 <stdlib.h> |
|---|
| 38 | #include <math.h> |
|---|
| 39 | #include "portaudio.h" |
|---|
| 40 | #define NUM_SECONDS (1) |
|---|
| 41 | #define SAMPLE_RATE (44100) |
|---|
| 42 | #ifndef M_PI |
|---|
| 43 | #define M_PI (3.14159265) |
|---|
| 44 | #endif |
|---|
| 45 | #define TABLE_SIZE (200) |
|---|
| 46 | |
|---|
| 47 | #define BUFFER_TABLE 9 |
|---|
| 48 | long buffer_table[] = {200,256,500,512,600, 723, 1000, 1024, 2345}; |
|---|
| 49 | |
|---|
| 50 | typedef struct |
|---|
| 51 | { |
|---|
| 52 | short sine[TABLE_SIZE]; |
|---|
| 53 | int left_phase; |
|---|
| 54 | int right_phase; |
|---|
| 55 | unsigned int sampsToGo; |
|---|
| 56 | } |
|---|
| 57 | paTestData; |
|---|
| 58 | PaError TestOnce( int buffersize ); |
|---|
| 59 | |
|---|
| 60 | static int patest1Callback( void *inputBuffer, void *outputBuffer, |
|---|
| 61 | unsigned long framesPerBuffer, |
|---|
| 62 | PaTimestamp outTime, void *userData ); |
|---|
| 63 | /* This routine will be called by the PortAudio engine when audio is needed. |
|---|
| 64 | ** It may called at interrupt level on some machines so don't do anything |
|---|
| 65 | ** that could mess up the system like calling malloc() or free(). |
|---|
| 66 | */ |
|---|
| 67 | static int patest1Callback( void *inputBuffer, void *outputBuffer, |
|---|
| 68 | unsigned long framesPerBuffer, |
|---|
| 69 | PaTimestamp outTime, void *userData ) |
|---|
| 70 | { |
|---|
| 71 | paTestData *data = (paTestData*)userData; |
|---|
| 72 | short *out = (short*)outputBuffer; |
|---|
| 73 | unsigned int i; |
|---|
| 74 | int finished = 0; |
|---|
| 75 | (void) inputBuffer; /* Prevent "unused variable" warnings. */ |
|---|
| 76 | (void) outTime; |
|---|
| 77 | |
|---|
| 78 | if( data->sampsToGo < framesPerBuffer ) |
|---|
| 79 | { |
|---|
| 80 | /* final buffer... */ |
|---|
| 81 | |
|---|
| 82 | for( i=0; i<data->sampsToGo; i++ ) |
|---|
| 83 | { |
|---|
| 84 | *out++ = data->sine[data->left_phase]; /* left */ |
|---|
| 85 | *out++ = data->sine[data->right_phase]; /* right */ |
|---|
| 86 | data->left_phase += 1; |
|---|
| 87 | if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
|---|
| 88 | data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ |
|---|
| 89 | if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
|---|
| 90 | } |
|---|
| 91 | /* zero remainder of final buffer */ |
|---|
| 92 | for( ; i<framesPerBuffer; i++ ) |
|---|
| 93 | { |
|---|
| 94 | *out++ = 0; /* left */ |
|---|
| 95 | *out++ = 0; /* right */ |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | finished = 1; |
|---|
| 99 | } |
|---|
| 100 | else |
|---|
| 101 | { |
|---|
| 102 | for( i=0; i<framesPerBuffer; i++ ) |
|---|
| 103 | { |
|---|
| 104 | *out++ = data->sine[data->left_phase]; /* left */ |
|---|
| 105 | *out++ = data->sine[data->right_phase]; /* right */ |
|---|
| 106 | data->left_phase += 1; |
|---|
| 107 | if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
|---|
| 108 | data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ |
|---|
| 109 | if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
|---|
| 110 | } |
|---|
| 111 | data->sampsToGo -= framesPerBuffer; |
|---|
| 112 | } |
|---|
| 113 | return finished; |
|---|
| 114 | } |
|---|
| 115 | /*******************************************************************/ |
|---|
| 116 | int main(void); |
|---|
| 117 | int main(void) |
|---|
| 118 | { |
|---|
| 119 | int i; |
|---|
| 120 | PaError err; |
|---|
| 121 | printf("Test opening streams with different buffer sizes\n\n"); |
|---|
| 122 | |
|---|
| 123 | for (i = 0 ; i < BUFFER_TABLE; i++) |
|---|
| 124 | { |
|---|
| 125 | printf("Buffer size %d\n", buffer_table[i]); |
|---|
| 126 | err = TestOnce(buffer_table[i]); |
|---|
| 127 | if( err < 0 ) return 0; |
|---|
| 128 | |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | PaError TestOnce( int buffersize ) |
|---|
| 134 | { |
|---|
| 135 | PortAudioStream *stream; |
|---|
| 136 | PaError err; |
|---|
| 137 | paTestData data; |
|---|
| 138 | int i; |
|---|
| 139 | int totalSamps; |
|---|
| 140 | /* initialise sinusoidal wavetable */ |
|---|
| 141 | for( i=0; i<TABLE_SIZE; i++ ) |
|---|
| 142 | { |
|---|
| 143 | data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. )); |
|---|
| 144 | } |
|---|
| 145 | data.left_phase = data.right_phase = 0; |
|---|
| 146 | data.sampsToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */ |
|---|
| 147 | err = Pa_Initialize(); |
|---|
| 148 | if( err != paNoError ) goto error; |
|---|
| 149 | err = Pa_OpenStream( |
|---|
| 150 | &stream, |
|---|
| 151 | paNoDevice,/* default input device */ |
|---|
| 152 | 0, /* no input */ |
|---|
| 153 | paInt16, /* sample format */ |
|---|
| 154 | NULL, |
|---|
| 155 | Pa_GetDefaultOutputDeviceID(), /* default output device */ |
|---|
| 156 | 2, /* stereo output */ |
|---|
| 157 | paInt16, /* sample format */ |
|---|
| 158 | NULL, |
|---|
| 159 | SAMPLE_RATE, |
|---|
| 160 | buffersize, /* frames per buffer */ |
|---|
| 161 | 0, /* number of buffers, if zero then use default minimum */ |
|---|
| 162 | paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
|---|
| 163 | patest1Callback, |
|---|
| 164 | &data ); |
|---|
| 165 | if( err != paNoError ) goto error; |
|---|
| 166 | |
|---|
| 167 | err = Pa_StartStream( stream ); |
|---|
| 168 | if( err != paNoError ) goto error; |
|---|
| 169 | printf("Waiting for sound to finish.\n"); |
|---|
| 170 | Pa_Sleep(1000); |
|---|
| 171 | err = Pa_CloseStream( stream ); |
|---|
| 172 | if( err != paNoError ) goto error; |
|---|
| 173 | Pa_Terminate(); |
|---|
| 174 | return paNoError; |
|---|
| 175 | error: |
|---|
| 176 | Pa_Terminate(); |
|---|
| 177 | fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
|---|
| 178 | fprintf( stderr, "Error number: %d\n", err ); |
|---|
| 179 | fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
|---|
| 180 | return err; |
|---|
| 181 | } |
|---|