| 1 |
/* |
| 2 |
* $Id: test_w_saw.c,v 1.3 2006/06/10 21:30:55 dmazzoni Exp $ |
| 3 |
* test_w_saw.c |
| 4 |
* Generate stereo 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 (44100) |
| 46 |
#define NUM_SECONDS (6) |
| 47 |
#define SAMPLES_PER_FRAME (2) |
| 48 |
|
| 49 |
#define FREQUENCY (220.0f) |
| 50 |
#define PHASE_INCREMENT (2.0f * FREQUENCY / SAMPLE_RATE) |
| 51 |
#define FRAMES_PER_BLOCK (100) |
| 52 |
|
| 53 |
float samples[FRAMES_PER_BLOCK][SAMPLES_PER_FRAME]; |
| 54 |
float phases[SAMPLES_PER_FRAME]; |
| 55 |
|
| 56 |
/*******************************************************************/ |
| 57 |
int main(void); |
| 58 |
int main(void) |
| 59 |
{ |
| 60 |
int i,j; |
| 61 |
PaError err; |
| 62 |
PABLIO_Stream *aOutStream; |
| 63 |
|
| 64 |
printf("Generate sawtooth waves using PABLIO.\n"); |
| 65 |
fflush(stdout); |
| 66 |
|
| 67 |
/* Open simplified blocking I/O layer on top of PortAudio. */ |
| 68 |
err = OpenAudioStream( &aOutStream, SAMPLE_RATE, paFloat32, |
| 69 |
(PABLIO_WRITE | PABLIO_STEREO) ); |
| 70 |
if( err != paNoError ) goto error; |
| 71 |
|
| 72 |
/* Initialize oscillator phases. */ |
| 73 |
phases[0] = 0.0; |
| 74 |
phases[1] = 0.0; |
| 75 |
|
| 76 |
for( i=0; i<(NUM_SECONDS * SAMPLE_RATE); i += FRAMES_PER_BLOCK ) |
| 77 |
{ |
| 78 |
/* Generate sawtooth waveforms in a block for efficiency. */ |
| 79 |
for( j=0; j<FRAMES_PER_BLOCK; j++ ) |
| 80 |
{ |
| 81 |
/* Generate a sawtooth wave by incrementing a variable. */ |
| 82 |
phases[0] += PHASE_INCREMENT; |
| 83 |
/* The signal range is -1.0 to +1.0 so wrap around if we go over. */ |
| 84 |
if( phases[0] > 1.0f ) phases[0] -= 2.0f; |
| 85 |
samples[j][0] = phases[0]; |
| 86 |
|
| 87 |
/* On the second channel, generate a sawtooth wave a fifth higher. */ |
| 88 |
phases[1] += PHASE_INCREMENT * (3.0f / 2.0f); |
| 89 |
if( phases[1] > 1.0f ) phases[1] -= 2.0f; |
| 90 |
samples[j][1] = phases[1]; |
| 91 |
} |
| 92 |
|
| 93 |
/* Write samples to output. */ |
| 94 |
WriteAudioStream( aOutStream, samples, FRAMES_PER_BLOCK ); |
| 95 |
} |
| 96 |
|
| 97 |
CloseAudioStream( aOutStream ); |
| 98 |
|
| 99 |
printf("Sawtooth sound test complete.\n" ); |
| 100 |
fflush(stdout); |
| 101 |
return 0; |
| 102 |
|
| 103 |
error: |
| 104 |
fprintf( stderr, "An error occured while using PABLIO\n" ); |
| 105 |
fprintf( stderr, "Error number: %d\n", err ); |
| 106 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 107 |
return -1; |
| 108 |
} |