Mercurial > molko
comparison src/libmlk-core/core/sound.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | libmlk-core/core/sound.c@d01e83210ca2 |
children | 9eb25198d706 |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * sound.c -- sound support | |
3 * | |
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <assert.h> | |
20 #include <stdio.h> | |
21 | |
22 #include <SDL_mixer.h> | |
23 | |
24 #include "error.h" | |
25 #include "sound.h" | |
26 | |
27 int | |
28 sound_open(struct sound *snd, const char *path) | |
29 { | |
30 assert(snd); | |
31 assert(path); | |
32 | |
33 if (!(snd->handle = Mix_LoadWAV(path))) | |
34 return errorf("%s", SDL_GetError()); | |
35 | |
36 return 0; | |
37 } | |
38 | |
39 int | |
40 sound_openmem(struct sound *snd, const void *buffer, size_t buffersz) | |
41 { | |
42 assert(snd); | |
43 assert(buffer); | |
44 | |
45 SDL_RWops *ops; | |
46 | |
47 if (!(ops = SDL_RWFromConstMem(buffer, buffersz)) || | |
48 !(snd->handle = Mix_LoadWAV_RW(ops, 1))) | |
49 return errorf("%s", SDL_GetError()); | |
50 | |
51 return 0; | |
52 } | |
53 | |
54 int | |
55 sound_ok(const struct sound *snd) | |
56 { | |
57 return snd && snd->handle; | |
58 } | |
59 | |
60 int | |
61 sound_play(struct sound *snd, int channel, unsigned int fadein) | |
62 { | |
63 assert(sound_ok(snd)); | |
64 | |
65 int ret; | |
66 | |
67 if (fadein > 0) | |
68 ret = Mix_FadeInChannel(channel, snd->handle, 0, fadein); | |
69 else | |
70 ret = Mix_PlayChannel(channel, snd->handle, 0); | |
71 | |
72 if (ret < 0) | |
73 return errorf("%s", SDL_GetError()); | |
74 | |
75 snd->channel = channel; | |
76 | |
77 return 0; | |
78 } | |
79 | |
80 void | |
81 sound_pause(struct sound *snd) | |
82 { | |
83 Mix_Pause(snd ? snd->channel : -1); | |
84 } | |
85 | |
86 void | |
87 sound_resume(struct sound *snd) | |
88 { | |
89 Mix_Resume(snd ? snd->channel : -1); | |
90 } | |
91 | |
92 void | |
93 sound_stop(struct sound *snd, unsigned int fadeout) | |
94 { | |
95 if (fadeout > 0) | |
96 Mix_FadeOutChannel(snd ? snd->channel : -1, fadeout); | |
97 else | |
98 Mix_HaltChannel(snd ? snd->channel : -1); | |
99 } | |
100 | |
101 void | |
102 sound_finish(struct sound *snd) | |
103 { | |
104 assert(snd); | |
105 | |
106 if (snd->handle) { | |
107 Mix_HaltChannel(snd->channel); | |
108 Mix_FreeChunk(snd->handle); | |
109 } | |
110 | |
111 memset(snd, 0, sizeof (*snd)); | |
112 } |