title: SDL2 extraneous hum when playback recorded voice
body: <p>I'm using SDL2 to capture audio from a microphone and play them back. When playback starts, in addition to the recorded voice, an additional hum is also heard in the headphones. Why is this happening and how can you get rid of it? For example, when we talk on the phone, we hear only a voice and no extraneous hum. There is code:</p>
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &quot;SDL.h&quot;
  #ifdef __MINGW32__
  #undef main
  #endif
  
  #define SCREEN_WIDTH 640
  #define SCREEN_HEIGHT 480
  
  //Maximum recording time
  const int MAX_RECORDING_SECONDS = 5;
  
  //Maximum recording time plus padding
  const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;
  
  //Recording data buffer
  Uint8* gRecordingBuffer = NULL;
  
  //Size of data buffer
  Uint32 gBufferByteSize = 0;
  
  //Position in data buffer
  Uint32 gBufferBytePosition = 0;
  
  //Maximum position in data buffer for recording
  Uint32 gBufferByteMaxPosition = 0;
  
  void cb_record(void *userdata, Uint8 *stream, int len) {
      //qDebug() &lt;&lt; &quot;...Record&quot;;
      //Copy audio from stream
      memcpy( &amp;gRecordingBuffer[ gBufferBytePosition ], stream, len );
  
      //Move along buffer
      gBufferBytePosition += len;
  }
  
  void cb_playback(void *userdata, Uint8 *stream, int len) {
      //qDebug() &lt;&lt; &quot;...Playback  &quot; &lt;&lt; gBufferBytePosition;
      //Copy audio to stream
      memcpy( stream, &amp;gRecordingBuffer[ gBufferBytePosition ], len );
  
      //Move along buffer
      gBufferBytePosition += len;
  }
  
  int main(int argc, char** argv)
  {
      QCoreApplication app(argc,argv);
  
      SDL_Init(SDL_INIT_AUDIO);
      SDL_AudioSpec want, have;
      SDL_AudioSpec want2, have2;
  
      SDL_zero(want);
      SDL_zero(want2);
  
      want.freq = 44100;
      want.format = AUDIO_S16SYS;
      want.channels = 1;
      want.samples = 1024;
      want.callback = cb_record;
      SDL_AudioDeviceID recordingDevice = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(0, 1), 1, &amp;want, &amp;have, 0);
      if (recordingDevice == 0) {
          qDebug() &lt;&lt; &quot;Cannot open recording device&quot;;
          return 1;
      }
  
      if (have.format != want.format) {
          qDebug() &lt;&lt; &quot;We didn't get the wanted format.&quot;;
          return 1;
      }
  
      //Calculate per sample bytes
      int bytesPerSample = want.channels * ( SDL_AUDIO_BITSIZE( want.format ) / 8 );
  
      //Calculate bytes per second
      int bytesPerSecond = want.freq * bytesPerSample;
  
      //Calculate buffer size
      gBufferByteSize = RECORDING_BUFFER_SECONDS * bytesPerSecond;
  
      //Calculate max buffer use
      gBufferByteMaxPosition = MAX_RECORDING_SECONDS * bytesPerSecond;
  
      //Allocate and initialize byte buffer
      gRecordingBuffer = new Uint8[ gBufferByteSize ];
      memset( gRecordingBuffer, 0, gBufferByteSize );
  
      // playback
  
      want2.freq = 44100;
      want2.format = AUDIO_S16SYS;
      want2.channels = 1;
      want2.samples = 1024;
      want2.callback = cb_playback;
      SDL_AudioDeviceID playbackDevice = SDL_OpenAudioDevice(NULL, SDL_FALSE, &amp;want2, &amp;have2, 0);
  
      if (playbackDevice == 0) {
          qDebug() &lt;&lt; &quot;Failed to open audio device: &quot;;
          return 1;
      }
  
      // start recording
      SDL_PauseAudioDevice(recordingDevice, 0);
      SDL_Delay(3000);
      // stop recording
      SDL_PauseAudioDevice(recordingDevice, 1);
  
      // reset buffer position
      gBufferBytePosition = 0;
  
      //start playback
      SDL_PauseAudioDevice(playbackDevice, 0);
      SDL_Delay(3000);
      // stop playback
      SDL_PauseAudioDevice(playbackDevice, 1);
      return app.exec();
  }
  </code></pre>