PortAudio  2.0
paex_mono_asio_channel_select.c
Go to the documentation of this file.
1 
7 /*
8  * $Id$
9  *
10  * Authors:
11  * Ross Bencina <rossb@audiomulch.com>
12  * Phil Burk <philburk@softsynth.com>
13  *
14  * This program uses the PortAudio Portable Audio Library.
15  * For more information see: http://www.portaudio.com
16  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining
19  * a copy of this software and associated documentation files
20  * (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, sublicense, and/or sell copies of the Software,
23  * and to permit persons to whom the Software is furnished to do so,
24  * subject to the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be
27  * included in all copies or substantial portions of the Software.
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 /*
39  * The text above constitutes the entire PortAudio license; however,
40  * the PortAudio community also makes the following non-binding requests:
41  *
42  * Any person wishing to distribute modifications to the Software is
43  * requested to send the modifications to the original developer so that
44  * they can be incorporated into the canonical version. It is also
45  * requested that these non-binding requests be included along with the
46  * license above.
47  */
48 
49 #include <stdio.h>
50 #include <math.h>
51 #include "portaudio.h"
52 #include "pa_asio.h"
53 
54 #define NUM_SECONDS (10)
55 #define SAMPLE_RATE (44100)
56 #define AMPLITUDE (0.8)
57 #define FRAMES_PER_BUFFER (64)
58 #define OUTPUT_DEVICE Pa_GetDefaultOutputDevice()
59 
60 #ifndef M_PI
61 #define M_PI (3.14159265)
62 #endif
63 
64 #define TABLE_SIZE (200)
65 typedef struct
66 {
67  float sine[TABLE_SIZE];
68  int phase;
69 }
71 
72 /* This routine will be called by the PortAudio engine when audio is needed.
73 ** It may called at interrupt level on some machines so don't do anything
74 ** that could mess up the system like calling malloc() or free().
75 */
76 static int patestCallback( const void *inputBuffer, void *outputBuffer,
77  unsigned long framesPerBuffer,
78  const PaStreamCallbackTimeInfo* timeInfo,
79  PaStreamCallbackFlags statusFlags,
80  void *userData )
81 {
82  paTestData *data = (paTestData*)userData;
83  float *out = (float*)outputBuffer;
84  unsigned long i;
85  int finished = 0;
86  /* avoid unused variable warnings */
87  (void) inputBuffer;
88  (void) timeInfo;
89  (void) statusFlags;
90  for( i=0; i<framesPerBuffer; i++ )
91  {
92  *out++ = data->sine[data->phase]; /* left */
93  data->phase += 1;
94  if( data->phase >= TABLE_SIZE ) data->phase -= TABLE_SIZE;
95  }
96  return finished;
97 }
98 
99 /*******************************************************************/
100 int main(void);
101 int main(void)
102 {
103  PaStreamParameters outputParameters;
104  PaAsioStreamInfo asioOutputInfo;
105  PaStream *stream;
106  PaError err;
107  paTestData data;
108  int outputChannelSelectors[1];
109  int i;
110  printf("PortAudio Test: output MONO sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
111  /* initialise sinusoidal wavetable */
112  for( i=0; i<TABLE_SIZE; i++ )
113  {
114  data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
115  }
116  data.phase = 0;
117 
118  err = Pa_Initialize();
119  if( err != paNoError ) goto error;
120 
121  outputParameters.device = OUTPUT_DEVICE;
122  outputParameters.channelCount = 1; /* MONO output */
123  outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
124  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
125 
126  /* Use an ASIO specific structure. WARNING - this is not portable. */
127  asioOutputInfo.size = sizeof(PaAsioStreamInfo);
128  asioOutputInfo.hostApiType = paASIO;
129  asioOutputInfo.version = 1;
130  asioOutputInfo.flags = paAsioUseChannelSelectors;
131  outputChannelSelectors[0] = 1; /* skip channel 0 and use the second (right) ASIO device channel */
132  asioOutputInfo.channelSelectors = outputChannelSelectors;
133  outputParameters.hostApiSpecificStreamInfo = &asioOutputInfo;
134 
135  err = Pa_OpenStream(
136  &stream,
137  NULL, /* no input */
138  &outputParameters,
139  SAMPLE_RATE,
140  FRAMES_PER_BUFFER,
141  paClipOff, /* we won't output out of range samples so don't bother clipping them */
142  patestCallback,
143  &data );
144  if( err != paNoError ) goto error;
145 
146  err = Pa_StartStream( stream );
147  if( err != paNoError ) goto error;
148 
149  printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout);
150  Pa_Sleep( NUM_SECONDS * 1000 );
151 
152  err = Pa_StopStream( stream );
153  if( err != paNoError ) goto error;
154 
155  err = Pa_CloseStream( stream );
156  if( err != paNoError ) goto error;
157 
158  Pa_Terminate();
159  printf("Test finished.\n");
160  return err;
161 error:
162  Pa_Terminate();
163  fprintf( stderr, "An error occured while using the portaudio stream\n" );
164  fprintf( stderr, "Error number: %d\n", err );
165  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
166  return err;
167 }