@@ -42,10 +42,11 @@ inline MidiInterface<Transport, Settings, Platform>::MidiInterface(Transport& in
4242 , mCurrentNrpnNumber (0xffff )
4343 , mLastMessageSentTime (0 )
4444 , mLastMessageReceivedTime (0 )
45- , mReceiverActiveSensingActivated (false )
45+ , mReceiverActiveSensingActive (false )
4646 , mLastError (0 )
4747 , mSenderActiveSensingPeriodicity (Settings::SenderActiveSensingPeriodicity)
4848{
49+ static_assert (!(Settings::UseSenderActiveSensing && Settings::UseReceiverActiveSensing), " UseSenderActiveSensing and UseReceiverActiveSensing can't be both set to true." );
4950}
5051
5152/* ! \brief Destructor for MidiInterface.
@@ -81,7 +82,8 @@ MidiInterface<Transport, Settings, Platform>& MidiInterface<Transport, Settings,
8182 mCurrentRpnNumber = 0xffff ;
8283 mCurrentNrpnNumber = 0xffff ;
8384
84- mLastMessageSentTime = Platform::now ();
85+ mLastMessageSentTime =
86+ mLastMessageReceivedTime = Platform::now ();
8587
8688 mMessage .valid = false ;
8789 mMessage .type = InvalidType;
@@ -756,27 +758,36 @@ template<class Transport, class Settings, class Platform>
756758inline bool MidiInterface<Transport, Settings, Platform>::read(Channel inChannel)
757759{
758760 #ifndef RegionActiveSending
761+
759762 // Active Sensing. This message is intended to be sent
760763 // repeatedly to tell the receiver that a connection is alive. Use
761- // of this message is optional. When initially received, the
762- // receiver will expect to receive another Active Sensing
763- // message each 300ms (max), and if it does not then it will
764- // assume that the connection has been terminated. At
765- // termination, the receiver will turn off all voices and return to
766- // normal (non- active sensing) operation.
767- if (Settings::UseSenderActiveSensing && (Platform::now () - mLastMessageSentTime ) > Settings::SenderActiveSensingPeriodicity)
764+ // of this message is optional.
765+ if (Settings::UseSenderActiveSensing)
768766 {
769- sendActiveSensing ();
770- mLastMessageSentTime = Platform::now ();
767+ // Send ActiveSensing <Settings::ActiveSensingPeriodicity> ms after the last command
768+ if ((Platform::now () - mLastMessageSentTime ) > Settings::SenderActiveSensingPeriodicity)
769+ sendActiveSensing ();
771770 }
772771
773- if (Settings::UseReceiverActiveSensing && mReceiverActiveSensingActivated && ( mLastMessageReceivedTime + ActiveSensingTimeout < Platform::now ()) )
772+ if (Settings::UseReceiverActiveSensing && mReceiverActiveSensingActive )
774773 {
775- mReceiverActiveSensingActivated = false ;
776-
777- mLastError |= 1UL << ErrorActiveSensingTimeout; // set the ErrorActiveSensingTimeout bit
778- if (mErrorCallback )
779- mErrorCallback (mLastError );
774+ if ((Platform::now () - mLastMessageReceivedTime > Settings::ReceiverActiveSensingTimeout))
775+ {
776+ // Once an Active Sensing message is received, the unit will begin monitoring
777+ // the intervalbetween all subsequent messages. If there is an interval of 420 ms
778+ // or longer betweenmessages while monitoring is active, the same processing
779+ // as when All Sound Off, All Notes Off,and Reset All Controllers messages are
780+ // received will be carried out. The unit will then stopmonitoring the message interval.
781+ mReceiverActiveSensingActive = false ;
782+
783+ // its up to the error handler to send the stop processing messages
784+ // (also, no clue what the channel is on which to send them)
785+
786+ // no need to check if bit is already set, it is not (due to the mActiveSensingActive switch)
787+ mLastError |= 1UL << ErrorActiveSensingTimeout; // set the ErrorActiveSensingTimeout bit
788+ if (mErrorCallback )
789+ mErrorCallback (mLastError );
790+ }
780791 }
781792 #endif
782793
@@ -788,25 +799,26 @@ inline bool MidiInterface<Transport, Settings, Platform>::read(Channel inChannel
788799
789800 #ifndef RegionActiveSending
790801
791- if (Settings::UseReceiverActiveSensing && mMessage . type == ActiveSensing )
802+ if (Settings::UseReceiverActiveSensing)
792803 {
793- // When an ActiveSensing message is received, the time keeping is activated.
794- // When a timeout occurs, an error message is send and time keeping ends.
795- mReceiverActiveSensingActivated = true ;
804+ mLastMessageReceivedTime = Platform::now ();
796805
797- // is ErrorActiveSensingTimeout bit in mLastError on
798- if (mLastError & (1 << (ErrorActiveSensingTimeout - 1 )))
806+ if (mMessage .type == ActiveSensing && !mReceiverActiveSensingActive )
799807 {
800- mLastError &= ~(1UL << ErrorActiveSensingTimeout); // clear the ErrorActiveSensingTimeout bit
808+ // Once an Active Sensing message is received, the unit will begin monitoring
809+ // the intervalbetween all subsequent messages. If there is an interval of 420 ms
810+ // or longer betweenmessages while monitoring is active, the same processing
811+ // as when All Sound Off, All Notes Off,and Reset All Controllers messages are
812+ // received will be carried out. The unit will then stopmonitoring the message interval.
813+ mReceiverActiveSensingActive = true ;
814+
815+ // Clear the ErrorActiveSensingTimeout bit
816+ mLastError &= ~(1UL << ErrorActiveSensingTimeout);
801817 if (mErrorCallback )
802818 mErrorCallback (mLastError );
803819 }
804820 }
805821
806- // Keep the time of the last received message, so we can check for the timeout
807- if (Settings::UseReceiverActiveSensing && mReceiverActiveSensingActivated )
808- mLastMessageReceivedTime = Platform::now ();
809-
810822 #endif
811823
812824 handleNullVelocityNoteOnAsNoteOff ();
0 commit comments