SDL2播放录制的声音时无关的嗡嗡声
我正在使用SDL2从麦克风中捕获音频并播放它们。当播放开始时,除了录制的声音外,耳机还会听到额外的嗡嗡声。为什么会发生这种情况?如何摆脱它?例如,当我们在电话上交谈时,我们只会听到声音,也没有无关的嗡嗡声。有代码:
#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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论