sfizz/sources/Synth.h

109 lines
4.2 KiB
C
Raw Normal View History

2019-08-30 00:49:58 +02:00
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2019-07-29 02:13:03 +02:00
#pragma once
2019-08-23 15:46:02 +02:00
#include "FilePool.h"
2019-07-29 02:13:03 +02:00
#include "Parser.h"
#include "Region.h"
#include "SfzHelpers.h"
2019-08-28 17:23:03 +02:00
#include "Helpers.h"
#include "StereoSpan.h"
2019-08-23 20:48:44 +02:00
#include "absl/types/span.h"
2019-08-25 14:01:03 +02:00
#include <chrono>
2019-07-29 02:13:03 +02:00
#include <optional>
2019-08-23 14:16:33 +02:00
#include <random>
2019-08-23 15:46:02 +02:00
#include <set>
2019-07-29 02:13:03 +02:00
#include <string_view>
#include <thread>
2019-08-23 15:46:02 +02:00
#include <vector>
using namespace std::literals;
2019-07-29 02:13:03 +02:00
2019-08-23 15:46:02 +02:00
namespace sfz {
2019-07-29 02:13:03 +02:00
2019-08-23 15:46:02 +02:00
class Synth : public Parser {
2019-07-29 02:13:03 +02:00
public:
2019-08-29 11:48:38 +02:00
Synth();
2019-07-29 15:50:15 +02:00
bool loadSfzFile(const std::filesystem::path& file) final;
2019-08-29 16:23:35 +02:00
int getNumRegions() const noexcept;
int getNumGroups() const noexcept;
int getNumMasters() const noexcept;
int getNumCurves() const noexcept;
const Region* getRegionView(int idx) const noexcept;
std::set<std::string_view> getUnknownOpcodes() const noexcept;
size_t getNumPreloadedSamples() const noexcept;
2019-08-23 01:27:39 +02:00
2019-08-29 16:23:35 +02:00
void setSamplesPerBlock(int samplesPerBlock) noexcept;
void setSampleRate(float sampleRate) noexcept;
void renderBlock(StereoSpan<float> buffer) noexcept;
2019-08-24 02:46:40 +02:00
2019-08-29 16:23:35 +02:00
void noteOn(int delay, int channel, int noteNumber, uint8_t velocity) noexcept;
void noteOff(int delay, int channel, int noteNumber, uint8_t velocity) noexcept;
void cc(int delay, int channel, int ccNumber, uint8_t ccValue) noexcept;
void pitchWheel(int delay, int channel, int pitch) noexcept;
void aftertouch(int delay, int channel, uint8_t aftertouch) noexcept;
void tempo(int delay, float secondsPerQuarter) noexcept;
2019-08-23 14:16:33 +02:00
2019-08-29 16:23:35 +02:00
void getNumActiveVoices() const noexcept;
void garbageCollect() noexcept;
2019-07-29 02:13:03 +02:00
protected:
2019-08-25 14:01:03 +02:00
void callback(std::string_view header, const std::vector<Opcode>& members) final;
2019-08-23 15:46:02 +02:00
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);
2019-08-29 11:48:38 +02:00
void buildRegion(const std::vector<Opcode>& regionOpcodes);
2019-07-29 02:13:03 +02:00
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;
2019-08-29 16:23:35 +02:00
Voice* findFreeVoice() noexcept;
2019-07-29 02:13:03 +02:00
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-08-23 01:27:39 +02:00
StereoBuffer<float> tempBuffer { config::defaultSamplesPerBlock };
int samplesPerBlock { config::defaultSamplesPerBlock };
float sampleRate { config::defaultSampleRate };
2019-08-23 15:46:02 +02:00
std::random_device rd {};
2019-08-23 14:16:33 +02:00
std::mt19937 randomGenerator { rd() };
std::uniform_real_distribution<float> randNoteDistribution { 0, 1 };
2019-08-29 11:05:28 +02:00
LEAK_DETECTOR(Synth);
2019-07-29 02:13:03 +02:00
};
}