audio - 如何使用soundio一次播放多个流?

标签 audio

我是Soundio的新手。我想知道如何一次播放多个音频源。
我想了解是否应该创建多个流(似乎是错误的)并让操作系统进行混合,还是应该实现软件混合?
如果我的输入源以不同的频率运行,软件混合似乎也很困难。
我基本上是在问“如何混合音频”吗?
我需要一点指导。
这是我的用例:
我有5个不同的MP3文件。一种是背景音乐,另一种是声音效果。我想启动背景音乐,然后在用户执行某些操作(例如单击图形按钮)时播放声音效果。 (这是一个游戏)

最佳答案

您可以创建多个流并同时播放它们。您无需自己进行混合。无论如何,它需要大量的工作。
定义WAVE_INFOPLAYBACK_INFO:

struct WAVE_INFO
{
    SoundIoFormat format;
    std::vector<unsigned char*> data;
    int frames; // number of frames in this clip
}

struct PLAYBACK_INFO
{
    const WAVE_INFO* wave_info; // information of sound clip
    int progress; // number of frames already played
}
  • 从声音片段中提取WAVE信息,并将其存储在WAVE_INFO:std::vector<WAVE_INFO> waves_;数组中。初始化后,此 vector 将不会更改。
  • 当您想播放waves_[index]时:
  • SoundIoOutStream* outstream = soundio_outstream_create(sound_device_);
    outstream->write_callback = write_callback;
    PlayBackInfo* playback_info = new PlayBackInfo({&waves_[index], 0});
    outstream->format = playback_info->wave_info->format;
    outstream->userdata = playback_info;
    soundio_outstream_open(outstream);
    soundio_outstream_start(outstream);
    std::thread stopper([this, outstream]()
    {
        PlayBackInfo* playback_info = (PlayBackInfo*)outstream->userdata;
        while (playback_info->progress != playback_info->wave_info->frames)
        {
            soundio_wait_events(soundio_);
        }
        soundio_outstream_destroy(outstream);
        delete playback_info;
    });
    stopper.detach();
    
  • write_callback函数中:
  • PlayBackInfo* playback_info = (PlayBackInfo*)outstream->userdata;
    int frames_left = playback_info->audio_info->frames - playback_info->progress;
    if (frames_left == 0)
    {
        soundio_wakeup(Window::window_->soundio_);
        return;
    }
    if (frames_left > frame_count_max)
    {
        frames_left = frame_count_max;
    }
    // fill the buffer using
    // soundio_outstream_begin_write and
    // soundio_outstream_end_write by
    // data in playback_info->wave_info.data
    // considering playback_info->progress.
    
    // update playback_info->progress based on
    // number of frames are written to buffer
    
    // for background music:
    if (playback_info->audio_info->frames == playback_info->progress)
    {
        // if application has not exited:
        playback_info->progress = 0;
    }
    
    此解决方案也有效,需要大量改进。请仅将其视为POC。

    关于audio - 如何使用soundio一次播放多个流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64001616/

    相关文章:

    python - 在 python 中更改 wav 文件的音量

    objective-c - 使用UITableView的音频播放器

    ios - 如何使用 Phonegap 流式传输在线广播?

    python - 值错误 ("Incomplete wav chunk.") 值错误 : Incomplete wav chunk

    php - 如何判断视频是否有音频?

    objective-c - 设置SoundiD xcode错误-尝试播放音乐

    Android 浏览器在每次 play() 后卸载 HTML5 音频元素,导致延迟

    javascript - 尝试在 HTML5 中添加音频元素

    jquery - 随机音频onmouseover jQuery不起作用

    audio - 改变音频音调的最佳方法是什么?