-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmdxhelper.cpp
318 lines (258 loc) · 7.16 KB
/
xmdxhelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "xmdxhelper.h"
extern "C" {
#include <libxmdx/mdxmini/mdxmini.h>
#include <libxmdx/pmdmini/pmdmini.h>
#include <libxmdx/mucom88/utils/mucomtag.h>
#include <libxmdx/mucom88/module/mucom_module.h>
#include <libxmdx/vgs/vgsdec.h>
#include <libxmdx/vgs/vgsmml.h>
}
#define INPUT_BUFFER_SIZE 1024
class MDXFileReader : public AbstractReader
{
public:
MDXFileReader() = default;
virtual ~MDXFileReader();
virtual bool load(const QString &path) override final;
virtual qint64 read(unsigned char *data, qint64 maxSize) override final;
private:
MDXMini *m_input = nullptr;
};
MDXFileReader::~MDXFileReader()
{
if(m_input)
{
mdx_close(m_input);
delete m_input;
}
}
bool MDXFileReader::load(const QString &path)
{
m_input = new MDXMini;
mdx_set_rate(sampleRate());
if(mdx_open(m_input, qPrintable(path), nullptr) != 0)
{
qWarning("MDXFileReader: mdx_open error");
delete m_input;
m_input = nullptr;
return false;
}
char buffer[INPUT_BUFFER_SIZE] = {0};
mdx_set_max_loop(m_input, 0);
mdx_get_title(m_input, buffer);
m_title = buffer;
m_length = mdx_get_length(m_input) * 1000;
return true;
}
qint64 MDXFileReader::read(unsigned char *data, qint64)
{
if(m_length > 0 && m_offset >= m_length)
{
return 0; // stop song
}
mdx_calc_sample(m_input, (short*)data, INPUT_BUFFER_SIZE);
m_offset += INPUT_BUFFER_SIZE * 1000.0 / sampleRate();
return INPUT_BUFFER_SIZE * 4;
}
class PMDFileReader : public AbstractReader
{
public:
PMDFileReader() = default;
virtual ~PMDFileReader();
virtual bool load(const QString &path) override final;
virtual qint64 read(unsigned char *data, qint64 maxSize) override final;
private:
PMDMini *m_input = nullptr;
};
PMDFileReader::~PMDFileReader()
{
if(m_input)
{
m_input->pmd_stop();
delete m_input;
}
}
bool PMDFileReader::load(const QString &path)
{
m_input = new PMDMini;
m_input->pmd_init();
m_input->pmd_setrate(sampleRate());
if(m_input->pmd_play(qPrintable(path), nullptr) != 0)
{
qWarning("PMDFileReader: pmd_play error");
return false;
}
char buffer[INPUT_BUFFER_SIZE] = {0};
m_input->pmd_get_title(buffer);
m_title = buffer;
m_input->pmd_get_compo(buffer);
m_author = buffer;
m_length = m_input->pmd_length_sec() * 1000;
return true;
}
qint64 PMDFileReader::read(unsigned char *data, qint64)
{
if(m_length > 0 && m_offset >= m_length)
{
return 0; // stop song
}
m_input->pmd_renderer((short*)data, INPUT_BUFFER_SIZE);
m_offset += INPUT_BUFFER_SIZE * 1000.0 / sampleRate();
return INPUT_BUFFER_SIZE * 4;
}
class MUCFileReader : public AbstractReader
{
public:
MUCFileReader() = default;
virtual ~MUCFileReader();
virtual bool load(const QString &path) override final;
virtual qint64 read(unsigned char *data, qint64 maxSize) override final;
private:
MucomModule *m_input = nullptr;
};
MUCFileReader::~MUCFileReader()
{
delete m_input;
}
bool MUCFileReader::load(const QString &path)
{
QFile file(path);
if(!file.open(QIODevice::ReadOnly))
{
qWarning("MUCFileReader: open file failed");
return false;
}
const QByteArray &buffer = file.readAll();
file.close();
m_input = new MucomModule;
m_input->SetRate(sampleRate());
m_input->OpenMemory((unsigned char *)buffer.constData(), buffer.length(), path.toLower().endsWith(".mub"));
m_input->UseFader(true);
m_input->Play();
m_title = QString::fromStdString(m_input->tag->title);
m_author = QString::fromStdString(m_input->tag->author);
m_length = m_input->GetLength() * 1000;
return true;
}
qint64 MUCFileReader::read(unsigned char *data, qint64)
{
if(m_length > 0 && m_input->IsEnd())
{
return 0; // stop song
}
m_input->Mix((short*)data, INPUT_BUFFER_SIZE);
return INPUT_BUFFER_SIZE * 4;
}
class VGSFileReader : public AbstractReader
{
public:
VGSFileReader() = default;
virtual ~VGSFileReader();
virtual bool load(const QString &path) override final;
virtual qint64 read(unsigned char *data, qint64 maxSize) override final;
virtual int sampleRate() const override final { return 22050 / 2; }
private:
void *m_input = nullptr;
};
VGSFileReader::~VGSFileReader()
{
if(m_input)
{
vgsdec_release_context(m_input);
}
}
bool VGSFileReader::load(const QString &path)
{
QFile file(path);
if(!file.open(QIODevice::ReadOnly))
{
qWarning("VGSFileReader: open file failed");
return false;
}
const QByteArray &buffer = file.readAll();
file.close();
m_input = vgsdec_create_context();
if(!m_input)
{
qWarning("VGSFileReader: vgsdec create context failed");
return false;
}
if(path.toLower().endsWith(".mml"))
{
struct VgsMmlErrorInfo error;
struct VgsBgmData *data = vgsmml_compile_from_memory((void*)buffer.constData(), buffer.length(), &error);
if(!data)
{
qWarning("VGSFileReader: vgsmml compile data failed");
return false;
}
if(vgsdec_load_bgm_from_memory(m_input, data->data, data->size))
{
qWarning("VGSFileReader: vgsdec load bgm from mml data failed");
return false;
}
vgsmml_free_bgm_data(data);
}
else
{
if(vgsdec_load_bgm_from_memory(m_input, (void*)buffer.constData(), buffer.length()))
{
qWarning("VGSFileReader: vgsdec load bgm data failed");
return false;
}
}
struct VgsMetaData* meta = vgsdec_get_meta_data(m_input, 0);
if(meta)
{
m_title = QString::fromStdString(meta->song);
m_author = QString::fromStdString(meta->team);
}
m_length = vgsdec_get_value(m_input, VGSDEC_REG_TIME_LENGTH) * 1.0 / 22050 * 1000 + 5000;
return true;
}
qint64 VGSFileReader::read(unsigned char *data, qint64)
{
if(!vgsdec_get_value(m_input, VGSDEC_REG_PLAYING))
{
return 0;
}
if(vgsdec_get_value(m_input, VGSDEC_REG_LOOP_COUNT))
{
/* waiting for the end of fadeout if looped */
vgsdec_set_value(m_input, VGSDEC_REG_FADEOUT, 1);
}
/* decoding loop */
vgsdec_execute(m_input, data, INPUT_BUFFER_SIZE);
return INPUT_BUFFER_SIZE;
}
XMDXHelper::XMDXHelper(const QString &path)
: m_path(path)
{
}
XMDXHelper::~XMDXHelper()
{
deinit();
}
void XMDXHelper::deinit()
{
delete m_input;
}
bool XMDXHelper::initialize()
{
const QString &suffix = m_path.toLower();
if(suffix.endsWith(".mdx") || suffix.endsWith(".pdx")) m_input = new MDXFileReader;
else if(suffix.endsWith(".m")) m_input = new PMDFileReader;
else if(suffix.endsWith(".mub") || suffix.endsWith(".muc")) m_input = new MUCFileReader;
else if(suffix.endsWith(".vgs") || suffix.endsWith(".bgm") || suffix.endsWith(".mml")) m_input = new VGSFileReader;
if(!m_input)
{
qWarning("XMDXHelper: load file suffix error");
return false;
}
if(!m_input->load(m_path))
{
qWarning("XMDXHelper: unable to open file");
return false;
}
return true;
}