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