| 1 |
/** @file patest_write_stop.c |
| 2 |
@brief Play a few seconds of silence followed by a few cycles of a sine wave. Tests to make sure that pa_StopStream() completes playback in blocking I/O |
| 3 |
@author Bjorn Roche of XO Audio (www.xoaudio.com) |
| 4 |
@author Ross Bencina |
| 5 |
@author Phil Burk |
| 6 |
*/ |
| 7 |
/* |
| 8 |
* $Id: patest_write_stop.c,v 1.1 2006/06/10 21:30:57 dmazzoni Exp $ |
| 9 |
* |
| 10 |
* This program uses the PortAudio Portable Audio Library. |
| 11 |
* For more information see: http://www.portaudio.com/ |
| 12 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 13 |
* |
| 14 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 15 |
* a copy of this software and associated documentation files |
| 16 |
* (the "Software"), to deal in the Software without restriction, |
| 17 |
* including without limitation the rights to use, copy, modify, merge, |
| 18 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 19 |
* and to permit persons to whom the Software is furnished to do so, |
| 20 |
* subject to the following conditions: |
| 21 |
* |
| 22 |
* The above copyright notice and this permission notice shall be |
| 23 |
* included in all copies or substantial portions of the Software. |
| 24 |
* |
| 25 |
* Any person wishing to distribute modifications to the Software is |
| 26 |
* requested to send the modifications to the original developer so that |
| 27 |
* they can be incorporated into the canonical version. |
| 28 |
* |
| 29 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 32 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 33 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 34 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 |
* |
| 37 |
*/ |
| 38 |
#include <stdio.h> |
| 39 |
#include <math.h> |
| 40 |
#include "portaudio.h" |
| 41 |
|
| 42 |
#define NUM_SECONDS (5) |
| 43 |
#define SAMPLE_RATE (44100) |
| 44 |
#define FRAMES_PER_BUFFER (1024) |
| 45 |
|
| 46 |
#ifndef M_PI |
| 47 |
#define M_PI (3.14159265) |
| 48 |
#endif |
| 49 |
|
| 50 |
#define TABLE_SIZE (200) |
| 51 |
|
| 52 |
|
| 53 |
int main(void); |
| 54 |
int main(void) |
| 55 |
{ |
| 56 |
PaStreamParameters outputParameters; |
| 57 |
PaStream *stream; |
| 58 |
PaError err; |
| 59 |
float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */ |
| 60 |
float sine[TABLE_SIZE]; /* sine wavetable */ |
| 61 |
int left_phase = 0; |
| 62 |
int right_phase = 0; |
| 63 |
int left_inc = 1; |
| 64 |
int right_inc = 3; /* higher pitch so we can distinguish left and right. */ |
| 65 |
int i, j; |
| 66 |
int bufferCount; |
| 67 |
const int framesBy2 = FRAMES_PER_BUFFER >> 1; |
| 68 |
const float framesBy2f = (float) framesBy2 ; |
| 69 |
|
| 70 |
|
| 71 |
printf( "PortAudio Test: output silence, followed by one buffer of a ramped sine wave. SR = %d, BufSize = %d\n", |
| 72 |
SAMPLE_RATE, FRAMES_PER_BUFFER); |
| 73 |
|
| 74 |
/* initialise sinusoidal wavetable */ |
| 75 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 76 |
{ |
| 77 |
sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
err = Pa_Initialize(); |
| 82 |
if( err != paNoError ) goto error; |
| 83 |
|
| 84 |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ |
| 85 |
outputParameters.channelCount = 2; /* stereo output */ |
| 86 |
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ |
| 87 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency * 5; |
| 88 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 89 |
|
| 90 |
/* open the stream */ |
| 91 |
err = Pa_OpenStream( |
| 92 |
&stream, |
| 93 |
NULL, /* no input */ |
| 94 |
&outputParameters, |
| 95 |
SAMPLE_RATE, |
| 96 |
FRAMES_PER_BUFFER, |
| 97 |
paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
| 98 |
NULL, /* no callback, use blocking API */ |
| 99 |
NULL ); /* no callback, so no callback userData */ |
| 100 |
if( err != paNoError ) goto error; |
| 101 |
|
| 102 |
/* start the stream */ |
| 103 |
err = Pa_StartStream( stream ); |
| 104 |
if( err != paNoError ) goto error; |
| 105 |
|
| 106 |
printf("Playing %d seconds of silence followed by one buffer of a ramped sinusoid.\n", NUM_SECONDS ); |
| 107 |
|
| 108 |
bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER); |
| 109 |
|
| 110 |
/* clear buffer */ |
| 111 |
for( j=0; j < FRAMES_PER_BUFFER; j++ ) |
| 112 |
{ |
| 113 |
buffer[j][0] = 0; /* left */ |
| 114 |
buffer[j][1] = 0; /* right */ |
| 115 |
} |
| 116 |
/* play the silent buffer a bunch o' times */ |
| 117 |
for( i=0; i < bufferCount; i++ ) |
| 118 |
{ |
| 119 |
err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER ); |
| 120 |
if( err != paNoError ) goto error; |
| 121 |
} |
| 122 |
/* play a non-silent buffer once */ |
| 123 |
for( j=0; j < FRAMES_PER_BUFFER; j++ ) |
| 124 |
{ |
| 125 |
float ramp = 1; |
| 126 |
if( j < framesBy2 ) |
| 127 |
ramp = j / framesBy2f; |
| 128 |
else |
| 129 |
ramp = (FRAMES_PER_BUFFER - j) / framesBy2f ; |
| 130 |
|
| 131 |
buffer[j][0] = sine[left_phase] * ramp; /* left */ |
| 132 |
buffer[j][1] = sine[right_phase] * ramp; /* right */ |
| 133 |
left_phase += left_inc; |
| 134 |
if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE; |
| 135 |
right_phase += right_inc; |
| 136 |
if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE; |
| 137 |
} |
| 138 |
err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER ); |
| 139 |
if( err != paNoError ) goto error; |
| 140 |
|
| 141 |
/* stop stream, close, and terminate */ |
| 142 |
err = Pa_StopStream( stream ); |
| 143 |
if( err != paNoError ) goto error; |
| 144 |
|
| 145 |
err = Pa_CloseStream( stream ); |
| 146 |
if( err != paNoError ) goto error; |
| 147 |
|
| 148 |
Pa_Terminate(); |
| 149 |
printf("Test finished.\n"); |
| 150 |
|
| 151 |
return err; |
| 152 |
error: |
| 153 |
Pa_Terminate(); |
| 154 |
fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
| 155 |
fprintf( stderr, "Error number: %d\n", err ); |
| 156 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 157 |
return err; |
| 158 |
} |