1 year ago
#368896
John
SDL2 extraneous hum when playback recorded voice
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:
#include <QCoreApplication>
#include "SDL.h"
#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() << "...Record";
//Copy audio from stream
memcpy( &gRecordingBuffer[ gBufferBytePosition ], stream, len );
//Move along buffer
gBufferBytePosition += len;
}
void cb_playback(void *userdata, Uint8 *stream, int len) {
//qDebug() << "...Playback " << gBufferBytePosition;
//Copy audio to stream
memcpy( stream, &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, &want, &have, 0);
if (recordingDevice == 0) {
qDebug() << "Cannot open recording device";
return 1;
}
if (have.format != want.format) {
qDebug() << "We didn't get the wanted format.";
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, &want2, &have2, 0);
if (playbackDevice == 0) {
qDebug() << "Failed to open audio device: ";
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();
}
c++
audio
sdl-2
audio-recording
playback
0 Answers
Your Answer