1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Merge pull request #4772 from Laserlicht/mixer_audio_duration

fix: use sdl mixer to get audio duration
This commit is contained in:
Ivan Savenko 2024-10-14 21:51:49 +03:00 committed by GitHub
commit 94a0de716a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -147,18 +147,24 @@ uint32_t CSoundHandler::getSoundDurationMilliseconds(const AudioPath & sound)
auto data = CResourceHandler::get()->load(resourcePath)->readAll();
SDL_AudioSpec spec;
uint32_t audioLen;
uint8_t * audioBuf;
uint32_t milliseconds = 0;
if(SDL_LoadWAV_RW(SDL_RWFromMem(data.first.get(), data.second), 1, &spec, &audioBuf, &audioLen) != nullptr)
Mix_Chunk * chunk = Mix_LoadWAV_RW(SDL_RWFromMem(data.first.get(), data.second), 1);
int freq = 0;
Uint16 fmt = 0;
int channels = 0;
if(!Mix_QuerySpec(&freq, &fmt, &channels))
return 0;
if(chunk != nullptr)
{
SDL_FreeWAV(audioBuf);
uint32_t sampleSize = SDL_AUDIO_BITSIZE(spec.format) / 8;
uint32_t sampleCount = audioLen / sampleSize;
uint32_t sampleLen = sampleCount / spec.channels;
milliseconds = 1000 * sampleLen / spec.freq;
Uint32 sampleSizeBytes = (fmt & 0xFF) / 8;
Uint32 samples = (chunk->alen / sampleSizeBytes);
Uint32 frames = (samples / channels);
milliseconds = ((frames * 1000) / freq);
Mix_FreeChunk(chunk);
}
return milliseconds;