Skip to content

Commit

Permalink
try improve MTC Sender
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Sep 18, 2024
1 parent 26ba218 commit 386292f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
7 changes: 7 additions & 0 deletions Source/Common/MIDI/MIDIDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "MIDIDevice.h"
/*
==============================================================================
Expand Down Expand Up @@ -210,3 +211,9 @@ void MIDIOutputDevice::sendAfterTouch(int channel, int note, int value)
if (device == nullptr) return;
device->sendMessageNow(MidiMessage::aftertouchChange(channel, note, value));
}

void MIDIOutputDevice::sendMessageNow(const MidiMessage& message)
{
if (device == nullptr) return;
device->sendMessageNow(message);
}
1 change: 1 addition & 0 deletions Source/Common/MIDI/MIDIDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class MIDIOutputDevice :
void sendPitchWheel(int channel, int value);
void sendChannelPressure(int channel, int value);
void sendAfterTouch(int channel, int note, int value);
void sendMessageNow(const MidiMessage &message);

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MIDIOutputDevice)
};
35 changes: 22 additions & 13 deletions Source/Common/MIDI/MTCSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ void MTCSender::pause(bool resumeIfAlreadyPaused)
stopThread(10);
}
else if (resumeIfAlreadyPaused)
{
startThread();
}
}

void MTCSender::stop()
Expand All @@ -57,15 +59,24 @@ void MTCSender::setPosition(double position, bool fullFrame)
if (device == nullptr) return;

double unused;
lock.enter();

bool wasRunning = isThreadRunning();
if (wasRunning) stopThread(100);

GenericScopedLock _lock(lock);
m_frame = static_cast<int>(modf(position, &unused) * fps);
m_second = static_cast<int>(position) % 60;
m_minute = (static_cast<int>(position) / 60) % 60;
m_hour = (static_cast<int>(position) / 60 / 60) % 60;
m_piece = Piece::FrameLSB;
lock.exit();

if (fullFrame) device->sendFullframeTimecode(m_hour, m_minute, m_second, m_frame, fpsType);

if (fullFrame)
{
device->sendFullframeTimecode(m_hour, m_minute, m_second, m_frame, fpsType);
}

if (wasRunning) startThread();
}

void MTCSender::setSpeedFactor(float speed)
Expand All @@ -78,10 +89,10 @@ void MTCSender::setFPS(MidiMessage::SmpteTimecodeType val)
fpsType = val;
switch (fpsType)
{
case MidiMessage::fps24: fps = 24;
case MidiMessage::fps25: fps = 25;
case MidiMessage::fps30: fps = 30;
case MidiMessage::fps30drop: fps = 29.997;
case MidiMessage::fps24: fps = 24; break;
case MidiMessage::fps25: fps = 25; break;
case MidiMessage::fps30: fps = 30; break;
case MidiMessage::fps30drop: fps = 29.997; break;
}
}

Expand All @@ -94,6 +105,7 @@ void MTCSender::run()
while (!threadShouldExit())
{
sleep(1);
GenericScopedLock _lock(lock);

frameTime = (1000.0 / fps / 4) / speedFactor;

Expand All @@ -102,7 +114,6 @@ void MTCSender::run()

lastFrameTime += frameTime;

lock.enter();

const int value = getValue(m_piece);
device->sendQuarterframe(static_cast<int>(m_piece), value);
Expand All @@ -117,20 +128,18 @@ void MTCSender::run()
m_frame = 0;
if (++m_second >= 60)
{
m_second = 0;
m_second = m_second % 60;
if (++m_minute >= 60)
{
m_minute = 0;
m_minute = m_minute % 60;
if (++m_hour >= 24)
{
m_hour = 0;
m_hour = m_hour % 24;
}
}
}
}
}

lock.exit();
}

}
Expand Down
2 changes: 1 addition & 1 deletion Source/Common/MIDI/MTCSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MTCSender :
void start(double position = 0);
void pause(bool resumeIfAlreadyPaused = true);
void stop();
void setPosition(double position, bool fullFrame = false);
void setPosition(double position, bool fullFrame = true);
void setSpeedFactor(float speed);
void setFPS(MidiMessage::SmpteTimecodeType val);

Expand Down

0 comments on commit 386292f

Please sign in to comment.