pa_recplay.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. pa_recplay.c
  3. David Rowe
  4. July 8 2012
  5. Echos audio from sound card input to sound card output. Samples at
  6. 48 kHz, converts to 8 kHz, converts back to 48kHz, and plays using
  7. the default sound device. Used as an intermediate step in
  8. Portaudio integration.
  9. Modified from paex_record.c Portaudio example. Original author
  10. author Phil Burk http://www.softsynth.com
  11. */
  12. /*
  13. * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $
  14. *
  15. * This program uses the PortAudio Portable Audio Library.
  16. * For more information see: http://www.portaudio.com
  17. * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining
  20. * a copy of this software and associated documentation files
  21. * (the "Software"), to deal in the Software without restriction,
  22. * including without limitation the rights to use, copy, modify, merge,
  23. * publish, distribute, sublicense, and/or sell copies of the Software,
  24. * and to permit persons to whom the Software is furnished to do so,
  25. * subject to the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be
  28. * included in all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  33. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  34. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  35. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. */
  38. #include <assert.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "portaudio.h"
  42. #include "fdmdv.h"
  43. #include "fifo.h"
  44. #define SAMPLE_RATE 48000 /* 48 kHz sampling rate rec. as we
  45. can trust accuracy of sound
  46. card */
  47. #define N8 160 /* processing buffer size at 8 kHz */
  48. #define N48 (N8*FDMDV_OS) /* processing buffer size at 48 kHz */
  49. #define MEM8 (FDMDV_OS_TAPS/FDMDV_OS)
  50. #define NUM_CHANNELS 2 /* I think most sound cards prefer
  51. stereo, we will convert to mono
  52. as we sample */
  53. #define MAX_FPB 2048 /* maximum value of framesPerBuffer */
  54. /* state information passed to call back */
  55. typedef struct {
  56. float in48k[FDMDV_OS_TAPS + N48];
  57. float in8k[MEM8 + N8];
  58. struct FIFO *infifo;
  59. struct FIFO *outfifo;
  60. } paTestData;
  61. /*
  62. This routine will be called by the PortAudio engine when audio is
  63. required. It may be called at interrupt level on some machines so
  64. don't do anything that could mess up the system like calling
  65. malloc() or free().
  66. */
  67. static int callback( const void *inputBuffer, void *outputBuffer,
  68. unsigned long framesPerBuffer,
  69. const PaStreamCallbackTimeInfo* timeInfo,
  70. PaStreamCallbackFlags statusFlags,
  71. void *userData )
  72. {
  73. paTestData *data = (paTestData*)userData;
  74. int i;
  75. short *rptr = (short*)inputBuffer;
  76. short *wptr = (short*)outputBuffer;
  77. float *in8k = data->in8k;
  78. float *in48k = data->in48k;
  79. float out8k[N8];
  80. float out48k[N48];
  81. short out48k_short[N48];
  82. short in48k_short[N48];
  83. short indata[MAX_FPB];
  84. short outdata[MAX_FPB];
  85. (void) timeInfo;
  86. (void) statusFlags;
  87. assert(inputBuffer != NULL);
  88. assert(outputBuffer != NULL);
  89. /*
  90. framesPerBuffer is portaudio-speak for number of samples we
  91. actually get from the record side and need to provide to the
  92. play side. On Linux (at least) it was found that
  93. framesPerBuffer may not always be what we ask for in the
  94. framesPerBuffer field of Pa_OpenStream. For example a request
  95. for 960 sample buffers lead to framesPerBuffer = 1024.
  96. To perform the 48 to 8 kHz conversion we need an integer
  97. multiple of FDMDV_OS samples to support the interpolation and
  98. decimation. As we can't guarantee the size of framesPerBuffer
  99. we do a little FIFO buffering.
  100. */
  101. //printf("framesPerBuffer: %d N48 %d\n", framesPerBuffer, N48);
  102. /* assemble a mono buffer (just use left channel) and write to FIFO */
  103. assert(framesPerBuffer < MAX_FPB);
  104. for(i=0; i<framesPerBuffer; i++,rptr+=2)
  105. indata[i] = *rptr;
  106. fifo_write(data->infifo, indata, framesPerBuffer);
  107. /* while we have enough samples available ... */
  108. //printf("infifo before: %d\n", fifo_n(data->infifo));
  109. while (fifo_read(data->infifo, in48k_short, N48) == 0) {
  110. /* convert to float */
  111. for(i=0; i<N48; i++)
  112. in48k[FDMDV_OS_TAPS + i] = in48k_short[i];
  113. /* downsample and update filter memory */
  114. fdmdv_48_to_8(out8k, &in48k[FDMDV_OS_TAPS], N8);
  115. for(i=0; i<FDMDV_OS_TAPS; i++)
  116. in48k[i] = in48k[i+N48];
  117. /* play side, back up to 8k */
  118. for(i=0; i<N8; i++)
  119. in8k[MEM8+i] = out8k[i];
  120. /* upsample and update filter memory */
  121. fdmdv_8_to_48(out48k, &in8k[MEM8], N8);
  122. for(i=0; i<MEM8; i++)
  123. in8k[i] = in8k[i+N8];
  124. /* write signal to both channels */
  125. for(i=0; i<N48; i++)
  126. out48k_short[i] = (short)out48k[i];
  127. fifo_write(data->outfifo, out48k_short, N48);
  128. }
  129. //printf("infifo after: %d\n", fifo_n(data->infifo));
  130. //printf("outfifo : %d\n", fifo_n(data->outfifo));
  131. /* OK now set up output samples */
  132. if (fifo_read(data->outfifo, outdata, framesPerBuffer) == 0) {
  133. /* write signal to both channels */
  134. for(i=0; i<framesPerBuffer; i++,wptr+=2) {
  135. wptr[0] = outdata[i];
  136. wptr[1] = outdata[i];
  137. }
  138. }
  139. else {
  140. //printf("no data\n");
  141. /* zero output if no data available */
  142. for(i=0; i<framesPerBuffer; i++,wptr+=2) {
  143. wptr[0] = 0;
  144. wptr[1] = 0;
  145. }
  146. }
  147. return paContinue;
  148. }
  149. int main(int argc, char *argv[])
  150. {
  151. PaStreamParameters inputParameters, outputParameters;
  152. PaStream* stream;
  153. PaError err = paNoError;
  154. paTestData data;
  155. int i;
  156. /* init callback data */
  157. for(i=0; i<MEM8; i++)
  158. data.in8k[i] = 0.0;
  159. for(i=0; i<FDMDV_OS_TAPS; i++)
  160. data.in48k[i] = 0.0;
  161. data.infifo = fifo_create(2*N48);
  162. data.outfifo = fifo_create(2*N48);
  163. /* init port audio */
  164. err = Pa_Initialize();
  165. if( err != paNoError ) goto done;
  166. inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
  167. if (inputParameters.device == paNoDevice) {
  168. fprintf(stderr,"Error: No default input device.\n");
  169. goto done;
  170. }
  171. inputParameters.channelCount = NUM_CHANNELS; /* stereo input */
  172. inputParameters.sampleFormat = paInt16;
  173. inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
  174. inputParameters.hostApiSpecificStreamInfo = NULL;
  175. outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
  176. if (outputParameters.device == paNoDevice) {
  177. fprintf(stderr,"Error: No default output device.\n");
  178. goto done;
  179. }
  180. outputParameters.channelCount = NUM_CHANNELS; /* stereo output */
  181. outputParameters.sampleFormat = paInt16;
  182. outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
  183. outputParameters.hostApiSpecificStreamInfo = NULL;
  184. /* Play some audio --------------------------------------------- */
  185. err = Pa_OpenStream(
  186. &stream,
  187. &inputParameters,
  188. &outputParameters,
  189. SAMPLE_RATE,
  190. 512,
  191. paClipOff,
  192. callback,
  193. &data );
  194. if( err != paNoError ) goto done;
  195. err = Pa_StartStream( stream );
  196. if( err != paNoError ) goto done;
  197. while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
  198. {
  199. Pa_Sleep(100);
  200. }
  201. if( err < 0 ) goto done;
  202. err = Pa_CloseStream( stream );
  203. if( err != paNoError ) goto done;
  204. done:
  205. Pa_Terminate();
  206. if( err != paNoError )
  207. {
  208. fprintf( stderr, "An error occured while using the portaudio stream\n" );
  209. fprintf( stderr, "Error number: %d\n", err );
  210. fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
  211. err = 1; /* Always return 0 or 1, but no other return codes. */
  212. }
  213. fifo_destroy(data.infifo);
  214. fifo_destroy(data.outfifo);
  215. return err;
  216. }