]> git.draconx.ca Git - upkg.git/blob - src/engine/music-modplug.c
Stop using gnulib's flexmember module.
[upkg.git] / src / engine / music-modplug.c
1 /*
2  * upkg: tool for manipulating Unreal Tournament packages.
3  * Copyright © 2009-2011, 2019, 2022 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <libmodplug/modplug.h>
23 #include "modplug-types.h"
24
25 #include "music-module.h"
26 #include "upkg.h"
27
28 struct music_mod {
29         ModPlugFile *f;
30         size_t len, alloc;
31         unsigned char buf[];
32 };
33
34 #define BUF_INIT_SIZE 65536
35
36 int music_mod_init(void)
37 {
38         ModPlug_Settings settings = {
39                 .mFlags          = MODPLUG_ENABLE_OVERSAMPLING,
40                 .mChannels       = 2,
41                 .mFrequency      = 44100,
42                 .mResamplingMode = MODPLUG_RESAMPLE_SPLINE,
43                 .mLoopCount      = -1,
44         };
45
46         ModPlug_SetSettings(&settings);
47         return 0;
48 }
49
50 void music_mod_exit(void)
51 {
52 }
53
54 static struct music_mod *readfile(struct upkg_file *f)
55 {
56         struct music_mod *m = malloc(sizeof *m + BUF_INIT_SIZE);
57         if (!m) return NULL;
58         *m = (struct music_mod) {
59                 .alloc = BUF_INIT_SIZE
60         };
61
62         while (1) {
63                 struct music_mod *tmp;
64                 size_t sz = m->alloc - m->len;
65                 
66                 m->len += upkg_export_read(f, m->buf + m->len, sz);
67                 if (m->alloc != m->len) {
68                         if (f->eof) {
69                                 tmp = realloc(m, sizeof *m + m->len);
70                                 return tmp ? tmp : m;
71                         }
72
73                         break;
74                 }
75
76                 tmp = realloc(m, sizeof *m + 2*m->alloc);
77                 if (!tmp)
78                         break;
79                 m = tmp;
80                 m->alloc *= 2;
81         }
82
83         free(m);
84         return NULL;
85 }
86
87 struct music_mod *music_mod_open(struct upkg_file *f)
88 {
89         struct music_mod *m = readfile(f);
90         if (!m) return NULL;
91
92         m->f = ModPlug_Load(m->buf, m->len);
93         if (!m->f) {
94                 free(m);
95                 return NULL;
96         }
97
98         return m;
99 }
100
101 int music_mod_dump(struct music_mod *m, FILE *of)
102 {
103         if (fwrite(m->buf, m->len, 1, of) != 1)
104                 return -1;
105         return 0;
106 }
107
108 void music_mod_close(struct music_mod *m)
109 {
110         ModPlug_Unload(m->f);
111         free(m);
112 }
113
114 const char *music_mod_type(struct music_mod *m)
115 {
116         int type = ModPlug_GetModuleType(m->f);
117
118         if (type & MOD_TYPE_MOD)
119                 return "mod";
120         if (type & MOD_TYPE_S3M)
121                 return "s3m";
122         if (type & MOD_TYPE_XM)
123                 return "xm";
124         if (type & MOD_TYPE_IT)
125                 return "it";
126         return "unknown";
127 }