forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_midi.cpp
140 lines (116 loc) · 3.6 KB
/
audio_midi.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
/*
* 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/>.
*/
// Headers
#include "audio_midi.h"
#include "audio_decoder_midi.h"
#include "decoder_fluidsynth.h"
#include "decoder_fmmidi.h"
#include "decoder_wildmidi.h"
#include "output.h"
#ifdef USE_AUDIO_RESAMPLER
#include "audio_resampler.h"
#endif
void MidiDecoder::GetFormat(int& freq, AudioDecoderBase::Format& format, int& channels) const {
freq = frequency;
format = AudioDecoderBase::Format::S16;
channels = 2;
}
bool MidiDecoder::SetFormat(int frequency, AudioDecoderBase::Format format, int channels) {
if (frequency != EP_MIDI_FREQ || channels != 2 || format != AudioDecoderBase::Format::S16)
return false;
return true;
}
static struct {
bool fluidsynth = true;
bool wildmidi = true;
} works;
std::unique_ptr<AudioDecoderBase> MidiDecoder::Create(bool resample) {
std::unique_ptr<AudioDecoderBase> mididec;
mididec = CreateFluidsynth(resample);
if (!mididec) {
mididec = CreateWildMidi(resample);
if (!mididec) {
mididec = CreateFmMidi(resample);
}
}
return mididec;
}
std::unique_ptr<AudioDecoderBase> MidiDecoder::CreateFluidsynth(bool resample) {
std::unique_ptr<AudioDecoderBase> mididec;
#if defined(HAVE_FLUIDSYNTH) || defined(HAVE_FLUIDLITE)
std::string error_message;
if (works.fluidsynth && FluidSynthDecoder::Initialize(error_message)) {
auto dec = std::make_unique<FluidSynthDecoder>();
mididec = std::make_unique<AudioDecoderMidi>(std::move(dec));
}
else if (!mididec && works.fluidsynth) {
Output::Debug("{}", error_message);
works.fluidsynth = false;
}
#endif
#ifdef USE_AUDIO_RESAMPLER
if (mididec && resample) {
mididec = std::make_unique<AudioResampler>(std::move(mididec));
}
#endif
return mididec;
}
std::unique_ptr<AudioDecoderBase> MidiDecoder::CreateWildMidi(bool resample) {
std::unique_ptr<AudioDecoderBase> mididec;
#ifdef HAVE_LIBWILDMIDI
std::string error_message;
if (!mididec && works.wildmidi && WildMidiDecoder::Initialize(error_message)) {
auto dec = std::make_unique<WildMidiDecoder>();
mididec = std::make_unique<AudioDecoderMidi>(std::move(dec));
}
else if (!mididec && works.wildmidi) {
Output::Debug("{}", error_message);
works.wildmidi = false;
}
#endif
#ifdef USE_AUDIO_RESAMPLER
if (mididec && resample) {
mididec = std::make_unique<AudioResampler>(std::move(mididec));
}
#endif
return mididec;
}
std::unique_ptr<AudioDecoderBase> MidiDecoder::CreateFmMidi(bool resample) {
std::unique_ptr<AudioDecoderBase> mididec;
#if WANT_FMMIDI
if (!mididec) {
auto dec = std::make_unique<FmMidiDecoder>();
mididec = std::make_unique<AudioDecoderMidi>(std::move(dec));
}
#endif
#ifdef USE_AUDIO_RESAMPLER
if (mididec && resample) {
mididec = std::make_unique<AudioResampler>(std::move(mididec));
}
#endif
return mididec;
}
void MidiDecoder::Reset() {
works.fluidsynth = true;
works.wildmidi = true;
#ifdef HAVE_LIBWILDMIDI
WildMidiDecoder::ResetState();
#endif
#if defined(HAVE_FLUIDSYNTH) || defined(HAVE_FLUIDLITE)
FluidSynthDecoder::ResetState();
#endif
}