This commit is contained in:
bjorn 2022-07-03 21:48:43 -07:00
parent c34ee01c1b
commit 917b97ca2d
3 changed files with 12 additions and 14 deletions

View File

@ -125,7 +125,7 @@ static int l_lovrSoundGetFrames(lua_State* L) {
uint32_t frames = 0;
while (frames < count) {
char buffer[4096];
uint32_t chunk = MIN((uint32_t)(sizeof(buffer) / stride), count - frames);
uint32_t chunk = MIN((uint32_t) (sizeof(buffer) / stride), count - frames);
uint32_t read = lovrSoundRead(sound, srcOffset + frames, chunk, buffer);
uint32_t samples = read * channels;
if (read == 0) break;
@ -189,7 +189,8 @@ static int l_lovrSoundSetFrames(lua_State* L) {
if (blob) {
uint32_t srcOffset = luax_optu32(L, 5, 0);
uint32_t dstOffset = luax_optu32(L, 4, 0);
lua_Integer count = luax_optinteger(L, 3, (blob->size - srcOffset) / stride);
uint32_t defaultCount = (uint32_t) MIN((blob->size - srcOffset) / stride, UINT32_MAX);
uint32_t count = luax_optu32(L, 3, defaultCount);
uint32_t frames = lovrSoundWrite(sound, dstOffset, count, (char*) blob->data + srcOffset);
lua_pushinteger(L, frames);
return 1;
@ -221,7 +222,7 @@ static int l_lovrSoundSetFrames(lua_State* L) {
uint32_t frames = 0;
while (frames < count) {
char buffer[4096];
uint32_t chunk = MIN((uint32_t)(sizeof(buffer) / stride), count - frames);
uint32_t chunk = MIN((uint32_t) (sizeof(buffer) / stride), count - frames);
uint32_t samples = chunk * channels;
if (format == SAMPLE_I16) {

View File

@ -14,7 +14,6 @@
#define CTZL __builtin_ctzl
#endif
// Mysterious "m ^= (m & -m)" construct zeroes the lowest nonzero bit.
#define FOREACH_SOURCE(s) for (uint64_t m = state.sourceMask; s = m ? state.sources[CTZL(m)] : NULL, m; m ^= (m & -m))
#define OUTPUT_FORMAT SAMPLE_F32
#define OUTPUT_CHANNELS 2

View File

@ -7,6 +7,7 @@
#define MINIMP3_NO_STDIO
#include "lib/minimp3/minimp3_ex.h"
#include <stdlib.h>
#include <limits.h>
#include <string.h>
static const ma_format miniaudioFormats[] = {
@ -68,10 +69,8 @@ static uint32_t lovrSoundReadMp3(Sound* sound, uint32_t offset, uint32_t count,
}
uint32_t channels = lovrSoundGetChannelCount(sound);
uint32_t readSamples = (uint32_t)(count * channels);
lovrCheck(readSamples/count == channels, "Cannot request more than 2^32-1 samples of sound data at a time"); // Check for overflow
size_t samples = mp3dec_ex_read(sound->decoder, data, readSamples);
uint32_t frames = ((uint32_t)samples / channels);
size_t samples = mp3dec_ex_read(sound->decoder, data, count * channels);
uint32_t frames = (uint32_t) (samples / channels);
sound->cursor += frames;
return frames;
}
@ -133,13 +132,13 @@ static bool loadOgg(Sound* sound, Blob* blob, bool decode) {
if (decode) {
sound->read = lovrSoundReadRaw;
uint32_t channels = lovrSoundGetChannelCount(sound);
lovrAssert(sound->frames * channels <= INT_MAX, "Decoded OGG file has too many samples");
size_t size = sound->frames * lovrSoundGetStride(sound);
void* data = calloc(1, size);
lovrAssert(data, "Out of memory");
sound->blob = lovrBlobCreate(data, size, "Sound");
size_t samples = size / sizeof(float);
lovrCheck(samples < INT_MAX, "Sound is too big to work with (somewhere over 2 GiB)");
if (stb_vorbis_get_samples_float_interleaved(sound->decoder, lovrSoundGetChannelCount(sound), data, (int)(samples)) < (int) sound->frames) {
if (stb_vorbis_get_samples_float_interleaved(sound->decoder, channels, data, size / sizeof(float)) < (int) sound->frames) {
lovrThrow("Could not decode vorbis from '%s'", blob->name);
}
stb_vorbis_close(sound->decoder);
@ -285,13 +284,12 @@ static bool loadMP3(Sound* sound, Blob* blob, bool decode) {
mp3dec_file_info_t info;
int status = mp3dec_load_buf(&decoder, blob->data, blob->size, &info, NULL, NULL);
lovrAssert(!status, "Could not decode mp3 from '%s'", blob->name);
lovrAssert(info.samples / info.channels <= UINT32_MAX, "MP3 is too long");
sound->blob = lovrBlobCreate(info.buffer, info.samples * sizeof(float), blob->name);
sound->format = SAMPLE_F32;
sound->sampleRate = info.hz;
sound->layout = info.channels == 2 ? CHANNEL_STEREO : CHANNEL_MONO;
size_t frames = info.samples / info.channels;
lovrCheck(frames < UINT32_MAX, "Sound is too long (2^32 or more samples)")
sound->frames = (uint32_t)frames;
sound->frames = (uint32_t) (info.samples / info.channels);
sound->read = lovrSoundReadRaw;
return true;
} else {