Mercurial > molko
changeset 436:df92d5b056ce
core: use err.h in sound|music
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 16 Oct 2022 09:12:52 +0200 |
parents | 556c8e2ff995 |
children | e1eebc6bf25d |
files | libmlk-core/mlk/core/music.c libmlk-core/mlk/core/sound.c libmlk-core/mlk/core/sys.c libmlk-core/mlk/core/sys_p.h |
diffstat | 4 files changed, 96 insertions(+), 90 deletions(-) [+] |
line wrap: on
line diff
--- a/libmlk-core/mlk/core/music.c Sun Oct 16 08:31:01 2022 +0200 +++ b/libmlk-core/mlk/core/music.c Sun Oct 16 09:12:52 2022 +0200 @@ -31,10 +31,7 @@ assert(mus); assert(path); - if (!(mus->handle = audiostream_open(path))) - return -1; - - return 0; + return audiostream_open((struct audiostream **)&mus->handle, path); } int @@ -43,10 +40,7 @@ assert(mus); assert(buffer); - if (!(mus->handle = audiostream_openmem(buffer, buffersz))) - return -1; - - return 0; + return audiostream_openmem((struct audiostream **)&mus->handle, buffer, buffersz); } int
--- a/libmlk-core/mlk/core/sound.c Sun Oct 16 08:31:01 2022 +0200 +++ b/libmlk-core/mlk/core/sound.c Sun Oct 16 09:12:52 2022 +0200 @@ -32,10 +32,7 @@ assert(snd); assert(path); - if (!(snd->handle = audiostream_open(path))) - return -1; - - return 0; + return audiostream_open((struct audiostream **)&snd->handle, path); } int @@ -44,10 +41,7 @@ assert(snd); assert(buffer); - if (!(snd->handle = audiostream_openmem(buffer, buffersz))) - return -1; - - return 0; + return audiostream_openmem((struct audiostream **)&snd->handle, buffer, buffersz); } int
--- a/libmlk-core/mlk/core/sys.c Sun Oct 16 08:31:01 2022 +0200 +++ b/libmlk-core/mlk/core/sys.c Sun Oct 16 09:12:52 2022 +0200 @@ -16,6 +16,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <mlk/util/util.h> + #include <sys/stat.h> #include <assert.h> #include <errno.h> @@ -24,7 +26,7 @@ #include <string.h> #include <limits.h> -#if defined(_WIN32) +#if defined(MLK_OS_WINDOWS) # include <shlwapi.h> # include <windows.h> #else @@ -38,9 +40,8 @@ #include <sndfile.h> -#include <mlk/util/util.h> - #include "alloc.h" +#include "err.h" #include "error.h" #include "panic.h" #include "sound.h" @@ -58,6 +59,12 @@ .name = "molko" }; +struct viodata { + const unsigned char *data; + const size_t datasz; + sf_count_t offset; +}; + static inline char * normalize(char *str) { @@ -101,6 +108,68 @@ return 0; } +static sf_count_t +vio_get_filelen(void *data) +{ + const struct viodata *vio = data; + + return (sf_count_t)vio->datasz; +} + +static sf_count_t +vio_seek(sf_count_t offset, int whence, void *data) +{ + struct viodata *vio = data; + + switch (whence) { + case SEEK_SET: + vio->offset = offset; + break; + case SEEK_CUR: + vio->offset += offset; + break; + case SEEK_END: + vio->offset = vio->datasz - offset; + break; + default: + break; + } + + return vio->offset; +} + +static sf_count_t +vio_read(void *ptr, sf_count_t count, void *data) +{ + struct viodata *vio = data; + + memcpy(ptr, vio->data + vio->offset, count); + vio->offset += count; + + return count; +} + +static sf_count_t +vio_tell(void *data) +{ + const struct viodata *vio = data; + + return vio->offset; +} + +static inline int +sndfile_to_err(int e) +{ + switch (e) { + case SF_ERR_UNRECOGNISED_FORMAT: + case SF_ERR_MALFORMED_FILE: + case SF_ERR_UNSUPPORTED_ENCODING: + return ERR_FORMAT; + default: + return ERR_NO_MEM; + } +} + int sys_init(const char *organization, const char *name) { @@ -123,7 +192,7 @@ return errorf("%s", SDL_GetError()); /* OpenAL. */ -#if defined(_WIN32) +#if defined(MLK_OS_WINDOW) SetEnvironmentVariable("ALSOFT_LOGLEVEL", "0"); #else putenv("ALSOFT_LOGLEVEL=0"); @@ -197,10 +266,11 @@ } } -struct audiostream * -audiostream_create(SNDFILE *file, const SF_INFO *info) +static int +create_audiostream(struct audiostream **ptr, SNDFILE *file, const SF_INFO *info) { struct audiostream *stream; + int ret = 0; stream = alloc_new(sizeof (*stream)); stream->samplerate = info->samplerate; @@ -214,6 +284,7 @@ free(stream->samples); free(stream); stream = NULL; + ret = sndfile_to_err(sf_error(file)); } alGenBuffers(1, &stream->buffer); @@ -224,11 +295,13 @@ sf_close(file); - return stream; + *ptr = stream; + + return ret; } -struct audiostream * -audiostream_open(const char *path) +int +audiostream_open(struct audiostream **stream, const char *path) { assert(path); @@ -236,68 +309,13 @@ SNDFILE *file; if (!(file = sf_open(path, SFM_READ, &info))) - return NULL; - - return audiostream_create(file, &info); -} + return sf_error(NULL); -struct viodata { - const unsigned char *data; - const size_t datasz; - sf_count_t offset; -}; - -static sf_count_t -vio_get_filelen(void *data) -{ - const struct viodata *vio = data; - - return (sf_count_t)vio->datasz; + return create_audiostream(stream, file, &info); } -static sf_count_t -vio_seek(sf_count_t offset, int whence, void *data) -{ - struct viodata *vio = data; - - switch (whence) { - case SEEK_SET: - vio->offset = offset; - break; - case SEEK_CUR: - vio->offset += offset; - break; - case SEEK_END: - vio->offset = vio->datasz - offset; - break; - default: - break; - } - - return vio->offset; -} - -static sf_count_t -vio_read(void *ptr, sf_count_t count, void *data) -{ - struct viodata *vio = data; - - memcpy(ptr, vio->data + vio->offset, count); - vio->offset += count; - - return count; -} - -static sf_count_t -vio_tell(void *data) -{ - const struct viodata *vio = data; - - return vio->offset; -} - -struct audiostream * -audiostream_openmem(const void *data, size_t datasz) +int +audiostream_openmem(struct audiostream **stream, const void *data, size_t datasz) { assert(data); @@ -315,9 +333,9 @@ }; if (!(file = sf_open_virtual(&io, SFM_READ, &info, &viodata))) - return NULL; + return sndfile_to_err(sf_error(NULL));; - return audiostream_create(file, &info); + return create_audiostream(stream, file, &info); } void
--- a/libmlk-core/mlk/core/sys_p.h Sun Oct 16 08:31:01 2022 +0200 +++ b/libmlk-core/mlk/core/sys_p.h Sun Oct 16 09:12:52 2022 +0200 @@ -43,11 +43,11 @@ ALenum format; }; -struct audiostream * -audiostream_open(const char *); +int +audiostream_open(struct audiostream **, const char *); -struct audiostream * -audiostream_openmem(const void *, size_t); +int +audiostream_openmem(struct audiostream **, const void *, size_t); void audiostream_finish(struct audiostream *);