Skip to content

Commit 6a86303

Browse files
author
Oskar Widmark
committed
feat: play sound on setting
1 parent dc230e8 commit 6a86303

File tree

4 files changed

+71
-47
lines changed

4 files changed

+71
-47
lines changed

src/App.tsx

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import React, { MouseEvent } from 'react';
22
import './App.css';
3-
import { SelectChangeEvent, Tab, Tabs } from '@mui/material';
3+
import {
4+
Checkbox,
5+
FormControlLabel,
6+
SelectChangeEvent,
7+
Stack,
8+
Tab,
9+
Tabs,
10+
Typography,
11+
} from '@mui/material';
412
import {
513
SortValue,
614
SortName,
715
ResetPreset,
816
ResetFunction,
917
Operator,
10-
SortType,
1118
AlgorithmOptions,
1219
ColorPreset,
1320
} from './types';
1421
import { SortingAlgorithms } from './sorting-algorithms';
15-
import {
16-
createArr,
17-
shuffleArray,
18-
sleep,
19-
sortNameToSortType,
20-
timeScale,
21-
toHz,
22-
} from './utils';
22+
import { createArr, shuffleArray, sleep, timeScale, toHz } from './utils';
2323
import { SideDrawer } from './SideDrawer';
2424
import {
2525
RAINBOW_BACKGROUND_COLOR,
@@ -76,6 +76,8 @@ class App extends React.Component<Props> {
7676
soundType: NonCustomOscillatorType;
7777
soundVolume: number;
7878
frequencyRange: [number, number];
79+
playSoundOnSwap: boolean;
80+
playSoundOnComparison: boolean;
7981
};
8082
};
8183
canvasController: CanvasController;
@@ -252,10 +254,7 @@ class App extends React.Component<Props> {
252254
await sleep(this.state.settings.compareTime);
253255
}
254256

255-
if (
256-
sortNameToSortType[this.state.settings.chosenSortAlg] ===
257-
SortType.Comparison
258-
) {
257+
if (this.state.settings.playSoundOnComparison) {
259258
this.playSoundForColumn(arr, i1);
260259
}
261260

@@ -289,10 +288,7 @@ class App extends React.Component<Props> {
289288
await sleep(this.state.settings.compareTime);
290289
}
291290

292-
if (
293-
sortNameToSortType[this.state.settings.chosenSortAlg] ===
294-
SortType.Comparison
295-
) {
291+
if (this.state.settings.playSoundOnComparison) {
296292
this.playSoundForColumn(arr, i);
297293
}
298294

@@ -311,10 +307,7 @@ class App extends React.Component<Props> {
311307
public async swap(arr: SortValue[], i1: number, i2: number) {
312308
if (!this.state.isSorting) throw Error('isSorting is false!');
313309

314-
if (
315-
sortNameToSortType[this.state.settings.chosenSortAlg] ===
316-
SortType.Distribution
317-
) {
310+
if (this.state.settings.playSoundOnSwap) {
318311
this.playSoundForColumn(arr, i1);
319312
}
320313

@@ -619,6 +612,59 @@ class App extends React.Component<Props> {
619612
disableSwap
620613
valueLabelFormat={(value: number) => `${value} Hz`}
621614
/>
615+
<div>
616+
<Typography
617+
align="left"
618+
variant="subtitle1"
619+
color="textSecondary"
620+
>
621+
Play sound on
622+
</Typography>
623+
<Stack>
624+
<FormControlLabel
625+
control={
626+
<Checkbox
627+
checked={this.state.settings.playSoundOnComparison}
628+
onChange={(e) =>
629+
this.setSettings({
630+
playSoundOnComparison: !!e.target.checked,
631+
})
632+
}
633+
/>
634+
}
635+
label={
636+
<Typography
637+
align="left"
638+
variant="subtitle1"
639+
color="textSecondary"
640+
>
641+
Comparison
642+
</Typography>
643+
}
644+
/>
645+
<FormControlLabel
646+
control={
647+
<Checkbox
648+
checked={this.state.settings.playSoundOnSwap}
649+
onChange={(e) =>
650+
this.setSettings({
651+
playSoundOnSwap: !!e.target.checked,
652+
})
653+
}
654+
/>
655+
}
656+
label={
657+
<Typography
658+
align="left"
659+
variant="subtitle1"
660+
color="textSecondary"
661+
>
662+
Swap
663+
</Typography>
664+
}
665+
/>
666+
</Stack>
667+
</div>
622668
</TabPanel>
623669
</SideDrawer>
624670
</div>

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ export const INIT_SETTINGS = {
5050
soundVolume: DEFAULT_SOUND_VOLUME,
5151
soundType: DEFAULT_SOUND_TYPE,
5252
frequencyRange: [200, 640],
53+
playSoundOnSwap: false,
54+
playSoundOnComparison: true,
5355
};

src/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ export type DrawData = {
4646
value: number;
4747
};
4848

49-
export enum SortType {
50-
Comparison = 'Comparison',
51-
Distribution = 'Distribution',
52-
}
53-
5449
export enum ColorPreset {
5550
Rainbow = 'Rainbow',
5651
Custom = 'Custom',

src/utils.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SortName, SortType, SortValue } from './types';
1+
import { SortValue } from './types';
22

33
export function hsvToRgbHex(h: number, s: number, v: number) {
44
const f = (n: number) => {
@@ -44,25 +44,6 @@ export const sleep = (ms: number) => {
4444
return new Promise((resolve) => setTimeout(resolve, ms));
4545
};
4646

47-
export const sortNameToSortType: Record<SortName, string> = {
48-
[SortName.InsertionSort]: SortType.Comparison,
49-
[SortName.SelectionSort]: SortType.Comparison,
50-
[SortName.CocktailShakerSort]: SortType.Comparison,
51-
[SortName.BubbleSort]: SortType.Comparison,
52-
[SortName.OddEvenSort]: SortType.Comparison,
53-
[SortName.OddEvenMergesort]: SortType.Comparison,
54-
[SortName.RadixSortLSD]: SortType.Distribution,
55-
[SortName.RadixSortMSD]: SortType.Distribution,
56-
[SortName.QuickSort]: SortType.Comparison,
57-
[SortName.CombSort]: SortType.Comparison,
58-
[SortName.ShellSort]: SortType.Comparison,
59-
[SortName.BitonicSort]: SortType.Comparison,
60-
[SortName.BullySort]: SortType.Comparison,
61-
[SortName.AverageSort]: SortType.Comparison,
62-
[SortName.Heapsort]: SortType.Comparison,
63-
[SortName.PushSort]: SortType.Comparison,
64-
};
65-
6647
// something that sounds good
6748
export const toHz = (
6849
value: number,

0 commit comments

Comments
 (0)