gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

processor.h (6872B)


      1 #pragma once
      2 
      3 #include <juce_audio_processors/juce_audio_processors.h>
      4 #include <juce_audio_devices/juce_audio_devices.h>
      5 
      6 #include "bypassBuffer.h"
      7 #include "controller.h"
      8 #include "midiports.h"
      9 
     10 #include "bridgeLib/types.h"
     11 
     12 #include "synthLib/plugin.h"
     13 
     14 namespace bridgeClient
     15 {
     16 	class RemoteDevice;
     17 }
     18 
     19 namespace bridgeLib
     20 {
     21 	struct PluginDesc;
     22 }
     23 
     24 namespace baseLib
     25 {
     26 	class BinaryStream;
     27 	class ChunkReader;
     28 }
     29 
     30 namespace synthLib
     31 {
     32 	struct DeviceCreateParams;
     33 	class Plugin;
     34 	struct SMidiEvent;
     35 }
     36 
     37 namespace pluginLib
     38 {
     39 	class Processor : public juce::AudioProcessor
     40 	{
     41 	public:
     42 		struct BinaryDataRef
     43 		{
     44 			uint32_t listSize = 0;
     45 			const char** originalFileNames = nullptr;
     46 			const char** namedResourceList = nullptr;
     47 			const char* (*getNamedResourceFunc)(const char*, int&) = nullptr;
     48 		};
     49 
     50 		struct Properties
     51 		{
     52 			const std::string name;
     53 			const std::string vendor;
     54 			const bool isSynth;
     55 			const bool wantsMidiInput;
     56 			const bool producesMidiOut;
     57 			const bool isMidiEffect;
     58 			const std::string plugin4CC;
     59 			const std::string lv2Uri;
     60 			BinaryDataRef binaryData;
     61 		};
     62 
     63 		Processor(const BusesProperties& _busesProperties, Properties _properties);
     64 		~Processor() override;
     65 
     66 		void addMidiEvent(const synthLib::SMidiEvent& ev);
     67 
     68 		void handleIncomingMidiMessage(juce::MidiInput* _source, const juce::MidiMessage& _message);
     69 
     70 	    Controller& getController();
     71 		bool isPluginValid() { return getPlugin().isValid(); }
     72 
     73 		synthLib::Plugin& getPlugin();
     74 
     75 		virtual synthLib::Device* createDevice() = 0;
     76 		virtual bridgeClient::RemoteDevice* createRemoteDevice(const synthLib::DeviceCreateParams& _params);
     77 		virtual void getRemoteDeviceParams(synthLib::DeviceCreateParams& _params) const;
     78 		virtual bridgeClient::RemoteDevice* createRemoteDevice();
     79 		synthLib::Device* createDevice(DeviceType _type);
     80 
     81 		bool hasController() const
     82 		{
     83 			return m_controller.get();
     84 		}
     85 
     86 		virtual bool setLatencyBlocks(uint32_t _blocks);
     87 		virtual void updateLatencySamples();
     88 
     89 		virtual void saveCustomData(std::vector<uint8_t>& _targetBuffer);
     90 		virtual void saveChunkData(baseLib::BinaryStream& s);
     91 		virtual bool loadCustomData(const std::vector<uint8_t>& _sourceBuffer);
     92 		virtual void loadChunkData(baseLib::ChunkReader& _cr);
     93 
     94 		void readGain(baseLib::BinaryStream& _s);
     95 
     96 		template<size_t N> void applyOutputGain(std::array<float*, N>& _buffers, const size_t _numSamples)
     97 		{
     98 			applyGain(_buffers, _numSamples, getOutputGain());
     99 		}
    100 
    101 		template<size_t N> static void applyGain(std::array<float*, N>& _buffers, const size_t _numSamples, const float _gain)
    102 		{
    103 			if(_gain == 1.0f)
    104 				return;
    105 
    106 			if(!_numSamples)
    107 				return;
    108 
    109 			for (float* buf : _buffers)
    110 			{
    111 				if (buf)
    112 				{
    113 					for (size_t i = 0; i < _numSamples; ++i)
    114 						buf[i] *= _gain;
    115 				}
    116 			}
    117 		}
    118 		
    119 		float getOutputGain() const { return m_outputGain; }
    120 		void setOutputGain(const float _gain) { m_outputGain = _gain; }
    121 		
    122 		bool setDspClockPercent(uint32_t _percent = 100);
    123 		uint32_t getDspClockPercent() const;
    124 		uint64_t getDspClockHz() const;
    125 
    126 		bool setPreferredDeviceSamplerate(float _samplerate);
    127 		float getPreferredDeviceSamplerate() const;
    128 		std::vector<float> getDeviceSupportedSamplerates() const;
    129 		std::vector<float> getDevicePreferredSamplerates() const;
    130 
    131 		float getHostSamplerate() const { return m_hostSamplerate; }
    132 
    133 		const Properties& getProperties() const { return m_properties; }
    134 
    135 		virtual void processBpm(float _bpm) {}
    136 
    137 		bool rebootDevice();
    138 
    139 		auto& getMidiPorts() { return m_midiPorts; }
    140 
    141 		std::optional<std::pair<const char*, uint32_t>> findResource(const std::string& _filename) const;
    142 
    143 		std::string getDataFolder(bool _useFxFolder = false) const;
    144 		std::string getPublicRomFolder() const;
    145 		std::string getConfigFolder(bool _useFxFolder = false) const;
    146 		std::string getPatchManagerDataFolder(bool _useFxFolder = false) const;
    147 		std::string getConfigFile(bool _useFxFolder = false) const;
    148 		std::string getProductName(bool _useFxName = false) const;
    149 
    150 		void getPluginDesc(bridgeLib::PluginDesc& _desc) const;
    151 
    152 		void setDeviceType(DeviceType _type, bool _forceChange = false);
    153 		void setRemoteDevice(const std::string& _host, uint32_t _port);
    154 		const auto& getRemoteDeviceHost() const { return m_remoteHost; }
    155 		const auto& getRemoteDevicePort() const { return m_remotePort; }
    156 
    157 		auto getDeviceType() const { return m_deviceType; }
    158 
    159 	protected:
    160 		void destroyController();
    161 
    162 	private:
    163 		void prepareToPlay(double sampleRate, int maximumExpectedSamplesPerBlock) override;
    164 		void releaseResources() override;
    165 
    166 		//==============================================================================
    167 		bool isBusesLayoutSupported(const BusesLayout&) const override;
    168 	    void getStateInformation (juce::MemoryBlock& destData) override;
    169 	    void setStateInformation (const void* _data, int _sizeInBytes) override;
    170 	    void getCurrentProgramStateInformation (juce::MemoryBlock& destData) override;
    171 	    void setCurrentProgramStateInformation (const void* data, int sizeInBytes) override;
    172 		const juce::String getName() const override;
    173 		bool acceptsMidi() const override;
    174 		bool producesMidi() const override;
    175 		bool isMidiEffect() const override;
    176 		void processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages) override;
    177 		void processBlockBypassed(juce::AudioBuffer<float>& _buffer, juce::MidiBuffer& _midiMessages) override;
    178 
    179 #if !SYNTHLIB_DEMO_MODE
    180 		void setState(const void *_data, size_t _sizeInBytes);
    181 #endif
    182 
    183 	    //==============================================================================
    184 		int getNumPrograms() override;
    185 		int getCurrentProgram() override;
    186 		void setCurrentProgram(int _index) override;
    187 		const juce::String getProgramName(int _index) override;
    188 		void changeProgramName(int _index, const juce::String &_newName) override;
    189 
    190 	    //==============================================================================
    191 		double getTailLengthSeconds() const override;
    192 		//==============================================================================
    193 		virtual Controller *createController() = 0;
    194 
    195 	    std::unique_ptr<Controller> m_controller{};
    196 
    197 		synthLib::DeviceError getDeviceError() const { return m_deviceError; }
    198 
    199 		synthLib::Device* onDeviceInvalid(synthLib::Device* _device);
    200 
    201 	protected:
    202 		synthLib::DeviceError m_deviceError = synthLib::DeviceError::None;
    203 		std::unique_ptr<synthLib::Device> m_device;
    204 		std::unique_ptr<synthLib::Plugin> m_plugin;
    205 		std::vector<synthLib::SMidiEvent> m_midiOut;
    206 
    207 	private:
    208 		const Properties m_properties;
    209 		float m_outputGain = 1.0f;
    210 		float m_inputGain = 1.0f;
    211 		uint32_t m_dspClockPercent = 100;
    212 		float m_preferredDeviceSamplerate = 0.0f;
    213 		float m_hostSamplerate = 0.0f;
    214 		MidiPorts m_midiPorts;
    215 		BypassBuffer m_bypassBuffer;
    216 		DeviceType m_deviceType = DeviceType::Local;
    217 		std::string m_remoteHost;
    218 		uint32_t m_remotePort = 0;
    219 		bridgeLib::SessionId m_remoteSessionId;
    220 	};
    221 }