2
0

pa_impresp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. pa_impresp.c
  3. David Rowe
  4. August 29 2012
  5. Measures the impulse reponse of the path between the speaker and
  6. microphone. Used to explore why Codec audio quality is
  7. different through a speaker and headphones.
  8. Modified from pa_playrec.c
  9. */
  10. /*
  11. * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $
  12. *
  13. * This program uses the PortAudio Portable Audio Library.
  14. * For more information see: http://www.portaudio.com
  15. * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining
  18. * a copy of this software and associated documentation files
  19. * (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, sublicense, and/or sell copies of the Software,
  22. * and to permit persons to whom the Software is furnished to do so,
  23. * subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be
  26. * included in all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  31. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  32. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  33. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. */
  36. #include <assert.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include "portaudio.h"
  40. #include "fdmdv.h"
  41. #define SAMPLE_RATE 48000 /* 48 kHz sampling rate rec. as we
  42. can trust accuracy of sound
  43. card */
  44. #define N8 160 /* processing buffer size at 8 kHz */
  45. #define N48 (N8*FDMDV_OS) /* processing buffer size at 48 kHz */
  46. #define MEM8 (FDMDV_OS_TAPS/FDMDV_OS)
  47. #define NUM_CHANNELS 2 /* I think most sound cards prefer
  48. stereo, we will convert to mono
  49. as we sample */
  50. #define IMPULSE_AMP 16384 /* amplitide of impulse */
  51. #define IMPULSE_PERIOD 0.1 /* period (dly between impulses) in secs */
  52. /* state information passed to call back */
  53. typedef struct {
  54. float in48k[FDMDV_OS_TAPS + N48];
  55. float in8k[MEM8 + N8];
  56. FILE *fimp;
  57. float *impulse_buf;
  58. int impulse_buf_length;
  59. int impulse_sample_count;
  60. int framesLeft;
  61. } paTestData;
  62. /*
  63. This routine will be called by the PortAudio engine when audio is
  64. required. It may be called at interrupt level on some machines so
  65. don't do anything that could mess up the system like calling
  66. malloc() or free().
  67. */
  68. static int callback( const void *inputBuffer, void *outputBuffer,
  69. unsigned long framesPerBuffer,
  70. const PaStreamCallbackTimeInfo* timeInfo,
  71. PaStreamCallbackFlags statusFlags,
  72. void *userData )
  73. {
  74. paTestData *data = (paTestData*)userData;
  75. int i;
  76. short *rptr = (short*)inputBuffer;
  77. short *wptr = (short*)outputBuffer;
  78. float *in8k = data->in8k;
  79. float *in48k = data->in48k;
  80. float out8k[N8];
  81. float out48k[N48];
  82. short out48k_short[N48];
  83. short out8k_short[N8];
  84. (void) timeInfo;
  85. (void) statusFlags;
  86. assert(inputBuffer != NULL);
  87. /* just use left channel */
  88. for(i=0; i<framesPerBuffer; i++,rptr+=2)
  89. data->in48k[i+FDMDV_OS_TAPS] = *rptr;
  90. /* downsample and update filter memory */
  91. fdmdv_48_to_8(out8k, &in48k[FDMDV_OS_TAPS], N8);
  92. for(i=0; i<FDMDV_OS_TAPS; i++)
  93. in48k[i] = in48k[i+framesPerBuffer];
  94. /* write impulse response to disk */
  95. for(i=0; i<N8; i++)
  96. out8k_short[i] = out8k[i];
  97. fwrite(out8k_short, sizeof(short), N8, data->fimp);
  98. /* play side, read from impulse buffer */
  99. for(i=0; i<N8; i++) {
  100. in8k[MEM8+i] = data->impulse_buf[data->impulse_sample_count];
  101. data->impulse_sample_count++;
  102. if (data->impulse_sample_count == data->impulse_buf_length)
  103. data->impulse_sample_count = 0;
  104. }
  105. /* upsample and update filter memory */
  106. fdmdv_8_to_48(out48k, &in8k[MEM8], N8);
  107. for(i=0; i<MEM8; i++)
  108. in8k[i] = in8k[i+N8];
  109. assert(outputBuffer != NULL);
  110. /* write signal to both channels */
  111. for(i=0; i<N48; i++)
  112. out48k_short[i] = (short)out48k[i];
  113. for(i=0; i<framesPerBuffer; i++,wptr+=2) {
  114. wptr[0] = out48k_short[i];
  115. wptr[1] = out48k_short[i];
  116. }
  117. data->framesLeft -= framesPerBuffer;
  118. if (data->framesLeft > 0)
  119. return paContinue;
  120. else
  121. return paComplete;
  122. }
  123. int main(int argc, char *argv[])
  124. {
  125. PaStreamParameters inputParameters, outputParameters;
  126. PaStream* stream;
  127. PaError err = paNoError;
  128. paTestData data;
  129. int i, numSecs;
  130. if (argc != 3) {
  131. printf("usage: %s impulseRawFile time(s)\n", argv[0]);
  132. exit(0);
  133. }
  134. data.fimp = fopen(argv[1], "wb");
  135. if (data.fimp == NULL) {
  136. printf("Error opening impulse output file %s\n", argv[1]);
  137. exit(1);
  138. }
  139. numSecs = atoi(argv[2]);
  140. data.framesLeft = numSecs * SAMPLE_RATE;
  141. /* init filter states */
  142. for(i=0; i<MEM8; i++)
  143. data.in8k[i] = 0.0;
  144. for(i=0; i<FDMDV_OS_TAPS; i++)
  145. data.in48k[i] = 0.0;
  146. /* init imupulse */
  147. data.impulse_buf_length = IMPULSE_PERIOD*(SAMPLE_RATE/FDMDV_OS);
  148. printf("%d\n",data.impulse_buf_length);
  149. data.impulse_buf = (float*)malloc(data.impulse_buf_length*sizeof(float));
  150. assert(data.impulse_buf != NULL);
  151. data.impulse_buf[0] = IMPULSE_AMP;
  152. for(i=1; i<data.impulse_buf_length; i++)
  153. data.impulse_buf[i] = 0;
  154. data.impulse_sample_count = 0;
  155. err = Pa_Initialize();
  156. if( err != paNoError ) goto done;
  157. inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
  158. if (inputParameters.device == paNoDevice) {
  159. fprintf(stderr,"Error: No default input device.\n");
  160. goto done;
  161. }
  162. inputParameters.channelCount = NUM_CHANNELS; /* stereo input */
  163. inputParameters.sampleFormat = paInt16;
  164. inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
  165. inputParameters.hostApiSpecificStreamInfo = NULL;
  166. outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
  167. if (outputParameters.device == paNoDevice) {
  168. fprintf(stderr,"Error: No default output device.\n");
  169. goto done;
  170. }
  171. outputParameters.channelCount = NUM_CHANNELS; /* stereo output */
  172. outputParameters.sampleFormat = paInt16;
  173. outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
  174. outputParameters.hostApiSpecificStreamInfo = NULL;
  175. /* Play some audio --------------------------------------------- */
  176. err = Pa_OpenStream(
  177. &stream,
  178. &inputParameters,
  179. &outputParameters,
  180. SAMPLE_RATE,
  181. N48,
  182. paClipOff,
  183. callback,
  184. &data );
  185. if( err != paNoError ) goto done;
  186. err = Pa_StartStream( stream );
  187. if( err != paNoError ) goto done;
  188. while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
  189. {
  190. Pa_Sleep(100);
  191. }
  192. if( err < 0 ) goto done;
  193. err = Pa_CloseStream( stream );
  194. if( err != paNoError ) goto done;
  195. done:
  196. Pa_Terminate();
  197. if( err != paNoError )
  198. {
  199. fprintf( stderr, "An error occured while using the portaudio stream\n" );
  200. fprintf( stderr, "Error number: %d\n", err );
  201. fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
  202. err = 1; /* Always return 0 or 1, but no other return codes. */
  203. }
  204. fclose(data.fimp);
  205. return err;
  206. }