1 module pixelperfectengine.audio.base.modulebase;
2 
3 import std.bitmanip;
4 import collections.sortedlist;
5 
6 public import pixelperfectengine.audio.base.types;
7 public import pixelperfectengine.audio.base.handler;
8 
9 
10 /*
11  * Copyright (C) 2015-2021, by Laszlo Szeremi under the Boost license.
12  *
13  * Pixel Perfect Engine, audio.base.modulebase module.
14  */
15 
16 /**
17  * Implements the base class for all audio modules.
18  */
19 public abstract class AudioModule {
20 	/**
21 	 * Contains all data related to module info.
22 	 */
23 	public struct ModuleInfo {
24 		public ubyte		nOfAudioInput;		///Number of audio input channels
25 		public ubyte		nOfAudioOutput;		///Number of audio output channels
26 		mixin(bitfields!(
27 			bool, "isInstrument", 1,
28 			bool, "isEffect", 1,
29 			bool, "midiSendback", 1,
30 			bool, "hasMidiIn", 1,
31 			bool, "hasMidiOut", 1,
32 			uint, "reserved", 11,
33 		));
34 		public string[]		inputChNames;		///Names of the input channels
35 		public string[]		outputChNames;		///Names of the output channels
36     }
37 	protected size_t		bufferSize;			///The size of the output buffers (must kept as a constant)
38 	protected int			sampleRate;			///The sample rate that the audio subsystem runs at
39 	protected ModuleInfo	info;				///Basic info about the plugin
40 	protected ModuleManager	handler;			///The main audio handler, also MIDI outs can be passed there
41 	alias StreamIDSet = SortedList!(ubyte, "a < b", false);
42 	protected StreamIDSet	enabledInputs;		///List of enabled input channel numbers
43 	protected StreamIDSet	enabledOutputs;		///List of enabled output channel numbers
44 	public @nogc nothrow void delegate(uint[4] data, uint offset)	midiOut;	///A delegate where MIDI messages are being routed
45 	/**
46 	 * Returns the basic informations about this module.
47 	 */
48 	public ModuleInfo getInfo() @nogc @safe pure nothrow {
49 		return info;
50 	}
51 	/**
52 	 * Returns the current sample rate.
53 	 */
54 	public int getSamplerate() @nogc @safe pure nothrow const {
55 		return sampleRate;
56 	}
57 	/**
58 	 * Sets the module up.
59 	 *
60 	 * Can be overridden in child classes to allow resets.
61 	 */
62 	public void moduleSetup(ubyte[] inputs, ubyte[] outputs, int sampleRate, size_t bufferSize) @safe {
63 		enabledInputs = StreamIDSet(inputs);
64 		enabledOutputs = StreamIDSet(outputs);
65 		this.sampleRate = sampleRate;
66 		this.bufferSize = bufferSize;
67 	}
68 	/**
69 	 * MIDI 2.0 data received here.
70 	 *
71 	 * data: up to 128 bits of MIDI 2.0 commands. Any packets that are shorter should be padded with zeros.
72 	 * offset: time offset of the command. This can reduce jitter caused by the asynchronous operation of the 
73 	 * sequencer and the audio plugin system.
74 	 */
75 	public abstract void midiReceive(uint[4] data, uint offset) @nogc nothrow;
76 	/**
77 	 * Renders the current audio frame.
78 	 * 
79 	 * input: the input buffers if any, null if none.
80 	 * output: the output buffers if any, null if none.
81 	 * length: the lenth of the current audio frame in samples.
82 	 *
83 	 * NOTE: Buffers must have matching sizes.
84 	 */
85 	public abstract void renderFrame(float*[] input, float*[] output) @nogc nothrow;
86 	/**
87 	 * Receives waveform data that has been loaded from disk for reading. Returns zero if successful, or a specific 
88 	 * errorcode.
89 	 *
90 	 * id: The ID of the waveform.
91 	 * rawData: The data itself, in unprocessed form.
92 	 * format: The format of the wave data, including the data type, bit depth, base sampling rate
93 	 *
94 	 * Note: This function needs the audio system to be unlocked.
95 	 */
96 	public abstract int waveformDataReceive(uint id, ubyte[] rawData, WaveFormat format) nothrow;
97 	/**
98 	 * Restores a parameter to the given preset.
99 	 * Returns an errorcode on failure.
100 	 */
101 	public abstract int recallParam_int(uint presetID, uint paramID, int value) @nogc nothrow;
102 	/**
103 	 * Restores a parameter to the given preset.
104 	 * Returns an errorcode on failure.
105 	 */
106 	public abstract int recallParam_uint(uint presetID, uint paramID, uint value) @nogc nothrow;
107 	/**
108 	 * Restores a parameter to the given preset.
109 	 * Returns an errorcode on failure.
110 	 */
111 	public abstract int recallParam_double(uint presetID, uint paramID, double value) @nogc nothrow;
112 	/**
113 	 * Restores a parameter to the given preset.
114 	 * Returns an errorcode on failure.
115 	 */
116 	public abstract int recallParam_string(uint presetID, uint paramID, string value) @nogc nothrow;
117 }