Use correct sample size for processing

Change mveaudio_process() to use sample size defined by stream.
This commit is contained in:
Azamat H. Hackimov 2024-07-12 16:37:28 +03:00
parent 976f00a1f3
commit 4b8328cdfa
5 changed files with 16 additions and 7 deletions

View File

@ -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() {

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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 */

View File

@ -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);