1 /* 2 * Copyright (C) 2016 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #include <stdio.h> 39 #include <math.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <portaudio.h> 43 44 #include "btstack_ring_buffer.h" 45 #include "wav_util.h" 46 47 #define NUM_CHANNELS 2 48 #define NUM_SECONDS 1 49 #define PA_SAMPLE_TYPE paInt16 50 #define SAMPLE_RATE 44100 51 #define FRAMES_PER_BUFFER 400 52 #define BYTES_PER_FRAME (2*NUM_CHANNELS) 53 54 #ifndef M_PI 55 #define M_PI 3.14159265 56 #endif 57 58 #define TABLE_SIZE 100 59 60 typedef struct { 61 int16_t sine[TABLE_SIZE]; 62 int left_phase; 63 int right_phase; 64 } paTestData; 65 66 static uint8_t ring_buffer_storage[3*FRAMES_PER_BUFFER*BYTES_PER_FRAME]; 67 static btstack_ring_buffer_t ring_buffer; 68 69 static int total_num_samples = 0; 70 static char * wav_filename = "portaudio_sine.wav"; 71 72 static void write_wav_data(int16_t * data, int num_frames, int num_channels, int sample_rate){ 73 wav_writer_write_int16(num_frames*num_channels, data); 74 total_num_samples+=num_frames*num_channels; 75 if (total_num_samples>5*SAMPLE_RATE) wav_writer_close(); 76 } 77 78 static void fill_ring_buffer(void *userData){ 79 paTestData *data = (paTestData*)userData; 80 81 while (btstack_ring_buffer_bytes_free(&ring_buffer) > BYTES_PER_FRAME){ 82 uint8_t write_data[BYTES_PER_FRAME]; 83 *(int16_t*)&write_data[0] = data->sine[data->left_phase]; 84 *(int16_t*)&write_data[2] = data->sine[data->right_phase]; 85 86 btstack_ring_buffer_write(&ring_buffer, write_data, BYTES_PER_FRAME); 87 write_wav_data((int16_t*)write_data, 1, NUM_CHANNELS, SAMPLE_RATE); 88 89 data->left_phase += 1; 90 if (data->left_phase >= TABLE_SIZE){ 91 data->left_phase -= TABLE_SIZE; 92 } 93 data->right_phase += 2; /* higher pitch so we can distinguish left and right. */ 94 if (data->right_phase >= TABLE_SIZE){ 95 data->right_phase -= TABLE_SIZE; 96 } 97 } 98 } 99 100 static int patestCallback( const void *inputBuffer, void *outputBuffer, 101 unsigned long framesPerBuffer, 102 const PaStreamCallbackTimeInfo* timeInfo, 103 PaStreamCallbackFlags statusFlags, 104 void *userData ) { 105 (void) timeInfo; /* Prevent unused variable warnings. */ 106 (void) statusFlags; 107 (void) inputBuffer; 108 109 uint32_t bytes_read = 0; 110 int bytes_per_buffer = framesPerBuffer * BYTES_PER_FRAME; 111 112 if (btstack_ring_buffer_bytes_available(&ring_buffer) >= bytes_per_buffer){ 113 btstack_ring_buffer_read(&ring_buffer, outputBuffer, bytes_per_buffer, &bytes_read); 114 } else { 115 printf("NOT ENOUGH DATA!\n"); 116 memset(outputBuffer, 0, bytes_per_buffer); 117 } 118 return paContinue; 119 } 120 121 122 int main(int argc, const char * argv[]){ 123 PaError err; 124 static paTestData data; 125 static PaStream * stream; 126 127 /* initialise sinusoidal wavetable */ 128 int i; 129 for (i=0; i<TABLE_SIZE; i++){ 130 data.sine[i] = sin(((double)i/(double)TABLE_SIZE) * M_PI * 2.)*32767; 131 } 132 data.left_phase = data.right_phase = 0; 133 134 err = Pa_Initialize(); 135 if (err != paNoError){ 136 printf("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err)); 137 return paNoError; 138 } 139 140 PaStreamParameters outputParameters; 141 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 142 outputParameters.channelCount = NUM_CHANNELS; 143 outputParameters.sampleFormat = PA_SAMPLE_TYPE; 144 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; 145 outputParameters.hostApiSpecificStreamInfo = NULL; 146 147 /* -- setup stream -- */ 148 err = Pa_OpenStream( 149 &stream, 150 NULL, /* &inputParameters */ 151 &outputParameters, 152 SAMPLE_RATE, 153 FRAMES_PER_BUFFER, 154 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 155 patestCallback, /* use callback */ 156 &data ); /* callback userData */ 157 158 memset(ring_buffer_storage, 0, sizeof(ring_buffer_storage)); 159 btstack_ring_buffer_init(&ring_buffer, ring_buffer_storage, sizeof(ring_buffer_storage)); 160 161 wav_writer_open(wav_filename, NUM_CHANNELS, SAMPLE_RATE); 162 163 if (err != paNoError){ 164 printf("Error opening default stream: \"%s\"\n", Pa_GetErrorText(err)); 165 return paNoError; 166 } 167 168 err = Pa_StartStream(stream); 169 if (err != paNoError){ 170 printf("Error starting the stream: \"%s\"\n", Pa_GetErrorText(err)); 171 return paNoError; 172 } 173 174 /* Sleep for several seconds. */ 175 while (1){ 176 fill_ring_buffer(&data); 177 Pa_Sleep(1); 178 } 179 180 err = Pa_StopStream(stream); 181 if (err != paNoError){ 182 printf("Error stopping the stream: \"%s\"\n", Pa_GetErrorText(err)); 183 return paNoError; 184 } 185 186 err = Pa_CloseStream(stream); 187 if (err != paNoError){ 188 printf("Error closing the stream: \"%s\"\n", Pa_GetErrorText(err)); 189 return paNoError; 190 } 191 192 err = Pa_Terminate(); 193 if (err != paNoError){ 194 printf("Error terminating portaudio: \"%s\"\n", Pa_GetErrorText(err)); 195 return paNoError; 196 } 197 return 0; 198 } 199