Skip to content

Commit

Permalink
Merge pull request #58 from h39s/jat-104
Browse files Browse the repository at this point in the history
JAT-104 Add functionality to clear/reset AQUA settings to "default"
  • Loading branch information
selinali2010 authored Oct 8, 2023
2 parents 449015a + cc86f6b commit fffc53c
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 101 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/unit_tests/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import {
cloneFilters,
computeAvgFreq,
isFixedBandSizeEnumValue,
isRestrictedPresetName,
roundToPrecision,
} from 'common/utils';
Expand Down Expand Up @@ -94,4 +95,19 @@ describe('utils', () => {
});
});
});

describe('isFixedBandSizeEnumValue', () => {
it('should be true for valid fixed band size values', () => {
expect(isFixedBandSizeEnumValue(6)).toBeTruthy();
expect(isFixedBandSizeEnumValue(10)).toBeTruthy();
expect(isFixedBandSizeEnumValue(15)).toBeTruthy();
expect(isFixedBandSizeEnumValue(31)).toBeTruthy();
});

it('should be false for invalid fixed band size values', () => {
expect(isFixedBandSizeEnumValue(1)).toBeFalsy();
expect(isFixedBandSizeEnumValue(-1)).toBeFalsy();
expect(isFixedBandSizeEnumValue(16)).toBeFalsy();
});
});
});
2 changes: 2 additions & 0 deletions src/common/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ enum ChannelEnum {
GET_AUTO_EQ_DEVICE_LIST = 'getAutoEqDeviceList',
GET_AUTO_EQ_RESPONSE_LIST = 'getAutoEqResponseList',
LOAD_AUTO_EQ_PRESET = 'loadAutoEqPreset',
CLEAR_GAINS = 'clearGains',
SET_FIXED_BAND = 'setFixedBand',
}

export default ChannelEnum;
31 changes: 26 additions & 5 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,28 @@ export interface IPresetV2 {

/** ----- Default Values ----- */

const FIXED_FREQUENCIES = [
32, 64, 125, 250, 500, 1000, 2000, 4000, 8000, 16000,
];
export enum FixedBandSizeEnum {
SIX = 6,
TEN = 10,
FIFTEEN = 15,
THIRTY_ONE = 31,
}

export const FIXED_BAND_FREQUENCIES: Record<FixedBandSizeEnum, number[]> = {
[FixedBandSizeEnum.SIX]: [100, 200, 400, 800, 1600, 3200],
[FixedBandSizeEnum.TEN]: [
32, 64, 125, 250, 500, 1000, 2000, 4000, 8000, 16000,
],
[FixedBandSizeEnum.FIFTEEN]: [
25, 40, 63, 100, 160, 250, 400, 630, 1000, 1600, 2500, 4000, 6300, 10000,
16000,
],
[FixedBandSizeEnum.THIRTY_ONE]: [
20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630,
800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000,
12500, 16000, 20000,
],
};

const DEFAULT_FILTER_TEMPLATE = {
frequency: 1000,
Expand All @@ -128,9 +147,11 @@ export const getDefaultFilterWithId = (): IFilter => {
};
};

const getDefaultFilters = (): IFiltersMap => {
export const getDefaultFilters = (
size: FixedBandSizeEnum = FixedBandSizeEnum.TEN
): IFiltersMap => {
const filters: IFiltersMap = {};
FIXED_FREQUENCIES.forEach((f) => {
FIXED_BAND_FREQUENCIES[size].forEach((f) => {
const filter: IFilter = { ...getDefaultFilterWithId(), frequency: f };
filters[filter.id] = filter;
});
Expand Down
6 changes: 6 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
MAX_FREQUENCY,
MIN_FREQUENCY,
RESERVED_FILE_NAMES_SET,
FixedBandSizeEnum,
} from './constants';

export const roundToPrecision = (value: number, precision: number) => {
Expand Down Expand Up @@ -52,3 +53,8 @@ export const cloneFilters = (filters: IFiltersMap) => {
});
return filtersClone;
};

export const isFixedBandSizeEnumValue = (value: number) =>
Object.values(FixedBandSizeEnum)
.filter((key) => !Number.isNaN(Number(key)))
.some((enumValue) => enumValue === value);
30 changes: 29 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ import {
WINDOW_HEIGHT_EXPANDED,
WINDOW_WIDTH,
getDefaultFilterWithId,
FixedBandSizeEnum,
getDefaultFilters,
IFiltersMap,
} from '../common/constants';
import { ErrorCode } from '../common/errors';
import { isRestrictedPresetName } from '../common/utils';
import {
isFixedBandSizeEnumValue,
isRestrictedPresetName,
} from '../common/utils';
import { TSuccess, TError } from '../renderer/utils/equalizerApi';
import {
getAutoEqDeviceList,
Expand Down Expand Up @@ -637,6 +643,28 @@ ipcMain.on(ChannelEnum.REMOVE_FILTER, async (event, arg) => {
await handleUpdate(event, channel);
});

ipcMain.on(ChannelEnum.CLEAR_GAINS, async (event) => {
const channel = ChannelEnum.CLEAR_GAINS;

Object.keys(state.filters).forEach((key) => {
state.filters[key].gain = 0;
});

await handleUpdate(event, channel);
});

ipcMain.on(ChannelEnum.SET_FIXED_BAND, async (event, arg) => {
const channel = ChannelEnum.SET_FIXED_BAND;
const size: FixedBandSizeEnum = arg[0];
if (!isFixedBandSizeEnumValue(size)) {
handleError(event, channel, ErrorCode.INVALID_PARAMETER);
}

state.filters = getDefaultFilters(size);

await handleUpdateHelper<IFiltersMap>(event, channel, state.filters);
});

ipcMain.on(ChannelEnum.SET_WINDOW_SIZE, async (event, arg) => {
const channel = ChannelEnum.SET_WINDOW_SIZE;
setWindowDimension(arg[0]);
Expand Down
67 changes: 35 additions & 32 deletions src/renderer/AutoEQ.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,38 +99,41 @@ const AutoEQ = () => {
);

return (
<div className="auto-eq">
Audio Device:
<Dropdown
name="Audio Device"
options={deviceOptions}
value={currentDevice}
handleChange={handleDeviceChange}
isDisabled={!!globalError}
noSelectionPlaceholder={NO_DEVICE_SELECTION}
isFilterable
/>
Target Frequency Response:
<Dropdown
name="Target Frequency Response"
options={responseOptions}
value={currentResponse}
handleChange={(newValue) => setCurrentResponse(newValue)}
isDisabled={!!globalError || responses.length === 0}
emptyOptionsPlaceholder={NO_RESPONSES}
noSelectionPlaceholder={NO_RESPONSE_SELECTION}
/>
<Button
className="small"
ariaLabel="Apply Auto EQ"
isDisabled={
!!globalError || currentDevice === '' || currentResponse === ''
}
handleChange={applyAutoEQ}
>
Apply
</Button>
</div>
<>
<h4 className="auto-eq-title">Auto EQ</h4>
<div className="auto-eq">
Audio Device:
<Dropdown
name="Audio Device"
options={deviceOptions}
value={currentDevice}
handleChange={handleDeviceChange}
isDisabled={!!globalError}
noSelectionPlaceholder={NO_DEVICE_SELECTION}
isFilterable
/>
Target Frequency Response:
<Dropdown
name="Target Frequency Response"
options={responseOptions}
value={currentResponse}
handleChange={(newValue) => setCurrentResponse(newValue)}
isDisabled={!!globalError || responses.length === 0}
emptyOptionsPlaceholder={NO_RESPONSES}
noSelectionPlaceholder={NO_RESPONSE_SELECTION}
/>
<Button
className="small"
ariaLabel="Apply Auto EQ"
isDisabled={
!!globalError || currentDevice === '' || currentResponse === ''
}
handleChange={applyAutoEQ}
>
Apply
</Button>
</div>
</>
);
};

Expand Down
Loading

0 comments on commit fffc53c

Please sign in to comment.