| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * patest_sine_formats.c |
|---|
| 4 | * Play a sine wave using the Portable Audio api for several seconds. |
|---|
| 5 | * Test various data formats. |
|---|
| 6 | * |
|---|
| 7 | * Author: Phil Burk |
|---|
| 8 | * |
|---|
| 9 | * This program uses the PortAudio Portable Audio Library. |
|---|
| 10 | * For more information see: http://www.audiomulch.com/portaudio/ |
|---|
| 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 NUM_SECONDS (10) |
|---|
| 42 | #define SAMPLE_RATE (44100) |
|---|
| 43 | #define FRAMES_PER_BUFFER (256) |
|---|
| 44 | |
|---|
| 45 | #define LEFT_FREQ (SAMPLE_RATE/512.0) /* So we hit 1.0 */ |
|---|
| 46 | #define RIGHT_FREQ (500.0) |
|---|
| 47 | |
|---|
| 48 | #define AMPLITUDE (1.0) |
|---|
| 49 | |
|---|
| 50 | /* Select ONE format for testing. */ |
|---|
| 51 | #define TEST_UINT8 (1) |
|---|
| 52 | #define TEST_INT8 (0) |
|---|
| 53 | #define TEST_INT16 (0) |
|---|
| 54 | #define TEST_FLOAT32 (0) |
|---|
| 55 | |
|---|
| 56 | #if TEST_UINT8 |
|---|
| 57 | #define TEST_FORMAT paUInt8 |
|---|
| 58 | typedef unsigned char SAMPLE_t; |
|---|
| 59 | #define SAMPLE_ZERO (0x80) |
|---|
| 60 | #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x))) |
|---|
| 61 | #define FORMAT_NAME "Unsigned 8 Bit" |
|---|
| 62 | |
|---|
| 63 | #elif TEST_INT8 |
|---|
| 64 | #define TEST_FORMAT paInt8 |
|---|
| 65 | typedef char SAMPLE_t; |
|---|
| 66 | #define SAMPLE_ZERO (0) |
|---|
| 67 | #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x))) |
|---|
| 68 | #define FORMAT_NAME "Signed 8 Bit" |
|---|
| 69 | |
|---|
| 70 | #elif TEST_INT16 |
|---|
| 71 | #define TEST_FORMAT paInt16 |
|---|
| 72 | typedef short SAMPLE_t; |
|---|
| 73 | #define SAMPLE_ZERO (0) |
|---|
| 74 | #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(32767 * (x))) |
|---|
| 75 | #define FORMAT_NAME "Signed 16 Bit" |
|---|
| 76 | |
|---|
| 77 | #elif TEST_FLOAT32 |
|---|
| 78 | #define TEST_FORMAT paFloat32 |
|---|
| 79 | typedef float SAMPLE_t; |
|---|
| 80 | #define SAMPLE_ZERO (0.0) |
|---|
| 81 | #define DOUBLE_TO_SAMPLE(x) ((SAMPLE_t)(x)) |
|---|
| 82 | #define FORMAT_NAME "Float 32 Bit" |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | #ifndef M_PI |
|---|
| 86 | #define M_PI (3.14159265) |
|---|
| 87 | #endif |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | typedef struct |
|---|
| 91 | { |
|---|
| 92 | double left_phase; |
|---|
| 93 | double right_phase; |
|---|
| 94 | unsigned int framesToGo; |
|---|
| 95 | } |
|---|
| 96 | paTestData; |
|---|
| 97 | /* This routine will be called by the PortAudio engine when audio is needed. |
|---|
| 98 | ** It may called at interrupt level on some machines so don't do anything |
|---|
| 99 | ** that could mess up the system like calling malloc() or free(). |
|---|
| 100 | */ |
|---|
| 101 | static int patestCallback( void *inputBuffer, void *outputBuffer, |
|---|
| 102 | unsigned long framesPerBuffer, |
|---|
| 103 | PaTimestamp outTime, void *userData ) |
|---|
| 104 | { |
|---|
| 105 | paTestData *data = (paTestData*)userData; |
|---|
| 106 | SAMPLE_t *out = (SAMPLE_t *)outputBuffer; |
|---|
| 107 | SAMPLE_t sample; |
|---|
| 108 | int i; |
|---|
| 109 | int framesToCalc; |
|---|
| 110 | int finished = 0; |
|---|
| 111 | (void) outTime; /* Prevent unused variable warnings. */ |
|---|
| 112 | (void) inputBuffer; |
|---|
| 113 | |
|---|
| 114 | if( data->framesToGo < framesPerBuffer ) |
|---|
| 115 | { |
|---|
| 116 | framesToCalc = data->framesToGo; |
|---|
| 117 | data->framesToGo = 0; |
|---|
| 118 | finished = 1; |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | framesToCalc = framesPerBuffer; |
|---|
| 123 | data->framesToGo -= framesPerBuffer; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | for( i=0; i<framesToCalc; i++ ) |
|---|
| 127 | { |
|---|
| 128 | data->left_phase += (LEFT_FREQ / SAMPLE_RATE); |
|---|
| 129 | if( data->left_phase > 1.0) data->left_phase -= 1.0; |
|---|
| 130 | sample = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->left_phase * M_PI * 2. ))); /**/ |
|---|
| 131 | *out++ = sample; |
|---|
| 132 | /* *out++ = sample; /**/ |
|---|
| 133 | /* *out++ = 0; /**/ |
|---|
| 134 | |
|---|
| 135 | data->right_phase += (RIGHT_FREQ / SAMPLE_RATE); |
|---|
| 136 | if( data->right_phase > 1.0) data->right_phase -= 1.0; |
|---|
| 137 | *out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->right_phase * M_PI * 2. ))); /**/ |
|---|
| 138 | /* *out++ = 0; /* */ |
|---|
| 139 | } |
|---|
| 140 | /* zero remainder of final buffer */ |
|---|
| 141 | for( ; i<(int)framesPerBuffer; i++ ) |
|---|
| 142 | { |
|---|
| 143 | *out++ = SAMPLE_ZERO; /* left */ |
|---|
| 144 | *out++ = SAMPLE_ZERO; /* right */ |
|---|
| 145 | } |
|---|
| 146 | return finished; |
|---|
| 147 | } |
|---|
| 148 | /*******************************************************************/ |
|---|
| 149 | int main(void); |
|---|
| 150 | int main(void) |
|---|
| 151 | { |
|---|
| 152 | PortAudioStream *stream; |
|---|
| 153 | PaError err; |
|---|
| 154 | paTestData data; |
|---|
| 155 | int totalSamps; |
|---|
| 156 | |
|---|
| 157 | printf("PortAudio Test: output " FORMAT_NAME "\n"); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | data.left_phase = data.right_phase = 0.0; |
|---|
| 161 | data.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */ |
|---|
| 162 | err = Pa_Initialize(); |
|---|
| 163 | if( err != paNoError ) goto error; |
|---|
| 164 | |
|---|
| 165 | err = Pa_OpenStream( |
|---|
| 166 | &stream, |
|---|
| 167 | paNoDevice,/* default input device */ |
|---|
| 168 | 0, /* no input */ |
|---|
| 169 | TEST_FORMAT, |
|---|
| 170 | NULL, |
|---|
| 171 | Pa_GetDefaultOutputDeviceID(), /* default output device */ |
|---|
| 172 | 2, /* stereo output */ |
|---|
| 173 | TEST_FORMAT, |
|---|
| 174 | NULL, |
|---|
| 175 | SAMPLE_RATE, |
|---|
| 176 | FRAMES_PER_BUFFER, |
|---|
| 177 | 0, /* number of buffers, if zero then use default minimum */ |
|---|
| 178 | paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
|---|
| 179 | patestCallback, |
|---|
| 180 | &data ); |
|---|
| 181 | if( err != paNoError ) goto error; |
|---|
| 182 | |
|---|
| 183 | err = Pa_StartStream( stream ); |
|---|
| 184 | if( err != paNoError ) goto error; |
|---|
| 185 | |
|---|
| 186 | printf("Waiting %d seconds for sound to finish.\n", NUM_SECONDS ); |
|---|
| 187 | while( Pa_StreamActive( stream ) ) Pa_Sleep(10); |
|---|
| 188 | |
|---|
| 189 | err = Pa_CloseStream( stream ); |
|---|
| 190 | if( err != paNoError ) goto error; |
|---|
| 191 | Pa_Terminate(); |
|---|
| 192 | |
|---|
| 193 | printf("PortAudio Test Finished: " FORMAT_NAME "\n"); |
|---|
| 194 | |
|---|
| 195 | return err; |
|---|
| 196 | error: |
|---|
| 197 | Pa_Terminate(); |
|---|
| 198 | fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
|---|
| 199 | fprintf( stderr, "Error number: %d\n", err ); |
|---|
| 200 | fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
|---|
| 201 | return err; |
|---|
| 202 | } |
|---|