From 47ad10cbad1ba59b39e3490faea9477a37741f35 Mon Sep 17 00:00:00 2001
From: probonopd <probonopd@users.noreply.github.com>
Date: Thu, 21 Apr 2022 08:52:23 +0200
Subject: [PATCH] Implement MIDI CC 120 (All Sound Off) and 123 (All Notes Off)
 (#127)

Implement MIDI CC 120 (All Sound Off) and 123 (All Notes Off)

Closes https://github.com/probonopd/MiniDexed/issues/126
---
 src/mididevice.cpp | 10 ++++++++++
 src/minidexed.cpp  | 18 ++++++++++++++++++
 src/minidexed.h    |  2 ++
 3 files changed, 30 insertions(+)

diff --git a/src/mididevice.cpp b/src/mididevice.cpp
index dd9a916..45c2342 100644
--- a/src/mididevice.cpp
+++ b/src/mididevice.cpp
@@ -40,6 +40,8 @@
 	#define MIDI_CC_FREQUENCY_CUTOFF	74
 	#define MIDI_CC_REVERB_LEVEL		91
 	#define MIDI_CC_DETUNE_LEVEL		94
+	#define MIDI_CC_ALL_SOUND_OFF		120
+	#define MIDI_CC_ALL_NOTES_OFF		123
 #define MIDI_PROGRAM_CHANGE	0b1100
 #define MIDI_PITCH_BEND		0b1110
 
@@ -194,6 +196,14 @@ void CMIDIDevice::MIDIMessageHandler (const u8 *pMessage, size_t nLength, unsign
 				case MIDI_CC_DETUNE_LEVEL:
 					m_pSynthesizer->SetMasterTune (pMessage[2], nTG);
 					break;
+
+				case MIDI_CC_ALL_SOUND_OFF:
+					m_pSynthesizer->panic (pMessage[2], nTG);
+					break;
+
+				case MIDI_CC_ALL_NOTES_OFF:
+					m_pSynthesizer->notesOff (pMessage[2], nTG);
+					break;
 				}
 				break;
 
diff --git a/src/minidexed.cpp b/src/minidexed.cpp
index 1361aa3..68908bf 100644
--- a/src/minidexed.cpp
+++ b/src/minidexed.cpp
@@ -521,6 +521,24 @@ void CMiniDexed::setSustain(bool sustain, unsigned nTG)
 	m_pTG[nTG]->setSustain (sustain);
 }
 
+void CMiniDexed::panic(uint8_t value, unsigned nTG)
+{
+	assert (nTG < CConfig::ToneGenerators);
+	assert (m_pTG[nTG]);
+	if (value == 0) {
+		m_pTG[nTG]->panic ();
+	}
+}
+
+void CMiniDexed::notesOff(uint8_t value, unsigned nTG)
+{
+	assert (nTG < CConfig::ToneGenerators);
+	assert (m_pTG[nTG]);
+	if (value == 0) {
+		m_pTG[nTG]->notesOff ();
+	}
+}
+
 void CMiniDexed::setModWheel (uint8_t value, unsigned nTG)
 {
 	assert (nTG < CConfig::ToneGenerators);
diff --git a/src/minidexed.h b/src/minidexed.h
index 9e54cf8..1292e53 100644
--- a/src/minidexed.h
+++ b/src/minidexed.h
@@ -76,6 +76,8 @@ public:
 	void keydown (int16_t pitch, uint8_t velocity, unsigned nTG);
 
 	void setSustain (bool sustain, unsigned nTG);
+	void panic (uint8_t value, unsigned nTG);
+	void notesOff (uint8_t value, unsigned nTG);
 	void setModWheel (uint8_t value, unsigned nTG);
 	void setPitchbend (int16_t value, unsigned nTG);
 	void ControllersRefresh (unsigned nTG);