sfizz/sources/Voice.h

91 lines
2.8 KiB
C
Raw Normal View History

2019-07-29 02:13:03 +02:00
#pragma once
2019-08-24 15:32:06 +02:00
#include "ADSREnvelope.h"
2019-08-23 01:27:39 +02:00
#include "Globals.h"
#include "Region.h"
2019-08-24 15:50:55 +02:00
#include "StereoBuffer.h"
#include "StereoSpan.h"
2019-08-29 11:48:38 +02:00
#include <absl/types/span.h>
#include <atomic>
2019-07-29 02:13:03 +02:00
2019-08-24 15:50:55 +02:00
namespace sfz {
class Voice {
2019-07-29 02:13:03 +02:00
public:
2019-08-24 15:32:06 +02:00
Voice() = delete;
2019-08-29 11:48:38 +02:00
Voice(const CCValueArray& ccState);
2019-08-24 15:50:55 +02:00
enum class TriggerType {
2019-08-24 15:32:06 +02:00
NoteOn,
NoteOff,
CC
};
2019-08-29 16:23:35 +02:00
void setSampleRate(float sampleRate) noexcept;
void setSamplesPerBlock(int samplesPerBlock) noexcept;
2019-08-29 11:48:38 +02:00
2019-08-29 16:23:35 +02:00
void startVoice(Region* region, int delay, int channel, int number, uint8_t value, TriggerType triggerType) noexcept;
2019-08-29 11:48:38 +02:00
2019-08-29 16:23:35 +02:00
void setFileData(std::unique_ptr<StereoBuffer<float>> file) noexcept;
void registerNoteOff(int delay, int channel, int noteNumber, uint8_t velocity) noexcept;
void registerCC(int delay, int channel, int ccNumber, uint8_t ccValue) noexcept;
void registerPitchWheel(int delay, int channel, int pitch) noexcept;
void registerAftertouch(int delay, int channel, uint8_t aftertouch) noexcept;
void registerTempo(int delay, float secondsPerQuarter) noexcept;
2019-08-29 11:48:38 +02:00
bool checkOffGroup(int delay [[maybe_unused]], uint32_t group) noexcept;
2019-08-23 01:27:39 +02:00
2019-08-29 16:23:35 +02:00
void renderBlock(StereoSpan<float> buffer) noexcept;
2019-08-24 15:32:06 +02:00
2019-08-29 16:23:35 +02:00
bool isFree() const noexcept;
int getTriggerNumber() const noexcept;
int getTriggerChannel() const noexcept;
uint8_t getTriggerValue() const noexcept;
TriggerType getTriggerType() const noexcept;
2019-08-24 15:32:06 +02:00
2019-08-29 16:23:35 +02:00
void reset() noexcept;
void garbageCollect() noexcept;
2019-07-29 02:13:03 +02:00
private:
2019-08-29 16:23:35 +02:00
void fillWithData(StereoSpan<float> buffer) noexcept;
void fillWithGenerator(StereoSpan<float> buffer) noexcept;
void prepareEGEnvelope(int delay, uint8_t velocity) noexcept;
2019-08-29 11:48:38 +02:00
2019-08-25 14:26:20 +02:00
Region* region { nullptr };
2019-08-24 15:32:06 +02:00
2019-08-24 15:50:55 +02:00
enum class State {
2019-08-24 15:32:06 +02:00
idle,
playing,
release
};
2019-08-25 14:26:20 +02:00
State state { State::idle };
bool noteIsOff { false };
2019-08-23 01:27:39 +02:00
TriggerType triggerType;
int triggerNumber;
int triggerChannel;
uint8_t triggerValue;
2019-08-24 15:50:55 +02:00
float speedRatio { 1.0 };
float pitchRatio { 1.0 };
float baseGain { 1.0 };
float baseFrequency { 440.0 };
float phase { 0.0f };
2019-08-24 15:32:06 +02:00
2019-08-25 14:26:20 +02:00
uint32_t sourcePosition { 0 };
float floatPosition { 0.0f };
2019-08-25 14:26:20 +02:00
uint32_t initialDelay { 0 };
2019-08-24 15:32:06 +02:00
std::atomic<bool> dataReady { false };
std::unique_ptr<StereoBuffer<float>> fileData { nullptr };
2019-08-23 01:27:39 +02:00
2019-08-24 15:32:06 +02:00
Buffer<float> tempBuffer1;
Buffer<float> tempBuffer2;
Buffer<int> indexBuffer;
2019-08-24 15:50:55 +02:00
absl::Span<float> tempSpan1 { absl::MakeSpan(tempBuffer1) };
absl::Span<float> tempSpan2 { absl::MakeSpan(tempBuffer2) };
absl::Span<int> indexSpan { absl::MakeSpan(indexBuffer) };
2019-08-24 15:32:06 +02:00
2019-08-24 15:50:55 +02:00
int samplesPerBlock { config::defaultSamplesPerBlock };
double sampleRate { config::defaultSampleRate };
2019-08-24 15:32:06 +02:00
2019-08-24 15:50:55 +02:00
const CCValueArray& ccState;
2019-08-24 15:32:06 +02:00
ADSREnvelope<float> egEnvelope;
LEAK_DETECTOR(Voice);
2019-07-29 02:13:03 +02:00
};
2019-08-23 01:27:39 +02:00
2019-08-24 15:32:06 +02:00
} // namespace sfz