Debian Codegraph @

prboom2/src/MUSIC/xmpplayer.c

raw
1 //
2 // Copyright(C) 2025 ceski
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 //
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include "musicplayer.h"
22
23 #ifndef HAVE_LIBXMP
24
25 #include <stddef.h>
26
27 static const char *xmp_name(void)
28 {
29 return "libxmp tracker player (DISABLED)";
30 }
31
32 static int xmp_init(int samplerate)
33 {
34 return 0;
35 }
36
37 const music_player_t xmp_player =
38 {
39 xmp_name,
40 xmp_init,
41 NULL,
42 NULL,
43 NULL,
44 NULL,
45 NULL,
46 NULL,
47 NULL,
48 NULL,
49 NULL
50 };
51
52 #else // HAVE_LIBXMP
53
54 #include <xmp.h>
55 #include <string.h>
56 #include "lprintf.h"
57
58 static xmp_context context;
59 static int xmp_samplerate;
60 static float xmp_volume;
61 static int xmp_looping;
62 static int xmp_playing;
63 static int xmp_paused;
64
65 static const char *xmp_name(void)
66 {
67 return "libxmp tracker player";
68 }
69
70 static int xmp_init(int samplerate)
71 {
72 xmp_samplerate = samplerate;
73 context = xmp_create_context();
74
75 if (!context)
76 {
77 lprintf(LO_WARN, "xmp_init: failed to create context.\n");
78 return 0;
79 }
80
81 return 1;
82 }
83
84 static void xmp_shutdown(void)
85 {
86 xmp_free_context(context);
87 context = NULL;
88 }
89
90 static void xmp_setvolume(int v)
91 {
92 xmp_volume = (float)v / 15.0f;
93 }
94
95 static const void *xmp_registersong(const void *data, unsigned len)
96 {
97 if (xmp_load_module_from_memory(context, data, len) < 0)
98 {
99 return NULL;
100 }
101
102 return data;
103 }
104
105 static void xmp_unregistersong(const void *handle)
106 {
107 xmp_stop_module(context);
108 xmp_end_player(context);
109 xmp_release_module(context);
110 }
111
112 static void xmp_play(const void *handle, int looping)
113 {
114 if (xmp_start_player(context, xmp_samplerate, 0) < 0)
115 {
116 lprintf(LO_WARN, "xmp_play: failed to start player.\n");
117 xmp_playing = 0;
118 return;
119 }
120
121 xmp_set_player(context, XMP_PLAYER_VOLUME, 100);
122 xmp_looping = looping;
123 xmp_playing = 1;
124 }
125
126 static void xmp_stop(void)
127 {
128 xmp_playing = 0;
129 }
130
131 static void xmp_pause(void)
132 {
133 xmp_paused = 1;
134 }
135
136 static void xmp_resume(void)
137 {
138 xmp_paused = 0;
139 }
140
141 static void xmp_render(void *dest, unsigned nsamp)
142 {
143 if (xmp_playing && !xmp_paused)
144 {
145 if (xmp_play_buffer(context, dest, nsamp * 4, xmp_looping ? 0 : 1) == 0)
146 {
147 short *sdest = (short *)dest;
148
149 for (int i = 0; i < nsamp * 2; i++)
150 {
151 sdest[i] = (short)(sdest[i] * xmp_volume);
152 }
153 }
154 else
155 {
156 if (xmp_looping)
157 {
158 xmp_restart_module(context);
159 xmp_set_position(context, 0);
160 }
161 else
162 {
163 xmp_stop();
164 }
165
166 memset(dest, 0, nsamp * 4);
167 }
168 }
169 else
170 {
171 memset(dest, 0, nsamp * 4);
172 }
173 }
174
175 const music_player_t xmp_player =
176 {
177 xmp_name,
178 xmp_init,
179 xmp_shutdown,
180 xmp_setvolume,
181 xmp_pause,
182 xmp_resume,
183 xmp_registersong,
184 xmp_unregistersong,
185 xmp_play,
186 xmp_stop,
187 xmp_render
188 };
189
190 #endif // HAVE_LIBXMP