pa_play.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. pa_play.c
  3. David Rowe
  4. July 8 2012
  5. Converts samples from a 16 bit short 8000 Hz rawfile to 480000Hz
  6. sample rate and plays them using the default sound device. Used as
  7. an intermediate step in Portaudio integration.
  8. Modified from paex_record.c Portaudio example. Original author
  9. author Phil Burk http://www.softsynth.com
  10. */
  11. /*
  12. * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $
  13. *
  14. * This program uses the PortAudio Portable Audio Library.
  15. * For more information see: http://www.portaudio.com
  16. * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining
  19. * a copy of this software and associated documentation files
  20. * (the "Software"), to deal in the Software without restriction,
  21. * including without limitation the rights to use, copy, modify, merge,
  22. * publish, distribute, sublicense, and/or sell copies of the Software,
  23. * and to permit persons to whom the Software is furnished to do so,
  24. * subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be
  27. * included in all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  33. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  34. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. */
  37. #include <assert.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "portaudio.h"
  41. #include "fdmdv.h"
  42. #define SAMPLE_RATE 48000 /* 48 kHz sampling rate rec. as we
  43. can trust accuracy of sound
  44. card */
  45. #define N8 160 /* processing buffer size at 8 kHz */
  46. #define N48 (N8*FDMDV_OS) /* processing buffer size at 48 kHz */
  47. #define MEM8 (FDMDV_OS_TAPS/FDMDV_OS)
  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 *fin;
  54. float in8k[MEM8 + N8];
  55. } paTestData;
  56. /*
  57. This routine will be called by the PortAudio engine when audio is
  58. required. It may be called at interrupt level on some machines so
  59. don't do anything that could mess up the system like calling
  60. malloc() or free().
  61. */
  62. static int playCallback( const void *inputBuffer, void *outputBuffer,
  63. unsigned long framesPerBuffer,
  64. const PaStreamCallbackTimeInfo* timeInfo,
  65. PaStreamCallbackFlags statusFlags,
  66. void *userData )
  67. {
  68. paTestData *data = (paTestData*)userData;
  69. FILE *fin = data->fin;
  70. int i, nread;
  71. int finished;
  72. short *wptr = (short*)outputBuffer;
  73. float *in8k = data->in8k;
  74. float out48k[N48];
  75. short out48k_short[N48];
  76. short in8k_short[N8];
  77. (void) outputBuffer; /* Prevent unused variable warnings. */
  78. (void) timeInfo;
  79. (void) statusFlags;
  80. (void) userData;
  81. /* note Portaudio docs recs. against making systems calls like
  82. fwrite() in this callback but seems to work OK */
  83. nread = fread(in8k_short, sizeof(short), N8, fin);
  84. if (nread == N8)
  85. finished = paContinue;
  86. else
  87. finished = paComplete;
  88. for(i=0; i<N8; i++)
  89. in8k[MEM8+i] = in8k_short[i];
  90. /* upsample and update filter memory */
  91. fdmdv_8_to_48(out48k, &in8k[MEM8], N8);
  92. for(i=0; i<MEM8; i++)
  93. in8k[i] = in8k[i+N8];
  94. assert(outputBuffer != NULL);
  95. /* write signal to both channels */
  96. for(i=0; i<N48; i++)
  97. out48k_short[i] = (short)out48k[i];
  98. for(i=0; i<framesPerBuffer; i++,wptr+=2) {
  99. wptr[0] = out48k_short[i];
  100. wptr[1] = out48k_short[i];
  101. }
  102. return finished;
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. PaStreamParameters outputParameters;
  107. PaStream* stream;
  108. PaError err = paNoError;
  109. paTestData data;
  110. int i;
  111. if (argc != 2) {
  112. printf("usage: %s rawFile\n", argv[0]);
  113. exit(0);
  114. }
  115. data.fin = fopen(argv[1], "rt");
  116. if (data.fin == NULL) {
  117. printf("Error opening input raw file %s\n", argv[1]);
  118. exit(1);
  119. }
  120. for(i=0; i<MEM8; i++)
  121. data.in8k[i] = 0.0;
  122. err = Pa_Initialize();
  123. if( err != paNoError ) goto done;
  124. outputParameters.device = Pa_GetDefaultOutputDevice(); /* default input device */
  125. if (outputParameters.device == paNoDevice) {
  126. fprintf(stderr,"Error: No default output device.\n");
  127. goto done;
  128. }
  129. outputParameters.channelCount = NUM_CHANNELS; /* stereo input */
  130. outputParameters.sampleFormat = paInt16;
  131. outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
  132. outputParameters.hostApiSpecificStreamInfo = NULL;
  133. /* Play some audio --------------------------------------------- */
  134. err = Pa_OpenStream(
  135. &stream,
  136. NULL,
  137. &outputParameters,
  138. SAMPLE_RATE,
  139. N48,
  140. paClipOff,
  141. playCallback,
  142. &data );
  143. if( err != paNoError ) goto done;
  144. err = Pa_StartStream( stream );
  145. if( err != paNoError ) goto done;
  146. while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
  147. {
  148. Pa_Sleep(100);
  149. }
  150. if( err < 0 ) goto done;
  151. err = Pa_CloseStream( stream );
  152. if( err != paNoError ) goto done;
  153. fclose(data.fin);
  154. done:
  155. Pa_Terminate();
  156. if( err != paNoError )
  157. {
  158. fprintf( stderr, "An error occured while using the portaudio stream\n" );
  159. fprintf( stderr, "Error number: %d\n", err );
  160. fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
  161. err = 1; /* Always return 0 or 1, but no other return codes. */
  162. }
  163. return err;
  164. }