1
/** @file patest_mono.c
3
@brief Play a monophonic sine wave using the Portable Audio api for several seconds.
4
@author Phil Burk http://www.softsynth.com
7
* $Id: patest_mono.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
10
* Ross Bencina <rossb@audiomulch.com>
11
* Phil Burk <philburk@softsynth.com>
13
* This program uses the PortAudio Portable Audio Library.
14
* For more information see: http://www.portaudio.com
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
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38
* The text above constitutes the entire PortAudio license; however,
39
* the PortAudio community also makes the following non-binding requests:
41
* Any person wishing to distribute modifications to the Software is
42
* requested to send the modifications to the original developer so that
43
* they can be incorporated into the canonical version. It is also
44
* requested that these non-binding requests be included along with the
50
#include "portaudio.h"
52
#define NUM_SECONDS (10)
53
#define SAMPLE_RATE (44100)
54
#define AMPLITUDE (0.8)
55
#define FRAMES_PER_BUFFER (64)
56
#define OUTPUT_DEVICE Pa_GetDefaultOutputDevice()
59
#define M_PI (3.14159265)
62
#define TABLE_SIZE (200)
65
float sine[TABLE_SIZE];
70
/* This routine will be called by the PortAudio engine when audio is needed.
71
** It may called at interrupt level on some machines so don't do anything
72
** that could mess up the system like calling malloc() or free().
74
static int patestCallback( const void *inputBuffer, void *outputBuffer,
75
unsigned long framesPerBuffer,
76
const PaStreamCallbackTimeInfo* timeInfo,
77
PaStreamCallbackFlags statusFlags,
80
paTestData *data = (paTestData*)userData;
81
float *out = (float*)outputBuffer;
84
/* avoid unused variable warnings */
88
for( i=0; i<framesPerBuffer; i++ )
90
*out++ = data->sine[data->phase]; /* left */
92
if( data->phase >= TABLE_SIZE ) data->phase -= TABLE_SIZE;
97
/*******************************************************************/
101
PaStreamParameters outputParameters;
106
printf("PortAudio Test: output MONO sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
107
/* initialise sinusoidal wavetable */
108
for( i=0; i<TABLE_SIZE; i++ )
110
data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
114
err = Pa_Initialize();
115
if( err != paNoError ) goto error;
117
outputParameters.device = OUTPUT_DEVICE;
118
outputParameters.channelCount = 1; /* MONO output */
119
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
120
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
121
outputParameters.hostApiSpecificStreamInfo = NULL;
129
paClipOff, /* we won't output out of range samples so don't bother clipping them */
132
if( err != paNoError ) goto error;
134
err = Pa_StartStream( stream );
135
if( err != paNoError ) goto error;
137
printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout);
138
Pa_Sleep( NUM_SECONDS * 1000 );
140
err = Pa_StopStream( stream );
141
if( err != paNoError ) goto error;
143
err = Pa_CloseStream( stream );
144
if( err != paNoError ) goto error;
147
printf("Test finished.\n");
151
fprintf( stderr, "An error occured while using the portaudio stream\n" );
152
fprintf( stderr, "Error number: %d\n", err );
153
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
b'\\ No newline at end of file'