Skip to content

Commit 5972ae5

Browse files
author
lathoub
committed
reworked ActiveSensing
reworked ActiveSensing using input from a variety of device MIDI Implementation manuals (Roland, KORG, Yamaha) found on the internet. Receiving ActiveSensing: Once an Active Sensing message is received, the unit will begin monitoring the intervalbetween all subsequent messages. If there is an interval of ActiveSensingPeriodicity ms or longer betweenmessages while monitoring is active, the same processing as when All Sound Off, All Notes Off,and Reset All Controllers messages are received will be carried out. The unit will then stopmonitoring the message interval. Sending ActiveSensing: send x ms after the last sent command
1 parent 2685bb4 commit 5972ae5

File tree

6 files changed

+169
-43
lines changed

6 files changed

+169
-43
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <MIDI.h>
2+
USING_NAMESPACE_MIDI
3+
4+
struct MyMIDISettings : public MIDI_NAMESPACE::DefaultSettings
5+
{
6+
// When setting UseReceiverActiveSensing to true, MIDI.read() *must* be called
7+
// as often as possible (1000 / SenderActiveSensingPeriodicity per second).
8+
//
9+
// setting UseReceiverActiveSensing to true, adds 174 bytes of code.
10+
//
11+
// (Taken from a Roland MIDI Implementation Owner's manual)
12+
// Once an Active Sensing message is received, the unit will begin monitoring
13+
// the interval between all subsequent messages. If there is an interval of 420 ms
14+
// or longer between messages while monitoring is active, the same processing
15+
// as when All Sound Off, All Notes Off,and Reset All Controllers messages are
16+
// received will be carried out. The unit will then stopmonitoring the message interval.
17+
18+
static const bool UseReceiverActiveSensing = true;
19+
20+
static const uint16_t ReceiverActiveSensingTimeout = 420;
21+
};
22+
23+
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, MIDI, MyMIDISettings);
24+
25+
void errorHandler(int8_t err)
26+
{
27+
if (bitRead(err, ErrorActiveSensingTimeout))
28+
{
29+
MIDI.sendControlChange(AllSoundOff, 0, 1);
30+
MIDI.sendControlChange(AllNotesOff, 0, 1);
31+
MIDI.sendControlChange(ResetAllControllers, 0, 1);
32+
33+
digitalWrite(LED_BUILTIN, HIGH);
34+
}
35+
else
36+
digitalWrite(LED_BUILTIN, LOW);
37+
}
38+
39+
void setup()
40+
{
41+
pinMode(LED_BUILTIN, OUTPUT);
42+
digitalWrite(LED_BUILTIN, LOW);
43+
44+
MIDI.setHandleError(errorHandler);
45+
MIDI.begin(1);
46+
}
47+
48+
void loop()
49+
{
50+
MIDI.read();
51+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <MIDI.h>
2+
USING_NAMESPACE_MIDI
3+
4+
struct MyMIDISettings : public MIDI_NAMESPACE::DefaultSettings
5+
{
6+
// When setting UseSenderActiveSensing to true, MIDI.read() *must* be called
7+
// as often as possible (1000 / SenderActiveSensingPeriodicity per second).
8+
//
9+
// setting UseSenderActiveSensing to true, adds 34 bytes of code.
10+
//
11+
// When using Active Sensing, call MIDI.read(); in the Arduino loop()
12+
//
13+
// from 'a' MIDI implementation manual: "Sent periodically"
14+
// In the example here, a NoteOn is send every 1000ms (1s), ActiveSensing is
15+
// send every 250ms after the last command.
16+
// Logging the command will look like this:
17+
//
18+
// ...
19+
// A.Sense FE
20+
// A.Sense FE
21+
// A.Sense FE
22+
// NoteOn 90 04 37 [E-2]
23+
// A.Sense FE
24+
// A.Sense FE
25+
// A.Sense FE
26+
// NoteOn 90 04 37 [E-2]
27+
// A.Sense FE
28+
// A.Sense FE
29+
// A.Sense FE
30+
// NoteOn 90 04 37 [E-2]
31+
// ...
32+
33+
static const bool UseSenderActiveSensing = true;
34+
35+
static const uint16_t SenderActiveSensingPeriodicity = 250;
36+
};
37+
38+
unsigned long t1 = millis();
39+
40+
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, MIDI, MyMIDISettings);
41+
42+
void setup()
43+
{
44+
MIDI.begin(1);
45+
}
46+
47+
void loop()
48+
{
49+
MIDI.read();
50+
51+
// send a note every second
52+
if ((millis() - t1) > 1000)
53+
{
54+
t1 = millis();
55+
56+
MIDI.sendNoteOn(random(1, 127), 55, 1);
57+
}
58+
}

src/MIDI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class MidiInterface
282282
MidiMessage mMessage;
283283
unsigned long mLastMessageSentTime;
284284
unsigned long mLastMessageReceivedTime;
285-
bool mReceiverActiveSensingActivated;
285+
bool mReceiverActiveSensingActive;
286286
int8_t mLastError;
287287

288288
private:

src/MIDI.hpp

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ inline MidiInterface<Transport, Settings, Platform>::MidiInterface(Transport& in
4444
, mThruFilterMode(Thru::Full)
4545
, mLastMessageSentTime(0)
4646
, mLastMessageReceivedTime(0)
47-
, mReceiverActiveSensingActivated(false)
47+
, mReceiverActiveSensingActive(false)
4848
, mLastError(0)
4949
{
50+
static_assert(!(Settings::UseSenderActiveSensing && Settings::UseReceiverActiveSensing), "UseSenderActiveSensing and UseReceiverActiveSensing can't be both set to true.");
5051
}
5152

5253
/*! \brief Destructor for MidiInterface.
@@ -82,7 +83,8 @@ void MidiInterface<Transport, Settings, Platform>::begin(Channel inChannel)
8283
mCurrentRpnNumber = 0xffff;
8384
mCurrentNrpnNumber = 0xffff;
8485

85-
mLastMessageSentTime = Platform::now();
86+
mLastMessageSentTime =
87+
mLastMessageReceivedTime = Platform::now();
8688

8789
mMessage.valid = false;
8890
mMessage.type = InvalidType;
@@ -710,27 +712,36 @@ template<class Transport, class Settings, class Platform>
710712
inline bool MidiInterface<Transport, Settings, Platform>::read(Channel inChannel)
711713
{
712714
#ifndef RegionActiveSending
715+
713716
// Active Sensing. This message is intended to be sent
714717
// repeatedly to tell the receiver that a connection is alive. Use
715-
// of this message is optional. When initially received, the
716-
// receiver will expect to receive another Active Sensing
717-
// message each 300ms (max), and if it does not then it will
718-
// assume that the connection has been terminated. At
719-
// termination, the receiver will turn off all voices and return to
720-
// normal (non- active sensing) operation.
721-
if (Settings::UseSenderActiveSensing && (Platform::now() - mLastMessageSentTime) > Settings::SenderActiveSensingPeriodicity)
718+
// of this message is optional.
719+
if (Settings::UseSenderActiveSensing)
722720
{
723-
sendActiveSensing();
724-
mLastMessageSentTime = Platform::now();
721+
// Send ActiveSensing <Settings::ActiveSensingPeriodicity> ms after the last command
722+
if ((Platform::now() - mLastMessageSentTime) > Settings::SenderActiveSensingPeriodicity)
723+
sendActiveSensing();
725724
}
726725

727-
if (Settings::UseReceiverActiveSensing && mReceiverActiveSensingActivated && (mLastMessageReceivedTime + ActiveSensingTimeout < Platform::now()))
726+
if (Settings::UseReceiverActiveSensing && mReceiverActiveSensingActive)
728727
{
729-
mReceiverActiveSensingActivated = false;
730-
731-
mLastError |= 1UL << ErrorActiveSensingTimeout; // set the ErrorActiveSensingTimeout bit
732-
if (mErrorCallback)
733-
mErrorCallback(mLastError);
728+
if ((Platform::now() - mLastMessageReceivedTime > Settings::ReceiverActiveSensingTimeout))
729+
{
730+
// Once an Active Sensing message is received, the unit will begin monitoring
731+
// the intervalbetween all subsequent messages. If there is an interval of 420 ms
732+
// or longer betweenmessages while monitoring is active, the same processing
733+
// as when All Sound Off, All Notes Off,and Reset All Controllers messages are
734+
// received will be carried out. The unit will then stopmonitoring the message interval.
735+
mReceiverActiveSensingActive = false;
736+
737+
// its up to the error handler to send the stop processing messages
738+
// (also, no clue what the channel is on which to send them)
739+
740+
// no need to check if bit is already set, it is not (due to the mActiveSensingActive switch)
741+
mLastError |= 1UL << ErrorActiveSensingTimeout; // set the ErrorActiveSensingTimeout bit
742+
if (mErrorCallback)
743+
mErrorCallback(mLastError);
744+
}
734745
}
735746
#endif
736747

@@ -742,25 +753,26 @@ inline bool MidiInterface<Transport, Settings, Platform>::read(Channel inChannel
742753

743754
#ifndef RegionActiveSending
744755

745-
if (Settings::UseReceiverActiveSensing && mMessage.type == ActiveSensing)
756+
if (Settings::UseReceiverActiveSensing)
746757
{
747-
// When an ActiveSensing message is received, the time keeping is activated.
748-
// When a timeout occurs, an error message is send and time keeping ends.
749-
mReceiverActiveSensingActivated = true;
758+
mLastMessageReceivedTime = Platform::now();
750759

751-
// is ErrorActiveSensingTimeout bit in mLastError on
752-
if (mLastError & (1 << (ErrorActiveSensingTimeout - 1)))
760+
if (mMessage.type == ActiveSensing && !mReceiverActiveSensingActive)
753761
{
754-
mLastError &= ~(1UL << ErrorActiveSensingTimeout); // clear the ErrorActiveSensingTimeout bit
762+
// Once an Active Sensing message is received, the unit will begin monitoring
763+
// the intervalbetween all subsequent messages. If there is an interval of 420 ms
764+
// or longer betweenmessages while monitoring is active, the same processing
765+
// as when All Sound Off, All Notes Off,and Reset All Controllers messages are
766+
// received will be carried out. The unit will then stopmonitoring the message interval.
767+
mReceiverActiveSensingActive = true;
768+
769+
// Clear the ErrorActiveSensingTimeout bit
770+
mLastError &= ~(1UL << ErrorActiveSensingTimeout);
755771
if (mErrorCallback)
756772
mErrorCallback(mLastError);
757773
}
758774
}
759775

760-
// Keep the time of the last received message, so we can check for the timeout
761-
if (Settings::UseReceiverActiveSensing && mReceiverActiveSensingActivated)
762-
mLastMessageReceivedTime = Platform::now();
763-
764776
#endif
765777

766778
handleNullVelocityNoteOnAsNoteOff();

src/midi_Defs.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ BEGIN_MIDI_NAMESPACE
4646
#define MIDI_PITCHBEND_MIN -8192
4747
#define MIDI_PITCHBEND_MAX 8191
4848

49-
/*! Receiving Active Sensing
50-
*/
51-
static const uint16_t ActiveSensingTimeout = 300;
52-
5349
// -----------------------------------------------------------------------------
5450
// Type definitions
5551

src/midi_Settings.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,14 @@ struct DefaultSettings
7474

7575
/*! Global switch to turn on/off sender ActiveSensing
7676
Set to true to send ActiveSensing
77-
Set to false will not send ActiveSensing message (will also save memory)
78-
as often as possible (1000 / SenderActiveSensingPeriodicity per second).
79-
*/
80-
static const bool UseSenderActiveSensing = false;
77+
/*! Global switch to turn on/off sending and receiving ActiveSensing
78+
Set to true to activate ActiveSensing
79+
Set to false will not send/receive ActiveSensing message (will also save 236 bytes of memory)
8180
82-
/*! Global switch to turn on/off receiver ActiveSensing
83-
Set to true to check for message timeouts (via ErrorCallback)
84-
Set to false will not check if chained device are still alive (if they use ActiveSensing) (will also save memory)
81+
When setting UseActiveSensing to true, MIDI.read() *must* be called
82+
as often as possible (1000 / ActiveSensingPeriodicity per second).
8583
*/
86-
static const bool UseReceiverActiveSensing = false;
84+
static const bool UseSenderActiveSensing = false;
8785

8886
/*! Active Sensing is intended to be sent
8987
repeatedly by the sender to tell the receiver that a connection is alive. Use
@@ -95,9 +93,20 @@ struct DefaultSettings
9593
normal (non- active sensing) operation.
9694
9795
Typical value is 250 (ms) - an Active Sensing command is send every 250ms.
98-
(All Roland devices send Active Sensing every 250ms)
96+
(Most Roland devices send Active Sensing every 250ms)
9997
*/
100-
static const uint16_t SenderActiveSensingPeriodicity = 250;
98+
static const uint16_t SenderActiveSensingPeriodicity = 300;
99+
100+
/*! Once an Active Sensing message is received, the unit will begin monitoring
101+
the intervalbetween all subsequent messages. If there is an interval of ActiveSensingPeriodicity ms
102+
or longer betweenmessages while monitoring is active, the same processing
103+
as when All Sound Off, All Notes Off,and Reset All Controllers messages are
104+
received will be carried out. The unit will then stopmonitoring the message interval.
105+
*/
106+
static const bool UseReceiverActiveSensing = false;
107+
108+
static const uint16_t ReceiverActiveSensingTimeout = 300;
109+
101110
};
102111

103112
END_MIDI_NAMESPACE

0 commit comments

Comments
 (0)