| 133 | } |
| 134 | |
| 135 | void MidiClockIn::OnMidi(const juce::MidiMessage& message) |
| 136 | { |
| 137 | const int kDebugMaxLineCount = 40; |
| 138 | |
| 139 | if (message.isMidiClock() || message.isSongPositionPointer()) |
| 140 | { |
| 141 | const int kMinRequiredPulseCount = 48; |
| 142 | double time = message.getTimeStamp(); |
| 143 | if (mDrawDebug) |
| 144 | AddDebugLine("midi clock " + ofToString(time, 3), kDebugMaxLineCount); |
| 145 | |
| 146 | if (mReceivedPulseCount == 0) |
| 147 | { |
| 148 | double currentTempoDeltaSeconds = 1.0 / (TheTransport->GetTempo() / 60 * 24); |
| 149 | mDelayLockedLoop.reset(time, currentTempoDeltaSeconds, 1); |
| 150 | mDelayLockedLoop.setParams(gBufferSize, gSampleRate); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | mDelayLockedLoop.update(time); |
| 155 | } |
| 156 | |
| 157 | if (mReceivedPulseCount >= kMinRequiredPulseCount && time - mLastTimestamp > .001f) |
| 158 | { |
| 159 | double deltaSeconds = mDelayLockedLoop.timeDiff(); |
| 160 | double pulsesPerSecond = 1 / deltaSeconds; |
| 161 | double beatsPerSecond = pulsesPerSecond / 24; |
| 162 | double instantTempo = beatsPerSecond * 60; |
| 163 | |
| 164 | if (instantTempo > 20 && instantTempo < 999) |
| 165 | { |
| 166 | if (mTempoIdx == -1) |
| 167 | mTempoHistory.fill(instantTempo); |
| 168 | else |
| 169 | mTempoHistory[mTempoIdx] = instantTempo; |
| 170 | mTempoIdx = (mTempoIdx + 1) % mTempoHistory.size(); |
| 171 | |
| 172 | if (mEnabled) |
| 173 | TheTransport->SetTempo(GetRoundedTempo()); |
| 174 | } |
| 175 | |
| 176 | if (mDrawDebug) |
| 177 | AddDebugLine(" deltaSeconds=" + ofToString(deltaSeconds, 6) + " instantTempo=" + ofToString(instantTempo, 2) + " rounded tempo:" + ofToString(GetRoundedTempo(), 1), kDebugMaxLineCount); |
| 178 | |
| 179 | mLastTimestamp = time; |
| 180 | } |
| 181 | ++mReceivedPulseCount; |
| 182 | } |
| 183 | if (message.isMidiStart()) |
| 184 | { |
| 185 | if (mDrawDebug) |
| 186 | AddDebugLine("midi start", kDebugMaxLineCount); |
| 187 | if (mObeyClockStartStop) |
| 188 | { |
| 189 | mTempoIdx = -1; |
| 190 | mReceivedPulseCount = 0; |
| 191 | if (TheSynth->IsAudioPaused()) |
| 192 | { |
no test coverage detected