mirror of
https://github.com/quantum5/MusicKeyboard.git
synced 2025-04-24 21:21:59 -04:00
Proper note stopping in case of modifier releasing.
Signed-off-by: Xiaomao Chen <xiaomao5@live.com>
This commit is contained in:
parent
3b4ebc97fd
commit
b15645ed50
|
@ -43,6 +43,7 @@ protected:
|
||||||
|
|
||||||
virtual HICON GetIcon();
|
virtual HICON GetIcon();
|
||||||
|
|
||||||
|
bool state[128];
|
||||||
bool useBeep;
|
bool useBeep;
|
||||||
HANDLE hBeep;
|
HANDLE hBeep;
|
||||||
unsigned lastFrequency;
|
unsigned lastFrequency;
|
||||||
|
@ -62,7 +63,7 @@ protected:
|
||||||
PianoControl *piano;
|
PianoControl *piano;
|
||||||
LPWSTR m_keychars;
|
LPWSTR m_keychars;
|
||||||
MIDI_FILE *m_midifile;
|
MIDI_FILE *m_midifile;
|
||||||
DWORD deltaTime;
|
DWORD lastTime;
|
||||||
bool saving;
|
bool saving;
|
||||||
private:
|
private:
|
||||||
HFONT hFont;
|
HFONT hFont;
|
||||||
|
|
|
@ -151,7 +151,7 @@ LRESULT MainWindow::OnCreate()
|
||||||
m_midifile = NULL;
|
m_midifile = NULL;
|
||||||
m_instrument = 0;
|
m_instrument = 0;
|
||||||
saving = false;
|
saving = false;
|
||||||
deltaTime = (DWORD) -1;
|
lastTime = (DWORD) -1;
|
||||||
|
|
||||||
WCHAR buf[MAX_PATH];
|
WCHAR buf[MAX_PATH];
|
||||||
int piano;
|
int piano;
|
||||||
|
@ -183,6 +183,7 @@ LRESULT MainWindow::OnCreate()
|
||||||
if (midiOutOpen(&m_midi, 0, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR)
|
if (midiOutOpen(&m_midi, 0, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR)
|
||||||
MessageBox(m_hwnd, L"Failed to open MIDI device!", L"Fatal Error", MB_ICONERROR);
|
MessageBox(m_hwnd, L"Failed to open MIDI device!", L"Fatal Error", MB_ICONERROR);
|
||||||
|
|
||||||
|
memset(state, 0, 128 * sizeof(bool));
|
||||||
this->piano = PianoControl::Create(NULL, m_hwnd, WS_VISIBLE | WS_CHILD, 0, 0, 0, 0);
|
this->piano = PianoControl::Create(NULL, m_hwnd, WS_VISIBLE | WS_CHILD, 0, 0, 0, 0);
|
||||||
this->piano->SetBackground(GetSysColorBrush(COLOR_3DFACE));
|
this->piano->SetBackground(GetSysColorBrush(COLOR_3DFACE));
|
||||||
|
|
||||||
|
@ -372,6 +373,23 @@ bool MainWindow::Play(WPARAM wParam, LPARAM lParam, bool down)
|
||||||
|
|
||||||
void MainWindow::PlayNote(int note, bool down)
|
void MainWindow::PlayNote(int note, bool down)
|
||||||
{
|
{
|
||||||
|
bool save = m_midifile && saving;
|
||||||
|
if (save) {
|
||||||
|
if (lastTime == (DWORD) -1)
|
||||||
|
lastTime = GetTickCount();
|
||||||
|
midiTrackAddRest(m_midifile, 1, GetTickCount() - lastTime, TRUE);
|
||||||
|
midiTrackAddMsg(m_midifile, 1, down ? msgNoteOn : msgNoteOff, note, m_force);
|
||||||
|
lastTime = GetTickCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = note % 12; i < 0x7F; i += 12) {
|
||||||
|
if (i != note && state[i]) {
|
||||||
|
if (!useBeep) MIDI_MESSAGE(m_midi, 0x90, i, 0);
|
||||||
|
if (save) midiTrackAddMsg(m_midifile, 1, msgNoteOff, i, 0);
|
||||||
|
state[i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (useBeep) {
|
if (useBeep) {
|
||||||
BEEP_PARAM param = {frequency[note], (ULONG) -1};
|
BEEP_PARAM param = {frequency[note], (ULONG) -1};
|
||||||
|
|
||||||
|
@ -406,24 +424,9 @@ void MainWindow::PlayNote(int note, bool down)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (down) {
|
|
||||||
int num = note % 24;
|
|
||||||
while (num < 0x7F) {
|
|
||||||
if (num != note)
|
|
||||||
MIDI_MESSAGE(m_midi, 0x90, num, 0);
|
|
||||||
num += 24;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MIDI_MESSAGE(m_midi, down ? 0x90 : 0x80, note, m_force);
|
MIDI_MESSAGE(m_midi, down ? 0x90 : 0x80, note, m_force);
|
||||||
}
|
}
|
||||||
|
state[note] = down;
|
||||||
if (m_midifile && saving) {
|
|
||||||
if (deltaTime == (DWORD) -1)
|
|
||||||
deltaTime = GetTickCount();
|
|
||||||
midiTrackAddRest(m_midifile, 1, GetTickCount() - deltaTime, TRUE);
|
|
||||||
midiTrackAddMsg(m_midifile, 1, down ? msgNoteOn : msgNoteOff, note, m_force);
|
|
||||||
deltaTime = GetTickCount();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -455,7 +458,7 @@ void MainWindow::OnReOpenMIDI()
|
||||||
|
|
||||||
if (m_midifile)
|
if (m_midifile)
|
||||||
midiFileClose(m_midifile);
|
midiFileClose(m_midifile);
|
||||||
deltaTime = (DWORD) -1;
|
lastTime = (DWORD) -1;
|
||||||
m_midifile = midiFileCreate(cpath, TRUE);
|
m_midifile = midiFileCreate(cpath, TRUE);
|
||||||
if (!m_midifile) {
|
if (!m_midifile) {
|
||||||
MessageBox(m_hwnd, L"Can't open the file!\r\n\r\n"
|
MessageBox(m_hwnd, L"Can't open the file!\r\n\r\n"
|
||||||
|
|
Loading…
Reference in a new issue