1 /*
2  * $Id: test_w_saw8.c,v 1.3 2006/06/10 21:30:55 dmazzoni Exp $
3  * test_w_saw8.c
4  * Generate stereo 8 bit sawtooth waveforms.
5  *
6  * Author: Phil Burk, http://www.softsynth.com
7  *
8  * This program uses PABLIO, the Portable Audio Blocking I/O Library.
9  * PABLIO is built on top of PortAudio, the Portable Audio Library.
10  *
11  * For more information see: http://www.audiomulch.com/portaudio/
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  * Any person wishing to distribute modifications to the Software is
26  * requested to send the modifications to the original developer so that
27  * they can be incorporated into the canonical version.
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 #include <stdio.h>
40 #include <stdlib.h>
41 #include <math.h>
42 #include "pablio.h"
43 #include <string.h>
44 
45 #define SAMPLE_RATE         (22050)
46 #define NUM_SECONDS             (6)
47 #define SAMPLES_PER_FRAME       (2)
48 
49 
50 #define FRAMES_PER_BLOCK    (100)
51 
52 unsigned char   samples[FRAMES_PER_BLOCK][SAMPLES_PER_FRAME];
53 unsigned char   phases[SAMPLES_PER_FRAME];
54 
55 /*******************************************************************/
56 int main(void);
main(void)57 int main(void)
58 {
59     int             i,j;
60     PaError         err;
61     PABLIO_Stream  *aOutStream;
62 
63     printf("Generate unsigned 8 bit sawtooth waves using PABLIO.\n");
64     fflush(stdout);
65 
66     /* Open simplified blocking I/O layer on top of PortAudio. */
67     err = OpenAudioStream( &aOutStream, SAMPLE_RATE, paUInt8,
68                            (PABLIO_WRITE | PABLIO_STEREO) );
69     if( err != paNoError ) goto error;
70 
71     /* Initialize oscillator phases to "ground" level for paUInt8. */
72     phases[0] = 128;
73     phases[1] = 128;
74 
75     for( i=0; i<(NUM_SECONDS * SAMPLE_RATE); i += FRAMES_PER_BLOCK )
76     {
77         /* Generate sawtooth waveforms in a block for efficiency. */
78         for( j=0; j<FRAMES_PER_BLOCK; j++ )
79         {
80             /* Generate a sawtooth wave by incrementing a variable. */
81             phases[0] += 1;
82             /* We don't have to do anything special to wrap when using paUint8 because
83              * 8 bit arithmetic automatically wraps. */
84             samples[j][0] = phases[0];
85 
86             /* On the second channel, generate a higher sawtooth wave. */
87             phases[1] += 3;
88             samples[j][1] = phases[1];
89         }
90 
91         /* Write samples to output. */
92         WriteAudioStream( aOutStream, samples, FRAMES_PER_BLOCK );
93     }
94 
95     CloseAudioStream( aOutStream );
96 
97     printf("Sawtooth sound test complete.\n" );
98     fflush(stdout);
99     return 0;
100 
101 error:
102     fprintf( stderr, "An error occured while using PABLIO\n" );
103     fprintf( stderr, "Error number: %d\n", err );
104     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
105     return -1;
106 }
107