Thanks for the reply
Tutorial: ffmpeg tutorial
FMod: 2.02
code snippet:
create sound:
// sound name is used to identify sounds, so here it just the pointer address cast to long
inline void FMI_CreateSound(std::wstring&& soundName, void *userdata
, std::function<void(FMOD_CREATESOUNDEXINFO& exinfo)>callBack) {
memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); /* Required. */
callBack(exinfo);
exinfo.userdata = userdata;
FMOD::Sound* sound;
result = system->createSound(0, mode, &exinfo, &sound);
if (result == FMOD_OK) {
this->soundMap.emplace(soundName, SoundMapItem{sound});
}
}
creater:
this->cFMI.FMI_CreateSound(std::forward<std::wstring>(_itos((long)ppFFMpeg)), ppFFMpeg, [] (FMOD_CREATESOUNDEXINFO& exinfo) {
exinfo.numchannels = TARGET_CHANNEL_NUMBER;
// match the output of swr
exinfo.defaultfrequency = TARGET_SAMPLE_RATE;
// if size = SDL_AUDIO_BUFFER_SIZE, will micro shutter when seeking
// if size >> SDL_AUDIO_BUFFER_SIZE will cause lag due if sync with audio timer
exinfo.decodebuffersize = SDL_AUDIO_BUFFER_SIZE;
exinfo.length = SDL_AUDIO_BUFFER_SIZE;
exinfo.format = FMOD_SOUND_FORMAT_PCM16;
exinfo.pcmreadcallback =
[](FMOD_SOUND* sound, void* data, unsigned int datalen) {
auto pSound = (FMOD::Sound*)sound;
void* lockPtr_1 = nullptr;
void* lockPtr_2 = nullptr;
size_t prtLength_1 = 0;
size_t prtLength_2 = 0;
pSound->lock(0, datalen
, &lockPtr_1, &lockPtr_2
, &prtLength_1, &prtLength_2);
void* userdata = nullptr;
pSound->getUserData((void**)&userdata);
AudioCallback(userdata, (Uint8*)data, datalen);
pSound->unlock(lockPtr_1, lockPtr_2
, prtLength_1, prtLength_2);
return FMOD_OK;
};
exinfo.pcmsetposcallback = nullptr; /* User callback for seeking. */
});
audio callback:
static void AudioCallback(void* userdata, Uint8* stream, int len) {
// No mutex needed here as audio is paused when deleting pFFMpeg
auto CallBackCore = [&](FFMpeg** ppFFMpeg, Setter setter, Mixer mixer) {
if (ppFFMpeg == nullptr) {
setter(stream, 0, len);
return;
}
auto pFFMpeg = *ppFFMpeg;
if (pFFMpeg == nullptr) {
setter(stream, 0, len);
return;
}
pFFMpeg->audio_fillData(stream, len, setter, mixer);
return;
};
auto ppFFMpeg = (FFMpeg**)userdata;
CallBackCore(ppFFMpeg, memset, [](void* dst, const void* src, size_t len, int volume) {
memcpy(dst, src, len);
});
};
pFFMpeg->audio_fillData(stream, len, setter, mixer) is alomst the same of tutorial but add mutex and asbtract the mixer and memory setter to compatible with both SDL & FMod