簡體   English   中英

RtAudio-播放wav文件中的樣本

[英]RtAudio - Playing samples from wav file

我目前正在嘗試學習音頻編程。 我的目標是打開一個wav文件,提取所有內容並使用RtAudio播放樣本。

我制作了一個WaveLoader類,讓我提取示例和元數據。 我使用了指南,並使用010編輯器檢查了所有內容是否正確。 這是010編輯器的快照,顯示了結構和數據。

010編輯器

這就是我將原始樣本存儲在WaveLoader類中的方式:

        data = new short[wave_data.payloadSize]; // - Allocates memory size of chunk size

        if (!fread(data, 1, wave_data.payloadSize, sound_file))
        {
            throw ("Could not read wav data");
        }

如果我打印出每個樣本,我會得到:1,-3,4,-5 ...看起來還可以。

問題是我不確定如何演奏。 這是我所做的:

/*
 * Using PortAudio to play samples
 */
bool Player::Play() 
{
    ShowDevices();
    rt.showWarnings(true);

    RtAudio::StreamParameters oParameters; //, iParameters;
    oParameters.deviceId = rt.getDefaultOutputDevice();
    oParameters.firstChannel = 0;
    oParameters.nChannels = mAudio.channels;

    //iParameters.deviceId = rt.getDefaultInputDevice();
    //iParameters.nChannels = 2;

    unsigned int sampleRate = mAudio.sampleRate;

    // Use a buffer of 512, we need to feed callback with 512 bytes everytime!
    unsigned int nBufferFrames = 512;

    RtAudio::StreamOptions options;
    options.flags = RTAUDIO_SCHEDULE_REALTIME;
    options.flags = RTAUDIO_NONINTERLEAVED;

    //&parameters, NULL, RTAUDIO_FLOAT64,sampleRate, &bufferFrames, &mCallback, (void *)&rawData

    try {
        rt.openStream(&oParameters, NULL, RTAUDIO_SINT16, sampleRate, &nBufferFrames, &mCallback, (void*) &mAudio);
        rt.startStream();
    }
    catch (RtAudioError& e) {
        std::cout << e.getMessage() << std::endl;
        return false;
    }
    return true;
}

/*
* RtAudio Callback
*
*/
int mCallback(void * outputBuffer, void * inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void * userData)
{
    unsigned int i = 0;
    short *out = static_cast<short*>(outputBuffer);
    auto *data = static_cast<Player::AUDIO_DATA*>(userData);

    // if i is more than our data size, we are done!
    if (i > data->dataSize) return 1;

    // First time callback is called data->ptr is 0, this means that the offset is 0
    // Second time data->ptr is 1, this means offset = nBufferFrames (512) * 1 = 512
    unsigned int offset = nBufferFrames * data->ptr++;

    printf("Offset: %i\n", offset);
    // First time callback is called offset is 0, we are starting from 0 and looping nBufferFrames (512) times, this gives us 512 bytes
    // Second time, the offset is 1, we are starting from 512 bytes and looping to 512 + 512 = 1024 
    for (i = offset; i < offset + nBufferFrames; ++i)
    {
        short sample = data->rawData[i]; // Get raw sample from our struct
        *out++ = sample;                // Pass to output buffer for playback

        printf("Current sample value: %i\n", sample);       // this is showing 1, -3, 4, -5 check 010 editor
    }

    printf("Current time: %f\n", streamTime);
    return 0;
}

在回調函數內部,當我打印出樣本值時,我得到的像是010編輯器嗎? 為什么不是rtaudio播放它們。 怎么了 我是否需要將樣本值標准化為-1和1之間?

編輯:我正在嘗試播放的wav文件:

  • 塊大小:16
  • 格式:1
  • 頻道:1
  • SampleRate:48000
  • 字節率:96000
  • BlockAlign:2
  • BitPerSample:16
  • 原始樣本總數:2217044字節

由於某些原因,當我將輸入參數傳遞給openStream()時,它可以工作

    RtAudio::StreamParameters oParameters, iParameters;
    oParameters.deviceId = rt.getDefaultOutputDevice();
    oParameters.firstChannel = 0;
    //oParameters.nChannels = mAudio.channels;
    oParameters.nChannels = mAudio.channels;

    iParameters.deviceId = rt.getDefaultInputDevice();
    iParameters.nChannels = 1;

    unsigned int sampleRate = mAudio.sampleRate;

    // Use a buffer of 512, we need to feed callback with 512 bytes everytime!
    unsigned int nBufferFrames = 512;

    RtAudio::StreamOptions options;
    options.flags = RTAUDIO_SCHEDULE_REALTIME;
    options.flags = RTAUDIO_NONINTERLEAVED;

    //&parameters, NULL, RTAUDIO_FLOAT64,sampleRate, &bufferFrames, &mCallback, (void *)&rawData

    try {
        rt.openStream(&oParameters, &iParameters, RTAUDIO_SINT16, sampleRate, &nBufferFrames, &mCallback, (void*) &mAudio);
        rt.startStream();
    }
    catch (RtAudioError& e) {
        std::cout << e.getMessage() << std::endl;
        return false;
    }
    return true;

當我嘗試播放麥克風時,聲音是如此隨機。 我離開了輸入參數,我的wav文件突然播放了。 這是一個錯誤嗎?

暫無
暫無

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

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