1 module pixelperfectengine.audio.base.modulebase;
2 
3 import std.bitmanip;
4 import collections.sortedlist;
5 import midi2.types.structs;
6 
7 public import pixelperfectengine.audio.base.types;
8 public import pixelperfectengine.audio.base.handler;
9 
10 
11 /*
12  * Copyright (C) 2015-2021, by Laszlo Szeremi under the Boost license.
13  *
14  * Pixel Perfect Engine, audio.base.modulebase module.
15  */
16 
17 /**
18  * Implements the base class for all audio modules.
19  */
20 public abstract class AudioModule {
21 	/**
22 	 * Contains all data related to module info.
23 	 */
24 	public struct ModuleInfo {
25 		public ubyte		nOfAudioInput;		///Number of audio input channels
26 		public ubyte		nOfAudioOutput;		///Number of audio output channels
27 		mixin(bitfields!(
28 			bool, "isInstrument", 1,
29 			bool, "isEffect", 1,
30 			bool, "midiSendback", 1,
31 			bool, "hasMidiIn", 1,
32 			bool, "hasMidiOut", 1,
33 			uint, "reserved", 11,
34 		));
35 		public string[]		inputChNames;		///Names of the input channels
36 		public string[]		outputChNames;		///Names of the output channels
37     }
38 	protected size_t		bufferSize;			///The size of the output buffers (must kept as a constant)
39 	protected int			sampleRate;			///The sample rate that the audio subsystem runs at
40 	protected ModuleInfo	info;				///Basic info about the plugin
41 	protected ModuleManager	handler;			///The main audio handler, also MIDI outs can be passed there
42 	alias StreamIDSet = SortedList!(ubyte, "a < b", false);
43 	protected StreamIDSet	enabledInputs;		///List of enabled input channel numbers
44 	protected StreamIDSet	enabledOutputs;		///List of enabled output channel numbers
45 	public @nogc nothrow void delegate(UMP data0, uint data1 = 0, uint data2 = 0, uint data3 = 0)	midiOut;	///A delegate where MIDI messages are being routed
46 	/**
47 	 * Returns the basic informations about this module.
48 	 */
49 	public ModuleInfo getInfo() @nogc @safe pure nothrow {
50 		return info;
51 	}
52 	/**
53 	 * Returns the current sample rate.
54 	 */
55 	public int getSamplerate() @nogc @safe pure nothrow const {
56 		return sampleRate;
57 	}
58 	/**
59 	 * Sets the module up.
60 	 *
61 	 * Can be overridden in child classes to allow resets.
62 	 */
63 	public void moduleSetup(ubyte[] inputs, ubyte[] outputs, int sampleRate, size_t bufferSize, ModuleManager handler) 
64 			@safe nothrow {
65 		enabledInputs = StreamIDSet(inputs);
66 		enabledOutputs = StreamIDSet(outputs);
67 		this.sampleRate = sampleRate;
68 		this.bufferSize = bufferSize;
69 		this.handler = handler;
70 	}
71 	/**
72 	 * MIDI 2.0 data received here.
73 	 *
74 	 * data0: Header of the up to 128 bit MIDI 2.0 data.
75 	 * data1-3: Other packets if needed.
76 	 */
77 	public abstract void midiReceive(UMP data0, uint data1 = 0, uint data2 = 0, uint data3 = 0) @nogc nothrow;
78 	/**
79 	 * Renders the current audio frame.
80 	 * 
81 	 * input: the input buffers if any, null if none.
82 	 * output: the output buffers if any, null if none.
83 	 *
84 	 * NOTE: Buffers must have matching sizes.
85 	 */
86 	public abstract void renderFrame(float*[] input, float*[] output) @nogc nothrow;
87 	/**
88 	 * Receives waveform data that has been loaded from disk for reading. Returns zero if successful, or a specific 
89 	 * errorcode.
90 	 *
91 	 * id: The ID of the waveform.
92 	 * rawData: The data itself, in unprocessed form.
93 	 * format: The format of the wave data, including the data type, bit depth, base sampling rate
94 	 */
95 	public abstract int waveformDataReceive(uint id, ubyte[] rawData, WaveFormat format) nothrow;
96 	/** 
97 	 * Creates a new waveform from an existing one using slicing.
98 	 * Params:
99 	 *   id = The ID of the new sample.
100 	 *   src = The ID of the original sample.
101 	 *   pos = The position where the slice begins.
102 	 *   length = The length of the slice.
103 	 * Returns: 0 on success, -1 if module don't support this feature, -2 if slice is out of bounds (longer than the
104 	 * sample, etc.), -3 if sample is not slicable (ADPCM, etc.).
105 	 */
106 	public int waveformSlice(uint id, uint src, uint pos, uint length) nothrow {
107 		return -1;
108 	}
109 	/** 
110 	 * Returns the waveform data from the
111 	 * Params:
112 	 *   id = The ID of the waveform.
113 	 * Returns: The raw waveform data, or null on error (unsupported feature, waveform not found, etc.)
114 	 */
115 	public const(ubyte)[] getWaveformData(uint id) nothrow {
116 		return null;
117 	}
118 	/** 
119 	 * Returns the format of the selected waveform
120 	 * Params:
121 	 *   id = The ID of the waveform.
122 	 * Returns: The format of the waveform data, or WaveFormat.init if not available.
123 	 */
124 	public WaveFormat getWaveformDataFormat(uint id) nothrow {
125 		return WaveFormat.init;
126 	}
127 	///Returns the available waveform ID list
128 	public uint[] getWaveformIDList() nothrow {
129 		return null;
130 	}
131 	///Returns the list of internal waveform IDs if there are any.
132 	public uint[] getInternalWaveformIDList() nothrow {
133 		return null;
134 	}
135 	///Returns the names of the internal waveforms if there are any.
136 	public string[] getInternalWaveformNames() nothrow {
137 		return null;
138 	}
139 	/**
140 	 * Restores a parameter to the given preset.
141 	 * Returns an errorcode on failure.
142 	 */
143 	public abstract int writeParam_int(uint presetID, uint paramID, int value) nothrow;
144 	/**
145 	 * Restores a parameter to the given preset.
146 	 * Returns an errorcode on failure.
147 	 */
148 	public abstract int writeParam_long(uint presetID, uint paramID, long value) nothrow;
149 	/**
150 	 * Restores a parameter to the given preset.
151 	 * Returns an errorcode on failure.
152 	 */
153 	public abstract int writeParam_double(uint presetID, uint paramID, double value) nothrow;
154 	/**
155 	 * Restores a parameter to the given preset.
156 	 * Returns an errorcode on failure.
157 	 */
158 	public abstract int writeParam_string(uint presetID, uint paramID, string value) nothrow;
159 	/** 
160 	 * Returns all the possible parameters this module has.
161 	 */
162 	public abstract MValue[] getParameters() nothrow;
163 	/** 
164 	 * Reads the given value (int).
165 	 * Params:
166 	 *   presetID = The preset ID, or uint.max for global module values.
167 	 *   paramID = The parameter ID.
168 	 * Returns: The value of the given preset and parameter
169 	 */
170 	public abstract int readParam_int(uint presetID, uint paramID) nothrow;
171 	/** 
172 	 * Reads the given value (int).
173 	 * Params:
174 	 *   presetID = The preset ID, or uint.max for global module values.
175 	 *   paramID = The parameter ID.
176 	 * Returns: The value of the given preset and parameter
177 	 */
178 	public abstract long readParam_long(uint presetID, uint paramID) nothrow;
179 	/** 
180 	 * Reads the given value (int).
181 	 * Params:
182 	 *   presetID = The preset ID, or uint.max for global module values.
183 	 *   paramID = The parameter ID.
184 	 * Returns: The value of the given preset and parameter
185 	 */
186 	public abstract double readParam_double(uint presetID, uint paramID) nothrow;
187 	/** 
188 	 * Reads the given value (int).
189 	 * Params:
190 	 *   presetID = The preset ID, or uint.max for global module values.
191 	 *   paramID = The parameter ID.
192 	 * Returns: The value of the given preset and parameter
193 	 */
194 	public abstract string readParam_string(uint presetID, uint paramID) nothrow;
195 }