1
/** @file patest_sine_formats.c
2
@brief Play a sine wave for several seconds. Test various data formats.
6
* $Id: patest_sine_formats.c,v 1.1 2006/06/10 21:30:57 dmazzoni Exp $
8
* This program uses the PortAudio Portable Audio Library.
9
* For more information see: http://www.audiomulch.com/portaudio/
10
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12
* Permission is hereby granted, free of charge, to any person obtaining
13
* a copy of this software and associated documentation files
14
* (the "Software"), to deal in the Software without restriction,
15
* including without limitation the rights to use, copy, modify, merge,
16
* publish, distribute, sublicense, and/or sell copies of the Software,
17
* and to permit persons to whom the Software is furnished to do so,
18
* subject to the following conditions:
20
* The above copyright notice and this permission notice shall be
21
* included in all copies or substantial portions of the Software.
23
* Any person wishing to distribute modifications to the Software is
24
* requested to send the modifications to the original developer so that
25
* they can be incorporated into the canonical version.
27
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
31
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38
#include "portaudio.h"
40
#define NUM_SECONDS (10)
41
#define SAMPLE_RATE (44100)
42
#define FRAMES_PER_BUFFER (512)
43
#define LEFT_FREQ (SAMPLE_RATE/256.0) /* So we hit 1.0 */
44
#define RIGHT_FREQ (500.0)
45
#define AMPLITUDE (1.0)
47
/* Select ONE format for testing. */
48
#define TEST_UINT8 (0)
50
#define TEST_INT16 (1)
51
#define TEST_FLOAT32 (0)
54
#define TEST_FORMAT paUInt8
55
typedef unsigned char SAMPLE_t;
56
#define SAMPLE_ZERO (0x80)
57
#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
58
#define FORMAT_NAME "Unsigned 8 Bit"
61
#define TEST_FORMAT paInt8
62
typedef char SAMPLE_t;
63
#define SAMPLE_ZERO (0)
64
#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
65
#define FORMAT_NAME "Signed 8 Bit"
68
#define TEST_FORMAT paInt16
69
typedef short SAMPLE_t;
70
#define SAMPLE_ZERO (0)
71
#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(32767 * (x)))
72
#define FORMAT_NAME "Signed 16 Bit"
75
#define TEST_FORMAT paFloat32
76
typedef float SAMPLE_t;
77
#define SAMPLE_ZERO (0.0)
78
#define DOUBLE_TO_SAMPLE(x) ((SAMPLE_t)(x))
79
#define FORMAT_NAME "Float 32 Bit"
83
#define M_PI (3.14159265)
91
unsigned int framesToGo;
94
/* This routine will be called by the PortAudio engine when audio is needed.
95
** It may called at interrupt level on some machines so don't do anything
96
** that could mess up the system like calling malloc() or free().
98
static int patestCallback( const void *inputBuffer,
100
unsigned long framesPerBuffer,
101
const PaStreamCallbackTimeInfo* timeInfo,
102
PaStreamCallbackFlags statusFlags,
105
paTestData *data = (paTestData*)userData;
106
SAMPLE_t *out = (SAMPLE_t *)outputBuffer;
110
(void) inputBuffer; /* Prevent unused variable warnings. */
112
if( data->framesToGo < framesPerBuffer )
114
framesToCalc = data->framesToGo;
115
data->framesToGo = 0;
120
framesToCalc = framesPerBuffer;
121
data->framesToGo -= framesPerBuffer;
124
for( i=0; i<framesToCalc; i++ )
126
data->left_phase += (LEFT_FREQ / SAMPLE_RATE);
127
if( data->left_phase > 1.0) data->left_phase -= 1.0;
128
*out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->left_phase * M_PI * 2. )));
130
data->right_phase += (RIGHT_FREQ / SAMPLE_RATE);
131
if( data->right_phase > 1.0) data->right_phase -= 1.0;
132
*out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->right_phase * M_PI * 2. )));
134
/* zero remainder of final buffer */
135
for( ; i<(int)framesPerBuffer; i++ )
137
*out++ = SAMPLE_ZERO; /* left */
138
*out++ = SAMPLE_ZERO; /* right */
142
/*******************************************************************/
147
PaStreamParameters outputParameters;
152
printf("PortAudio Test: output " FORMAT_NAME "\n");
154
data.left_phase = data.right_phase = 0.0;
155
data.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
156
err = Pa_Initialize();
157
if( err != paNoError ) goto error;
160
outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
161
outputParameters.channelCount = 2; /* Stereo output */
162
outputParameters.sampleFormat = TEST_FORMAT; /* Selected above. */
163
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
164
outputParameters.hostApiSpecificStreamInfo = NULL;
165
err = Pa_OpenStream( &stream,
166
NULL, /* No input. */
167
&outputParameters, /* As above. */
170
paClipOff, /* we won't output out of range samples so don't bother clipping them */
173
if( err != paNoError ) goto error;
175
err = Pa_StartStream( stream );
176
if( err != paNoError ) goto error;
178
printf("Waiting %d seconds for sound to finish.\n", NUM_SECONDS );
180
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
181
if( err < 0 ) goto error;
183
err = Pa_CloseStream( stream );
184
if( err != paNoError ) goto error;
187
printf("PortAudio Test Finished: " FORMAT_NAME "\n");
192
fprintf( stderr, "An error occured while using the portaudio stream\n" );
193
fprintf( stderr, "Error number: %d\n", err );
194
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );