pa_rec.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. pa_rec.c
  3. David Rowe
  4. July 6 2012
  5. Records at 48000 Hz from default sound device, convertes to 8 kHz,
  6. and saves to raw file. Used to get experience with Portaudio.
  7. Modified from paex_record.c Portaudio example. Original author
  8. author Phil Burk http://www.softsynth.com
  9. To Build:
  10. gcc paex_rec.c -o paex_rec -lm -lrt -lportaudio -pthread
  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. #define SAMPLE_RATE 48000 /* 48 kHz sampling rate rec. as we
  44. can trust accuracy of sound
  45. card */
  46. #define N8 160 /* processing buffer size at 8 kHz */
  47. #define N48 (N8*FDMDV_OS) /* processing buffer size at 48 kHz */
  48. #define NUM_CHANNELS 2 /* I think most sound cards prefer
  49. stereo, we will convert to mono
  50. as we sample */
  51. /* state information passed to call back */
  52. typedef struct {
  53. FILE *fout;
  54. int framesLeft;
  55. float in48k[FDMDV_OS_TAPS + N48];
  56. } paTestData;
  57. /*
  58. This routine will be called by the PortAudio engine when audio is
  59. available. It may be called at interrupt level on some machines so
  60. don't do anything that could mess up the system like calling
  61. malloc() or free().
  62. */
  63. static int recordCallback( const void *inputBuffer, void *outputBuffer,
  64. unsigned long framesPerBuffer,
  65. const PaStreamCallbackTimeInfo* timeInfo,
  66. PaStreamCallbackFlags statusFlags,
  67. void *userData )
  68. {
  69. paTestData *data = (paTestData*)userData;
  70. FILE *fout = data->fout;
  71. int framesToCopy;
  72. int i;
  73. int finished;
  74. short *rptr = (short*)inputBuffer;
  75. float out8k[N8];
  76. short out8k_short[N8];
  77. (void) outputBuffer; /* Prevent unused variable warnings. */
  78. (void) timeInfo;
  79. (void) statusFlags;
  80. (void) userData;
  81. if (data->framesLeft < framesPerBuffer) {
  82. framesToCopy = data->framesLeft;
  83. finished = paComplete;
  84. }
  85. else {
  86. framesToCopy = framesPerBuffer;
  87. finished = paContinue;
  88. }
  89. data->framesLeft -= framesToCopy;
  90. assert(inputBuffer != NULL);
  91. /* just use left channel */
  92. for(i=0; i<framesToCopy; i++,rptr+=2)
  93. data->in48k[i+FDMDV_OS_TAPS] = *rptr;
  94. /* downsample and update filter memory */
  95. fdmdv_48_to_8(out8k, &data->in48k[FDMDV_OS_TAPS], N8);
  96. for(i=0; i<FDMDV_OS_TAPS; i++)
  97. data->in48k[i] = data->in48k[i+framesToCopy];
  98. /* save 8k to disk */
  99. for(i=0; i<N8; i++)
  100. out8k_short[i] = (short)out8k[i];
  101. /* note Portaudio docs recs. against making systems calls like
  102. fwrite() in this callback but seems to work OK */
  103. fwrite(out8k_short, sizeof(short), N8, fout);
  104. return finished;
  105. }
  106. int main(int argc, char *argv[])
  107. {
  108. PaStreamParameters inputParameters;
  109. PaStream* stream;
  110. PaError err = paNoError;
  111. paTestData data;
  112. int i;
  113. int numSecs;
  114. if (argc != 3) {
  115. printf("usage: %s rawFile time(s)\n", argv[0]);
  116. exit(0);
  117. }
  118. data.fout = fopen(argv[1], "wt");
  119. if (data.fout == NULL) {
  120. printf("Error opening output raw file %s\n", argv[1]);
  121. exit(1);
  122. }
  123. numSecs = atoi(argv[2]);
  124. data.framesLeft = numSecs * SAMPLE_RATE;
  125. for(i=0; i<FDMDV_OS_TAPS; i++)
  126. data.in48k[i] = 0.0;
  127. err = Pa_Initialize();
  128. if( err != paNoError ) goto done;
  129. printf( "PortAudio version number = %d\nPortAudio version text = '%s'\n",
  130. Pa_GetVersion(), Pa_GetVersionText() );
  131. inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
  132. if (inputParameters.device == paNoDevice) {
  133. fprintf(stderr,"Error: No default input device.\n");
  134. goto done;
  135. }
  136. inputParameters.channelCount = NUM_CHANNELS; /* stereo input */
  137. inputParameters.sampleFormat = paInt16;
  138. inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
  139. inputParameters.hostApiSpecificStreamInfo = NULL;
  140. /* Record some audio --------------------------------------------- */
  141. err = Pa_OpenStream(
  142. &stream,
  143. &inputParameters,
  144. NULL, /* &outputParameters, */
  145. SAMPLE_RATE,
  146. N48,
  147. paClipOff, /* we won't output out of range samples so don't bother clipping them */
  148. recordCallback,
  149. &data );
  150. if( err != paNoError ) goto done;
  151. err = Pa_StartStream( stream );
  152. if( err != paNoError ) goto done;
  153. while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
  154. {
  155. Pa_Sleep(100);
  156. }
  157. if( err < 0 ) goto done;
  158. err = Pa_CloseStream( stream );
  159. if( err != paNoError ) goto done;
  160. fclose(data.fout);
  161. done:
  162. Pa_Terminate();
  163. if( err != paNoError )
  164. {
  165. fprintf( stderr, "An error occured while using the portaudio stream\n" );
  166. fprintf( stderr, "Error number: %d\n", err );
  167. fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
  168. err = 1; /* Always return 0 or 1, but no other return codes. */
  169. }
  170. return err;
  171. }