diff --git a/Source/JaySynth.cpp b/Source/JaySynth.cpp index 5864937..388e9f3 100644 --- a/Source/JaySynth.cpp +++ b/Source/JaySynth.cpp @@ -129,8 +129,6 @@ JaySynth::JaySynth (int num_voices, String pathToWaves) } m_clock_cnt_bar = 0; - m_lastTime_ms = 0; - m_estimatedTime_ms = 0; m_numMidiClocksPerBeat = 1; m_numMidiClocksPerBar = 1; m_isMidiClockStarted = false; @@ -1132,7 +1130,6 @@ void JaySynth::handleMidiEvent (const MidiMessage& m) SynthDebug("SongPositionPointer\n"); m_clock_cnt_bar = (6 * m.getSongPositionPointerMidiBeat()) % m_numMidiClocksPerBar; SynthDebug("getSongPositionPointerMidiBeat(): %u -> %u clocks\n", m.getSongPositionPointerMidiBeat(), m_clock_cnt_bar); - m_lastTime_ms = 0; } if (m.isQuarterFrame()) { @@ -1150,30 +1147,20 @@ void JaySynth::handleMidiEvent (const MidiMessage& m) } if (m.isMidiClock()) { - const double alpha_1 = 2.0/m_numMidiclocksPerQuarterNote; - const double currTime_ms = juce::Time::getMillisecondCounterHiRes(); - - // BPM Estimation - double deltaTime_ms = currTime_ms - m_lastTime_ms; - if (m_lastTime_ms > 0.0) - { - m_estimatedTime_ms = (1.0-alpha_1)*m_estimatedTime_ms + alpha_1*deltaTime_ms; - } - m_lastTime_ms = currTime_ms; - // Quarter clock generation m_clock_cnt_bar = m_clock_cnt_bar % m_numMidiClocksPerBar; if (m_isMidiClockStarted) { #if WITH_DEV_SYNCED_LFO - voiceSetMidiBeat(m_clock_cnt_bar); + // Sync LFO and BPM Estimation + synth_float_t bpm = voiceSetMidiBeat(m_clock_cnt_bar); #endif if (m_clock_cnt_bar % m_numMidiClocksPerBeat == 0) { // Send quarter pulse with current BPM estimation midi_quarter_clock_info_t info; - info.bpm = (uint32_t)(m_numMidiclocksPerQuarterNote*m_estimatedTime_ms + 0.5); + info.bpm = (uint32_t)(bpm + 0.5); info.type = 1; if (m_clock_cnt_bar == 0) { @@ -1186,13 +1173,14 @@ void JaySynth::handleMidiEvent (const MidiMessage& m) } } -void JaySynth::voiceSetMidiBeat(int barBeatCount) +synth_float_t JaySynth::voiceSetMidiBeat(int barBeatCount) { + synth_float_t bpm = 0; for (int i=0; i < VOICE_NUM_LFO; i++) { lfo_t *pLfo = &m_pVoices[0].pCom->glfo[i]; synth_float_t phase = Sync_mod((synth_float_t)barBeatCount/m_numMidiClocksPerBar); - LFO_sync(pLfo, phase); + bpm += LFO_sync(pLfo, phase)/VOICE_NUM_LFO; } for (int i=0; i < max_num_voices; i++) @@ -1200,6 +1188,8 @@ void JaySynth::voiceSetMidiBeat(int barBeatCount) JaySynthVoice* const voice = voices.getUnchecked (i); voice->setMidiBeat(barBeatCount); } + + return bpm; } //============================================================================== diff --git a/Source/JaySynth.h b/Source/JaySynth.h index 50bcc8a..7a16e66 100644 --- a/Source/JaySynth.h +++ b/Source/JaySynth.h @@ -87,7 +87,7 @@ public: const float velocity, bool doTrigger); void Voicestop (JaySynthVoice* voice, const bool allowTailOff); - void voiceSetMidiBeat(int quarterBeatCount); + synth_float_t voiceSetMidiBeat(int quarterBeatCount); int getNumVoicesPlaying (SynthesiserSound* soundToPlay); void clearKeysPressed(void); @@ -258,8 +258,6 @@ private: int m_clock_cnt_bar; int m_numMidiClocksPerBeat; int m_numMidiClocksPerBar; - double m_lastTime_ms; - double m_estimatedTime_ms; bool m_isMidiClockStarted; void updateMidiTiming(const MidiMessage& m); diff --git a/Source/JaySynthAudioProcessorEditor.cpp b/Source/JaySynthAudioProcessorEditor.cpp index b82b4d0..ee88cbd 100644 --- a/Source/JaySynthAudioProcessorEditor.cpp +++ b/Source/JaySynthAudioProcessorEditor.cpp @@ -5184,10 +5184,9 @@ void JaySynthAudioProcessorEditor::actionListenerCallback (const String &message case JaySynthActionListener::JAYSYNTH_CHANGED_MIDICLOCK: { - uint32_t deltaTime_ms = spa.getValue("deltatime", "-1").getIntValue(); + uint32_t bpm = spa.getValue("bpm", "-1").getIntValue(); uint32_t brightness = spa.getValue("brightness", "-1").getIntValue(); m_btn_midiClockLed->flash(brightness*50, brightness); - uint32_t bpm = (uint32_t)(60*1000.0/deltaTime_ms + 0.5); m_labelDebug->setText(String("BPM = ") + String(bpm), juce::NotificationType::dontSendNotification); } break; diff --git a/Source/PluginProcessor.h b/Source/PluginProcessor.h index df6480a..269f73f 100644 --- a/Source/PluginProcessor.h +++ b/Source/PluginProcessor.h @@ -113,12 +113,12 @@ public: sendActionMessage(s); } - void callMidiClock (uint32_t deltaTime_ms, uint32_t type) + void callMidiClock (uint32_t bpm, uint32_t type) { String s; s = String("type=") + String(JAYSYNTH_CHANGED_MIDICLOCK); s = s + String(" "); - s = s + String("deltatime=") + String(deltaTime_ms); + s = s + String("bpm=") + String(bpm); s = s + String(" "); s = s + String("brightness=") + String(type); s = s + String(" "); diff --git a/Source/synth/lfo.c b/Source/synth/lfo.c index d1968c8..64134c1 100644 --- a/Source/synth/lfo.c +++ b/Source/synth/lfo.c @@ -178,7 +178,9 @@ void Sync_processV(lfo_t* pObj, int len) for (int i=0; i < len; i++) { pObj->pPhase[i].phase = pSync->phase; + synth_float_t phase = pSync->phase; pSync->phase = Sync_mod(pSync->phase + omega_base); + pObj->pPhase[i].is_cycle_start = (phase > pSync->phase); } } break; @@ -188,7 +190,9 @@ void Sync_processV(lfo_t* pObj, int len) for (int i=0; i < len; i++) { pObj->pPhase[i].phase = pSync->phase; + synth_float_t phase = pSync->phase; pSync->phase = Sync_mod(pSync->phase + pSync->omega); + pObj->pPhase[i].is_cycle_start = (phase > pSync->phase); } } break; @@ -197,7 +201,10 @@ void Sync_processV(lfo_t* pObj, int len) { for (int i=0; i < len; i++) { - pObj->pPhase[i].phase = pObj->pPhase[i].phase_int; + synth_float_t phase = pSync->phase; + pSync->phase = pObj->pPhase[i].phase_int; + pObj->pPhase[i].is_cycle_start = (phase > pSync->phase); + pObj->pPhase[i].phase = pSync->phase; } } break; @@ -292,11 +299,14 @@ void LFO_Reset(lfo_t *pObj, synth_float_t initial_phase) } -void LFO_sync(lfo_t *pObj, synth_float_t phase_ref) +synth_float_t LFO_sync(lfo_t *pObj, synth_float_t phase_ref) { synth_float_t note_1_x = pObj->param[LFO_PARAM2_MIDISYNC_BEATDIV]; phase_ref = Sync_mod(note_1_x*phase_ref); Sync_phase_update(&pObj->sync, phase_ref); + + // Return BPM + return 4*60*pObj->fs*pObj->sync.omega/note_1_x; } void LFO_Param2Set(lfo_t *pObj, UINT32 type, synth_float_t value) diff --git a/Source/synth/lfo.h b/Source/synth/lfo.h index 2530acd..26c29b4 100644 --- a/Source/synth/lfo.h +++ b/Source/synth/lfo.h @@ -123,7 +123,7 @@ void LFO_Free(lfo_t *pObj); void LFO_SetFS(lfo_t *pObj, synth_float_t fs); void LFO_SetBufsize(lfo_t *pObj, UINT32 bufsize); void LFO_Reset(lfo_t *pObj, synth_float_t initial_phase); -void LFO_sync(lfo_t *pObj, synth_float_t phase); +synth_float_t LFO_sync(lfo_t *pObj, synth_float_t phase); void LFO_Param2Set(lfo_t *pObj, UINT32 type, synth_float_t value); synth_float_t* LFO_ProcessDataV(lfo_t *pObj, UINT32 len); void Sync_processV(lfo_t *pObj, int len);