我是Soundio的新手。我想知道如何一次播放多个音频源。
我想了解是否应该创建多个流(似乎是错误的)并让操作系统进行混合,还是应该实现软件混合?
如果我的输入源以不同的频率运行,软件混合似乎也很困难。
我基本上是在问“如何混合音频”吗?
我需要一点指导。
这是我的用例:
我有5个不同的MP3文件。一种是背景音乐,另一种是声音效果。我想启动背景音乐,然后在用户执行某些操作(例如单击图形按钮)时播放声音效果。 (这是一个游戏)
最佳答案
您可以创建多个流并同时播放它们。您无需自己进行混合。无论如何,它需要大量的工作。
定义WAVE_INFO
和PLAYBACK_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_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/