Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "portaudio.h"
  5. #include <sys/time.h>
  6. #include <windows.h>
  7. #include <windowsx.h>
  8. #include <unistd.h>
  9.  
  10. #include "webrtc/modules/audio_processing/include/audio_processing.h"
  11. using webrtc::AudioProcessing;
  12. using webrtc::AudioFrame;
  13. using webrtc::GainControl;
  14. using webrtc::NoiseSuppression;
  15. using webrtc::EchoCancellation;
  16. using webrtc::VoiceDetection;
  17.  
  18.  
  19. #define SAMPLE_RATE       (32000)
  20. #define FRAMES_PER_BUFFER   (320)
  21. #define DITHER_FLAG           (0)
  22.  
  23. #define PA_SAMPLE_TYPE  paFloat32
  24. #define SAMPLE_SIZE (4)
  25. #define SAMPLE_SILENCE  (0)
  26. #define PRINTF_S_FORMAT "%8f"
  27.  
  28. /*******************************************************************/
  29. int main(int argc, char **argv);
  30. /* error handling */
  31. int xrun(PaStream *stream, int err, char* sampleBlock);
  32. void error1(PaStream *stream, char* sampleBlock);
  33. void error2(PaStream *stream, int err);
  34. int main (int argc, char **argv)
  35. {
  36.  
  37.     PaStreamParameters inputParameters;
  38.     PaStream *stream = NULL;
  39.     PaError err;
  40.     const PaDeviceInfo* inputInfo;
  41.     char *sampleBlock = NULL;
  42.     int i;
  43.     int numBytes;
  44.     int numChannels;
  45.  
  46.     err = Pa_Initialize();
  47.     if( err != paNoError ) error2(stream, err);
  48.  
  49.     inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
  50.     inputInfo = Pa_GetDeviceInfo( inputParameters.device );
  51.     numChannels = inputInfo->maxInputChannels;
  52.     inputParameters.channelCount = 1;// numChannels;
  53.     inputParameters.sampleFormat = PA_SAMPLE_TYPE;
  54.     inputParameters.suggestedLatency = inputInfo->defaultHighInputLatency ;
  55.     inputParameters.hostApiSpecificStreamInfo = NULL;
  56.     printf( "Input device # %d.\n", inputParameters.device );
  57.     printf( "    Name: %s\n", inputInfo->name );
  58.  
  59.     /* -- setup -- */
  60.  
  61.     err = Pa_OpenStream(
  62.               &stream,
  63.               &inputParameters,
  64.               NULL,
  65.               SAMPLE_RATE,
  66.               FRAMES_PER_BUFFER,
  67.               paClipOff,      /* we won't output out of range samples so don't bother clipping them */
  68.               NULL, /* no callback, use blocking API */
  69.               NULL ); /* no callback, so no callback userData */
  70.     if( err != paNoError ) error2(stream, err);
  71.  
  72.     numBytes = FRAMES_PER_BUFFER * numChannels * SAMPLE_SIZE ;
  73.     sampleBlock = (char *) malloc( numBytes );
  74.     if( sampleBlock == NULL )
  75.     {
  76.         printf("Could not allocate record array.\n");
  77.         error1(stream, sampleBlock);
  78.     }
  79.  
  80.     err = Pa_StartStream( stream );
  81.     if( err != paNoError ) error1(stream, sampleBlock);
  82.  
  83.         // Configure webrtc::audioprocessing
  84.         AudioProcessing* apm = AudioProcessing::Create();
  85.  
  86.         apm->high_pass_filter()->Enable(true);
  87.  
  88.         apm->echo_cancellation()->enable_drift_compensation(false);
  89.         apm->echo_cancellation()->Enable(true);
  90.  
  91.         apm->noise_suppression()->set_level(apm->noise_suppression()->kHigh);
  92.         apm->noise_suppression()->Enable(true);
  93.  
  94.         apm->gain_control()->set_analog_level_limits(0, 255);
  95.         apm->gain_control()->set_mode(apm->gain_control()->kAdaptiveAnalog);
  96.         apm->gain_control()->Enable(true);
  97.  
  98.         apm->voice_detection()->Enable(true);
  99.  
  100.         int analog_level = apm->gain_control()->stream_analog_level();
  101.         int delay_ms = 20;
  102.         int voiceDetected = 0;
  103.  
  104.  
  105.     long int holdTime = 600; //milliseconds
  106.     int prevVoiceDetected = -1;
  107.     int holding = 0;
  108.     int transmitting = 0;
  109.     int prevTransmitting = -1;
  110.     struct timeval startHoldTime, currentTime, elapsedHoldTime;
  111.  
  112.         while (1) {
  113.                 // Read in input frames
  114.         err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
  115.         if( err ) xrun(stream, err, sampleBlock);
  116.  
  117.                 // Run webrtc vad
  118.                 apm->set_stream_delay_ms(delay_ms);
  119.                 apm->gain_control()->set_stream_analog_level(analog_level);
  120.  
  121.                 /*
  122.                 // A apm->ProcessStream call is required here. The one I've tried here seg faults, probably due to those casts I don't understand
  123.                 webrtc::StreamConfig inputConfig = webrtc::StreamConfig(SAMPLE_RATE, numChannels, false);
  124.                 webrtc::StreamConfig outputConfig = webrtc::StreamConfig(SAMPLE_RATE, numChannels, false);
  125.                 apm->ProcessStream((const float* const*)sampleBlock, inputConfig, outputConfig, (float* const*)sampleBlock);
  126.                 */
  127.  
  128.  
  129.                 analog_level = apm->gain_control()->stream_analog_level();
  130.                 voiceDetected = apm->voice_detection()->stream_has_voice();
  131.  
  132.                 transmitting = 0;
  133.                 if (voiceDetected) {
  134.                         transmitting = 1;
  135.                         holding = 0;
  136.                 } else if (holding) {
  137.                         gettimeofday (&currentTime, NULL);
  138.                         long elapsedHoldTime =  (((currentTime.tv_sec - startHoldTime.tv_sec)*1000000L+currentTime.tv_usec) - startHoldTime.tv_usec)/1000;
  139.                         //printf("elapsedtime: %d\n", elapsedHoldTime); fflush(stdout);
  140.                         if (elapsedHoldTime > holdTime) {
  141.                                 //printf("completedhold\n"); fflush(stdout);
  142.                                 holding = 0;
  143.                         } else {
  144.                                 //printf("holding\n"); fflush(stdout);
  145.                                 transmitting = 1;
  146.                         }
  147.                 } else if (prevVoiceDetected) {
  148.                         holding = 1;
  149.                         gettimeofday (&startHoldTime, NULL);
  150.                         transmitting = 1;
  151.                 }
  152.                 prevVoiceDetected = voiceDetected;
  153.  
  154.                 if (prevTransmitting != transmitting) {
  155.                         printf("Transmitting: %s\n", (transmitting) ? "true" : "false"); fflush(stdout);
  156.                 }
  157.                 prevTransmitting = transmitting;
  158.     }
  159.     printf("Wire off.\n"); fflush(stdout);
  160.  
  161.     err = Pa_StopStream( stream );
  162.     if( err != paNoError ) error1(stream, sampleBlock);
  163.  
  164.     free( sampleBlock );
  165.  
  166.     Pa_Terminate();
  167.     return 0;
  168.  
  169. }
  170.  
  171. int xrun(PaStream *stream, int err, char* sampleBlock) {
  172.     printf("err = %d\n", err); fflush(stdout);
  173.     if( stream ) {
  174.        Pa_AbortStream( stream );
  175.        Pa_CloseStream( stream );
  176.     }
  177.     free( sampleBlock );
  178.     Pa_Terminate();
  179.     if( err & paInputOverflow )
  180.        fprintf( stderr, "Input Overflow.\n" );
  181.     if( err & paOutputUnderflow )
  182.        fprintf( stderr, "Output Underflow.\n" );
  183.     return -2;
  184. }
  185.  
  186. void error1(PaStream *stream, char* sampleBlock) {
  187.     free( sampleBlock );
  188.     exit(-1);
  189. }
  190. void error2(PaStream *stream, int err) {
  191.     if( stream ) {
  192.        Pa_AbortStream( stream );
  193.        Pa_CloseStream( stream );
  194.     }
  195.     Pa_Terminate();
  196.     fprintf( stderr, "An error occured while using the portaudio stream\n" );
  197.     fprintf( stderr, "Error number: %d\n", err );
  198.     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
  199.     exit(-1);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement