🔃 Refactor AudioFilters

This commit is contained in:
DevAndromeda 2022-07-23 11:44:22 +05:45
parent 1fcf26ff35
commit 40f772fa5b

View file

@ -2,45 +2,13 @@ import { FiltersName } from "../types/types";
const bass = (g: number) => `bass=g=${g}:f=110:w=0.3`; const bass = (g: number) => `bass=g=${g}:f=110:w=0.3`;
/** class AudioFilters {
* The available audio filters public constructor() {
* @typedef {object} AudioFilters return AudioFilters;
* @property {string} bassboost_low The bassboost filter (+15dB) }
* @property {string} bassboost The bassboost filter (+20dB)
* @property {string} bassboost_high The bassboost filter (+30dB)
* @property {string} 8D The 8D filter
* @property {string} vaporwave The vaporwave filter
* @property {string} nightcore The nightcore filter
* @property {string} phaser The phaser filter
* @property {string} tremolo The tremolo filter
* @property {string} vibrato The vibrato filter
* @property {string} reverse The reverse filter
* @property {string} treble The treble filter
* @property {string} normalizer The normalizer filter (dynamic audio normalizer based)
* @property {string} normalizer2 The normalizer filter (audio compressor based)
* @property {string} surrounding The surrounding filter
* @property {string} pulsator The pulsator filter
* @property {string} subboost The subboost filter
* @property {string} karaoke The kakaoke filter
* @property {string} flanger The flanger filter
* @property {string} gate The gate filter
* @property {string} haas The haas filter
* @property {string} mcompand The mcompand filter
* @property {string} mono The mono filter
* @property {string} mstlr The mstlr filter
* @property {string} mstrr The mstrr filter
* @property {string} compressor The compressor filter
* @property {string} expander The expander filter
* @property {string} softlimiter The softlimiter filter
* @property {string} chorus The chorus filter
* @property {string} chorus2d The chorus2d filter
* @property {string} chorus3d The chorus3d filter
* @property {string} fadein The fadein filter
* @property {string} dim The dim filter
* @property {string} earrape The earrape filter
*/
const FilterList = { public static get filters(): Record<FiltersName, string> {
return {
bassboost_low: bass(15), bassboost_low: bass(15),
bassboost: bass(20), bassboost: bass(20),
bassboost_high: bass(30), bassboost_high: bass(30),
@ -73,44 +41,67 @@ const FilterList = {
chorus3d: "chorus=0.5:0.9:50|60|40:0.4|0.32|0.3:0.25|0.4|0.3:2|2.3|1.3", chorus3d: "chorus=0.5:0.9:50|60|40:0.4|0.32|0.3:0.25|0.4|0.3:2|2.3|1.3",
fadein: "afade=t=in:ss=0:d=10", fadein: "afade=t=in:ss=0:d=10",
dim: `afftfilt="'real=re * (1-clip((b/nb)*b,0,1))':imag='im * (1-clip((b/nb)*b,0,1))'"`, dim: `afftfilt="'real=re * (1-clip((b/nb)*b,0,1))':imag='im * (1-clip((b/nb)*b,0,1))'"`,
earrape: "channelsplit,sidechaingate=level_in=64", earrape: "channelsplit,sidechaingate=level_in=64"
*[Symbol.iterator](): IterableIterator<{ name: FiltersName; value: string }> {
for (const [k, v] of Object.entries(this)) {
if (typeof this[k as FiltersName] === "string") yield { name: k as FiltersName, value: v as string };
}
},
get names() {
return Object.keys(this).filter((p) => !["names", "length"].includes(p) && typeof this[p as FiltersName] !== "function") as FiltersName[];
},
get length() {
return Object.keys(this).filter((p) => !["names", "length"].includes(p) && typeof this[p as FiltersName] !== "function").length;
},
toString() {
return this.names.map((m) => (this as any)[m]).join(","); // eslint-disable-line @typescript-eslint/no-explicit-any
},
create(filter?: FiltersName[]): string {
if (!filter || !Array.isArray(filter)) return this.toString();
return filter
.filter((predicate) => typeof predicate === "string")
.map((m) => this[m])
.join(",");
},
define(filterName: string, value: string): void {
if (typeof this[filterName as FiltersName] && typeof this[filterName as FiltersName] === "function") return;
this[filterName as FiltersName] = value;
},
defineBulk(filterArray: { name: string; value: string }[]): void {
filterArray.forEach((arr) => this.define(arr.name, arr.value));
}
}; };
}
export default FilterList; public static get<K extends FiltersName>(name: K) {
export { FilterList as AudioFilters }; return this.filters[name];
}
public static has<K extends FiltersName>(name: K) {
return name in this.filters;
}
public static *[Symbol.iterator](): IterableIterator<{ name: FiltersName; value: string }> {
for (const [k, v] of Object.entries(this)) {
if (typeof this.filters[k as FiltersName] === "string") yield { name: k as FiltersName, value: v as string };
}
}
public static get names() {
return Object.keys(this).filter((p) => !["names", "length"].includes(p) && typeof this.filters[p as FiltersName] !== "function") as FiltersName[];
}
// @ts-expect-error AudioFilters.length
public static get length() {
return Object.keys(this).filter((p) => !["names", "length"].includes(p) && typeof this.filters[p as FiltersName] !== "function").length;
}
public static toString() {
return this.names.map((m) => (this as any)[m]).join(","); // eslint-disable-line @typescript-eslint/no-explicit-any
}
/**
* Create ffmpeg args from the specified filters name
* @param filter The filter name
* @returns
*/
public static create(filters?: FiltersName[]) {
if (!filters || !Array.isArray(filters)) return this.toString();
return filters
.filter((predicate) => typeof predicate === "string")
.map((m) => this.get(m))
.join(",");
}
/**
* Defines audio filter
* @param filterName The name of the filter
* @param value The ffmpeg args
*/
public static define(filterName: string, value: string) {
this.filters[filterName as FiltersName] = value;
}
/**
* Defines multiple audio filters
* @param filtersArray Array of filters containing the filter name and ffmpeg args
*/
public static defineBulk(filtersArray: { name: string; value: string }[]) {
filtersArray.forEach((arr) => this.define(arr.name, arr.value));
}
}
export default AudioFilters;
export { AudioFilters };