PortAudio  2.0
paex_write_sine.c
Go to the documentation of this file.
1 
7 /*
8  * $Id: paex_write_sine.c 1752 2011-09-08 03:21:55Z philburk $
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 
45 #include <stdio.h>
46 #include <math.h>
47 #include "portaudio.h"
48 
49 #define NUM_SECONDS (5)
50 #define SAMPLE_RATE (44100)
51 #define FRAMES_PER_BUFFER (1024)
52 
53 #ifndef M_PI
54 #define M_PI (3.14159265)
55 #endif
56 
57 #define TABLE_SIZE (200)
58 
59 
60 int main(void);
61 int main(void)
62 {
63  PaStreamParameters outputParameters;
64  PaStream *stream;
65  PaError err;
66  float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
67  float sine[TABLE_SIZE]; /* sine wavetable */
68  int left_phase = 0;
69  int right_phase = 0;
70  int left_inc = 1;
71  int right_inc = 3; /* higher pitch so we can distinguish left and right. */
72  int i, j, k;
73  int bufferCount;
74 
75 
76  printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
77 
78  /* initialise sinusoidal wavetable */
79  for( i=0; i<TABLE_SIZE; i++ )
80  {
81  sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
82  }
83 
84 
85  err = Pa_Initialize();
86  if( err != paNoError ) goto error;
87 
88  outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
89  if (outputParameters.device == paNoDevice) {
90  fprintf(stderr,"Error: No default output device.\n");
91  goto error;
92  }
93  outputParameters.channelCount = 2; /* stereo output */
94  outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
95  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
96  outputParameters.hostApiSpecificStreamInfo = NULL;
97 
98  err = Pa_OpenStream(
99  &stream,
100  NULL, /* no input */
101  &outputParameters,
102  SAMPLE_RATE,
103  FRAMES_PER_BUFFER,
104  paClipOff, /* we won't output out of range samples so don't bother clipping them */
105  NULL, /* no callback, use blocking API */
106  NULL ); /* no callback, so no callback userData */
107  if( err != paNoError ) goto error;
108 
109 
110  printf( "Play 3 times, higher each time.\n" );
111 
112  for( k=0; k < 3; ++k )
113  {
114  err = Pa_StartStream( stream );
115  if( err != paNoError ) goto error;
116 
117  printf("Play for %d seconds.\n", NUM_SECONDS );
118 
119  bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
120 
121  for( i=0; i < bufferCount; i++ )
122  {
123  for( j=0; j < FRAMES_PER_BUFFER; j++ )
124  {
125  buffer[j][0] = sine[left_phase]; /* left */
126  buffer[j][1] = sine[right_phase]; /* right */
127  left_phase += left_inc;
128  if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
129  right_phase += right_inc;
130  if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
131  }
132 
133  err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
134  if( err != paNoError ) goto error;
135  }
136 
137  err = Pa_StopStream( stream );
138  if( err != paNoError ) goto error;
139 
140  ++left_inc;
141  ++right_inc;
142 
143  Pa_Sleep( 1000 );
144  }
145 
146  err = Pa_CloseStream( stream );
147  if( err != paNoError ) goto error;
148 
149  Pa_Terminate();
150  printf("Test finished.\n");
151 
152  return err;
153 error:
154  Pa_Terminate();
155  fprintf( stderr, "An error occured while using the portaudio stream\n" );
156  fprintf( stderr, "Error number: %d\n", err );
157  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
158  return err;
159 }
PaError Pa_Initialize(void)
PaError Pa_StopStream(PaStream *stream)
void PaStream
Definition: portaudio.h:584
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
Definition: portaudio.h:528
The portable PortAudio API.
#define paFloat32
Definition: portaudio.h:436
PaSampleFormat sampleFormat
Definition: portaudio.h:508
#define paClipOff
Definition: portaudio.h:610
int PaError
Definition: portaudio.h:70
PaTime suggestedLatency
Definition: portaudio.h:521
#define paNoDevice
Definition: portaudio.h:169
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
PaDeviceIndex device
Definition: portaudio.h:495
void Pa_Sleep(long msec)
PaError Pa_WriteStream(PaStream *stream, const void *buffer, unsigned long frames)
const char * Pa_GetErrorText(PaError errorCode)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)