From 4b8328cdfacd6768d4aa2d43fdb086a943b09741 Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Fri, 12 Jul 2024 16:37:28 +0300 Subject: [PATCH] Use correct sample size for processing Change mveaudio_process() to use sample size defined by stream. --- libmve/movie_sound.cpp | 1 + libmve/movie_sound.h | 7 +++++++ libmve/mve_audio.cpp | 10 +++++----- libmve/mve_audio.h | 3 ++- libmve/mveplay.cpp | 2 +- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/libmve/movie_sound.cpp b/libmve/movie_sound.cpp index ecbd69d9..345f3b0c 100644 --- a/libmve/movie_sound.cpp +++ b/libmve/movie_sound.cpp @@ -34,6 +34,7 @@ MovieSoundDevice::MovieSoundDevice(int sample_rate, uint16_t sample_size, uint8_ m_device_id = SDL_OpenAudioDevice(nullptr, 0, &spec, nullptr, 0); m_is_compressed = is_compressed; + m_sample_size = sample_size; }; MovieSoundDevice::~MovieSoundDevice() { diff --git a/libmve/movie_sound.h b/libmve/movie_sound.h index c306926c..43bf84ad 100644 --- a/libmve/movie_sound.h +++ b/libmve/movie_sound.h @@ -29,6 +29,7 @@ namespace D3 { class MovieSoundDevice : ISoundDevice { private: SDL_AudioDeviceID m_device_id = 0; + uint16_t m_sample_size = 0; public: /** @@ -55,6 +56,12 @@ public: */ void FillBuffer(char *stream, int len) const; + /** + * Get sample size + * @return 2 (for 16 bit), 1 (for 8 bit) + */ + [[nodiscard]] uint16_t GetSampleSize() const { return m_sample_size; }; + void Play() override; void Stop() override; void Lock() override; diff --git a/libmve/mve_audio.cpp b/libmve/mve_audio.cpp index e3c27e7a..c4acd4d1 100644 --- a/libmve/mve_audio.cpp +++ b/libmve/mve_audio.cpp @@ -61,23 +61,23 @@ static int16_t getWord(unsigned char **fin) { return value; } -void mveaudio_process(char *buffer, unsigned char *data, bool is_compressed) { +void mveaudio_process(char *buffer, unsigned char *data, int sample_size, bool is_compressed) { if (is_compressed) { int nCurOffsets[2]; data += 4; - int samples = getWord(&data) / 2; + int samples = getWord(&data) / sample_size; // Fill predictors nCurOffsets[0] = getWord(&data); nCurOffsets[1] = getWord(&data); for (int i = 0; i < samples; i++) { nCurOffsets[i & 1] = std::clamp(nCurOffsets[i & 1] + audio_exp_table[data[i]], -32768, 32767); - memcpy(buffer + i * 2, &nCurOffsets[i & 1], 2); + memcpy(buffer + i * sample_size, &nCurOffsets[i & 1], sample_size); } } else { data += 2; - int samples = getWord(&data); - memcpy(buffer, &data, samples * 2); + int size = getWord(&data); + memcpy(buffer, &data, size); } } diff --git a/libmve/mve_audio.h b/libmve/mve_audio.h index 68124758..eb0e234a 100644 --- a/libmve/mve_audio.h +++ b/libmve/mve_audio.h @@ -22,8 +22,9 @@ * Process input data and send parsed data into queue buffer * @param buffer output queue buffer * @param data input data + * @param sample_size size of sample (1 for 8 bit, 2 for 16 bit) * @param is_compress true if input data is compressed */ -void mveaudio_process(char *buffer, unsigned char *data, bool is_compressed = true); +void mveaudio_process(char *buffer, unsigned char *data, int sample_size, bool is_compressed = true); #endif /* INCLUDED_MVE_AUDIO_H */ diff --git a/libmve/mveplay.cpp b/libmve/mveplay.cpp index f0fca7b0..7c672d13 100644 --- a/libmve/mveplay.cpp +++ b/libmve/mveplay.cpp @@ -208,7 +208,7 @@ static int audio_data_handler(unsigned char major, unsigned char minor, unsigned if (chan & selected_chan) { void *buf = malloc(size); if (major == MVE_OPCODE_AUDIOFRAMEDATA) { - mveaudio_process((char *)buf, data, snd_ds->IsCompressed()); + mveaudio_process((char *)buf, data, snd_ds->GetSampleSize(), snd_ds->IsCompressed()); } else { // SILENCE, MORTALS! memset(data, 0, size);