1
/** @file patest_leftright.c
2
@brief Play different tone sine waves that
3
alternate between left and right channel.
5
The low tone should be on the left channel.
7
@author Ross Bencina <rossb@audiomulch.com>
8
@author Phil Burk <philburk@softsynth.com>
11
* $Id: patest_leftright.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
13
* This program uses the PortAudio Portable Audio Library.
14
* For more information see: http://www.audiomulch.com/portaudio/
15
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
17
* Permission is hereby granted, free of charge, to any person obtaining
18
* a copy of this software and associated documentation files
19
* (the "Software"), to deal in the Software without restriction,
20
* including without limitation the rights to use, copy, modify, merge,
21
* publish, distribute, sublicense, and/or sell copies of the Software,
22
* and to permit persons to whom the Software is furnished to do so,
23
* subject to the following conditions:
25
* The above copyright notice and this permission notice shall be
26
* included in all copies or substantial portions of the Software.
28
* Any person wishing to distribute modifications to the Software is
29
* requested to send the modifications to the original developer so that
30
* they can be incorporated into the canonical version.
32
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
35
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
36
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
37
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43
#include "portaudio.h"
45
#define NUM_SECONDS (8)
46
#define SAMPLE_RATE (44100)
47
#define FRAMES_PER_BUFFER (512)
49
#define M_PI (3.14159265)
51
#define TABLE_SIZE (200)
54
float sine[TABLE_SIZE];
61
/* This routine will be called by the PortAudio engine when audio is needed.
62
** It may called at interrupt level on some machines so don't do anything
63
** that could mess up the system like calling malloc() or free().
65
static int patestCallback( const void *inputBuffer,
67
unsigned long framesPerBuffer,
68
const PaStreamCallbackTimeInfo* timeInfo,
69
PaStreamCallbackFlags statusFlags,
72
paTestData *data = (paTestData*)userData;
73
float *out = (float*)outputBuffer;
76
/* Prevent unused variable warnings. */
79
for( i=0; i<framesPerBuffer; i++ )
83
*out++ = data->sine[data->left_phase]; /* left */
84
*out++ = 0; /* right */
88
*out++ = 0; /* left */
89
*out++ = data->sine[data->right_phase]; /* right */
92
data->left_phase += 1;
93
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
94
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
95
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
98
if( data->countDown < 0 )
100
data->countDown = SAMPLE_RATE;
101
data->toggle = !data->toggle;
103
data->countDown -= framesPerBuffer;
108
/*******************************************************************/
113
PaStreamParameters outputParameters;
119
printf("Play different tone sine waves that alternate between left and right channel.\n");
120
printf("The low tone should be on the left channel.\n");
122
/* initialise sinusoidal wavetable */
123
for( i=0; i<TABLE_SIZE; i++ )
125
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
127
data.left_phase = data.right_phase = data.toggle = 0;
128
data.countDown = SAMPLE_RATE;
130
err = Pa_Initialize();
131
if( err != paNoError ) goto error;
133
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
134
outputParameters.channelCount = 2; /* stereo output */
135
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
136
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
137
outputParameters.hostApiSpecificStreamInfo = NULL;
139
err = Pa_OpenStream( &stream,
140
NULL, /* No input. */
141
&outputParameters, /* As above. */
144
paClipOff, /* we won't output out of range samples so don't bother clipping them */
147
if( err != paNoError ) goto error;
149
err = Pa_StartStream( stream );
150
if( err != paNoError ) goto error;
152
printf("Play for several seconds.\n");
153
timeout = NUM_SECONDS * 4;
156
Pa_Sleep( 300 ); /*(Irix very much likes sleeps <= 1000 ms.)*/
160
err = Pa_StopStream( stream );
161
if( err != paNoError ) goto error;
162
err = Pa_CloseStream( stream );
163
if( err != paNoError ) goto error;
165
printf("Test finished.\n");
169
fprintf( stderr, "An error occured while using the portaudio stream\n" );
170
fprintf( stderr, "Error number: %d\n", err );
171
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );