Audio/Music: Fix Seek method

This commit is contained in:
Jérôme Leclercq 2021-06-03 17:20:09 +02:00
parent 5bc3cd8d8c
commit f0b26efcd3
3 changed files with 13 additions and 11 deletions

View File

@ -250,7 +250,7 @@ namespace Nz
else else
{ {
UInt64 readSample = drwav_read_pcm_frames_s16(&m_decoder, sampleCount / m_decoder.channels, static_cast<Int16*>(buffer)); UInt64 readSample = drwav_read_pcm_frames_s16(&m_decoder, sampleCount / m_decoder.channels, static_cast<Int16*>(buffer));
m_readSampleCount += readSample; m_readSampleCount += readSample * m_decoder.channels;
return readSample * m_decoder.channels; return readSample * m_decoder.channels;
} }
@ -258,7 +258,7 @@ namespace Nz
void Seek(UInt64 offset) override void Seek(UInt64 offset) override
{ {
drwav_seek_to_pcm_frame(&m_decoder, offset); drwav_seek_to_pcm_frame(&m_decoder, (m_mixToMono) ? offset : offset / m_decoder.channels);
m_readSampleCount = offset; m_readSampleCount = offset;
} }

View File

@ -338,8 +338,8 @@ namespace Nz
void Seek(UInt64 offset) override void Seek(UInt64 offset) override
{ {
if (m_mixToMono) if (!m_mixToMono)
offset *= m_channelCount; offset /= m_channelCount;
ov_pcm_seek(&m_decoder, Int64(offset)); ov_pcm_seek(&m_decoder, Int64(offset));
} }
@ -347,8 +347,8 @@ namespace Nz
UInt64 Tell() override UInt64 Tell() override
{ {
UInt64 offset = UInt64(ov_pcm_tell(&m_decoder)); UInt64 offset = UInt64(ov_pcm_tell(&m_decoder));
if (m_mixToMono) if (!m_mixToMono)
offset /= m_channelCount; offset *= m_channelCount;
return offset; return offset;
} }

View File

@ -34,7 +34,7 @@ namespace Nz
std::mutex bufferLock; std::mutex bufferLock;
std::shared_ptr<SoundStream> stream; std::shared_ptr<SoundStream> stream;
std::thread thread; std::thread thread;
UInt64 playingOffset; UInt64 streamOffset;
bool loop = false; bool loop = false;
unsigned int sampleRate; unsigned int sampleRate;
}; };
@ -342,8 +342,10 @@ namespace Nz
if (isPlaying) if (isPlaying)
Stop(); Stop();
m_impl->playingOffset = offset; UInt64 sampleOffset = UInt64(offset) * m_impl->sampleRate * GetChannelCount(m_impl->stream->GetFormat()) / 1000ULL;
m_impl->processedSamples = UInt64(offset) * m_impl->sampleRate * GetChannelCount(m_impl->stream->GetFormat()) / 1000ULL;
m_impl->processedSamples = sampleOffset;
m_impl->streamOffset = sampleOffset;
if (isPlaying) if (isPlaying)
Play(); Play();
@ -371,7 +373,7 @@ namespace Nz
{ {
std::lock_guard<std::mutex> lock(m_impl->stream->GetMutex()); std::lock_guard<std::mutex> lock(m_impl->stream->GetMutex());
m_impl->stream->Seek(m_impl->playingOffset); m_impl->stream->Seek(m_impl->streamOffset);
// Fill the buffer by reading from the stream // Fill the buffer by reading from the stream
for (;;) for (;;)
@ -388,7 +390,7 @@ namespace Nz
break; break;
} }
m_impl->playingOffset = m_impl->stream->Tell(); m_impl->streamOffset = m_impl->stream->Tell();
} }
// Update the buffer (send it to OpenAL) and queue it if we got any data // Update the buffer (send it to OpenAL) and queue it if we got any data