forked from GrandOrgue/grandorgue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGOMidiPlayer.cpp
243 lines (214 loc) · 5.96 KB
/
GOMidiPlayer.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
/*
* Copyright 2006 Milan Digital Audio LLC
* Copyright 2009-2025 GrandOrgue contributors (see AUTHORS)
* License GPL-2.0 or later
* (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
*/
#include "GOMidiPlayer.h"
#include <wx/intl.h>
#include "config/GOConfig.h"
#include "control/GOCallbackButtonControl.h"
#include "midi/GOMidiEvent.h"
#include "midi/GOMidiFileReader.h"
#include "midi/GOMidiMap.h"
#include "GOEvent.h"
#include "GOMidi.h"
#include "GOOrganController.h"
enum {
ID_MIDI_PLAYER_PLAY = 0,
ID_MIDI_PLAYER_STOP,
ID_MIDI_PLAYER_PAUSE,
};
const struct GOElementCreator::ButtonDefinitionEntry
GOMidiPlayer::m_element_types[]
= {
{wxT("MidiPlayerPlay"), ID_MIDI_PLAYER_PLAY, false, true, false},
{wxT("MidiPlayerStop"), ID_MIDI_PLAYER_STOP, false, true, false},
{wxT("MidiPlayerPause"), ID_MIDI_PLAYER_PAUSE, false, true, false},
{wxT(""), -1, false, false, false},
};
const struct GOElementCreator::ButtonDefinitionEntry *GOMidiPlayer::
GetButtonDefinitionList() {
return m_element_types;
}
void GOMidiPlayer::ResetUI() {
m_buttons[ID_MIDI_PLAYER_PLAY]->Display(false);
m_buttons[ID_MIDI_PLAYER_PAUSE]->Display(false);
UpdateDisplay();
}
GOMidiPlayer::GOMidiPlayer(GOOrganController *organController)
: r_MidiMap(organController->GetSettings().GetMidiMap()),
r_timer(*organController->GetTimer()),
p_midi(nullptr),
m_content(),
m_PlayingTime(*organController),
m_Start(0),
m_PlayingSeconds(0),
m_Speed(1),
m_IsPlaying(false),
m_Pause(false) {
CreateButtons(*organController);
m_DeviceID = r_MidiMap.GetDeviceIdByLogicalName(_("GrandOrgue MIDI Player"));
ResetUI();
}
GOMidiPlayer::~GOMidiPlayer() { Cleanup(); }
void GOMidiPlayer::Load(GOConfigReader &cfg) {
m_buttons[ID_MIDI_PLAYER_PLAY]->Init(cfg, wxT("MidiPlayerPlay"), _("PLAY"));
m_buttons[ID_MIDI_PLAYER_STOP]->Init(cfg, wxT("MidiPlayerStop"), _("STOP"));
m_buttons[ID_MIDI_PLAYER_PAUSE]->Init(
cfg, wxT("MidiPlayerPause"), _("PAUSE"));
m_PlayingTime.Init(cfg, wxT("MidiPlayerTime"), _("MIDI playing time"));
}
void GOMidiPlayer::ButtonStateChanged(int id, bool newState) {
switch (id) {
case ID_MIDI_PLAYER_STOP:
StopPlaying();
break;
case ID_MIDI_PLAYER_PLAY:
Play();
break;
case ID_MIDI_PLAYER_PAUSE:
Pause();
break;
}
}
void GOMidiPlayer::LoadFile(
const wxString &filename, unsigned manuals, bool pedal) {
StopPlaying();
m_content.Clear();
GOMidiFileReader reader(r_MidiMap);
if (!reader.Open(filename)) {
GOMessageBox(
wxString::Format(_("Failed to load %s"), filename.c_str()),
_("MIDI Player"),
wxOK | wxICON_ERROR,
NULL);
return;
}
if (!m_content.Load(reader, r_MidiMap, manuals, pedal)) {
m_content.Clear();
GOMessageBox(
wxString::Format(_("Failed to load %s"), filename.c_str()),
_("MIDI Player"),
wxOK | wxICON_ERROR,
NULL);
return;
}
if (!reader.Close()) {
GOMessageBox(
wxString::Format(_("Failed to decode %s"), filename.c_str()),
_("MIDI Player"),
wxOK | wxICON_ERROR,
NULL);
return;
}
}
bool GOMidiPlayer::IsLoaded() { return m_content.IsLoaded(); }
void GOMidiPlayer::Play() {
StopPlaying();
m_content.Reset();
m_Start = wxGetLocalTimeMillis();
m_PlayingSeconds = 0;
m_IsPlaying = IsLoaded();
m_Pause = false;
if (m_IsPlaying) {
m_buttons[ID_MIDI_PLAYER_PLAY]->Display(true);
UpdateDisplay();
HandleTimer();
} else
StopPlaying();
}
void GOMidiPlayer::Pause() {
if (!m_IsPlaying)
return;
if (m_Pause) {
m_Pause = false;
m_buttons[ID_MIDI_PLAYER_PAUSE]->Display(m_Pause);
m_Start = wxGetLocalTimeMillis() - m_Start;
HandleTimer();
} else {
m_Pause = true;
m_buttons[ID_MIDI_PLAYER_PAUSE]->Display(m_Pause);
m_Start = wxGetLocalTimeMillis() - m_Start;
r_timer.DeleteTimer(this);
}
}
void GOMidiPlayer::PlayMidiEvent(const GOMidiEvent &e) {
GOMidi *pMidi = p_midi;
if (pMidi)
pMidi->PlayEvent(e);
}
void GOMidiPlayer::StopPlaying() {
if (m_IsPlaying) {
for (unsigned i = 1; i < 16; i++) {
GOMidiEvent e;
e.SetMidiType(GOMidiEvent::MIDI_CTRL_CHANGE);
e.SetChannel(i);
e.SetKey(MIDI_CTRL_NOTES_OFF);
e.SetValue(0);
e.SetDevice(m_DeviceID);
e.SetTime(wxGetLocalTimeMillis());
e.SetAllowedToReload(false);
PlayMidiEvent(e);
}
}
m_IsPlaying = false;
ResetUI();
r_timer.DeleteTimer(this);
}
bool GOMidiPlayer::IsPlaying() { return m_IsPlaying; }
void GOMidiPlayer::UpdateDisplay() {
if (!IsLoaded())
m_PlayingTime.SetContent(_("<no file loaded>"));
else if (!IsPlaying())
m_PlayingTime.SetContent(_("-:--:--"));
else
m_PlayingTime.SetContent(wxString::Format(
_("%d:%02d:%02d"),
m_PlayingSeconds / 3600,
(m_PlayingSeconds / 60) % 60,
m_PlayingSeconds % 60));
}
void GOMidiPlayer::HandleTimer() {
if (!m_IsPlaying)
return;
GOTime now = wxGetLocalTimeMillis();
if (m_Start + m_Speed * (m_PlayingSeconds + 1) * 1000 <= now) {
m_PlayingSeconds++;
UpdateDisplay();
}
do {
GOMidiEvent e = m_content.GetCurrentEvent();
if (e.GetTime() * m_Speed + m_Start <= now) {
if (!m_content.Next()) {
StopPlaying();
return;
}
e.SetDevice(m_DeviceID);
e.SetTime(wxGetLocalTimeMillis());
e.SetAllowedToReload(false);
PlayMidiEvent(e);
} else {
GOTime next = e.GetTime() * m_Speed + m_Start;
if (next > m_Start + m_Speed * (m_PlayingSeconds + 1) * 1000)
next = m_Start + m_Speed * (m_PlayingSeconds + 1) * 1000;
r_timer.SetTimer(next, this);
return;
}
} while (true);
}
GOEnclosure *GOMidiPlayer::GetEnclosure(const wxString &name, bool is_panel) {
return NULL;
}
GOLabelControl *GOMidiPlayer::GetLabelControl(
const wxString &name, bool is_panel) {
if (is_panel)
return NULL;
if (name == wxT("MidiPlayerLabel"))
return &m_PlayingTime;
return NULL;
}
void GOMidiPlayer::Cleanup() {
StopPlaying();
p_midi = nullptr;
}