forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
audio_midi.h
208 lines (184 loc) · 5.6 KB
/
audio_midi.h
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
/*
* 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/>.
*/
#ifndef EP_AUDIO_MIDI_H
#define EP_AUDIO_MIDI_H
class AudioDecoderMidi;
#include "audio_decoder_base.h"
#if defined(GEKKO) || defined(_3DS)
# define EP_MIDI_FREQ 22050
#else
# define EP_MIDI_FREQ 44100
#endif
/**
* The MidiDecoder is an abstraction over Midi libraries.
*
* Create will instantiate a proper Midi decoder and calling Decode will
* fill a buffer with audio data which must be passed to the audio hardware.
*/
class MidiDecoder {
public:
enum MidiEvent {
MidiEvent_NoteOff = 0x80,
MidiEvent_NoteOn = 0x90,
MidiEvent_KeyPressure = 0xA0,
MidiEvent_Controller = 0xB0,
MidiEvent_ProgramChange = 0xC0,
MidiEvent_ChannelPressure = 0xD0,
MidiEvent_PitchBend = 0xE0
};
MidiDecoder() = default;
MidiDecoder(MidiDecoder&&) = delete;
MidiDecoder(const MidiDecoder&) = delete;
MidiDecoder& operator=(const MidiDecoder&) = delete;
MidiDecoder& operator=(MidiDecoder&&) = delete;
virtual ~MidiDecoder() = default;
/*
* All libraries must implement:
* - FillBuffer
* - GetName
*
* The library must implement the following commands:
* - SendMidiMessage
* - SendSysExMessage (nice to have)
* - SendMidiReset
*
* When Midi messages are not supported (library uses own sequencer)
* - Open
* - Seek
* - Tell
* - SetPitch - Return "false" for resampling
* - SupportsMidiMessages - Must return "false"
*/
/**
* Passes a buffer containing a Midi file to the Midi Decoder
* @param data Midi Data
* @return true when the decoder supports this data
*/
virtual bool Open(std::vector<uint8_t>& data) {
(void)data;
return true;
}
/**
* Seeks in the midi stream. The value of offset is implementation
* defined.
*
* @param offset Offset to seek to
* @param origin Position to seek from
* @return Whether seek was successful
*/
virtual bool Seek(std::streamoff offset, std::ios_base::seekdir origin) {
(void)offset;
(void)origin;
return true;
};
/**
* Sets the pitch multiplier.
* 100 = normal speed
* 200 = double speed and so on
* Not all audio decoders support this. Using the audio hardware is
* recommended.
*
* @param pitch Pitch multiplier to use
* @return true if pitch was set, false otherwise
*/
virtual bool SetPitch(int pitch) {
(void)pitch;
return true;
}
/**
* Retrieves the format of the Midi decoder.
* It is guaranteed that these settings will stay constant the whole time.
*
* @param frequency Filled with the audio frequency
* @param format Filled with the audio format
* @param channels Filled with the amount of channels
*/
virtual void GetFormat(int& freq, AudioDecoderBase::Format& format, int& channels) const;
/**
* Requests a preferred format from the audio decoder. Not all decoders
* support everything and it's recommended to use the audio hardware
* for audio processing.
* When false is returned use GetFormat to get the real format of the
* output data.
*
* @param frequency Audio frequency
* @param format Audio format
* @param channels Number of channels
* @return true when all settings were set, otherwise false (use GetFormat)
*/
virtual bool SetFormat(int frequency, AudioDecoderBase::Format format, int channels);
/**
* Forwards a Midi message to the Midi decoder
*
* @param message Midi message
*/
virtual void SendMidiMessage(uint32_t message) {
(void)message;
}
/**
* Forwards a SysEx Message to the Midi Decoder
*
* @param data sysex content
* @param size length of sysex content (data)
*/
virtual void SendSysExMessage(const uint8_t* data, size_t size) {
(void)data;
(void)size;
}
/**
* Requests a reset of the Midi Sequencer
*/
virtual void SendMidiReset() {}
/**
* Called when the synthesizer shall write data in a buffer.
*
* @param buffer Buffer to fill
* @param size Buffer size
* @return number of bytes read or -1 on error
*/
virtual int FillBuffer(uint8_t* buffer, int length) {
(void)buffer;
(void)length;
return -1;
};
/**
* Returns the unique name of the Midi decoder.
*
* @return decoder name
*/
virtual std::string GetName() = 0;
/**
* @return False if the Library is sequencer based
*/
virtual bool SupportsMidiMessages() {
return true;
}
/**
* Attempts to initialize a Midi library for processing the Midi data.
*
* @param stream handle to parse
* @param resample Whether the decoder shall be wrapped into a resampler (if supported)
* @return A Midi decoder instance when the Midi data is supported, otherwise null
*/
static std::unique_ptr<AudioDecoderBase> Create(Filesystem_Stream::InputStream& stream, bool resample);
static std::unique_ptr<AudioDecoderBase> CreateFluidsynth(Filesystem_Stream::InputStream& stream, bool resample);
static std::unique_ptr<AudioDecoderBase> CreateWildMidi(Filesystem_Stream::InputStream& stream, bool resample);
static std::unique_ptr<AudioDecoderBase> CreateFmMidi(Filesystem_Stream::InputStream& stream, bool resample);
protected:
int frequency = EP_MIDI_FREQ;
};
#endif