Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_hang.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /** @file patest_hang.c
2 @brief Play a sine then hang audio callback to test watchdog.
3 @author Ross Bencina <[email protected]>
4 @author Phil Burk <[email protected]>
5 */
6 /*
7 * $Id: patest_hang.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
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
40 #include "portaudio.h"
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( const void *inputBuffer, void *outputBuffer,
61 unsigned long framesPerBuffer,
62 const PaStreamCallbackTimeInfo* timeInfo,
63 PaStreamCallbackFlags statusFlags,
64 void *userData )
65 {
66 paTestData *data = (paTestData*)userData;
67 float *out = (float*)outputBuffer;
68 unsigned long i;
69 int finished = 0;
70 double phaseInc = 0.02;
71 double phase = data->phase;
72
73 (void) inputBuffer; /* Prevent unused argument warning. */
74
75 for( i=0; i<framesPerBuffer; i++ )
76 {
77 phase += phaseInc;
78 if( phase > TWOPI ) phase -= TWOPI;
79 /* This is not a very efficient way to calc sines. */
80 *out++ = (float) sin( phase ); /* mono */
81 }
82
83 if( data->sleepFor > 0 )
84 {
85 Pa_Sleep( data->sleepFor );
86 }
87
88 data->phase = phase;
89 return finished;
90 }
91
92 /*******************************************************************/
93 int main(void);
94 int main(void)
95 {
96 PaStream* stream;
97 PaStreamParameters outputParameters;
98 PaError err;
99 int i;
100 paTestData data = {0};
101
102 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",
103 SAMPLE_RATE, FRAMES_PER_BUFFER );
104
105 err = Pa_Initialize();
106 if( err != paNoError ) goto error;
107
108 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
109 outputParameters.channelCount = 1; /* Mono output. */
110 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point. */
111 outputParameters.hostApiSpecificStreamInfo = NULL;
112 outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)
113 ->defaultLowOutputLatency;
114 err = Pa_OpenStream(&stream,
115 NULL, /* No input. */
116 &outputParameters,
117 SAMPLE_RATE,
118 FRAMES_PER_BUFFER,
119 paClipOff, /* No out of range samples. */
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 /* Was: for( i=0; i<10000; i+= 1000 ) */
129 for(i=0; i <= 1000; i += 100)
130 {
131 printf("Sleep for %d milliseconds in audio callback.\n", i );
132 data.sleepFor = i;
133 Pa_Sleep( ((i<1000) ? 1000 : i) );
134 }
135
136 printf("Suffer for 10 seconds.\n");
137 Pa_Sleep( 10000 );
138
139 err = Pa_StopStream( stream );
140 if( err != paNoError ) goto error;
141 err = Pa_CloseStream( stream );
142 if( err != paNoError ) goto error;
143 Pa_Terminate();
144 printf("Test finished.\n");
145 return err;
146 error:
147 Pa_Terminate();
•148 fprintf( stderr, "An error occurred while using the portaudio stream\n" );
149 fprintf( stderr, "Error number: %d\n", err );
150 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
151 return err;
152 }