From ad6400345030da0f4367cacf61fe199a1adc55a9 Mon Sep 17 00:00:00 2001
From: Rene Stange <rsta2@o2online.de>
Date: Tue, 1 Mar 2022 12:40:05 +0100
Subject: [PATCH] Add class CDexedAdapter

Some Dexed methods require to be guarded from being interrupted
by other Dexed calls. This is done in the class CDexedAdapter.
---
 src/dexedadapter.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++
 src/minidexed.h    |  6 ++--
 2 files changed, 73 insertions(+), 3 deletions(-)
 create mode 100644 src/dexedadapter.h

diff --git a/src/dexedadapter.h b/src/dexedadapter.h
new file mode 100644
index 0000000..0322dd4
--- /dev/null
+++ b/src/dexedadapter.h
@@ -0,0 +1,70 @@
+//
+// dexedadapter.h
+//
+// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
+// Copyright (C) 2022  The MiniDexed Team
+//
+// This program 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.
+//
+// This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+#ifndef _dexedadapter_h
+#define _dexedadapter_h
+
+#include <synth_dexed.h>
+#include <circle/spinlock.h>
+#include <stdint.h>
+
+// Some Dexed methods require to be guarded from being interrupted
+// by other Dexed calls. This is done herein.
+
+class CDexedAdapter : public Dexed
+{
+public:
+	CDexedAdapter (uint8_t maxnotes, int rate)
+	: Dexed (maxnotes, rate)
+	{
+	}
+
+	void loadVoiceParameters (uint8_t* data)
+	{
+		m_SpinLock.Acquire ();
+		Dexed::loadVoiceParameters (data);
+		m_SpinLock.Release ();
+	}
+
+	void keyup (int16_t pitch)
+	{
+		m_SpinLock.Acquire ();
+		Dexed::keyup (pitch);
+		m_SpinLock.Release ();
+	}
+
+	void keydown (int16_t pitch, uint8_t velo)
+	{
+		m_SpinLock.Acquire ();
+		Dexed::keydown (pitch, velo);
+		m_SpinLock.Release ();
+	}
+
+	void getSamples (uint16_t n_samples, int16_t* buffer)
+	{
+		m_SpinLock.Acquire ();
+		Dexed::getSamples (n_samples, buffer);
+		m_SpinLock.Release ();
+	}
+
+private:
+	CSpinLock m_SpinLock;
+};
+
+#endif
diff --git a/src/minidexed.h b/src/minidexed.h
index 79605c7..e4d15aa 100644
--- a/src/minidexed.h
+++ b/src/minidexed.h
@@ -4,7 +4,7 @@
 #ifndef _minidexed_h
 #define _minidexed_h
 
-#include <synth_dexed.h>
+#include "dexedadapter.h"
 #include <stdint.h>
 #include <math.h>
 #include <circle/interrupt.h>
@@ -21,11 +21,11 @@
 #include "perftimer.h"
 #include <display/hd44780device.h>
 
-class CMiniDexed : public Dexed
+class CMiniDexed : public CDexedAdapter
 {
   public:
     CMiniDexed(CConfig *pConfig, CInterruptSystem *pInterrupt)
-:   Dexed (CConfig::MaxNotes, pConfig->GetSampleRate ()),
+:   CDexedAdapter (CConfig::MaxNotes, pConfig->GetSampleRate ()),
     m_pMIDIDevice (0),
     m_PCKeyboard (this),
     m_Serial (pInterrupt, TRUE),