| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Play a sine then hang audio callback to test watchdog. |
|---|
| 4 | * |
|---|
| 5 | * Authors: |
|---|
| 6 | * Ross Bencina <rossb@audiomulch.com> |
|---|
| 7 | * Phil Burk <philburk@softsynth.com> |
|---|
| 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 | |
|---|
| 42 | #define SAMPLE_RATE (44100) |
|---|
| 43 | #define FRAMES_PER_BUFFER (1024) |
|---|
| 44 | #ifndef M_PI |
|---|
| 45 | #define M_PI (3.14159265) |
|---|
| 46 | #endif |
|---|
| 47 | #define TWOPI (M_PI * 2.0) |
|---|
| 48 | |
|---|
| 49 | typedef struct paTestData |
|---|
| 50 | { |
|---|
| 51 | int sleepFor; |
|---|
| 52 | double phase; |
|---|
| 53 | } |
|---|
| 54 | paTestData; |
|---|
| 55 | |
|---|
| 56 | /* This routine will be called by the PortAudio engine when audio is needed. |
|---|
| 57 | ** It may called at interrupt level on some machines so don't do anything |
|---|
| 58 | ** that could mess up the system like calling malloc() or free(). |
|---|
| 59 | */ |
|---|
| 60 | static int patestCallback( void *inputBuffer, void *outputBuffer, |
|---|
| 61 | unsigned long framesPerBuffer, |
|---|
| 62 | PaTimestamp outTime, void *userData ) |
|---|
| 63 | { |
|---|
| 64 | paTestData *data = (paTestData*)userData; |
|---|
| 65 | float *out = (float*)outputBuffer; |
|---|
| 66 | unsigned long i; |
|---|
| 67 | int finished = 0; |
|---|
| 68 | double phaseInc = 0.02; |
|---|
| 69 | double phase = data->phase; |
|---|
| 70 | |
|---|
| 71 | (void) outTime; /* Prevent unused variable warnings. */ |
|---|
| 72 | (void) inputBuffer; |
|---|
| 73 | |
|---|
| 74 | for( i=0; i<framesPerBuffer; i++ ) |
|---|
| 75 | { |
|---|
| 76 | phase += phaseInc; |
|---|
| 77 | if( phase > TWOPI ) phase -= TWOPI; |
|---|
| 78 | /* This is not a very efficient way to calc sines. */ |
|---|
| 79 | *out++ = (float) sin( phase ); /* mono */ |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | if( data->sleepFor > 0 ) |
|---|
| 83 | { |
|---|
| 84 | Pa_Sleep( data->sleepFor ); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | data->phase = phase; |
|---|
| 88 | return finished; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /*******************************************************************/ |
|---|
| 92 | int main(void); |
|---|
| 93 | int main(void) |
|---|
| 94 | { |
|---|
| 95 | PortAudioStream *stream; |
|---|
| 96 | PaError err; |
|---|
| 97 | int i; |
|---|
| 98 | paTestData data = {0}; |
|---|
| 99 | |
|---|
| 100 | printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", |
|---|
| 101 | SAMPLE_RATE, FRAMES_PER_BUFFER ); |
|---|
| 102 | |
|---|
| 103 | err = Pa_Initialize(); |
|---|
| 104 | if( err != paNoError ) goto error; |
|---|
| 105 | |
|---|
| 106 | err = Pa_OpenStream( |
|---|
| 107 | &stream, |
|---|
| 108 | paNoDevice,/* default input device */ |
|---|
| 109 | 0, /* no input */ |
|---|
| 110 | paFloat32, /* 32 bit floating point input */ |
|---|
| 111 | NULL, |
|---|
| 112 | Pa_GetDefaultOutputDeviceID(), /* default output device */ |
|---|
| 113 | 1, /* mono output */ |
|---|
| 114 | paFloat32, /* 32 bit floating point output */ |
|---|
| 115 | NULL, |
|---|
| 116 | SAMPLE_RATE, |
|---|
| 117 | FRAMES_PER_BUFFER, /* frames per buffer */ |
|---|
| 118 | 0, /* number of buffers, if zero then use default minimum */ |
|---|
| 119 | paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
|---|
| 120 | patestCallback, |
|---|
| 121 | &data ); |
|---|
| 122 | if( err != paNoError ) goto error; |
|---|
| 123 | |
|---|
| 124 | err = Pa_StartStream( stream ); |
|---|
| 125 | if( err != paNoError ) goto error; |
|---|
| 126 | |
|---|
| 127 | /* Gradually increase sleep time. */ |
|---|
| 128 | for( i=0; i<10000; i+= 1000 ) |
|---|
| 129 | { |
|---|
| 130 | printf("Sleep for %d milliseconds in audio callback.\n", i ); |
|---|
| 131 | data.sleepFor = i; |
|---|
| 132 | Pa_Sleep( ((i<1000) ? 1000 : i) ); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | printf("Suffer for 10 seconds.\n"); |
|---|
| 136 | Pa_Sleep( 10000 ); |
|---|
| 137 | |
|---|
| 138 | err = Pa_StopStream( stream ); |
|---|
| 139 | if( err != paNoError ) goto error; |
|---|
| 140 | err = Pa_CloseStream( stream ); |
|---|
| 141 | if( err != paNoError ) goto error; |
|---|
| 142 | Pa_Terminate(); |
|---|
| 143 | printf("Test finished.\n"); |
|---|
| 144 | return err; |
|---|
| 145 | error: |
|---|
| 146 | Pa_Terminate(); |
|---|
| 147 | fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
|---|
| 148 | fprintf( stderr, "Error number: %d\n", err ); |
|---|
| 149 | fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
|---|
| 150 | return err; |
|---|
| 151 | } |
|---|