Skip to content

Commit

Permalink
Refactor function
Browse files Browse the repository at this point in the history
  • Loading branch information
xenown authored and selinali2010 committed Feb 22, 2023
1 parent 13cb5e4 commit fa28456
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"**/*"
]
},
"./auto_eq/**"
"./autoeq/**"
],
"publish": {
"provider": "github",
Expand Down
81 changes: 31 additions & 50 deletions src/main/autoeq.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import fs from 'fs';
import path from 'path';
import { app } from 'electron';
import { FilterTypeEnum, IFilter, IPreset } from '../common/constants';
import { uid } from 'uid';
import {
FilterTypeEnum,
getDefaultFilter,
IFilter,
IPreset,
} from '../common/constants';

let AUTOEQ_DIR = './resources/autoeq';
if (!app.isPackaged) {
Expand All @@ -20,73 +26,48 @@ export const getAutoEqResponseList = (device: string) => {
export const getAutoEqPreset = (device: string, response: string) => {
const PREAMP_REGEX = new RegExp('Preamp:[\\s]*([-][\\d]*[.][\\d]*)');

// filter# PK/LS/HS <freq> HZ Gain <gain> dB Q <quality>
// filter #: PK/LS/HS <freq> HZ Gain <gain> dB Q <quality>
const LINE_REGEX = new RegExp(
'Filter ([\\d]+)[:][a-zA-Z\\s]+(PK|LS|HS)[a-zA-Z\\s]+([\\d]+)[a-zA-Z\\s]+([-]?[\\d]*[.][\\d]*)[a-zA-Z\\s]+([\\d]*[.][\\d]*)'
);

let preAmpParsed = 0;
const filterIds: string[] = [];
const filterTypes: FilterTypeEnum[] = [];
const frequencies: number[] = [];
const gains: number[] = [];
const qualities: number[] = [];
const filtersList: IFilter[] = [];

// todo: how to use device and string??
const file = fs.readFileSync(
'A-Audio Elite (bass mode) ParametricEQ.txt',
'utf8'
);
const file = fs.readFileSync(path.join(AUTOEQ_DIR, device, response), 'utf8');
file.split('\n').forEach((line) => {
if (line.match(PREAMP_REGEX)) {
const match = line.match(PREAMP_REGEX);
if (match == null || match.length !== 2) {
throw new Error(
`Preamp regex matched but could not extract all information for preamp: Length is: ${match.length}`
);
let match = line.match(PREAMP_REGEX);
if (match) {
if (match.length !== 2) {
throw new Error(`Regex match error on ${line}`);
}
preAmpParsed = parseFloat(match[1]);
} else if (line.match(LINE_REGEX)) {
const match = line.match(LINE_REGEX);
if (match == null || match.length !== 6) {
throw new Error(
`Line regex matched but could not extract all information for filters. Length is: ${match.length}`
);
return;
}
const filter: IFilter = getDefaultFilter();
match = line.match(LINE_REGEX);
if (match) {
if (match.length !== 6) {
throw new Error(`Regex match error on ${line}`);
}
const filterId = match[1];
let filterType;
if (match[2] === 'PK') {
filterType = FilterTypeEnum.PK;
filter.type = FilterTypeEnum.PK;
} else if (match[2] === 'LS') {
filterType = FilterTypeEnum.LSC;
filter.type = FilterTypeEnum.LSC;
} else if (match[2] === 'HS') {
filterType = FilterTypeEnum.HSC;
filter.type = FilterTypeEnum.HSC;
} else {
throw new Error('Unknown filter type');
// We don't support other filters right now, use PK as default.
filter.type = FilterTypeEnum.PK;
}
const frequency = parseInt(match[3], 10);
const gain = parseFloat(match[4]);
const quality = parseFloat(match[5]);
filterIds.push(filterId);
filterTypes.push(filterType);
frequencies.push(frequency);
gains.push(gain);
qualities.push(quality);
filter.frequency = parseInt(match[3], 10);
filter.gain = parseFloat(match[4]);
filter.quality = parseFloat(match[5]);
filtersList.push(filter);
}
// Ignore any lines which we do not recognize
});

const filtersList: IFilter[] = [];

// what is id here???
for (let i = 0; i < filterIds.length; i += 1) {
filtersList.push({
id: filterIds[i],
frequency: frequencies[i],
gain: gains[i],
type: filterTypes[i],
quality: qualities[i],
});
}
const preset: IPreset = {
preAmp: preAmpParsed,
filters: filtersList,
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/styles/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ body {
'side-bar middle-content presets-bar'
'graph-wrapper graph-wrapper graph-wrapper';
grid-template-columns: minmax($sidebar-width, max-content) 1fr $preset-bar-width;
grid-template-rows: $equalizer-height repeat(1fr, 2);
grid-template-rows: $equalizer-height 1fr;
gap: $spacing-s;
}
}
Expand Down

0 comments on commit fa28456

Please sign in to comment.