Mercurial > molko
view src/libmlk-core/core/sound.c @ 343:9eb25198d706
js: add sound bindings
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 16 Oct 2021 22:17:09 +0200 |
parents | 8f9937403749 |
children | 12367bfc2df6 |
line wrap: on
line source
/* * sound.c -- sound support * * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> #include <stdio.h> #include <SDL_mixer.h> #include "error.h" #include "sound.h" #include "vfs.h" #include "vfs_p.h" int sound_open(struct sound *snd, const char *path) { assert(snd); assert(path); if (!(snd->handle = Mix_LoadWAV(path))) return errorf("%s", SDL_GetError()); return 0; } int sound_openmem(struct sound *snd, const void *buffer, size_t buffersz) { assert(snd); assert(buffer); SDL_RWops *ops; if (!(ops = SDL_RWFromConstMem(buffer, buffersz)) || !(snd->handle = Mix_LoadWAV_RW(ops, 1))) return errorf("%s", SDL_GetError()); return 0; } int sound_openvfs(struct sound *snd, struct vfs_file *file) { assert(snd); assert(vfs_file_ok(file)); SDL_RWops *ops; if (!(ops = vfs_to_rw(file))) return -1; if (!(snd->handle = Mix_LoadWAV_RW(ops, 1))) return errorf("%s", SDL_GetError()); return 0; } int sound_ok(const struct sound *snd) { return snd && snd->handle; } int sound_play(struct sound *snd, int channel, unsigned int fadein) { assert(sound_ok(snd)); int ret; if (fadein > 0) ret = Mix_FadeInChannel(channel, snd->handle, 0, fadein); else ret = Mix_PlayChannel(channel, snd->handle, 0); if (ret < 0) return errorf("%s", SDL_GetError()); snd->channel = channel; return 0; } void sound_pause(struct sound *snd) { Mix_Pause(snd ? snd->channel : -1); } void sound_resume(struct sound *snd) { Mix_Resume(snd ? snd->channel : -1); } void sound_stop(struct sound *snd, unsigned int fadeout) { if (fadeout > 0) Mix_FadeOutChannel(snd ? snd->channel : -1, fadeout); else Mix_HaltChannel(snd ? snd->channel : -1); } void sound_finish(struct sound *snd) { assert(snd); if (snd->handle) { Mix_HaltChannel(snd->channel); Mix_FreeChunk(snd->handle); } memset(snd, 0, sizeof (*snd)); }