PortAudio  2.0
paex_saw.c
Go to the documentation of this file.
1 
6 /*
7  * $Id$
8  *
9  * This program uses the PortAudio Portable Audio Library.
10  * For more information see: http://www.portaudio.com
11  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files
15  * (the "Software"), to deal in the Software without restriction,
16  * including without limitation the rights to use, copy, modify, merge,
17  * publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so,
19  * subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  */
32 
33 /*
34  * The text above constitutes the entire PortAudio license; however,
35  * the PortAudio community also makes the following non-binding requests:
36  *
37  * Any person wishing to distribute modifications to the Software is
38  * requested to send the modifications to the original developer so that
39  * they can be incorporated into the canonical version. It is also
40  * requested that these non-binding requests be included along with the
41  * license above.
42  */
43 
44 #include <stdio.h>
45 #include <math.h>
46 #include "portaudio.h"
47 #define NUM_SECONDS (4)
48 #define SAMPLE_RATE (44100)
49 
50 typedef struct
51 {
52  float left_phase;
53  float right_phase;
54 }
56 
57 /* This routine will be called by the PortAudio engine when audio is needed.
58 ** It may called at interrupt level on some machines so don't do anything
59 ** that could mess up the system like calling malloc() or free().
60 */
61 static int patestCallback( const void *inputBuffer, void *outputBuffer,
62  unsigned long framesPerBuffer,
63  const PaStreamCallbackTimeInfo* timeInfo,
64  PaStreamCallbackFlags statusFlags,
65  void *userData )
66 {
67  /* Cast data passed through stream to our structure. */
68  paTestData *data = (paTestData*)userData;
69  float *out = (float*)outputBuffer;
70  unsigned int i;
71  (void) inputBuffer; /* Prevent unused variable warning. */
72 
73  for( i=0; i<framesPerBuffer; i++ )
74  {
75  *out++ = data->left_phase; /* left */
76  *out++ = data->right_phase; /* right */
77  /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
78  data->left_phase += 0.01f;
79  /* When signal reaches top, drop back down. */
80  if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f;
81  /* higher pitch so we can distinguish left and right. */
82  data->right_phase += 0.03f;
83  if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f;
84  }
85  return 0;
86 }
87 
88 /*******************************************************************/
89 static paTestData data;
90 int main(void);
91 int main(void)
92 {
93  PaStream *stream;
94  PaError err;
95 
96  printf("PortAudio Test: output sawtooth wave.\n");
97  /* Initialize our data for use by callback. */
98  data.left_phase = data.right_phase = 0.0;
99  /* Initialize library before making any other calls. */
100  err = Pa_Initialize();
101  if( err != paNoError ) goto error;
102 
103  /* Open an audio I/O stream. */
104  err = Pa_OpenDefaultStream( &stream,
105  0, /* no input channels */
106  2, /* stereo output */
107  paFloat32, /* 32 bit floating point output */
108  SAMPLE_RATE,
109  256, /* frames per buffer */
110  patestCallback,
111  &data );
112  if( err != paNoError ) goto error;
113 
114  err = Pa_StartStream( stream );
115  if( err != paNoError ) goto error;
116 
117  /* Sleep for several seconds. */
118  Pa_Sleep(NUM_SECONDS*1000);
119 
120  err = Pa_StopStream( stream );
121  if( err != paNoError ) goto error;
122  err = Pa_CloseStream( stream );
123  if( err != paNoError ) goto error;
124  Pa_Terminate();
125  printf("Test finished.\n");
126  return err;
127 error:
128  Pa_Terminate();
129  fprintf( stderr, "An error occured while using the portaudio stream\n" );
130  fprintf( stderr, "Error number: %d\n", err );
131  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
132  return err;
133 }