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 	 * Restores a parameter to the given preset.
98 	 * Returns an errorcode on failure.
99 	 */
100 	public abstract int writeParam_int(uint presetID, uint paramID, int value) nothrow;
101 	/**
102 	 * Restores a parameter to the given preset.
103 	 * Returns an errorcode on failure.
104 	 */
105 	public abstract int writeParam_long(uint presetID, uint paramID, long value) nothrow;
106 	/**
107 	 * Restores a parameter to the given preset.
108 	 * Returns an errorcode on failure.
109 	 */
110 	public abstract int writeParam_double(uint presetID, uint paramID, double value) nothrow;
111 	/**
112 	 * Restores a parameter to the given preset.
113 	 * Returns an errorcode on failure.
114 	 */
115 	public abstract int writeParam_string(uint presetID, uint paramID, string value) nothrow;
116 	/** 
117 	 * Returns all the possible parameters this module has.
118 	 */
119 	public abstract MValue[] getParameters() nothrow;
120 	/** 
121 	 * Reads the given value (int).
122 	 * Params:
123 	 *   presetID = The preset ID, or uint.max for global module values.
124 	 *   paramID = The parameter ID.
125 	 * Returns: The value of the given preset and parameter
126 	 */
127 	public abstract int readParam_int(uint presetID, uint paramID) nothrow;
128 	/** 
129 	 * Reads the given value (int).
130 	 * Params:
131 	 *   presetID = The preset ID, or uint.max for global module values.
132 	 *   paramID = The parameter ID.
133 	 * Returns: The value of the given preset and parameter
134 	 */
135 	public abstract long readParam_long(uint presetID, uint paramID) nothrow;
136 	/** 
137 	 * Reads the given value (int).
138 	 * Params:
139 	 *   presetID = The preset ID, or uint.max for global module values.
140 	 *   paramID = The parameter ID.
141 	 * Returns: The value of the given preset and parameter
142 	 */
143 	public abstract double readParam_double(uint presetID, uint paramID) nothrow;
144 	/** 
145 	 * Reads the given value (int).
146 	 * Params:
147 	 *   presetID = The preset ID, or uint.max for global module values.
148 	 *   paramID = The parameter ID.
149 	 * Returns: The value of the given preset and parameter
150 	 */
151 	public abstract string readParam_string(uint presetID, uint paramID) nothrow;
152 }