To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / src / portaudio_20161030_catalina_patch / test / patest_hang.c @ 162:d43aab368df9

History | View | Annotate | Download (5.61 KB)

1
/** @file patest_hang.c
2
        @ingroup test_src
3
        @brief Play a sine then hang audio callback to test watchdog.
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

    
48
#include "portaudio.h"
49

    
50
#define SAMPLE_RATE       (44100)
51
#define FRAMES_PER_BUFFER (1024)
52
#ifndef M_PI
53
#define M_PI              (3.14159265)
54
#endif
55
#define TWOPI             (M_PI * 2.0)
56

    
57
typedef struct paTestData
58
{
59
    int    sleepFor;
60
    double phase;
61
}
62
paTestData;
63

    
64
/* This routine will be called by the PortAudio engine when audio is needed.
65
** It may called at interrupt level on some machines so don't do anything
66
** that could mess up the system like calling malloc() or free().
67
*/
68
static int patestCallback( const void *inputBuffer, void *outputBuffer,
69
                           unsigned long framesPerBuffer,
70
                           const PaStreamCallbackTimeInfo* timeInfo,
71
                           PaStreamCallbackFlags statusFlags,
72
                           void *userData )
73
{
74
    paTestData *data = (paTestData*)userData;
75
    float *out = (float*)outputBuffer;
76
    unsigned long i;
77
    int finished = 0;
78
    double phaseInc = 0.02;
79
    double phase = data->phase;
80
    
81
    (void) inputBuffer; /* Prevent unused argument warning. */
82

    
83
    for( i=0; i<framesPerBuffer; i++ )
84
    {
85
        phase += phaseInc;
86
        if( phase > TWOPI ) phase -= TWOPI;
87
        /* This is not a very efficient way to calc sines. */
88
        *out++ = (float) sin( phase ); /* mono */
89
    }
90
    
91
    if( data->sleepFor > 0 )
92
    {
93
        Pa_Sleep( data->sleepFor );
94
    }
95
    
96
    data->phase = phase;
97
    return finished;
98
}
99

    
100
/*******************************************************************/
101
int main(void);
102
int main(void)
103
{
104
    PaStream*           stream;
105
    PaStreamParameters  outputParameters;
106
    PaError             err;
107
    int                 i;
108
    paTestData          data = {0};
109
    
110
    printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",
111
        SAMPLE_RATE, FRAMES_PER_BUFFER );
112

    
113
    err = Pa_Initialize();
114
    if( err != paNoError ) goto error;
115

    
116
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
117
    if (outputParameters.device == paNoDevice) {
118
      fprintf(stderr,"Error: No default output device.\n");
119
      goto error;
120
    }
121
    outputParameters.channelCount = 1;                     /* Mono output. */
122
    outputParameters.sampleFormat = paFloat32;             /* 32 bit floating point. */
123
    outputParameters.hostApiSpecificStreamInfo = NULL;
124
    outputParameters.suggestedLatency          = Pa_GetDeviceInfo(outputParameters.device)
125
                                                 ->defaultLowOutputLatency;
126
    err = Pa_OpenStream(&stream,
127
                        NULL,                    /* No input. */
128
                        &outputParameters,
129
                        SAMPLE_RATE,
130
                        FRAMES_PER_BUFFER,
131
                        paClipOff,               /* No out of range samples. */
132
                        patestCallback,
133
                        &data);
134
    if (err != paNoError) goto error;
135
    
136
    err = Pa_StartStream( stream );
137
    if( err != paNoError ) goto error;
138

    
139
    /* Gradually increase sleep time. */
140
    /* Was: for( i=0; i<10000; i+= 1000 ) */
141
    for(i=0; i <= 1000; i += 100)
142
    {
143
        printf("Sleep for %d milliseconds in audio callback.\n", i );
144
        data.sleepFor = i;
145
        Pa_Sleep( ((i<1000) ? 1000 : i) );
146
    }
147
    
148
    printf("Suffer for 10 seconds.\n");
149
    Pa_Sleep( 10000 );
150
    
151
    err = Pa_StopStream( stream );
152
    if( err != paNoError ) goto error;
153
    err = Pa_CloseStream( stream );
154
    if( err != paNoError ) goto error;
155
    Pa_Terminate();
156
    printf("Test finished.\n");
157
    return err;
158
error:
159
    Pa_Terminate();
160
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
161
    fprintf( stderr, "Error number: %d\n", err );
162
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
163
    return err;
164
}