k2-fsa / sherpa-onnx

Speech-to-text, text-to-speech, speaker diarization, and VAD using next-gen Kaldi with onnxruntime without Internet connection. Support embedded systems, Android, iOS, Raspberry Pi, RISC-V, x86_64 servers, websocket server/client, C/C++, Python, Kotlin, C#, Go, NodeJS, Java, Swift, Dart, JavaScript, Flutter, Object Pascal, Lazarus, Rust
https://k2-fsa.github.io/sherpa/onnx/index.html
Apache License 2.0
3.66k stars 425 forks source link

sherpa_onnx::AlsaPlay 可以播放立体声音频吗 #1528

Closed Mactarvish closed 1 week ago

Mactarvish commented 1 week ago

Ubuntu 22.04 073dc7577a7e282a569677312f3a708d44511421

我正在尝试使用框架提供的工具读取音频并播放,以下是我的部分代码:

int main()
{

    int sampleRate;
    bool isOk;
    // auto cmd = fmt::format("aplay {}/{}.wav", config.srcMusicDir, musicName);
    // system(cmd.c_str());
    auto samples = sherpa_onnx::ReadWave("青花瓷.wav", &sampleRate, &isOk);
    if (!isOk)
    {
        return -1;
    }
    logger->info("开始播放 {} ...");
    sherpa_onnx::AlsaPlay ap("default", sampleRate);
    ap.Play({samples.data(), samples.data() + samples.size()});
    ap.Drain();

    cout <<11 << endl;
    return 0;
}

青花瓷.wav 是一个立体声音频,但是上述代码播放出来的似乎只有一个声道。 我尝试修改Play函数的实现,将第57行 err = snd_pcm_hw_params_set_channels(handle_, params, 1); 中的1改成2,似乎还是不行,请问还有哪里需要修改,谢谢。

csukuangfj commented 1 week ago

ReadWav 只取第0个通道。你需要修改这个。

Mactarvish commented 1 week ago

改了半天没改好,不过网上找了个能直接用的哈哈 https://blog.csdn.net/wangcong02345/article/details/52399210