1
/** @file patest_sine8.c
3
@brief Test 8 bit data: play a sine wave for several seconds.
4
@author Ross Bencina <rossb@audiomulch.com>
7
* $Id: patest_sine8.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
9
* This program uses the PortAudio Portable Audio Library.
10
* For more information see: http://www.portaudio.com
11
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13
* Permission is hereby granted, free of charge, to any person obtaining
14
* a copy of this software and associated documentation files
15
* (the "Software"), to deal in the Software without restriction,
16
* including without limitation the rights to use, copy, modify, merge,
17
* publish, distribute, sublicense, and/or sell copies of the Software,
18
* and to permit persons to whom the Software is furnished to do so,
19
* subject to the following conditions:
21
* The above copyright notice and this permission notice shall be
22
* included in all copies or substantial portions of the Software.
24
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
* The text above constitutes the entire PortAudio license; however,
35
* the PortAudio community also makes the following non-binding requests:
37
* Any person wishing to distribute modifications to the Software is
38
* requested to send the modifications to the original developer so that
39
* they can be incorporated into the canonical version. It is also
40
* requested that these non-binding requests be included along with the
46
#include "portaudio.h"
48
#define NUM_SECONDS (8)
49
#define SAMPLE_RATE (44100)
50
#define TABLE_SIZE (200)
51
#define TEST_UNSIGNED (0)
54
#define TEST_FORMAT paUInt8
56
#define TEST_FORMAT paInt8
60
#define M_PI (3.14159265)
66
unsigned char sine[TABLE_SIZE];
68
char sine[TABLE_SIZE];
72
unsigned int framesToGo;
76
/* This routine will be called by the PortAudio engine when audio is needed.
77
** It may called at interrupt level on some machines so don't do anything
78
** that could mess up the system like calling malloc() or free().
80
static int patestCallback( const void *inputBuffer, void *outputBuffer,
81
unsigned long framesPerBuffer,
82
const PaStreamCallbackTimeInfo* timeInfo,
83
PaStreamCallbackFlags statusFlags,
86
paTestData *data = (paTestData*)userData;
87
char *out = (char*)outputBuffer;
91
(void) inputBuffer; /* Prevent unused variable warnings. */
93
if( data->framesToGo < framesPerBuffer )
95
framesToCalc = data->framesToGo;
101
framesToCalc = framesPerBuffer;
102
data->framesToGo -= framesPerBuffer;
105
for( i=0; i<framesToCalc; i++ )
107
*out++ = data->sine[data->left_phase]; /* left */
108
*out++ = data->sine[data->right_phase]; /* right */
109
data->left_phase += 1;
110
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
111
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
112
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
114
/* zero remainder of final buffer */
115
for( ; i<(int)framesPerBuffer; i++ )
118
*out++ = (unsigned char) 0x80; /* left */
119
*out++ = (unsigned char) 0x80; /* right */
121
*out++ = 0; /* left */
122
*out++ = 0; /* right */
129
/*******************************************************************/
133
PaStreamParameters outputParameters;
141
printf("PortAudio Test: output UNsigned 8 bit sine wave.\n");
143
printf("PortAudio Test: output signed 8 bit sine wave.\n");
145
/* initialise sinusoidal wavetable */
146
for( i=0; i<TABLE_SIZE; i++ )
148
data.sine[i] = (char) (127.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
150
data.sine[i] += (unsigned char) 0x80;
153
data.left_phase = data.right_phase = 0;
154
data.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
156
err = Pa_Initialize();
157
if( err != paNoError )
160
outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
161
outputParameters.channelCount = 2; /* Stereo output. */
162
outputParameters.sampleFormat = TEST_FORMAT;
163
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
164
outputParameters.hostApiSpecificStreamInfo = NULL;
165
err = Pa_OpenStream( &stream,
166
NULL, /* No input. */
169
256, /* Frames per buffer. */
170
paClipOff, /* We won't output out of range samples so don't bother clipping them. */
173
if( err != paNoError )
176
streamOpened = Pa_GetStreamTime( stream ); /* Time in seconds when stream was opened (approx). */
178
err = Pa_StartStream( stream );
179
if( err != paNoError )
182
/* Watch until sound is halfway finished. */
183
/* (Was ( Pa_StreamTime( stream ) < (totalSamps/2) ) in V18. */
184
while( (Pa_GetStreamTime( stream ) - streamOpened) < (PaTime)NUM_SECONDS / 2.0 )
187
/* Stop sound until ENTER hit. (Hu? don't see any keyboard-input here.) */
188
err = Pa_StopStream( stream );
189
if( err != paNoError )
192
printf("Pause for 2 seconds.\n");
195
err = Pa_StartStream( stream );
196
if( err != paNoError )
199
printf("Waiting for sound to finish.\n");
201
while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
206
err = Pa_CloseStream( stream );
207
if( err != paNoError )
211
printf("Test finished.\n");
215
fprintf( stderr, "An error occured while using the portaudio stream\n" );
216
fprintf( stderr, "Error number: %d\n", err );
217
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );