sfizz/src/Synth.h

119 lines
4.5 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 "LeakDetector.h"
#include "MidiState.h"
#include "AudioSpan.h"
2019-08-23 20:48:44 +02:00
#include "absl/types/span.h"
#include <absl/types/optional.h>
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>
2019-08-23 15:46:02 +02:00
#include <vector>
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-11-20 23:45:44 +01:00
Synth();
2019-11-20 22:35:51 +01:00
Synth(int numVoices);
2019-09-21 13:01:50 +02:00
bool loadSfzFile(const fs::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<absl::string_view> getUnknownOpcodes() const noexcept;
2019-08-29 16:23:35 +02:00
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;
2019-11-20 22:13:56 +01:00
float getVolume() const noexcept;
void setVolume(float volume) noexcept;
void renderBlock(AudioSpan<float> buffer) noexcept;
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
int getNumActiveVoices() const noexcept;
2019-11-20 22:35:51 +01:00
int getNumVoices() const noexcept;
void setNumVoices(int numVoices) noexcept;
2019-08-29 16:23:35 +02:00
void garbageCollect() noexcept;
2019-07-29 02:13:03 +02:00
protected:
void callback(absl::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();
2019-11-20 22:35:51 +01:00
void resetVoices(int numVoices);
2019-07-29 02:13:03 +02:00
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;
MidiState midiState;
2019-08-29 16:23:35 +02:00
Voice* findFreeVoice() noexcept;
2019-07-29 02:13:03 +02:00
std::vector<CCNamePair> ccNames;
absl::optional<uint8_t> defaultSwitch;
std::set<absl::string_view> unknownOpcodes;
2019-08-23 01:27:39 +02:00
using RegionPtrVector = std::vector<Region*>;
using VoicePtrVector = std::vector<Voice*>;
2019-08-23 01:27:39 +02:00
std::vector<std::unique_ptr<Region>> regions;
std::vector<std::unique_ptr<Voice>> voices;
VoicePtrVector voiceViewArray;
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
AudioBuffer<float> tempBuffer { 2, config::defaultSamplesPerBlock };
2019-08-23 01:27:39 +02:00
int samplesPerBlock { config::defaultSamplesPerBlock };
float sampleRate { config::defaultSampleRate };
2019-11-20 22:13:56 +01:00
float volume { Default::volume };
std::uniform_real_distribution<float> randNoteDistribution { 0, 1 };
unsigned fileTicket { 1 };
2019-08-29 11:05:28 +02:00
std::atomic<bool> canEnterCallback { true };
std::atomic<bool> inCallback { false };
2019-11-20 22:35:51 +01:00
int numVoices { config::numVoices };
LEAK_DETECTOR(Synth);
2019-07-29 02:13:03 +02:00
};
}