annotate libmlk-core/mlk/core/music.c @ 446:4607eea0eabc

util: prefix with mlk_
author David Demelier <markand@malikania.fr>
date Sat, 18 Feb 2023 12:15:14 +0100
parents 773a082f0b91
children a6933cd252b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * music.h -- music support
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
445
773a082f0b91 misc: update copyright years
David Demelier <markand@malikania.fr>
parents: 436
diff changeset
4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <assert.h>
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <string.h>
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
21
425
bf20b77968fd core: use err.h in script
David Demelier <markand@malikania.fr>
parents: 413
diff changeset
22 #include "err.h"
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include "music.h"
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
24 #include "sys_p.h"
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
25
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
26 #define SOURCE(mus) ((const struct audiostream *)mus->handle)->source
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
27
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
28 int
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 music_open(struct music *mus, const char *path)
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 {
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 assert(mus);
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 assert(path);
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
33
436
df92d5b056ce core: use err.h in sound|music
David Demelier <markand@malikania.fr>
parents: 431
diff changeset
34 return audiostream_open((struct audiostream **)&mus->handle, path);
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
36
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
37 int
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 music_openmem(struct music *mus, const void *buffer, size_t buffersz)
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 {
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 assert(mus);
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 assert(buffer);
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
42
436
df92d5b056ce core: use err.h in sound|music
David Demelier <markand@malikania.fr>
parents: 431
diff changeset
43 return audiostream_openmem((struct audiostream **)&mus->handle, buffer, buffersz);
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
45
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
46 int
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 music_ok(const struct music *mus)
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 {
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 return mus && mus->handle;
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 }
d3ef968745f5 core: rework audio 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 int
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
53 music_play(struct music *mus, enum music_flags flags)
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 {
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 assert(mus);
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
56
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
57 if (flags & MUSIC_LOOP)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
58 alSourcei(SOURCE(mus), AL_LOOPING, AL_TRUE);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
59 else
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
60 alSourcei(SOURCE(mus), AL_LOOPING, AL_TRUE);
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
61
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
62 alSourceRewind(SOURCE(mus));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
63 alSourcePlay(SOURCE(mus));
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
64
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
65 return 0;
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
66 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
67
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
68 void
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
69 music_pause(struct music *mus)
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
71 assert(music_ok(mus));
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
72
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
73 alSourcePause(SOURCE(mus));
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
75
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 void
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
77 music_resume(struct music *mus)
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
79 assert(music_ok(mus));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
80
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
81 alSourcePlay(SOURCE(mus));
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
83
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 void
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
85 music_stop(struct music *mus)
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
87 assert(music_ok(mus));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
88
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
89 alSourceStop(SOURCE(mus));
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
91
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 void
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 music_finish(struct music *mus)
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 {
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 assert(mus);
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
96
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 if (mus->handle) {
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
98 music_stop(mus);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
99 audiostream_finish(mus->handle);
203
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 }
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
101
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 memset(mus, 0, sizeof (*mus));
d3ef968745f5 core: rework audio API
David Demelier <markand@malikania.fr>
parents:
diff changeset
103 }