Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "portaudio.h"
  5.  
  6. /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
  7. #define SAMPLE_RATE (44100)
  8. #define FRAMES_PER_BUFFER (512)
  9. #define NUM_SECONDS (3)
  10. /* #define DITHER_FLAG (paDitherOff) */
  11. #define DITHER_FLAG (0)
  12.  
  13. /* Select sample format. */
  14. #if 1
  15. #define PA_SAMPLE_TYPE paFloat32
  16. #define SAMPLE_SIZE (4)
  17. #define SAMPLE_SILENCE (0.0f)
  18. #define PRINTF_S_FORMAT "%.8f"
  19. #elif 0
  20. #define PA_SAMPLE_TYPE paInt16
  21. #define SAMPLE_SIZE (2)
  22. #define SAMPLE_SILENCE (0)
  23. #define PRINTF_S_FORMAT "%d"
  24. #elif 0
  25. #define PA_SAMPLE_TYPE paInt24
  26. #define SAMPLE_SIZE (3)
  27. #define SAMPLE_SILENCE (0)
  28. #define PRINTF_S_FORMAT "%d"
  29. #elif 0
  30. #define PA_SAMPLE_TYPE paInt8
  31. #define SAMPLE_SIZE (1)
  32. #define SAMPLE_SILENCE (0)
  33. #define PRINTF_S_FORMAT "%d"
  34. #else
  35. #define PA_SAMPLE_TYPE paUInt8
  36. #define SAMPLE_SIZE (1)
  37. #define SAMPLE_SILENCE (128)
  38. #define PRINTF_S_FORMAT "%d"
  39. #endif
  40.  
  41. /*******************************************************************/
  42. int main(void)
  43. {
  44. PaStreamParameters inputParameters, outputParameters;
  45. PaStream *stream = NULL;
  46. PaError err;
  47. const PaDeviceInfo* inputInfo;
  48. const PaDeviceInfo* outputInfo;
  49. char *sampleBlock = NULL;
  50. int i;
  51. int numBytes;
  52. int numChannels;
  53.  
  54. printf("patest_read_write_wire.c\n"); fflush(stdout);
  55. printf("sizeof(int) = %lu\n", sizeof(int)); fflush(stdout);
  56. printf("sizeof(long) = %lu\n", sizeof(long)); fflush(stdout);
  57.  
  58. err = Pa_Initialize();
  59. if( err != paNoError ) goto error2;
  60.  
  61. inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
  62. printf( "Input device # %d.\n", inputParameters.device );
  63. inputInfo = Pa_GetDeviceInfo( inputParameters.device );
  64. printf( " Name: %s\n", inputInfo->name );
  65. printf( " LL: %g s\n", inputInfo->defaultLowInputLatency );
  66. printf( " HL: %g s\n", inputInfo->defaultHighInputLatency );
  67.  
  68. outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
  69. printf( "Output device # %d.\n", outputParameters.device );
  70. outputInfo = Pa_GetDeviceInfo( outputParameters.device );
  71. printf( " Name: %s\n", outputInfo->name );
  72. printf( " LL: %g s\n", outputInfo->defaultLowOutputLatency );
  73. printf( " HL: %g s\n", outputInfo->defaultHighOutputLatency );
  74.  
  75. numChannels = inputInfo->maxInputChannels < outputInfo->maxOutputChannels
  76. ? inputInfo->maxInputChannels : outputInfo->maxOutputChannels;
  77. printf( "Num channels = %d.\n", numChannels );
  78.  
  79. inputParameters.channelCount = numChannels;
  80. inputParameters.sampleFormat = PA_SAMPLE_TYPE;
  81. inputParameters.suggestedLatency = inputInfo->defaultHighInputLatency ;
  82. inputParameters.hostApiSpecificStreamInfo = NULL;
  83.  
  84. outputParameters.channelCount = numChannels;
  85. outputParameters.sampleFormat = PA_SAMPLE_TYPE;
  86. outputParameters.suggestedLatency = outputInfo->defaultHighOutputLatency;
  87. outputParameters.hostApiSpecificStreamInfo = NULL;
  88.  
  89. /* -- setup -- */
  90.  
  91. err = Pa_OpenStream(
  92. &stream,
  93. &inputParameters,
  94. &outputParameters,
  95. SAMPLE_RATE,
  96. FRAMES_PER_BUFFER,
  97. paClipOff, /* we won't output out of range samples so don't bother clipping them */
  98. NULL, /* no callback, use blocking API */
  99. NULL ); /* no callback, so no callback userData */
  100. if( err != paNoError ) goto error2;
  101.  
  102. numBytes = FRAMES_PER_BUFFER * numChannels * SAMPLE_SIZE ;
  103. sampleBlock = (char *) malloc( numBytes );
  104. if( sampleBlock == NULL )
  105. {
  106. printf("Could not allocate record array.\n");
  107. goto error1;
  108. }
  109. memset( sampleBlock, SAMPLE_SILENCE, numBytes );
  110.  
  111. err = Pa_StartStream( stream );
  112. if( err != paNoError ) goto error1;
  113. printf("Wire on. Will run %d seconds.\n", NUM_SECONDS); fflush(stdout);
  114.  
  115. for( i=0; i<(NUM_SECONDS*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
  116. {
  117. printf("HAAAA\n");
  118. // You may get underruns or overruns if the output is not primed by PortAudio.
  119. err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
  120. if( err ) goto xrun;
  121. err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
  122. if( err ) goto xrun;
  123. }
  124. printf("Wire off.\n"); fflush(stdout);
  125.  
  126. err = Pa_StopStream( stream );
  127. if( err != paNoError ) goto error1;
  128.  
  129. free( sampleBlock );
  130.  
  131. Pa_Terminate();
  132. return 0;
  133.  
  134. xrun:
  135. printf("err = %d\n", err); fflush(stdout);
  136. if( stream ) {
  137. Pa_AbortStream( stream );
  138. Pa_CloseStream( stream );
  139. }
  140. free( sampleBlock );
  141. Pa_Terminate();
  142. if( err & paInputOverflow )
  143. fprintf( stderr, "Input Overflow.\n" );
  144. if( err & paOutputUnderflow )
  145. fprintf( stderr, "Output Underflow.\n" );
  146. return -2;
  147. error1:
  148. free( sampleBlock );
  149. error2:
  150. if( stream ) {
  151. Pa_AbortStream( stream );
  152. Pa_CloseStream( stream );
  153. }
  154. Pa_Terminate();
  155. fprintf( stderr, "An error occured while using the portaudio stream\n" );
  156. fprintf( stderr, "Error number: %d\n", err );
  157. fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
  158. return -1;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement