From: drowe67 Date: Sun, 8 Jul 2012 05:30:51 +0000 (+0000) Subject: moved portaudio test programs so they dont break main automake system when portaudio... X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=d8e80376cb385289031366d0735ff637b9b5beb3;p=freetel-svn-tracking.git moved portaudio test programs so they dont break main automake system when portaudio library is absent git-svn-id: https://svn.code.sf.net/p/freetel/code@586 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/portaudio/Makefile b/codec2-dev/portaudio/Makefile new file mode 100644 index 00000000..6c1e2c12 --- /dev/null +++ b/codec2-dev/portaudio/Makefile @@ -0,0 +1,20 @@ +# Makefile for Portaudio test programs +# Requires Portaudio V19 + +CFLAGS = -g -Wall -I../src +LIBS = -lm -lportaudio -pthread +SRC = ../src/fdmdv.c ../src/kiss_fft.c + +all: pa_rec pa_play pa_recplay + +pa_rec: Makefile pa_rec.c $(SRC) + gcc $(CFLAGS) pa_rec.c $(SRC) -o pa_rec $(LIBS) + +pa_play: Makefile pa_play.c $(SRC) + gcc $(CFLAGS) pa_play.c $(SRC) -o pa_play $(LIBS) + +pa_recplay: Makefile pa_recplay.c $(SRC) + gcc $(CFLAGS) pa_recplay.c $(SRC) -o pa_recplay $(LIBS) + +clean: + rm -f pa_rec pa_play pa_recplay diff --git a/codec2-dev/portaudio/pa_play.c b/codec2-dev/portaudio/pa_play.c new file mode 100644 index 00000000..00ea0868 --- /dev/null +++ b/codec2-dev/portaudio/pa_play.c @@ -0,0 +1,199 @@ +/* + pa_play.c + David Rowe + July 8 2012 + + Converts samples from a 16 bit short 8000 Hz rawfile to 480000Hz + sample rate and plays them using the default sound device. Used as + an intermediate step in Portaudio integration. + + Modified from paex_record.c Portaudio example. Original author + author Phil Burk http://www.softsynth.com +*/ + +/* + * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $ + * + * This program uses the PortAudio Portable Audio Library. + * For more information see: http://www.portaudio.com + * Copyright (c) 1999-2000 Ross Bencina and Phil Burk + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "portaudio.h" +#include "fdmdv.h" + +#define SAMPLE_RATE 48000 /* 48 kHz sampling rate rec. as we + can trust accuracy of sound + card */ +#define N8 160 /* processing buffer size at 8 kHz */ +#define N48 (N8*FDMDV_OS) /* processing buffer size at 48 kHz */ +#define MEM8 (FDMDV_OS_TAPS/FDMDV_OS) +#define NUM_CHANNELS 2 /* I think most sound cards prefer + stereo, we will convert to mono + as we sample */ + +/* state information passed to call back */ + +typedef struct { + FILE *fin; + float in8k[MEM8 + N8]; +} paTestData; + + +/* + This routine will be called by the PortAudio engine when audio is + required. It may be called at interrupt level on some machines so + don't do anything that could mess up the system like calling + malloc() or free(). +*/ + +static int playCallback( const void *inputBuffer, void *outputBuffer, + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void *userData ) +{ + paTestData *data = (paTestData*)userData; + FILE *fin = data->fin; + int i, nread; + int finished; + short *wptr = (short*)outputBuffer; + float *in8k = data->in8k; + float out48k[N48]; + short out48k_short[N48]; + short in8k_short[N8]; + + (void) outputBuffer; /* Prevent unused variable warnings. */ + (void) timeInfo; + (void) statusFlags; + (void) userData; + + /* note Portaudio docs recs. against making systems calls like + fwrite() in this callback but seems to work OK */ + + nread = fread(in8k_short, sizeof(short), N8, fin); + if (nread == N8) + finished = paContinue; + else + finished = paComplete; + + for(i=0; idefaultLowOutputLatency; + outputParameters.hostApiSpecificStreamInfo = NULL; + + /* Play some audio --------------------------------------------- */ + + err = Pa_OpenStream( + &stream, + NULL, + &outputParameters, + SAMPLE_RATE, + N48, + paClipOff, + playCallback, + &data ); + if( err != paNoError ) goto done; + + err = Pa_StartStream( stream ); + if( err != paNoError ) goto done; + + while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) + { + Pa_Sleep(100); + } + if( err < 0 ) goto done; + + err = Pa_CloseStream( stream ); + if( err != paNoError ) goto done; + + fclose(data.fin); + + +done: + Pa_Terminate(); + if( err != paNoError ) + { + fprintf( stderr, "An error occured while using the portaudio stream\n" ); + fprintf( stderr, "Error number: %d\n", err ); + fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); + err = 1; /* Always return 0 or 1, but no other return codes. */ + } + return err; +} + diff --git a/codec2-dev/portaudio/pa_rec.c b/codec2-dev/portaudio/pa_rec.c new file mode 100644 index 00000000..79366621 --- /dev/null +++ b/codec2-dev/portaudio/pa_rec.c @@ -0,0 +1,210 @@ +/* + pa_rec.c + David Rowe + July 6 2012 + + Records at 48000 Hz from default sound device, convertes to 8 kHz, + and saves to raw file. Used to get experience with Portaudio. + + Modified from paex_record.c Portaudio example. Original author + author Phil Burk http://www.softsynth.com + + To Build: + + gcc paex_rec.c -o paex_rec -lm -lrt -lportaudio -pthread +*/ + +/* + * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $ + * + * This program uses the PortAudio Portable Audio Library. + * For more information see: http://www.portaudio.com + * Copyright (c) 1999-2000 Ross Bencina and Phil Burk + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "portaudio.h" +#include "fdmdv.h" + +#define SAMPLE_RATE 48000 /* 48 kHz sampling rate rec. as we + can trust accuracy of sound + card */ +#define N8 160 /* processing buffer size at 8 kHz */ +#define N48 (N8*FDMDV_OS) /* processing buffer size at 48 kHz */ +#define NUM_CHANNELS 2 /* I think most sound cards prefer + stereo, we will convert to mono + as we sample */ + +/* state information passed to call back */ + +typedef struct { + FILE *fout; + int framesLeft; + float in48k[FDMDV_OS_TAPS + N48]; +} paTestData; + + +/* + This routine will be called by the PortAudio engine when audio is + available. It may be called at interrupt level on some machines so + don't do anything that could mess up the system like calling + malloc() or free(). +*/ + +static int recordCallback( const void *inputBuffer, void *outputBuffer, + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void *userData ) +{ + paTestData *data = (paTestData*)userData; + FILE *fout = data->fout; + int framesToCopy; + int i, n8; + int finished; + short *rptr = (short*)inputBuffer; + float out8k[N8]; + short out8k_short[N8]; + + (void) outputBuffer; /* Prevent unused variable warnings. */ + (void) timeInfo; + (void) statusFlags; + (void) userData; + + if (data->framesLeft < framesPerBuffer) { + framesToCopy = data->framesLeft; + finished = paComplete; + } + else { + framesToCopy = framesPerBuffer; + finished = paContinue; + } + data->framesLeft -= framesToCopy; + + assert(inputBuffer != NULL); + + /* just use left channel */ + + for(i=0; iin48k[i+FDMDV_OS_TAPS] = *rptr; + + /* downsample and update filter memory */ + + fdmdv_48_to_8(out8k, &data->in48k[FDMDV_OS_TAPS], N8); + for(i=0; iin48k[i] = data->in48k[i+framesToCopy]; + + /* save 8k to disk */ + + for(i=0; idefaultLowInputLatency; + inputParameters.hostApiSpecificStreamInfo = NULL; + + /* Record some audio --------------------------------------------- */ + + err = Pa_OpenStream( + &stream, + &inputParameters, + NULL, /* &outputParameters, */ + SAMPLE_RATE, + N48, + paClipOff, /* we won't output out of range samples so don't bother clipping them */ + recordCallback, + &data ); + if( err != paNoError ) goto done; + + err = Pa_StartStream( stream ); + if( err != paNoError ) goto done; + + while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) + { + Pa_Sleep(100); + } + if( err < 0 ) goto done; + + err = Pa_CloseStream( stream ); + if( err != paNoError ) goto done; + + fclose(data.fout); + + +done: + Pa_Terminate(); + if( err != paNoError ) + { + fprintf( stderr, "An error occured while using the portaudio stream\n" ); + fprintf( stderr, "Error number: %d\n", err ); + fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); + err = 1; /* Always return 0 or 1, but no other return codes. */ + } + return err; +} + diff --git a/codec2-dev/portaudio/pa_recplay.c b/codec2-dev/portaudio/pa_recplay.c new file mode 100644 index 00000000..f745396f --- /dev/null +++ b/codec2-dev/portaudio/pa_recplay.c @@ -0,0 +1,203 @@ +/* + pa_recplay.c + David Rowe + July 8 2012 + + Echos audio from sound card input to sound card output. Samples at + 48 kHz, converts to 8 kHz, converts back to 48kHz, and plays using + the default sound device. Used as an intermediate step in + Portaudio integration. + + Modified from paex_record.c Portaudio example. Original author + author Phil Burk http://www.softsynth.com +*/ + +/* + * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $ + * + * This program uses the PortAudio Portable Audio Library. + * For more information see: http://www.portaudio.com + * Copyright (c) 1999-2000 Ross Bencina and Phil Burk + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "portaudio.h" +#include "fdmdv.h" + +#define SAMPLE_RATE 48000 /* 48 kHz sampling rate rec. as we + can trust accuracy of sound + card */ +#define N8 160 /* processing buffer size at 8 kHz */ +#define N48 (N8*FDMDV_OS) /* processing buffer size at 48 kHz */ +#define MEM8 (FDMDV_OS_TAPS/FDMDV_OS) +#define NUM_CHANNELS 2 /* I think most sound cards prefer + stereo, we will convert to mono + as we sample */ + +/* state information passed to call back */ + +typedef struct { + float in48k[FDMDV_OS_TAPS + N48]; + float in8k[MEM8 + N8]; +} paTestData; + + +/* + This routine will be called by the PortAudio engine when audio is + required. It may be called at interrupt level on some machines so + don't do anything that could mess up the system like calling + malloc() or free(). +*/ + +static int callback( const void *inputBuffer, void *outputBuffer, + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void *userData ) +{ + paTestData *data = (paTestData*)userData; + int i; + short *rptr = (short*)inputBuffer; + short *wptr = (short*)outputBuffer; + float *in8k = data->in8k; + float *in48k = data->in48k; + float out8k[N8]; + float out48k[N48]; + short out48k_short[N48]; + + (void) timeInfo; + (void) statusFlags; + + assert(inputBuffer != NULL); + + /* just use left channel */ + + for(i=0; iin48k[i+FDMDV_OS_TAPS] = *rptr; + + /* downsample and update filter memory */ + + fdmdv_48_to_8(out8k, &in48k[FDMDV_OS_TAPS], N8); + for(i=0; idefaultLowInputLatency; + inputParameters.hostApiSpecificStreamInfo = NULL; + + outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ + if (outputParameters.device == paNoDevice) { + fprintf(stderr,"Error: No default output device.\n"); + goto done; + } + outputParameters.channelCount = NUM_CHANNELS; /* stereo output */ + outputParameters.sampleFormat = paInt16; + outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; + outputParameters.hostApiSpecificStreamInfo = NULL; + + /* Play some audio --------------------------------------------- */ + + err = Pa_OpenStream( + &stream, + &inputParameters, + &outputParameters, + SAMPLE_RATE, + N48, + paClipOff, + callback, + &data ); + if( err != paNoError ) goto done; + + err = Pa_StartStream( stream ); + if( err != paNoError ) goto done; + + while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) + { + Pa_Sleep(100); + } + if( err < 0 ) goto done; + + err = Pa_CloseStream( stream ); + if( err != paNoError ) goto done; + + +done: + Pa_Terminate(); + if( err != paNoError ) + { + fprintf( stderr, "An error occured while using the portaudio stream\n" ); + fprintf( stderr, "Error number: %d\n", err ); + fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); + err = 1; /* Always return 0 or 1, but no other return codes. */ + } + return err; +} +