First commit.
This commit is contained in:
@@ -0,0 +1,338 @@
|
||||
#pragma once
|
||||
|
||||
#include "PiPedalException.hpp"
|
||||
#include "Lv2Log.hpp"
|
||||
#include "VuUpdate.hpp"
|
||||
#include "JackHost.hpp"
|
||||
|
||||
namespace pipedal
|
||||
{
|
||||
|
||||
enum class RingBufferCommand : int64_t
|
||||
{
|
||||
ReplaceEffect = 0,
|
||||
EffectReplaced = 1,
|
||||
SetValue = 2,
|
||||
SetBypass = 3,
|
||||
AudioStopped = 4,
|
||||
SetVuSubscriptions = 5,
|
||||
FreeVuSubscriptions = 6,
|
||||
SendVuUpdate = 7,
|
||||
AckVuUpdate = 8,
|
||||
|
||||
SetMonitorPortSubscription = 9,
|
||||
FreeMonitorPortSubscription = 10,
|
||||
SendMonitorPortUpdate = 11,
|
||||
AckMonitorPortUpdate = 12,
|
||||
ParameterRequest = 13,
|
||||
ParameterRequestComplete = 14,
|
||||
|
||||
MidiValueChanged = 15,
|
||||
|
||||
OnMidiListen = 16,
|
||||
|
||||
};
|
||||
|
||||
class RealtimeMonitorPortSubscription
|
||||
{
|
||||
public:
|
||||
RealtimeMonitorPortSubscription() { delete callbackPtr; }
|
||||
int64_t subscriptionHandle;
|
||||
int instanceIndex = 0;
|
||||
int portIndex = 0;
|
||||
PortMonitorCallback *callbackPtr = nullptr;
|
||||
int sampleRate = 0;
|
||||
int samplesToNextCallback = 0;
|
||||
bool waitingForAck = false;
|
||||
};
|
||||
|
||||
class RealtimeMonitorPortSubscriptions
|
||||
{
|
||||
public:
|
||||
std::vector<RealtimeMonitorPortSubscription> subscriptions;
|
||||
};
|
||||
|
||||
struct RealtimeVuBuffers
|
||||
{
|
||||
RealtimeVuBuffers()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
bool waitingForAcknowledge = false;
|
||||
|
||||
const std::vector<VuUpdate> *GetResult(size_t currentSample)
|
||||
{
|
||||
for (size_t i = 0; i < vuUpdateWorkingData.size(); ++i)
|
||||
{
|
||||
vuUpdateResponseData[i] = vuUpdateWorkingData[i];
|
||||
vuUpdateResponseData[i].sampleTime_ = currentSample;
|
||||
vuUpdateWorkingData[i].reset();
|
||||
}
|
||||
return &vuUpdateResponseData;
|
||||
}
|
||||
|
||||
std::vector<int> enabledIndexes;
|
||||
std::vector<VuUpdate> vuUpdateWorkingData;
|
||||
std::vector<VuUpdate> vuUpdateResponseData;
|
||||
|
||||
void Reset()
|
||||
{
|
||||
for (int i = 0; i < vuUpdateWorkingData.size(); ++i)
|
||||
{
|
||||
vuUpdateWorkingData[i].reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class AudioStoppedBody
|
||||
{
|
||||
bool dummy = true;
|
||||
};
|
||||
|
||||
class SetBypassBody
|
||||
{
|
||||
public:
|
||||
int effectIndex;
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
class MidiValueChangedBody
|
||||
{
|
||||
public:
|
||||
int64_t instanceId;
|
||||
int controlIndex;
|
||||
float value;
|
||||
};
|
||||
|
||||
|
||||
class SetControlValueBody
|
||||
{
|
||||
public:
|
||||
int effectIndex;
|
||||
int controlIndex;
|
||||
float value;
|
||||
};
|
||||
|
||||
class Lv2PedalBoard;
|
||||
|
||||
class ReplaceEffectBody
|
||||
{
|
||||
public:
|
||||
Lv2PedalBoard *effect;
|
||||
};
|
||||
class EffectReplacedBody
|
||||
{
|
||||
public:
|
||||
Lv2PedalBoard *oldEffect;
|
||||
};
|
||||
|
||||
template <bool MULTI_WRITE, bool SEMAPHORE_READ>
|
||||
class RingBufferReader
|
||||
{
|
||||
|
||||
private:
|
||||
RingBuffer<MULTI_WRITE,SEMAPHORE_READ> *ringBuffer = nullptr;
|
||||
|
||||
public:
|
||||
RingBufferReader()
|
||||
: ringBuffer(nullptr)
|
||||
{
|
||||
}
|
||||
RingBufferReader(RingBuffer<MULTI_WRITE,SEMAPHORE_READ> *ringBuffer)
|
||||
: ringBuffer(ringBuffer)
|
||||
{
|
||||
}
|
||||
|
||||
// 0 -> ready. -1: timed out. -2: closing.
|
||||
int wait(struct timespec &timeout) {
|
||||
return ringBuffer->readWait(timeout);
|
||||
}
|
||||
|
||||
bool wait() {
|
||||
return ringBuffer->readWait();
|
||||
}
|
||||
size_t readSpace() const
|
||||
{
|
||||
return ringBuffer->readSpace();
|
||||
}
|
||||
template <typename T>
|
||||
bool read(T *output)
|
||||
{
|
||||
size_t available = readSpace();
|
||||
if (available < sizeof(T))
|
||||
return false;
|
||||
if (!ringBuffer->read(sizeof(T),(uint8_t*)output))
|
||||
{
|
||||
throw PiPedalStateException("Ringbuffer read failed. Did you forget to check for space?");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <typename T>
|
||||
void readComplete(T *output)
|
||||
{
|
||||
if (!ringBuffer->read(sizeof(T),(uint8_t*)output))
|
||||
{
|
||||
throw PiPedalStateException("Ringbuffer read failed. Did you forget to check for space?");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class CommandBuffer
|
||||
{
|
||||
|
||||
public:
|
||||
CommandBuffer(RingBufferCommand command)
|
||||
: command(command)
|
||||
{
|
||||
}
|
||||
|
||||
CommandBuffer(RingBufferCommand command, const T &value)
|
||||
: command(command), value(value)
|
||||
{
|
||||
}
|
||||
RingBufferCommand command;
|
||||
|
||||
size_t size() const { return sizeof(RingBufferCommand) + sizeof(T); }
|
||||
|
||||
T value;
|
||||
};
|
||||
|
||||
template<bool MULTI_WRITER, bool SEMAPHORE_READER>
|
||||
class RingBufferWriter
|
||||
{
|
||||
|
||||
private:
|
||||
RingBuffer<MULTI_WRITER,SEMAPHORE_READER>* ringBuffer;
|
||||
|
||||
public:
|
||||
RingBufferWriter()
|
||||
: ringBuffer(nullptr)
|
||||
{
|
||||
}
|
||||
RingBufferWriter(RingBuffer<MULTI_WRITER,SEMAPHORE_READER>*ringBuffer)
|
||||
: ringBuffer(ringBuffer)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void write(RingBufferCommand command, const T &value)
|
||||
{
|
||||
|
||||
// the goal: to atomically write the command and associated data.
|
||||
CommandBuffer<T> buffer(command, value);
|
||||
|
||||
if (!ringBuffer->write(buffer.size(),(uint8_t *)&buffer))
|
||||
{
|
||||
Lv2Log::error("No space in audio service ringbuffer.");
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
void ParameterRequest(RealtimeParameterRequest *pRequest)
|
||||
{
|
||||
write(RingBufferCommand::ParameterRequest,pRequest);
|
||||
}
|
||||
void ParameterRequestComplete(RealtimeParameterRequest *pRequest)
|
||||
{
|
||||
write(RingBufferCommand::ParameterRequestComplete,pRequest);
|
||||
}
|
||||
void MidiValueChanged(int64_t instanceId, int controlIndex,float value)
|
||||
{
|
||||
MidiValueChangedBody body;
|
||||
body.instanceId = instanceId;
|
||||
body.controlIndex = controlIndex;
|
||||
body.value = value;
|
||||
write(RingBufferCommand::MidiValueChanged,body);
|
||||
}
|
||||
|
||||
void OnMidiListen(bool isNote, uint8_t noteOrControl) {
|
||||
uint16_t msg = noteOrControl;
|
||||
if (isNote) msg |= 0x100;
|
||||
write(RingBufferCommand::OnMidiListen, msg);
|
||||
}
|
||||
|
||||
void SetControlValue(int effectIndex, int controlIndex, float value)
|
||||
{
|
||||
SetControlValueBody body;
|
||||
body.effectIndex = effectIndex;
|
||||
body.controlIndex = controlIndex;
|
||||
body.value = value;
|
||||
write(RingBufferCommand::SetValue,body);
|
||||
}
|
||||
|
||||
void FreeVuSubscriptions(RealtimeVuBuffers *configuration)
|
||||
{
|
||||
write(RingBufferCommand::FreeVuSubscriptions, configuration);
|
||||
}
|
||||
void FreeMonitorPortSubscriptions(RealtimeMonitorPortSubscriptions *subscriptions)
|
||||
{
|
||||
|
||||
write(RingBufferCommand::FreeMonitorPortSubscription, subscriptions);
|
||||
}
|
||||
|
||||
void SendMonitorPortUpdate(
|
||||
PortMonitorCallback* callback,
|
||||
int64_t subscriptionHandle,
|
||||
float value)
|
||||
{
|
||||
MonitorPortUpdate body{callback, subscriptionHandle, value};
|
||||
write(RingBufferCommand::SendMonitorPortUpdate, body);
|
||||
}
|
||||
|
||||
void SendVuUpdate(const std::vector<VuUpdate> *pUpdates)
|
||||
{
|
||||
write(RingBufferCommand::SendVuUpdate,pUpdates);
|
||||
}
|
||||
void AckVuUpdate()
|
||||
{
|
||||
bool value = true;
|
||||
write(RingBufferCommand::AckVuUpdate,value);
|
||||
}
|
||||
void AckMonitorPortUpdate(int64_t subscriptionHandle)
|
||||
{
|
||||
// we assume no padding between the command and the data, so we can do an atomic write.
|
||||
|
||||
write(RingBufferCommand::AckMonitorPortUpdate, subscriptionHandle);
|
||||
}
|
||||
void SetVuSubscriptions(RealtimeVuBuffers *configuration)
|
||||
{
|
||||
write(RingBufferCommand::SetVuSubscriptions, configuration);
|
||||
}
|
||||
|
||||
void SetMonitorPortSubscriptions(RealtimeMonitorPortSubscriptions *subscriptions)
|
||||
{
|
||||
|
||||
write(RingBufferCommand::SetMonitorPortSubscription, subscriptions);
|
||||
}
|
||||
|
||||
void SetBypass(int effectIndex, bool enabled)
|
||||
{
|
||||
|
||||
SetBypassBody body;
|
||||
body.effectIndex = effectIndex;
|
||||
body.enabled = enabled;
|
||||
write(RingBufferCommand::SetBypass,body);
|
||||
}
|
||||
|
||||
void ReplaceEffect(Lv2PedalBoard *pedalBoard)
|
||||
{
|
||||
write(RingBufferCommand::ReplaceEffect, pedalBoard);
|
||||
}
|
||||
|
||||
void AudioStopped()
|
||||
{
|
||||
AudioStoppedBody body;
|
||||
write(RingBufferCommand::AudioStopped, body);
|
||||
}
|
||||
|
||||
void EffectReplaced(Lv2PedalBoard *pedalBoard)
|
||||
{
|
||||
write(RingBufferCommand::EffectReplaced, pedalBoard);
|
||||
}
|
||||
};
|
||||
|
||||
} //namespace
|
||||
Reference in New Issue
Block a user