sfizz/sources/Synth.h

86 lines
2.9 KiB
C
Raw Normal View History

2019-07-29 02:13:03 +02:00
#pragma once
#include "Parser.h"
#include "Region.h"
#include "SfzHelpers.h"
2019-08-04 01:38:31 +02:00
#include "FilePool.h"
2019-07-29 02:13:03 +02:00
#include <vector>
#include <set>
2019-07-29 02:13:03 +02:00
#include <optional>
#include <string_view>
namespace sfz
{
class Synth: public Parser
{
public:
2019-08-23 01:27:39 +02:00
Synth()
{
for (int i = 0; i < config::numVoices; ++i)
voices.push_back(std::make_unique<Voice>());
}
2019-07-29 15:50:15 +02:00
bool loadSfzFile(const std::filesystem::path& file) final;
2019-08-04 16:07:09 +02:00
int getNumRegions() const noexcept { return static_cast<int>(regions.size()); }
2019-07-29 02:13:03 +02:00
int getNumGroups() const noexcept { return numGroups; }
int getNumMasters() const noexcept { return numMasters; }
int getNumCurves() const noexcept { return numCurves; }
2019-08-04 01:38:31 +02:00
const Region* getRegionView(int idx) const noexcept { return (size_t)idx < regions.size() ? regions[idx].get() : nullptr; }
auto getUnknownOpcodes() { return unknownOpcodes; }
2019-08-04 16:07:09 +02:00
size_t getNumPreloadedSamples() { return filePool.getNumPreloadedSamples(); }
2019-08-23 01:27:39 +02:00
void prepareToPlay(int samplesPerBlock, double sampleRate)
{
this->samplesPerBlock = samplesPerBlock;
this->sampleRate = sampleRate;
this->tempBuffer = StereoBuffer<float>(samplesPerBlock);
}
void renderBlock(StereoBuffer<float>& buffer)
{
for (auto& voice: voices)
{
voice->renderBlock(tempBuffer);
buffer.add(tempBuffer);
}
}
void noteOn(int delay, int channel, int noteNumber, uint8_t velocity);
void noteOff(int delay, int channel, int noteNumber, uint8_t velocity);
void cc(int delay, int channel, int ccNumber, uint8_t ccValue);
void pitchWheel(int delay, int channel, int pitch);
void aftertouch(int delay, int channel, uint8_t aftertouch);
void tempo(int delay, float secondsPerQuarter);
2019-07-29 02:13:03 +02:00
protected:
2019-07-29 15:50:15 +02:00
void callback(std::string_view header, std::vector<Opcode> members) final;
2019-07-29 02:13:03 +02:00
private:
bool hasGlobal { false };
bool hasControl { false };
int numGroups { 0 };
int numMasters { 0 };
int numCurves { 0 };
void clear();
void handleGlobalOpcodes(const std::vector<Opcode>& members);
void handleControlOpcodes(const std::vector<Opcode>& members);
std::vector<Opcode> globalOpcodes;
std::vector<Opcode> masterOpcodes;
std::vector<Opcode> groupOpcodes;
2019-08-04 01:38:31 +02:00
FilePool filePool;
2019-07-29 02:13:03 +02:00
CCValueArray ccState;
std::vector<CCNamePair> ccNames;
std::optional<uint8_t> defaultSwitch;
std::set<std::string_view> unknownOpcodes;
2019-08-23 01:27:39 +02:00
using RegionPtrVector = std::vector<Region*>;
std::vector<std::unique_ptr<Region>> regions;
std::vector<std::unique_ptr<Voice>> voices;
2019-08-04 01:38:31 +02:00
std::array<RegionPtrVector, 128> noteActivationLists;
std::array<RegionPtrVector, 128> ccActivationLists;
2019-07-29 02:13:03 +02:00
void buildRegion(const std::vector<Opcode>& regionOpcodes);
2019-08-23 01:27:39 +02:00
StereoBuffer<float> tempBuffer { config::defaultSamplesPerBlock };
int samplesPerBlock { config::defaultSamplesPerBlock };
float sampleRate { config::defaultSampleRate };
2019-07-29 02:13:03 +02:00
};
}