forked from BespokeSynth/BespokeSynth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic ableton link support (BespokeSynth#702)
- Loading branch information
Showing
13 changed files
with
241 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
bespoke synth, a software modular synthesizer | ||
Copyright (C) 2021 Ryan Challinor (contact: [email protected]) | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
**/ | ||
// | ||
// AbletonLink.cpp | ||
// | ||
// Created by Ryan Challinor on 12/9/21. | ||
// | ||
// | ||
|
||
#include "AbletonLink.h" | ||
#include "OpenFrameworksPort.h" | ||
#include "Transport.h" | ||
#include "ModularSynth.h" | ||
#include "UIControlMacros.h" | ||
|
||
#include "juce_audio_devices/juce_audio_devices.h" | ||
|
||
#include "ableton/Link.hpp" | ||
#include "ableton/link/Timeline.hpp" | ||
#include "ableton/link/HostTimeFilter.hpp" | ||
|
||
static ableton::link::HostTimeFilter<ableton::link::platform::Clock> sHostTimeFilter; | ||
|
||
AbletonLink::AbletonLink() | ||
{ | ||
sHostTimeFilter.reset(); | ||
|
||
mNumPeers = 0; | ||
|
||
mTempo = TheTransport->GetTempo(); | ||
|
||
mLink = std::make_unique<ableton::Link>(mTempo); | ||
|
||
mLink->enable(true); | ||
|
||
mLink->setNumPeersCallback([this](std::size_t p) | ||
{ | ||
mNumPeers = p; | ||
}); | ||
|
||
mLink->setTempoCallback([this](const double bpm) | ||
{ | ||
TheTransport->SetTempo(bpm); | ||
mTempo = bpm; | ||
}); | ||
} | ||
|
||
void AbletonLink::Init() | ||
{ | ||
IDrawableModule::Init(); | ||
|
||
TheTransport->AddAudioPoller(this); | ||
} | ||
|
||
AbletonLink::~AbletonLink() | ||
{ | ||
TheTransport->RemoveAudioPoller(this); | ||
} | ||
|
||
void AbletonLink::CreateUIControls() | ||
{ | ||
IDrawableModule::CreateUIControls(); | ||
UIBLOCK0(); | ||
FLOATSLIDER(mOffsetMsSlider, "offset ms", &mOffsetMs, -1000, 1000); | ||
ENDUIBLOCK(mWidth, mHeight); | ||
|
||
mHeight = 80; | ||
} | ||
|
||
void AbletonLink::Poll() | ||
{ | ||
} | ||
|
||
void AbletonLink::OnTransportAdvanced(float amount) | ||
{ | ||
if (mEnabled && mLink.get() != nullptr) | ||
{ | ||
if (mTempo != TheTransport->GetTempo()) | ||
{ | ||
mTempo = TheTransport->GetTempo(); | ||
auto sessionState = mLink->captureAudioSessionState(); | ||
sessionState.setTempo(mTempo, mLink->clock().micros()); | ||
mLink->commitAudioSessionState(sessionState); | ||
} | ||
|
||
auto& deviceManager = TheSynth->GetAudioDeviceManager(); | ||
if (deviceManager.getCurrentAudioDevice() != nullptr) | ||
{ | ||
auto sampleRate = deviceManager.getCurrentAudioDevice()->getCurrentSampleRate(); | ||
auto outputLatencySamples = deviceManager.getCurrentAudioDevice()->getOutputLatencyInSamples(); | ||
auto bufferSize = deviceManager.getCurrentAudioDevice()->getCurrentBufferSizeSamples(); | ||
|
||
const auto hostTimeUs = sHostTimeFilter.sampleTimeToHostTime(mSampleTime); | ||
const auto outputLatencyUs = std::chrono::microseconds{ std::llround(1.0e6 * bufferSize / sampleRate) }; | ||
auto offsetUs = std::chrono::microseconds{ llround(mOffsetMs * 1000) }; | ||
|
||
auto adjustedTimeUs = hostTimeUs + outputLatencyUs + offsetUs; | ||
|
||
auto sessionState = mLink->captureAudioSessionState(); | ||
|
||
double quantum = TheTransport->GetTimeSigTop(); | ||
mLastReceivedBeat = sessionState.beatAtTime(adjustedTimeUs, quantum); | ||
|
||
TheTransport->SetMeasureTime(mLastReceivedBeat / quantum); | ||
|
||
|
||
// Timeline modifications are complete, commit the results | ||
//mLink->commitAudioSessionState(sessionState); | ||
|
||
mSampleTime += gBufferSize; | ||
} | ||
} | ||
} | ||
|
||
void AbletonLink::DrawModule() | ||
{ | ||
if (Minimized() || IsVisible() == false) | ||
return; | ||
|
||
mOffsetMsSlider->Draw(); | ||
|
||
DrawTextNormal("peers: " + ofToString(mNumPeers) + "\ntempo: " + ofToString(mTempo) + "\nbeat: " + ofToString(mLastReceivedBeat), 3, 40); | ||
} | ||
|
||
void AbletonLink::CheckboxUpdated(Checkbox *checkbox) | ||
{ | ||
} | ||
|
||
void AbletonLink::FloatSliderUpdated(FloatSlider* slider, float oldVal) | ||
{ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
bespoke synth, a software modular synthesizer | ||
Copyright (C) 2021 Ryan Challinor (contact: [email protected]) | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
**/ | ||
// | ||
// AbletonLink.h | ||
// | ||
// Created by Ryan Challinor on 12/9/21. | ||
// | ||
// | ||
|
||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
#include "IDrawableModule.h" | ||
#include "Checkbox.h" | ||
#include "IAudioPoller.h" | ||
#include "Slider.h" | ||
|
||
namespace ableton | ||
{ | ||
class Link; | ||
} | ||
|
||
class AbletonLink : public IDrawableModule, public IAudioPoller, public IFloatSliderListener | ||
{ | ||
public: | ||
AbletonLink(); | ||
virtual ~AbletonLink(); | ||
static IDrawableModule* Create() { return new AbletonLink(); } | ||
|
||
void Init() override; | ||
void CreateUIControls() override; | ||
void Poll() override; | ||
|
||
void OnTransportAdvanced(float amount) override; | ||
|
||
void SetEnabled(bool enabled) override { mEnabled = enabled; } | ||
|
||
void CheckboxUpdated(Checkbox* checkbox) override; | ||
void FloatSliderUpdated(FloatSlider* slider, float oldVal) override; | ||
|
||
private: | ||
//IDrawableModule | ||
void DrawModule() override; | ||
void GetModuleDimensions(float& width, float& height) override { width = mWidth; height = mHeight; } | ||
bool Enabled() const override { return mEnabled; } | ||
|
||
float mWidth; | ||
float mHeight; | ||
|
||
float mOffsetMs{ 0 }; | ||
FloatSlider* mOffsetMsSlider; | ||
|
||
std::unique_ptr<ableton::Link> mLink; | ||
double mTempo = 120.0; | ||
std::size_t mNumPeers = 0; | ||
double mLastReceivedBeat{ 0 }; | ||
double mSampleTime{ 0 }; | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters