PortAudio  2.0
paex_sine.c
Go to the documentation of this file.
1 
7 /*
8  * $Id$
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  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32  */
33 
34 /*
35  * The text above constitutes the entire PortAudio license; however,
36  * the PortAudio community also makes the following non-binding requests:
37  *
38  * Any person wishing to distribute modifications to the Software is
39  * requested to send the modifications to the original developer so that
40  * they can be incorporated into the canonical version. It is also
41  * requested that these non-binding requests be included along with the
42  * license above.
43  */
44 #include <stdio.h>
45 #include <math.h>
46 #include "portaudio.h"
47 
48 #define NUM_SECONDS (5)
49 #define SAMPLE_RATE (44100)
50 #define FRAMES_PER_BUFFER (64)
51 
52 #ifndef M_PI
53 #define M_PI (3.14159265)
54 #endif
55 
56 #define TABLE_SIZE (200)
57 typedef struct
58 {
59  float sine[TABLE_SIZE];
60  int left_phase;
61  int right_phase;
62  char message[20];
63 }
65 
66 /* This routine will be called by the PortAudio engine when audio is needed.
67 ** It may called at interrupt level on some machines so don't do anything
68 ** that could mess up the system like calling malloc() or free().
69 */
70 static int patestCallback( const void *inputBuffer, void *outputBuffer,
71  unsigned long framesPerBuffer,
72  const PaStreamCallbackTimeInfo* timeInfo,
73  PaStreamCallbackFlags statusFlags,
74  void *userData )
75 {
76  paTestData *data = (paTestData*)userData;
77  float *out = (float*)outputBuffer;
78  unsigned long i;
79 
80  (void) timeInfo; /* Prevent unused variable warnings. */
81  (void) statusFlags;
82  (void) inputBuffer;
83 
84  for( i=0; i<framesPerBuffer; i++ )
85  {
86  *out++ = data->sine[data->left_phase]; /* left */
87  *out++ = data->sine[data->right_phase]; /* right */
88  data->left_phase += 1;
89  if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
90  data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
91  if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
92  }
93 
94  return paContinue;
95 }
96 
97 /*
98  * This routine is called by portaudio when playback is done.
99  */
100 static void StreamFinished( void* userData )
101 {
102  paTestData *data = (paTestData *) userData;
103  printf( "Stream Completed: %s\n", data->message );
104 }
105 
106 /*******************************************************************/
107 int main(void);
108 int main(void)
109 {
110  PaStreamParameters outputParameters;
111  PaStream *stream;
112  PaError err;
113  paTestData data;
114  int i;
115 
116  printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
117 
118  /* initialise sinusoidal wavetable */
119  for( i=0; i<TABLE_SIZE; i++ )
120  {
121  data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
122  }
123  data.left_phase = data.right_phase = 0;
124 
125  err = Pa_Initialize();
126  if( err != paNoError ) goto error;
127 
128  outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
129  if (outputParameters.device == paNoDevice) {
130  fprintf(stderr,"Error: No default output device.\n");
131  goto error;
132  }
133  outputParameters.channelCount = 2; /* stereo output */
134  outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
135  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
136  outputParameters.hostApiSpecificStreamInfo = NULL;
137 
138  err = Pa_OpenStream(
139  &stream,
140  NULL, /* no input */
141  &outputParameters,
142  SAMPLE_RATE,
143  FRAMES_PER_BUFFER,
144  paClipOff, /* we won't output out of range samples so don't bother clipping them */
145  patestCallback,
146  &data );
147  if( err != paNoError ) goto error;
148 
149  sprintf( data.message, "No Message" );
150  err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
151  if( err != paNoError ) goto error;
152 
153  err = Pa_StartStream( stream );
154  if( err != paNoError ) goto error;
155 
156  printf("Play for %d seconds.\n", NUM_SECONDS );
157  Pa_Sleep( NUM_SECONDS * 1000 );
158 
159  err = Pa_StopStream( stream );
160  if( err != paNoError ) goto error;
161 
162  err = Pa_CloseStream( stream );
163  if( err != paNoError ) goto error;
164 
165  Pa_Terminate();
166  printf("Test finished.\n");
167 
168  return err;
169 error:
170  Pa_Terminate();
171  fprintf( stderr, "An error occurred while using the portaudio stream\n" );
172  fprintf( stderr, "Error number: %d\n", err );
173  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
174  return err;
175 }
PaError Pa_Initialize(void)
PaError Pa_StopStream(PaStream *stream)
void PaStream
Definition: portaudio.h:639
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaError Pa_SetStreamFinishedCallback(PaStream *stream, PaStreamFinishedCallback *streamFinishedCallback)
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
Definition: portaudio.h:583
The portable PortAudio API.
#define paFloat32
Definition: portaudio.h:491
PaSampleFormat sampleFormat
Definition: portaudio.h:563
#define paClipOff
Definition: portaudio.h:665
int PaError
Definition: portaudio.h:121
PaTime suggestedLatency
Definition: portaudio.h:576
unsigned long PaStreamCallbackFlags
Definition: portaudio.h:716
#define paNoDevice
Definition: portaudio.h:221
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
PaDeviceIndex device
Definition: portaudio.h:550
void Pa_Sleep(long msec)
const char * Pa_GetErrorText(PaError errorCode)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)