@fluex/fluexgl-dsp 0.1.3 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/README.md +86 -86
  2. package/lib/dist/console-codes.d.ts +13 -13
  3. package/lib/dist/console-codes.js +16 -16
  4. package/lib/dist/core/classes/AudioClip.d.ts +67 -62
  5. package/lib/dist/core/classes/AudioClip.d.ts.map +1 -1
  6. package/lib/dist/core/classes/AudioClip.js +406 -386
  7. package/lib/dist/core/classes/AudioDevice.d.ts +12 -12
  8. package/lib/dist/core/classes/AudioDevice.js +30 -30
  9. package/lib/dist/core/classes/Channel.d.ts +41 -29
  10. package/lib/dist/core/classes/Channel.d.ts.map +1 -1
  11. package/lib/dist/core/classes/Channel.js +210 -144
  12. package/lib/dist/core/classes/DpsPipeline.d.ts +59 -59
  13. package/lib/dist/core/classes/DpsPipeline.js +115 -115
  14. package/lib/dist/core/classes/Effector.d.ts +8 -11
  15. package/lib/dist/core/classes/Effector.d.ts.map +1 -1
  16. package/lib/dist/core/classes/Effector.js +8 -20
  17. package/lib/dist/core/classes/Master.d.ts +11 -11
  18. package/lib/dist/core/classes/Master.js +35 -35
  19. package/lib/dist/core/exports.d.ts +6 -6
  20. package/lib/dist/core/exports.js +6 -6
  21. package/lib/dist/effects/classes/Chorus.d.ts +19 -2
  22. package/lib/dist/effects/classes/Chorus.d.ts.map +1 -1
  23. package/lib/dist/effects/classes/Chorus.js +53 -2
  24. package/lib/dist/effects/classes/Distortion.d.ts +6 -2
  25. package/lib/dist/effects/classes/Distortion.d.ts.map +1 -1
  26. package/lib/dist/effects/classes/Distortion.js +9 -2
  27. package/lib/dist/effects/classes/Equalizer.d.ts +2 -2
  28. package/lib/dist/effects/classes/Equalizer.js +2 -2
  29. package/lib/dist/effects/classes/Limiter.d.ts +2 -2
  30. package/lib/dist/effects/classes/Limiter.js +2 -2
  31. package/lib/dist/effects/classes/Saturation.d.ts +2 -2
  32. package/lib/dist/effects/classes/Saturation.d.ts.map +1 -1
  33. package/lib/dist/effects/classes/Saturation.js +2 -2
  34. package/lib/dist/effects/classes/SoftClip.d.ts +12 -7
  35. package/lib/dist/effects/classes/SoftClip.d.ts.map +1 -1
  36. package/lib/dist/effects/classes/SoftClip.js +54 -38
  37. package/lib/dist/effects/classes/StereoPanner.d.ts +3 -2
  38. package/lib/dist/effects/classes/StereoPanner.d.ts.map +1 -1
  39. package/lib/dist/effects/classes/StereoPanner.js +3 -2
  40. package/lib/dist/effects/classes/compressors/Compressor.d.ts +2 -2
  41. package/lib/dist/effects/classes/compressors/Compressor.js +2 -2
  42. package/lib/dist/effects/classes/compressors/MultibandCompressor.d.ts +2 -2
  43. package/lib/dist/effects/classes/compressors/MultibandCompressor.js +2 -2
  44. package/lib/dist/effects/classes/delays/AdvancedDelay.d.ts +2 -2
  45. package/lib/dist/effects/classes/delays/AdvancedDelay.js +2 -2
  46. package/lib/dist/effects/classes/delays/MonoDelay.d.ts +2 -2
  47. package/lib/dist/effects/classes/delays/MonoDelay.js +2 -2
  48. package/lib/dist/effects/classes/delays/PingPongDelay.d.ts +2 -2
  49. package/lib/dist/effects/classes/delays/PingPongDelay.js +2 -2
  50. package/lib/dist/effects/classes/delays/StereoDelay.d.ts +2 -2
  51. package/lib/dist/effects/classes/delays/StereoDelay.js +2 -2
  52. package/lib/dist/effects/classes/filters/LowPassFilter.d.ts +14 -0
  53. package/lib/dist/effects/classes/filters/LowPassFilter.d.ts.map +1 -0
  54. package/lib/dist/effects/classes/filters/LowPassFilter.js +38 -0
  55. package/lib/dist/effects/classes/reverbs/ChamberReverb.d.ts +2 -2
  56. package/lib/dist/effects/classes/reverbs/ChamberReverb.js +2 -2
  57. package/lib/dist/effects/classes/reverbs/ConvolverReverb.d.ts +2 -2
  58. package/lib/dist/effects/classes/reverbs/ConvolverReverb.js +2 -2
  59. package/lib/dist/effects/classes/reverbs/GenericReverb.d.ts +2 -2
  60. package/lib/dist/effects/classes/reverbs/GenericReverb.js +2 -2
  61. package/lib/dist/effects/classes/reverbs/HallReverb.d.ts +2 -2
  62. package/lib/dist/effects/classes/reverbs/HallReverb.js +2 -2
  63. package/lib/dist/effects/classes/reverbs/RoomReverb.d.ts +2 -2
  64. package/lib/dist/effects/classes/reverbs/RoomReverb.js +2 -2
  65. package/lib/dist/effects/exports.d.ts +19 -19
  66. package/lib/dist/effects/exports.d.ts.map +1 -1
  67. package/lib/dist/effects/exports.js +19 -19
  68. package/lib/dist/index.d.ts +12 -12
  69. package/lib/dist/index.d.ts.map +1 -1
  70. package/lib/dist/index.js +28 -28
  71. package/lib/dist/typings.d.ts +97 -59
  72. package/lib/dist/typings.d.ts.map +1 -1
  73. package/lib/dist/typings.js +30 -1
  74. package/lib/dist/utilities/constants.d.ts +1 -1
  75. package/lib/dist/utilities/constants.js +9 -9
  76. package/lib/dist/utilities/debugger.d.ts +7 -7
  77. package/lib/dist/utilities/debugger.js +49 -49
  78. package/lib/dist/utilities/helpers.d.ts +46 -40
  79. package/lib/dist/utilities/helpers.d.ts.map +1 -1
  80. package/lib/dist/utilities/helpers.js +186 -143
  81. package/lib/dist/utilities/web-assembly.d.ts +3 -3
  82. package/lib/dist/utilities/web-assembly.d.ts.map +1 -1
  83. package/lib/dist/utilities/web-assembly.js +12 -12
  84. package/lib/src/console-codes.ts +15 -15
  85. package/lib/src/core/classes/AudioClip.ts +539 -539
  86. package/lib/src/core/classes/AudioDevice.ts +38 -38
  87. package/lib/src/core/classes/Channel.ts +288 -288
  88. package/lib/src/core/classes/DpsPipeline.ts +152 -152
  89. package/lib/src/core/classes/Effector.ts +11 -12
  90. package/lib/src/core/classes/Master.ts +49 -49
  91. package/lib/src/core/exports.ts +5 -5
  92. package/lib/src/effects/classes/Chorus.ts +65 -111
  93. package/lib/src/effects/classes/Distortion.ts +13 -13
  94. package/lib/src/effects/classes/Saturation.ts +2 -2
  95. package/lib/src/effects/classes/SoftClip.ts +60 -44
  96. package/lib/src/effects/classes/StereoPanner.ts +2 -2
  97. package/lib/src/effects/classes/filters/LowPassFilter.ts +54 -0
  98. package/lib/src/effects/exports.ts +23 -22
  99. package/lib/src/index.ts +98 -93
  100. package/lib/src/typings.ts +113 -78
  101. package/lib/src/utilities/constants.ts +8 -8
  102. package/lib/src/utilities/debugger.ts +65 -65
  103. package/lib/src/utilities/helpers.ts +265 -237
  104. package/lib/src/utilities/web-assembly.ts +12 -12
  105. package/package.json +51 -50
  106. package/lib/dist/effects/classes/Filter.d.ts +0 -3
  107. package/lib/dist/effects/classes/Filter.d.ts.map +0 -1
  108. package/lib/dist/effects/classes/Filter.js +0 -2
  109. package/lib/src/effects/classes/Filter.ts +0 -1
@@ -1,66 +1,66 @@
1
- import { ErrorCodes, WarningCodes } from "../console-codes";
2
-
3
- import { FluexGLAudio } from "../index";
4
-
5
- export namespace Debug {
6
-
7
- export function Log(message: string, additionalDetails?: string[]) {
8
-
9
- if (!FluexGLAudio.options.debugger.showInfo) return;
10
-
11
- let outputString: string = `%c[INFO]: %c${message} %c`;
12
-
13
- additionalDetails && additionalDetails.forEach(function (detail: string) {
14
- outputString += `\n\u0009 > ${detail}`
15
- });
16
-
17
- return console.log(outputString, "color: white;", "color: gray", "color: gray");
18
- }
19
-
20
- export function Success(message: string, additionalDetails?: string[]) {
21
-
22
- if (!FluexGLAudio.options.debugger.showInfo) return;
23
-
24
- let outputString: string = `%c[SUCCESS]: %c${message} %c`;
25
-
26
- additionalDetails && additionalDetails.forEach(function (detail: string) {
27
- outputString += `\n\u0009 > ${detail}`
28
- });
29
-
30
- return console.log(outputString, "color: lime;", "color: white", "color: gray");
31
- }
32
-
33
- export function Warn(message: string, additionalDetails?: string[], warningCode?: WarningCodes) {
34
-
35
- if (!FluexGLAudio.options.debugger.showWarnings) return;
36
-
37
- let outputString: string = `%c(${warningCode ? warningCode : "Unknown".toUpperCase()}) %c[WARNING]: %c${message} %c`;
38
-
39
- (additionalDetails && additionalDetails !== null) && additionalDetails.forEach(function (detail: string) {
40
- outputString += `\n\u0009 > ${detail}`
41
- });
42
-
43
- return console.log(outputString, "color: gray", "color: yellow;", "color: white", "color: white; font-style: italic;");
44
- }
45
-
46
- export function Error(message: string, additionalDetails?: string[], errorCode?: ErrorCodes) {
47
-
48
- if (!FluexGLAudio.options.debugger.showErrors) return;
49
-
50
- let outputString: string = `%c(${errorCode ? errorCode : "Unknown".toUpperCase()}) %c[ERROR]: %c${message} %c`;
51
-
52
- (additionalDetails && additionalDetails !== null) && additionalDetails.forEach(function (detail: string) {
53
- outputString += `\n\u0009 > ${detail}`
54
- });
55
-
56
- console.log(outputString, "color: red", "color: red;", "color: white", "color: white; font-style: italic;");
57
-
58
- if (FluexGLAudio.options.debugger.breakOnError) {
59
-
60
- for (let char of message)
61
- outputString = outputString.replace("%c", "");
62
-
63
- throw new window.Error(outputString);
64
- }
65
- }
1
+ import { ErrorCodes, WarningCodes } from "../console-codes";
2
+
3
+ import { FluexGLAudio } from "../index";
4
+
5
+ export namespace Debug {
6
+
7
+ export function Log(message: string, additionalDetails?: string[]) {
8
+
9
+ if (!FluexGLAudio.options.debugger.showInfo) return;
10
+
11
+ let outputString: string = `%c[INFO]: %c${message} %c`;
12
+
13
+ additionalDetails && additionalDetails.forEach(function (detail: string) {
14
+ outputString += `\n\u0009 > ${detail}`
15
+ });
16
+
17
+ return console.log(outputString, "color: white;", "color: gray", "color: gray");
18
+ }
19
+
20
+ export function Success(message: string, additionalDetails?: string[]) {
21
+
22
+ if (!FluexGLAudio.options.debugger.showInfo) return;
23
+
24
+ let outputString: string = `%c[SUCCESS]: %c${message} %c`;
25
+
26
+ additionalDetails && additionalDetails.forEach(function (detail: string) {
27
+ outputString += `\n\u0009 > ${detail}`
28
+ });
29
+
30
+ return console.log(outputString, "color: lime;", "color: white", "color: gray");
31
+ }
32
+
33
+ export function Warn(message: string, additionalDetails?: string[], warningCode?: WarningCodes) {
34
+
35
+ if (!FluexGLAudio.options.debugger.showWarnings) return;
36
+
37
+ let outputString: string = `%c(${warningCode ? warningCode : "Unknown".toUpperCase()}) %c[WARNING]: %c${message} %c`;
38
+
39
+ (additionalDetails && additionalDetails !== null) && additionalDetails.forEach(function (detail: string) {
40
+ outputString += `\n\u0009 > ${detail}`
41
+ });
42
+
43
+ return console.log(outputString, "color: gray", "color: yellow;", "color: white", "color: white; font-style: italic;");
44
+ }
45
+
46
+ export function Error(message: string, additionalDetails?: string[], errorCode?: ErrorCodes) {
47
+
48
+ if (!FluexGLAudio.options.debugger.showErrors) return;
49
+
50
+ let outputString: string = `%c(${errorCode ? errorCode : "Unknown".toUpperCase()}) %c[ERROR]: %c${message} %c`;
51
+
52
+ (additionalDetails && additionalDetails !== null) && additionalDetails.forEach(function (detail: string) {
53
+ outputString += `\n\u0009 > ${detail}`
54
+ });
55
+
56
+ console.log(outputString, "color: red", "color: red;", "color: white", "color: white; font-style: italic;");
57
+
58
+ if (FluexGLAudio.options.debugger.breakOnError) {
59
+
60
+ for (let char of message)
61
+ outputString = outputString.replace("%c", "");
62
+
63
+ throw new window.Error(outputString);
64
+ }
65
+ }
66
66
  }
@@ -1,237 +1,265 @@
1
- import mime from "mime";
2
- import { v4 } from "uuid";
3
-
4
- import { AudioDevice } from "../core/classes/AudioDevice";
5
- import { Master } from "../core/classes/Master";
6
- import { Effector } from "../core/classes/Effector";
7
-
8
- import { Debug } from "./debugger";
9
- import { SUPPORTED_FILE_TYPES } from "./constants";
10
-
11
- import { LoadWebAssemblyModule } from "./web-assembly";
12
- import { ErrorCodes, WarningCodes } from "../console-codes";
13
-
14
- import { Chorus, SoftClip } from "../effects/exports";
15
-
16
- import * as Effects from "../effects/exports";
17
-
18
- import { LoadAudioSourceOptions, AudioSourceData, DspPipelineInitializationOptions, DspPipelineInitializationState } from "../typings";
19
-
20
- /**
21
- * Initializes the DSP pipeline by requesting audio permissions and initializing the WASM module.
22
- * Very important to call this function and wait for it to complete before using any audio features.
23
- *
24
- * FluexGL DSP cannot be used without calling this function first.
25
- * @returns
26
- */
27
- export async function InitializeDspPipeline(options: DspPipelineInitializationOptions): Promise<DspPipelineInitializationState | null> {
28
- Debug.Log("Attempting to initialize DSP pipeline...");
29
-
30
- const start: number = Date.now();
31
- let initialized: boolean = true;
32
-
33
- try {
34
-
35
- const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
36
-
37
- Debug.Log(`Found ${stream.getTracks().length} media stream tracks.`);
38
-
39
- stream.getTracks().forEach(function (track: MediaStreamTrack) {
40
- track.stop();
41
- });
42
- } catch (err) {
43
-
44
- initialized = false;
45
-
46
- Debug.Error("Permission to access media devices has not been granted.", [
47
- "Make sure the user has granted FluentGL permission to access media devices."
48
- ], ErrorCodes.NO_CONTEXT_PERMISSION)
49
- }
50
-
51
- await LoadWebAssemblyModule(options.pathToWasm);
52
-
53
- const workletFileRequest = await fetch(options.pathToWorklet);
54
- const textContent: string = await workletFileRequest.text();
55
-
56
- const blobUrl: string = ConstructProcessorWorklet(textContent);
57
-
58
- const end: number = Date.now(),
59
- difference: number = end - start;
60
-
61
- Debug.Success(`Succesfully initialized DSP pipeline within ${difference}ms.`);
62
-
63
- return {
64
- success: true,
65
- workletBlobUrl: blobUrl
66
- };
67
- }
68
-
69
- /**
70
- * Resolves a list of available audio output devices.
71
- * @returns
72
- */
73
- export async function ResolveAudioOutputDevices(): Promise<AudioDevice[]> {
74
-
75
- const devices = await navigator.mediaDevices.enumerateDevices();
76
-
77
- const audioDevices: AudioDevice[] = [];
78
-
79
- for (let device of devices)
80
- device.kind === "audiooutput" && audioDevices.push(new AudioDevice(device));
81
-
82
- return audioDevices;
83
- }
84
-
85
- /**
86
- * Resolves a list of available audio input devices.
87
- * @returns
88
- */
89
- export async function ResolveAudioInputDevices(): Promise<AudioDevice[]> {
90
-
91
- const devices = await navigator.mediaDevices.enumerateDevices();
92
-
93
- const audioDevices: AudioDevice[] = [];
94
-
95
- for (let device of devices)
96
- device.kind === "audioinput" && audioDevices.push(new AudioDevice(device));
97
-
98
- return audioDevices;
99
- }
100
-
101
- /**
102
- * Resolves the default audio output device.
103
- * @returns
104
- */
105
- export async function ResolveDefaultAudioOutputDevice(init: DspPipelineInitializationState): Promise<AudioDevice | null> {
106
- Debug.Log("Attempting to resolve default audio output device...");
107
-
108
- const audioDeviceInfos: MediaDeviceInfo[] = [];
109
- const devices = await navigator.mediaDevices.enumerateDevices();
110
-
111
- for (let device of devices)
112
- (device.kind === "audiooutput" && device.deviceId == "default")
113
- && audioDeviceInfos.push(device);
114
-
115
- devices.length === 0 && Debug.Warn("No default audio device found.", [], WarningCodes.NO_DEFAULT_AUDIO_DEVICE_FOUND);
116
-
117
- const defaultAudioDevice = devices.length === 0 ? null : new AudioDevice(audioDeviceInfos[0]);
118
-
119
- if (!defaultAudioDevice) return null;
120
-
121
- await LoadWorkletOnMasterChannel(defaultAudioDevice.masterChannel, init.workletBlobUrl);
122
-
123
- return defaultAudioDevice;
124
- }
125
-
126
- /**
127
- * Resolves the default audio input device.
128
- * @returns
129
- */
130
- export async function ResolveDefaultAudioInputDevice(init: DspPipelineInitializationState): Promise<AudioDevice | null> {
131
- Debug.Log("Attempting to resolve default audio output device...");
132
-
133
- const audioDeviceInfos: MediaDeviceInfo[] = [];
134
- const devices = await navigator.mediaDevices.enumerateDevices();
135
-
136
- for (let device of devices)
137
- (device.kind === "audioinput" && device.deviceId == "default")
138
- && audioDeviceInfos.push(device);
139
-
140
- devices.length === 0 && Debug.Warn("No default audio device found.", [], WarningCodes.NO_DEFAULT_AUDIO_DEVICE_FOUND);
141
-
142
- const defaultAudioDevice = devices.length === 0 ? null : new AudioDevice(audioDeviceInfos[0]);
143
-
144
- if (!defaultAudioDevice) return null;
145
-
146
- await LoadWorkletOnMasterChannel(defaultAudioDevice.masterChannel, init.workletBlobUrl);
147
-
148
- return defaultAudioDevice;
149
- }
150
-
151
- /**
152
- * Loads an audio source from a specified path.
153
- * @param path
154
- * @param options
155
- * @returns
156
- */
157
- export async function LoadAudioSource(path: string, options: Partial<LoadAudioSourceOptions> = { allowForeignFileTypes: false }): Promise<AudioSourceData | null> {
158
-
159
- const extension: string | null = mime.getType(path);
160
-
161
- if (!extension && !options.allowForeignFileTypes) {
162
-
163
- Debug.Error("The file type of the specified file could not be identified.", [
164
- `Set allowForeignFileTypes to true in the properties to allow foreign or unknown file types.`
165
- ], ErrorCodes.NO_FILE_TYPE_FOUND);
166
- return null;
167
- }
168
-
169
- if (!SUPPORTED_FILE_TYPES.includes(extension as string)) Debug.Warn("The file type of the specified file is unknown and possibly unknown, but will be used anyways.");
170
-
171
- const file = await fetch(path, { method: "get" });
172
-
173
- if (file.status !== 200) {
174
-
175
- Debug.Error("The specified file could not be loaded.", [
176
- `Received status code: ${file.status}.`
177
- ], ErrorCodes.PATH_TO_FILE_NOT_FOUND);
178
- return null;
179
- }
180
-
181
- const tempContext = new AudioContext(),
182
- arrayBuffer = await file.arrayBuffer(),
183
- audioBuffer = await tempContext.decodeAudioData(arrayBuffer);
184
-
185
- // IMPORTANT!
186
- tempContext.close();
187
-
188
- return {
189
- arrayBuffer, audioBuffer,
190
- id: v4(),
191
- timestamp: Date.now()
192
- }
193
- }
194
-
195
- /**
196
- * Loads an audio file from a blob.
197
- */
198
- export async function LoadAudioSourceFromBlob(blob: Blob): Promise<AudioSourceData | null> {
199
-
200
- const tempContext: AudioContext = new AudioContext(),
201
- arrayBuffer: ArrayBuffer = await blob.arrayBuffer();
202
-
203
- const audioBuffer: AudioBuffer = await tempContext.decodeAudioData(arrayBuffer);
204
-
205
- return {
206
- id: v4(),
207
- timestamp: Date.now(),
208
- audioBuffer, arrayBuffer
209
- };
210
- }
211
-
212
- export function ConstructProcessorWorklet(code: string): string {
213
-
214
- const blob = new Blob([code], {
215
- type: "application/javascript"
216
- });
217
-
218
- return URL.createObjectURL(blob);
219
- }
220
-
221
- export async function LoadWorkletOnMasterChannel(master: Master, workletBlobUrl: string) {
222
- Debug.Log("Loading worklet modules on master channel...", [`Channel ID: ${master.id}`]);
223
-
224
- const context: AudioContext = master.context,
225
- start: number = Date.now();
226
-
227
- await context.audioWorklet.addModule(workletBlobUrl);
228
-
229
- const end: number = Date.now(),
230
- difference: number = end - start;
231
-
232
- Debug.Success("Succesfully loaded audio processor worklets into master channel.", [
233
- `Executed in ${difference}ms.`,
234
- ]);
235
-
236
- return true;
237
- }
1
+ import mime from "mime";
2
+ import { v4 } from "uuid";
3
+
4
+ import { AudioDevice } from "../core/classes/AudioDevice";
5
+ import { Master } from "../core/classes/Master";
6
+
7
+ import { Debug } from "./debugger";
8
+ import { SUPPORTED_FILE_TYPES } from "./constants";
9
+
10
+ import { compiledWebAssemblyModule, LoadWebAssemblyModule } from "./web-assembly";
11
+ import { ErrorCodes, WarningCodes } from "../console-codes";
12
+
13
+ import { LoadAudioSourceOptions, AudioSourceData, DspPipelineInitializationOptions, DspPipelineInitializationState, AudioWorkletProcessorNames } from "../typings";
14
+
15
+ /**
16
+ * Initializes the DSP pipeline by requesting audio permissions and initializing the WASM module.
17
+ * Very important to call this function and wait for it to complete before using any audio features.
18
+ *
19
+ * FluexGL DSP cannot be used without calling this function first.
20
+ * @returns
21
+ */
22
+ export async function InitializeDspPipeline(options: DspPipelineInitializationOptions): Promise<DspPipelineInitializationState | null> {
23
+ Debug.Log("Attempting to initialize DSP pipeline...");
24
+
25
+ const start: number = Date.now();
26
+ let initialized: boolean = true;
27
+
28
+ try {
29
+
30
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
31
+
32
+ Debug.Log(`Found ${stream.getTracks().length} media stream tracks.`);
33
+
34
+ stream.getTracks().forEach(function (track: MediaStreamTrack) {
35
+ track.stop();
36
+ });
37
+ } catch (err) {
38
+
39
+ initialized = false;
40
+
41
+ Debug.Error("Permission to access media devices has not been granted.", [
42
+ "Make sure the user has granted FluentGL permission to access media devices."
43
+ ], ErrorCodes.NO_CONTEXT_PERMISSION)
44
+ }
45
+
46
+ await LoadWebAssemblyModule(options.pathToWasm);
47
+
48
+ const workletFileRequest = await fetch(options.pathToWorklet);
49
+ const textContent: string = await workletFileRequest.text();
50
+
51
+ const blobUrl: string = ConstructProcessorWorklet(textContent);
52
+
53
+ const end: number = Date.now(),
54
+ difference: number = end - start;
55
+
56
+ Debug.Success(`Succesfully initialized DSP pipeline within ${difference}ms.`);
57
+
58
+ return {
59
+ success: true,
60
+ workletBlobUrl: blobUrl
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Resolves a list of available audio output devices.
66
+ * @returns
67
+ */
68
+ export async function ResolveAudioOutputDevices(): Promise<AudioDevice[]> {
69
+
70
+ const devices = await navigator.mediaDevices.enumerateDevices();
71
+
72
+ const audioDevices: AudioDevice[] = [];
73
+
74
+ for (let device of devices)
75
+ device.kind === "audiooutput" && audioDevices.push(new AudioDevice(device));
76
+
77
+ return audioDevices;
78
+ }
79
+
80
+ /**
81
+ * Resolves a list of available audio input devices.
82
+ * @returns
83
+ */
84
+ export async function ResolveAudioInputDevices(): Promise<AudioDevice[]> {
85
+
86
+ const devices = await navigator.mediaDevices.enumerateDevices();
87
+
88
+ const audioDevices: AudioDevice[] = [];
89
+
90
+ for (let device of devices)
91
+ device.kind === "audioinput" && audioDevices.push(new AudioDevice(device));
92
+
93
+ return audioDevices;
94
+ }
95
+
96
+ /**
97
+ * Resolves the default audio output device.
98
+ * @returns
99
+ */
100
+ export async function ResolveDefaultAudioOutputDevice(init: DspPipelineInitializationState): Promise<AudioDevice | null> {
101
+ Debug.Log("Attempting to resolve default audio output device...");
102
+
103
+ const audioDeviceInfos: MediaDeviceInfo[] = [];
104
+ const devices = await navigator.mediaDevices.enumerateDevices();
105
+
106
+ for (let device of devices)
107
+ (device.kind === "audiooutput" && device.deviceId == "default")
108
+ && audioDeviceInfos.push(device);
109
+
110
+ devices.length === 0 && Debug.Warn("No default audio device found.", [], WarningCodes.NO_DEFAULT_AUDIO_DEVICE_FOUND);
111
+
112
+ const defaultAudioDevice = devices.length === 0 ? null : new AudioDevice(audioDeviceInfos[0]);
113
+
114
+ if (!defaultAudioDevice) return null;
115
+
116
+ await LoadWorkletOnMasterChannel(defaultAudioDevice.masterChannel, init.workletBlobUrl);
117
+
118
+ return defaultAudioDevice;
119
+ }
120
+
121
+ /**
122
+ * Resolves the default audio input device.
123
+ * @returns
124
+ */
125
+ export async function ResolveDefaultAudioInputDevice(init: DspPipelineInitializationState): Promise<AudioDevice | null> {
126
+ Debug.Log("Attempting to resolve default audio output device...");
127
+
128
+ const audioDeviceInfos: MediaDeviceInfo[] = [];
129
+ const devices = await navigator.mediaDevices.enumerateDevices();
130
+
131
+ for (let device of devices)
132
+ (device.kind === "audioinput" && device.deviceId == "default")
133
+ && audioDeviceInfos.push(device);
134
+
135
+ devices.length === 0 && Debug.Warn("No default audio device found.", [], WarningCodes.NO_DEFAULT_AUDIO_DEVICE_FOUND);
136
+
137
+ const defaultAudioDevice = devices.length === 0 ? null : new AudioDevice(audioDeviceInfos[0]);
138
+
139
+ if (!defaultAudioDevice) return null;
140
+
141
+ await LoadWorkletOnMasterChannel(defaultAudioDevice.masterChannel, init.workletBlobUrl);
142
+
143
+ return defaultAudioDevice;
144
+ }
145
+
146
+ /**
147
+ * Loads an audio source from a specified path.
148
+ * @param path
149
+ * @param options
150
+ * @returns
151
+ */
152
+ export async function LoadAudioSource(path: string, options: Partial<LoadAudioSourceOptions> = { allowForeignFileTypes: false }): Promise<AudioSourceData | null> {
153
+
154
+ const extension: string | null = mime.getType(path);
155
+
156
+ if (!extension && !options.allowForeignFileTypes) {
157
+
158
+ Debug.Error("The file type of the specified file could not be identified.", [
159
+ `Set allowForeignFileTypes to true in the properties to allow foreign or unknown file types.`
160
+ ], ErrorCodes.NO_FILE_TYPE_FOUND);
161
+ return null;
162
+ }
163
+
164
+ if (!SUPPORTED_FILE_TYPES.includes(extension as string)) Debug.Warn("The file type of the specified file is unknown and possibly unknown, but will be used anyways.");
165
+
166
+ const file = await fetch(path, { method: "get" });
167
+
168
+ if (file.status !== 200) {
169
+
170
+ Debug.Error("The specified file could not be loaded.", [
171
+ `Received status code: ${file.status}.`
172
+ ], ErrorCodes.PATH_TO_FILE_NOT_FOUND);
173
+ return null;
174
+ }
175
+
176
+ const tempContext = new AudioContext(),
177
+ arrayBuffer = await file.arrayBuffer(),
178
+ audioBuffer = await tempContext.decodeAudioData(arrayBuffer);
179
+
180
+ // IMPORTANT!
181
+ tempContext.close();
182
+
183
+ return {
184
+ arrayBuffer, audioBuffer,
185
+ id: v4(),
186
+ timestamp: Date.now()
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Loads an audio file from a blob.
192
+ */
193
+ export async function LoadAudioSourceFromBlob(blob: Blob): Promise<AudioSourceData | null> {
194
+
195
+ const tempContext: AudioContext = new AudioContext(),
196
+ arrayBuffer: ArrayBuffer = await blob.arrayBuffer();
197
+
198
+ const audioBuffer: AudioBuffer = await tempContext.decodeAudioData(arrayBuffer);
199
+
200
+ return {
201
+ id: v4(),
202
+ timestamp: Date.now(),
203
+ audioBuffer, arrayBuffer
204
+ };
205
+ }
206
+
207
+ export function ConstructProcessorWorklet(code: string): string {
208
+
209
+ const blob = new Blob([code], {
210
+ type: "application/javascript"
211
+ });
212
+
213
+ return URL.createObjectURL(blob);
214
+ }
215
+
216
+ export async function LoadWorkletOnMasterChannel(master: Master, workletBlobUrl: string) {
217
+ Debug.Log("Loading worklet modules on master channel...", [`Channel ID: ${master.id}`]);
218
+
219
+ const context: AudioContext = master.context,
220
+ start: number = Date.now();
221
+
222
+ await context.audioWorklet.addModule(workletBlobUrl);
223
+
224
+ const end: number = Date.now(),
225
+ difference: number = end - start;
226
+
227
+ Debug.Success("Succesfully loaded audio processor worklets into master channel.", [
228
+ `Executed in ${difference}ms.`,
229
+ ]);
230
+
231
+ return true;
232
+ }
233
+
234
+ export function SendMessageToWorklet<T, K = any>(node: AudioWorkletNode | null, commandId: T, data: K) {
235
+
236
+ if (!node) return false;
237
+
238
+ node.port.postMessage({ commandId, data });
239
+
240
+ return true;
241
+ }
242
+
243
+ export function CreateAudioWorkletNode<T = any>(context: AudioContext, name: AudioWorkletProcessorNames | string, data: T) {
244
+
245
+ if (!compiledWebAssemblyModule) {
246
+
247
+ Debug.Error("Failed to create audio worklet node, because the web assembly module has not been compiled properly.", [
248
+ `Request audio worklet node: ${name}.`
249
+ ]);
250
+ return null;
251
+ }
252
+
253
+ return new AudioWorkletNode(context, name, {
254
+ numberOfInputs: 1,
255
+ numberOfOutputs: 1,
256
+ outputChannelCount: [2],
257
+ parameterData: {
258
+ ...data,
259
+ sampleRate: context.sampleRate
260
+ },
261
+ processorOptions: {
262
+ module: compiledWebAssemblyModule
263
+ }
264
+ })
265
+ }