2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2011, 2019, 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <libmodplug/modplug.h>
23 #include "modplug-types.h"
25 #include "music-module.h"
34 #define BUF_INIT_SIZE 65536
36 int music_mod_init(void)
38 ModPlug_Settings settings = {
39 .mFlags = MODPLUG_ENABLE_OVERSAMPLING,
42 .mResamplingMode = MODPLUG_RESAMPLE_SPLINE,
46 ModPlug_SetSettings(&settings);
50 void music_mod_exit(void)
54 static struct music_mod *readfile(struct upkg_file *f)
56 struct music_mod *m = malloc(sizeof *m + BUF_INIT_SIZE);
58 *m = (struct music_mod) {
59 .alloc = BUF_INIT_SIZE
63 struct music_mod *tmp;
64 size_t sz = m->alloc - m->len;
66 m->len += upkg_export_read(f, m->buf + m->len, sz);
67 if (m->alloc != m->len) {
69 tmp = realloc(m, sizeof *m + m->len);
76 tmp = realloc(m, sizeof *m + 2*m->alloc);
87 struct music_mod *music_mod_open(struct upkg_file *f)
89 struct music_mod *m = readfile(f);
92 m->f = ModPlug_Load(m->buf, m->len);
101 int music_mod_dump(struct music_mod *m, FILE *of)
103 if (fwrite(m->buf, m->len, 1, of) != 1)
108 void music_mod_close(struct music_mod *m)
110 ModPlug_Unload(m->f);
114 const char *music_mod_type(struct music_mod *m)
116 int type = ModPlug_GetModuleType(m->f);
118 if (type & MOD_TYPE_MOD)
120 if (type & MOD_TYPE_S3M)
122 if (type & MOD_TYPE_XM)
124 if (type & MOD_TYPE_IT)