我正在尝试制作一个C ++应用程序,以通过VoIP协议在2个客户端之间(使用UDP)传输音频。
我正在使用Portaudio C库,但在封装此库时遇到问题。为了将录制的音频发送给另一个客户端,我想获取录制的声音样本(实时)。
目前,我只能录制声音,然后播放录制的内容。 我对这个库一点都不满意,我们将不胜感激。
这是我到目前为止所做的。
Audio.cpp->回调方法:
static int PaRecordCallback(const void *input, void *output, unsigned long frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
Audio *audio = reinterpret_cast<Audio *>(userData);
return audio->RecordCallback(input, output, frameCount, timeInfo, statusFlags);
}
static int PaPlayCallback(const void *input, void *output, unsigned long frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
Audio *audio = reinterpret_cast<Audio *>(userData);
return audio->PlayCallback(input, output, frameCount, timeInfo, statusFlags);
}
int Audio::RecordCallback(const void *input, void *output, unsigned long &frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags &statusFlags)
{
std::cout << "Frame index:\t\t" << _recordedFrameIndex << std::endl << "Max frame index:\t" << _maxFrameIndex << std::endl << "--------------" << std::endl;
const SAMPLE *rptr = static_cast<const SAMPLE *>(input);
SAMPLE *wptr = &_recordedSamples[_recordedFrameIndex * NUM_CHANNELS];
unsigned long framesLeft = _maxFrameIndex - _recordedFrameIndex;
unsigned long framesToCalc;
int finished;
if (framesLeft < frameCount) {
framesToCalc = framesLeft;
finished = paComplete;
} else {
framesToCalc = frameCount;
finished = paContinue;
}
for (unsigned long i = 0; i < framesToCalc; i++) {
*wptr++ = *rptr++;
if (NUM_CHANNELS == 2)
*wptr++ = *rptr++;
}
_recordedFrameIndex += framesToCalc;
return finished;
}
int Audio::PlayCallback(const void *input, void *output, unsigned long &frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags &statusFlags)
{
SAMPLE *rptr = &_recordedSamples[_playedFrameIndex * NUM_CHANNELS];
SAMPLE *wptr = static_cast<SAMPLE *>(output);
unsigned long framesLeft = _maxFrameIndex - _playedFrameIndex;
unsigned int i;
int finished;
if (framesLeft < frameCount) {
for (i = 0; i < framesLeft; i++) {
*wptr++ = *rptr++;
if (NUM_CHANNELS == 2)
*wptr++ = *rptr++;
}
for (; i < frameCount; i++) {
*wptr++ = 0;
if (NUM_CHANNELS == 2)
*wptr++ = 0;
}
_playedFrameIndex += framesLeft;
finished = paComplete;
} else {
for (i = 0; i < frameCount; i++) {
*wptr++ = *rptr++;
if (NUM_CHANNELS == 2)
*wptr++ = *rptr++;
}
_playedFrameIndex += frameCount;
finished = paContinue;
}
return finished;
}
Audio.cpp->录制和播放方法:
void Audio::Record()
{
if (!_recordStream) {
OpenRecordStream();
_recordedFrameIndex = 0;
_err = Pa_StartStream(_recordStream);
if (_err != paNoError)
AudioError("Audio::Record -> Pa_StartStream()");
std::cout << "Audio record stream started." << std::endl;
std::cout << "Recording ..." << std::endl;
_recording = true;
fflush(stdout);
} else if (_recording)
Pa_Sleep(1000);
}
void Audio::Play()
{
if (!_playStream) {
OpenPlayStream();
_playedFrameIndex = 0;
_err = Pa_StartStream(_playStream);
if (_err != paNoError)
AudioError("Audio::Play -> Pa_StartStream()");
std::cout << "Audio play stream started." << std::endl;
std::cout << "Playing ..." << std::endl;
_playing = true;
fflush(stdout);
} else if (_playing)
Pa_Sleep(500);
}
Audio.hpp->音频类:
#include <portaudio.h>
typedef int16_t SAMPLE;
#define PA_SAMPLE_TYPE paInt16
#define PRINTF_S_FORMAT "%.8f"
#define SAMPLE_RATE 44100
#define SAMPLE_SILENCE 0.0f
#define FRAMES_PER_BUFFER 1
#define NUM_SECONDS 5
#define NUM_CHANNELS 2
#define DITHER_FLAG 0
#define WRITE_TO_FILE 0
#define SAMPLE_SIZE NUM_SECONDS * SAMPLE_RATE * NUM_CHANNELS
class Audio
{
public:
Audio();
~Audio();
void Record();
void Play();
void OpenRecordStream();
void OpenPlayStream();
void CloseRecordStream();
void ClosePlayStream();
const bool &isRecording() const;
const bool &isPlaying() const;
const PaStream *GetRecordStream() const;
const PaStream *GetPlayStream() const;
void GetSamples(SAMPLE *);
void SetSamples(SAMPLE *);
int RecordCallback(const void *, void *, unsigned long &, \
const PaStreamCallbackTimeInfo *, PaStreamCallbackFlags &);
int PlayCallback(const void *, void *, unsigned long &, \
const PaStreamCallbackTimeInfo *, PaStreamCallbackFlags &);
bool _recording;
bool _playing;
protected:
private:
// Functions:
void AudioError(const std::string &);
// Variables:
PaError _err;
PaStream *_playStream;
PaStream *_recordStream;
SAMPLE *_samplesToPlay;
SAMPLE *_recordedSamples;
unsigned long _recordedFrameIndex;
unsigned long _playedFrameIndex;
unsigned long _maxFrameIndex;
PaStreamParameters _inputParameters;
PaStreamParameters _outputParameters;
};
很长的时间我很抱歉,但是我希望您掌握所有必要的信息以了解我的问题。 我不经常问问题,所以我确实需要一些帮助。
谢谢。
答案 0 :(得分:1)
您需要使用全双工回调来实时录制和播放,以便您以块的形式捕获录音的输入缓冲区(当您想要录制时)并将录音(甚至传入的声音)发送到输出缓冲区,也是分块的。 块大小通常为 64 到 4096 帧,每帧通常包含 2 个样本(通道 L 和通道 R)
全双工回调是一种循环缓冲区,它在输入缓冲区被 ADC 填充时为您带来 x 帧,并在其中用 x 帧填充输出缓冲区,以便在 DAC 请求时做好准备。
>