annotate src/libmlk-core/core/sound.c @ 423:63ebfa352ae1

core: use err.h in font
author David Demelier <markand@malikania.fr>
date Sat, 15 Oct 2022 14:00:38 +0200
parents 67c1c46af2c8
children 35dd2068b4ab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * sound.c -- sound support
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
378
460c78706989 misc: update copyright years
David Demelier <markand@malikania.fr>
parents: 362
diff changeset
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <assert.h>
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <stdio.h>
362
12367bfc2df6 misc: even more warnings cleaned
David Demelier <markand@malikania.fr>
parents: 343
diff changeset
21 #include <string.h>
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
22
169
eb0a7ab71023 misc: extreme cleanup, closes #2506
David Demelier <markand@malikania.fr>
parents: 121
diff changeset
23 #include "error.h"
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 #include "sound.h"
343
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
25 #include "vfs.h"
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
26 #include "vfs_p.h"
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
27 #include "sys_p.h"
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
28
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
29 #define SOURCE(snd) ((const struct audiostream *)snd->handle)->source
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
30
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
31 int
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 sound_open(struct sound *snd, const char *path)
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 {
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 assert(snd);
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 assert(path);
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
36
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
37 if (!(snd->handle = audiostream_open(path)))
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
38 return -1;
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
39
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
40 return 0;
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 }
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
42
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
43 int
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 sound_openmem(struct sound *snd, const void *buffer, size_t buffersz)
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 {
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 assert(snd);
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 assert(buffer);
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
48
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
49 if (!(snd->handle = audiostream_openmem(buffer, buffersz)))
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
50 return -1;
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
51
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
52 return 0;
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 }
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
54
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
55 int
343
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
56 sound_openvfs(struct sound *snd, struct vfs_file *file)
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
57 {
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
58 assert(snd);
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
59 assert(vfs_file_ok(file));
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
60
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
61 char *data;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
62 size_t datasz;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
63 int ret = 0;
343
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
64
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
65 if (!(data = vfs_file_aread(file, &datasz)))
343
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
66 return -1;
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
67 if (!(snd->handle = audiostream_openmem(data, datasz)))
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
68 ret = -1;
343
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
69
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
70 free(data);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
71
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
72 return ret;
343
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
73 }
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
74
9eb25198d706 js: add sound bindings
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
75 int
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
76 sound_ok(const struct sound *snd)
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
77 {
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
78 return snd && snd->handle;
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
79 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
80
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
81 int
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
82 sound_play(struct sound *snd)
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
83 {
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
84 assert(sound_ok(snd));
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
85
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
86 alSourcePlay(SOURCE(snd));
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
87
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
88 return 0;
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 }
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
90
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 void
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 sound_pause(struct sound *snd)
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
94 assert(sound_ok(snd));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
95
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
96 alSourcePause(SOURCE(snd));
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 }
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
98
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
99 void
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 sound_resume(struct sound *snd)
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
102 assert(sound_ok(snd));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
103
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
104 alSourcePlay(SOURCE(snd));
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
105 }
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
106
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 void
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
108 sound_stop(struct sound *snd)
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
110 assert(sound_ok(snd));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
111
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
112 alSourceStop(SOURCE(snd));
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
113 }
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
114
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
115 void
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
116 sound_finish(struct sound *snd)
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
117 {
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
118 assert(snd);
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
119
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
120 if (snd->handle) {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
121 sound_stop(snd);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
122 audiostream_finish(snd->handle);
103
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
123 }
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
124
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
125 memset(snd, 0, sizeof (*snd));
68ce8e02061a sound: add basic sound API
David Demelier <markand@malikania.fr>
parents:
diff changeset
126 }