1
/** @file patest_two_rates.c
3
@brief Play two streams at different rates to make sure they don't interfere.
4
@author Phil Burk <philburk@softsynth.com>
7
* $Id: patest_two_rates.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
9
* Author: Phil Burk http://www.softsynth.com
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
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:
23
* The above copyright notice and this permission notice shall be
24
* included in all copies or substantial portions of the Software.
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.
36
* The text above constitutes the entire PortAudio license; however,
37
* the PortAudio community also makes the following non-binding requests:
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
48
#include "portaudio.h"
50
#define OUTPUT_DEVICE (Pa_GetDefaultOutputDeviceID())
51
#define SAMPLE_RATE_1 (44100)
52
#define SAMPLE_RATE_2 (44100)
53
#define FRAMES_PER_BUFFER (256)
54
#define FREQ_INCR (0.1)
57
#define M_PI (3.14159265)
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().
70
static int patestCallback( void *inputBuffer, void *outputBuffer,
71
unsigned long framesPerBuffer,
72
PaTimestamp outTime, void *userData )
74
paTestData *data = (paTestData*)userData;
75
float *out = (float*)outputBuffer;
76
int frameIndex, channelIndex;
78
(void) outTime; /* Prevent unused variable warnings. */
81
for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ )
83
/* Generate sine wave. */
84
float value = (float) 0.3 * sin(data->phase);
85
/* Stereo - two channels. */
89
data->phase += FREQ_INCR;
90
if( data->phase >= (2.0 * M_PI) ) data->phase -= (2.0 * M_PI);
96
/*******************************************************************/
101
PortAudioStream *stream1;
102
PortAudioStream *stream2;
103
paTestData data1 = {0};
104
paTestData data2 = {0};
105
printf("PortAudio Test: two rates.\n" );
107
err = Pa_Initialize();
108
if( err != paNoError ) goto error;
110
/* Start first stream. **********************/
113
paNoDevice, /* default input device */
115
paFloat32, /* 32 bit floating point input */
119
paFloat32, /* 32 bit floating point output */
122
FRAMES_PER_BUFFER, /* frames per buffer */
123
0, /* number of buffers, if zero then use default minimum */
124
paClipOff, /* we won't output out of range samples so don't bother clipping them */
127
if( err != paNoError ) goto error;
129
err = Pa_StartStream( stream1 );
130
if( err != paNoError ) goto error;
132
Pa_Sleep( 3 * 1000 );
134
/* Start second stream. **********************/
137
paNoDevice, /* default input device */
139
paFloat32, /* 32 bit floating point input */
143
paFloat32, /* 32 bit floating point output */
146
FRAMES_PER_BUFFER, /* frames per buffer */
147
0, /* number of buffers, if zero then use default minimum */
148
paClipOff, /* we won't output out of range samples so don't bother clipping them */
151
if( err != paNoError ) goto error;
153
err = Pa_StartStream( stream2 );
154
if( err != paNoError ) goto error;
156
Pa_Sleep( 3 * 1000 );
158
err = Pa_StopStream( stream2 );
159
if( err != paNoError ) goto error;
161
Pa_Sleep( 3 * 1000 );
163
err = Pa_StopStream( stream1 );
164
if( err != paNoError ) goto error;
166
Pa_CloseStream( stream2 );
167
Pa_CloseStream( stream1 );
170
printf("Test finished.\n");
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 ) );