forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_wildmidi.cpp
352 lines (295 loc) · 8.74 KB
/
decoder_wildmidi.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system.h"
#ifdef HAVE_LIBWILDMIDI
// Headers
#include <cassert>
#include <stdlib.h>
#include <wildmidi_lib.h>
#include "audio_decoder.h"
#include "output.h"
#include "filefinder.h"
#include "utils.h"
#include "decoder_wildmidi.h"
#ifdef USE_LIBRETRO
# include "platform/libretro/ui.h"
#endif
/* possible options include: WM_MO_REVERB|WM_MO_ENHANCED_RESAMPLING
* however, they cause high cpu usage, so not using them for now.
*/
#define WILDMIDI_OPTS 0
namespace {
bool once = false;
bool init = false;
}
static void WildMidiDecoder_deinit() {
if (init) {
WildMidi_Shutdown();
init = false;
}
}
#if LIBWILDMIDI_VERSION >= 1027 // at least 0.4.3
static void* vio_allocate_file_func(const char* filename, uint32_t* size) {
auto stream = FileFinder::Root().OpenInputStream(filename);
if (!stream) {
Output::Warning("WildMidi: vio_allocate_file_func failed for {}", filename);
return nullptr;
}
auto buf = Utils::ReadStream(stream);
*size = static_cast<uint32_t>(buf.size());
// Make buffer one byte larger, otherwise MSVC CRT detects a Heap Corruption (Wildmidi bug?)
char* buffer = reinterpret_cast<char*>(malloc(*size + 1));
memcpy(buffer, buf.data(), buf.size());
return buffer;
}
static void vio_free_file_func(void* buffer) {
free(buffer);
}
static struct _WM_VIO vio = {
vio_allocate_file_func,
vio_free_file_func
};
#endif
WildMidiDecoder::~WildMidiDecoder() {
if (handle)
WildMidi_Close(handle);
}
bool WildMidiDecoder::Initialize(std::string& error_message) {
std::string config_file;
bool found = false;
// only initialize once until a new game starts
if (once)
return init;
once = true;
/* find the configuration file in different paths on different platforms
* FIXME: move this logic into some configuration class
*/
#if defined(USE_LIBRETRO)
const char *dir = NULL;
// Game directory
if (LibretroUi::environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
config_file = std::string(dir) + "/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
// Content downloader
if (!found) {
if (LibretroUi::environ_cb(RETRO_ENVIRONMENT_GET_CORE_ASSETS_DIRECTORY, &dir) && dir) {
config_file = std::string(dir) + "/wildmidi/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
}
#elif defined(__wii__)
// preferred under /data
config_file = "usb:/data/wildmidi/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
if (!found) {
config_file = "sd:/data/wildmidi/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
// app directory
if (!found) {
config_file = "wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
// same, but legacy from SDL_mixer's timidity
if (!found) {
config_file = "usb:/data/timidity/timidity.cfg";
found = FileFinder::Root().Exists(config_file);
}
if (!found) {
config_file = "sd:/data/timidity/timidity.cfg";
found = FileFinder::Root().Exists(config_file);
}
if (!found) {
config_file = "timidity.cfg";
found = FileFinder::Root().Exists(config_file);
}
#elif defined(__WIIU__)
// preferred SD card directory
config_file = "/vol/external01/data/easyrpg-player/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
// Current directory
if (!found) {
config_file = "wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
#elif defined(__3DS__)
// Only wildmidi paths, no timidity because there was never timidity used on 3DS
// Shipped in a romfs (for CIA and newer 3dsx files)
config_file = "romfs:/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
// preferred SD card directory
if (!found) {
config_file = "sdmc:/3ds/easyrpg-player/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
// Current directory
if (!found) {
config_file = "wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
#elif defined(__SWITCH__)
// Only wildmidi paths, no timidity because it was never used on Switch
config_file = "./wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
if (!found) {
config_file = "/switch/easyrpg-player/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
#elif defined(__vita__)
// Only wildmidi paths, no timidity because it was never used on PSVita
// Shipped
config_file = "app0:/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
// Preferred global directory
if (!found) {
config_file = "ux0:/data/easyrpg-player/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
// Current directory
if (!found) {
config_file = "wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
#elif defined(__MORPHOS__)
// Shipped with library
config_file = "LIBS:timidity/timidity.cfg";
found = FileFinder::Root().Exists(config_file);
// Current directory
if (!found) {
config_file = "wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
#else
// Prefer wildmidi in current directory
config_file = "wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
// wildmidi command line player default config
if (!found) {
config_file = "/etc/wildmidi/wildmidi.cfg";
found = FileFinder::Root().Exists(config_file);
}
// Use Timidity strategy used in SDL mixer
// Environment variable
if (!found) {
const char *env = getenv("TIMIDITY_CFG");
if (env) {
config_file = env;
found = FileFinder::Root().Exists(config_file);
}
}
if (!found) {
config_file = "timidity.cfg";
found = FileFinder::Root().Exists(config_file);
}
# ifdef _WIN32
// Probably not too useful
if (!found) {
config_file = "C:\\TIMIDITY\\timidity.cfg";
found = FileFinder::Root().Exists(config_file);
}
// TODO: We need some installer which creates registry keys for wildmidi
# elif defined(PLAYER_AMIGA)
if (!found) {
config_file = "timidity/timidity.cfg";
found = FileFinder::Root().Exists(config_file);
}
# else
if (!found) {
config_file = "/etc/timidity.cfg";
found = FileFinder::Root().Exists(config_file);
}
if (!found) {
// Folders used in timidity code
const std::vector<std::string> folders = {
"/etc/timidity",
"/usr/share/timidity",
"/usr/local/share/timidity",
"/usr/local/lib/timidity"
};
for (const std::string& s : folders) {
config_file = s + "/timidity.cfg";
found = FileFinder::Root().Exists(config_file);
if (found) {
break;
}
// Some distributions have it in timidity++
config_file = s + "++/timidity.cfg";
found = FileFinder::Root().Exists(config_file);
if (found) {
break;
}
}
}
# endif
#endif
// bail, if nothing found
if (!found) {
error_message = "WildMidi: Could not find configuration file.";
return false;
}
Output::Debug("WildMidi: Using {} as configuration file...", config_file);
#if LIBWILDMIDI_VERSION >= 1027 // at least 0.4.3
init = (WildMidi_InitVIO(&vio, config_file.c_str(), EP_MIDI_FREQ, WILDMIDI_OPTS) == 0);
#else
init = (WildMidi_Init(config_file.c_str(), EP_MIDI_FREQ, WILDMIDI_OPTS) == 0);
#endif
if (!init) {
error_message = std::string("WildMidi_Init() failed : ") + WildMidi_GetError();
return false;
}
#if defined(PLAYER_AMIGA)
// the default volume is way too quiet with the SDL_mixer patches
WildMidi_MasterVolume(127);
#endif
// setup deinitialization
static bool atexit_once = false;
if (!atexit_once) {
atexit_once = true;
atexit(WildMidiDecoder_deinit);
}
return true;
}
void WildMidiDecoder::ResetState() {
once = false;
WildMidiDecoder_deinit();
}
bool WildMidiDecoder::Open(std::vector<uint8_t>& data) {
// this should not happen
if (handle) {
WildMidi_Close(handle);
Output::Debug("WildMidi: Previous handle was not closed.");
}
handle = WildMidi_OpenBuffer(data.data(), data.size());
return handle != nullptr;
}
bool WildMidiDecoder::Seek(std::streamoff offset, std::ios_base::seekdir origin) {
if (origin == std::ios_base::beg) {
if (handle) {
unsigned long int pos = offset;
WildMidi_FastSeek(handle, &pos);
}
return true;
}
return false;
}
int WildMidiDecoder::FillBuffer(uint8_t* buffer, int length) {
if (!handle)
return -1;
return WildMidi_GetOutput(handle, reinterpret_cast<int8_t*>(buffer), length);
}
#endif