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

History | View | Annotate | Download (5.73 KB)

1
/** @file patest_two_rates.c
2
        @ingroup test_src
3
        @brief Play two streams at different rates to make sure they don't interfere.
4
        @author Phil Burk <philburk@softsynth.com>
5
*/
6
/*
7
 * $Id$
8
 *
9
 * Author: Phil Burk  http://www.softsynth.com
10
 *
11
 * This program uses the PortAudio Portable Audio Library.
12
 * For more information see: http://www.portaudio.com
13
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining
16
 * a copy of this software and associated documentation files
17
 * (the "Software"), to deal in the Software without restriction,
18
 * including without limitation the rights to use, copy, modify, merge,
19
 * publish, distribute, sublicense, and/or sell copies of the Software,
20
 * and to permit persons to whom the Software is furnished to do so,
21
 * subject to the following conditions:
22
 *
23
 * The above copyright notice and this permission notice shall be
24
 * included in all copies or substantial portions of the Software.
25
 *
26
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
30
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
31
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
 */
34

    
35
/*
36
 * The text above constitutes the entire PortAudio license; however, 
37
 * the PortAudio community also makes the following non-binding requests:
38
 *
39
 * Any person wishing to distribute modifications to the Software is
40
 * requested to send the modifications to the original developer so that
41
 * they can be incorporated into the canonical version. It is also 
42
 * requested that these non-binding requests be included along with the 
43
 * license above.
44
 */
45

    
46
#include <stdio.h>
47
#include <math.h>
48
#include "portaudio.h"
49

    
50
#define OUTPUT_DEVICE       (Pa_GetDefaultOutputDeviceID())
51
#define SAMPLE_RATE_1       (44100)
52
#define SAMPLE_RATE_2       (48000)
53
#define FRAMES_PER_BUFFER   (256)
54
#define FREQ_INCR           (0.1)
55

    
56
#ifndef M_PI
57
#define M_PI  (3.14159265)
58
#endif
59

    
60
typedef struct
61
{
62
    double phase;
63
    int    numFrames;
64
} paTestData;
65

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

    
82
    for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ )
83
    {
84
        /* Generate sine wave. */
85
        float value = (float) 0.3 * sin(data->phase);
86
        /* Stereo - two channels. */
87
        *out++ = value;
88
        *out++ = value;
89

    
90
        data->phase += FREQ_INCR;
91
        if( data->phase >= (2.0 * M_PI) ) data->phase -= (2.0 * M_PI);
92
    }
93
        data->numFrames += 1;
94
    return 0;
95
}
96

    
97
/*******************************************************************/
98
int main(void);
99
int main(void)
100
{
101
    PaError err;
102
    PaStreamParameters outputParameters;
103
    PaStream *stream1;
104
    PaStream *stream2;
105
    paTestData data1 = {0};
106
    paTestData data2 = {0};
107
    printf("PortAudio Test: two rates.\n" );
108

    
109
    err = Pa_Initialize();
110
    if( err != paNoError ) goto error;
111
    
112
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
113
    if (outputParameters.device == paNoDevice) {
114
                fprintf(stderr,"Error: No default output device.\n");
115
                goto error;
116
    }
117
    outputParameters.channelCount = 2;       /* stereo output */
118
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
119
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
120
    outputParameters.hostApiSpecificStreamInfo = NULL;
121

    
122
    /* Start first stream. **********************/        
123
    err = Pa_OpenStream(
124
                                                &stream1,
125
                                                NULL, /* no input */
126
                                                &outputParameters,
127
                                                SAMPLE_RATE_1,
128
                                                FRAMES_PER_BUFFER,
129
                                                paClipOff,      /* we won't output out of range samples so don't bother clipping them */
130
                                                patestCallback,
131
                                                &data1 );
132
        if( err != paNoError ) goto error;
133

    
134
    err = Pa_StartStream( stream1 );
135
    if( err != paNoError ) goto error;
136

    
137
    Pa_Sleep( 3 * 1000 );
138

    
139
    /* Start second stream. **********************/
140
    err = Pa_OpenStream(
141
                                                &stream2,
142
                                                NULL, /* no input */
143
                                                &outputParameters,
144
                                                SAMPLE_RATE_2,
145
                                                FRAMES_PER_BUFFER,
146
                                                paClipOff,      /* we won't output out of range samples so don't bother clipping them */
147
                                                patestCallback,
148
                                                &data2 );
149
    if( err != paNoError ) goto error;
150

    
151
    err = Pa_StartStream( stream2 );
152
    if( err != paNoError ) goto error;
153

    
154
    Pa_Sleep( 3 * 1000 );
155
    
156
    err = Pa_StopStream( stream2 );
157
    if( err != paNoError ) goto error;
158

    
159
    Pa_Sleep( 3 * 1000 );
160
    
161
    err = Pa_StopStream( stream1 );
162
    if( err != paNoError ) goto error;
163

    
164
    Pa_CloseStream( stream2 );
165
    Pa_CloseStream( stream1 );
166
    
167
    Pa_Terminate();
168
        
169
    printf("NumFrames = %d on stream1, %d on stream2.\n", data1.numFrames, data2.numFrames );
170
    printf("Test finished.\n");
171
    return err;
172
error:
173
    Pa_Terminate();
174
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
175
    fprintf( stderr, "Error number: %d\n", err );
176
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
177
    return err;
178
}