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_write_stop.c @ 164:9fa11135915a

History | View | Annotate | Download (5.74 KB)

1
/** @file patest_write_stop.c
2
        @brief Play a few seconds of silence followed by a few cycles of a sine wave. Tests to make sure that pa_StopStream() completes playback in blocking I/O
3
        @author Bjorn Roche of XO Audio (www.xoaudio.com)
4
        @author Ross Bencina
5
        @author Phil Burk
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
#include "portaudio.h"
48

    
49
#define NUM_SECONDS         (5)
50
#define SAMPLE_RATE         (44100)
51
#define FRAMES_PER_BUFFER   (1024)
52

    
53
#ifndef M_PI
54
#define M_PI  (3.14159265)
55
#endif
56

    
57
#define TABLE_SIZE   (200)
58

    
59

    
60
int main(void);
61
int main(void)
62
{
63
    PaStreamParameters outputParameters;
64
    PaStream *stream;
65
    PaError err;
66
    float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
67
    float sine[TABLE_SIZE]; /* sine wavetable */
68
    int left_phase = 0;
69
    int right_phase = 0;
70
    int left_inc = 1;
71
    int right_inc = 3; /* higher pitch so we can distinguish left and right. */
72
    int i, j;
73
    int bufferCount;
74
    const int   framesBy2  = FRAMES_PER_BUFFER >> 1;
75
    const float framesBy2f = (float) framesBy2 ;
76

    
77
    
78
    printf( "PortAudio Test: output silence, followed by one buffer of a ramped sine wave. SR = %d, BufSize = %d\n",
79
            SAMPLE_RATE, FRAMES_PER_BUFFER);
80
    
81
    /* initialise sinusoidal wavetable */
82
    for( i=0; i<TABLE_SIZE; i++ )
83
    {
84
        sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
85
    }
86

    
87
    
88
    err = Pa_Initialize();
89
    if( err != paNoError ) goto error;
90

    
91
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
92
    outputParameters.channelCount = 2;       /* stereo output */
93
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
94
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency * 5;
95
    outputParameters.hostApiSpecificStreamInfo = NULL;
96

    
97
    /* open the stream */
98
    err = Pa_OpenStream(
99
              &stream,
100
              NULL, /* no input */
101
              &outputParameters,
102
              SAMPLE_RATE,
103
              FRAMES_PER_BUFFER,
104
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
105
              NULL, /* no callback, use blocking API */
106
              NULL ); /* no callback, so no callback userData */
107
    if( err != paNoError ) goto error;
108

    
109
    /* start the stream */
110
    err = Pa_StartStream( stream );
111
    if( err != paNoError ) goto error;
112

    
113
    printf("Playing %d seconds of silence followed by one buffer of a ramped sinusoid.\n", NUM_SECONDS );
114

    
115
    bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
116

    
117
    /* clear buffer */
118
    for( j=0; j < FRAMES_PER_BUFFER; j++ )
119
    {
120
        buffer[j][0] = 0;  /* left */
121
        buffer[j][1] = 0;  /* right */
122
    }
123
    /* play the silent buffer a bunch o' times */
124
    for( i=0; i < bufferCount; i++ )
125
    {
126
        err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
127
        if( err != paNoError ) goto error;
128
    }   
129
    /* play a non-silent buffer once */
130
    for( j=0; j < FRAMES_PER_BUFFER; j++ )
131
    {
132
        float ramp = 1;
133
        if( j < framesBy2 )
134
           ramp = j / framesBy2f;
135
        else
136
           ramp = (FRAMES_PER_BUFFER - j) / framesBy2f ;
137

    
138
        buffer[j][0] = sine[left_phase] * ramp;  /* left */
139
        buffer[j][1] = sine[right_phase] * ramp;  /* right */
140
        left_phase += left_inc;
141
        if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
142
        right_phase += right_inc;
143
        if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
144
    }
145
    err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
146
    if( err != paNoError ) goto error;
147

    
148
    /* stop stream, close, and terminate */
149
    err = Pa_StopStream( stream );
150
    if( err != paNoError ) goto error;
151

    
152
    err = Pa_CloseStream( stream );
153
    if( err != paNoError ) goto error;
154

    
155
    Pa_Terminate();
156
    printf("Test finished.\n");
157
    
158
    return err;
159
error:
160
    Pa_Terminate();
161
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
162
    fprintf( stderr, "Error number: %d\n", err );
163
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
164
    return err;
165
}