簡體   English   中英

使用 Portaudio C++ 進行實時音頻處理

[英]Real time audio processing with Portaudio C++

我正在嘗試制作一個 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;
};

如果它很長,我深表歉意,但我希望您擁有所有必要的信息來理解我的問題。 我不經常問問題,所以我真的需要一些幫助。

謝謝你。

您需要使用全雙工回調來實時錄制和播放,以便您以塊的形式捕獲錄音的輸入緩沖區(當您想要錄制時)並將錄音(甚至傳入的聲音)發送到輸出緩沖區,也在大塊。 塊大小通常為 64 到 4096 幀,每幀通常包含 2 個樣本(通道 L 和通道 R)

全雙工回調是一種循環緩沖區,當輸入緩沖區被 ADC 填充時,它會為您帶來 x 幀,並在其中用 x 幀填充輸出緩沖區,以便在 DAC 請求時准備就緒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM