Skip to content

Commit d2be563

Browse files
Added static method sound to make it less hard coded.
1 parent 44ab404 commit d2be563

File tree

5 files changed

+45
-29
lines changed

5 files changed

+45
-29
lines changed

scripts/app.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ canvas.addEventListener('mousemove', e => {
2929
}
3030
lastPositionX = e.offsetX;
3131
});
32-
window.addEventListener('mousedown', e => {
32+
window.addEventListener('mousedown', () => {
3333
isDrawing = true;
3434
});
35-
window.addEventListener('mouseup', e => {
35+
window.addEventListener('mouseup', () => {
3636
isDrawing = false;
3737
lastPositionX = null;
3838
});
@@ -67,7 +67,7 @@ function saveValue(x1, y1, x2) {
6767
const cnt = canvas.getContext("2d");
6868
cnt.font = "30px Arial";
6969

70-
function generateArrayAndDraw(event) {
70+
function generateArrayAndDraw() {
7171
arrayToSort = generateArray(sizeSlider.value);
7272
if (sortIndex) {
7373
updateSortMethod(sortIndex);
@@ -113,18 +113,15 @@ function switchSorting() {
113113
// Stop
114114
clearTimeout(sortingTimeout);
115115
sortingTimeout = null;
116-
osciallator.stop();
116+
oscillator.stop();
117117
startButton.innerText = "Start sorting";
118118
startButton.className = "startButton";
119119
generateButton.disabled = false;
120120
sizeSlider.disabled = false;
121121
} else {
122-
if (osciallator === null) {
123-
osciallator = new Oscillator();
124-
}
125122
isDoneSorting = false;
126-
if (sortIndex !== "7") {
127-
osciallator.resume();
123+
if (sortMethod.constructor.sound()) {
124+
oscillator.resume();
128125
}
129126
startButton.innerText = "Stop sorting";
130127
startButton.className = "stopButton";
@@ -163,17 +160,18 @@ function updateSortMethod(value) {
163160
arrayToSortAccess = 0;
164161
arrayToSortModifications = 0;
165162
isDoneSorting = false;
166-
if(value==="7"){
167-
osciallator.stop();
168-
}else if(sortIndex === "7" && sortingTimeout){
169-
osciallator.resume();
170-
}
171163
let sortClass = sortsDictionary[value];
172164
if (sortClass === undefined) {
173165
alert("Something went wrong pls report this to admin@debianserver.cz error-1:" + value);
174166
return;
175167
}
176168
sortMethod = new sortClass();
169+
170+
if (!sortClass.sound()) {
171+
oscillator.stop();
172+
} else if (sortsDictionary[sortIndex] !== undefined && !sortsDictionary[sortIndex].sound() && sortingTimeout) {
173+
oscillator.resume();
174+
}
177175
sortIndex = value;
178176
}
179177

scripts/arrayFunctions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function map_range(value, low1, high1, low2, high2) {
5757
}
5858

5959
function getValue(array, index, audio = true) {
60-
if (audio) osciallator.changeFrequency(array[index]);
60+
if (audio) oscillator.changeFrequency(array[index]);
6161
arrayToSortAccess++;
6262
return array[index];
6363
}

scripts/oscillator.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
class Oscillator {
22
constructor() {
3-
this.minF = 100;
4-
this.maxF = 700;
5-
this.oscillatorContext = new AudioContext();
6-
this.oscillatorContext.resume();
7-
this.oscillator = this.oscillatorContext.createOscillator();
8-
this.oscillator.type = "sine";
9-
this.gainOscillator = this.oscillatorContext.createGain();
10-
this.gainOscillator.gain.value = 0.005;
11-
this.oscillator.connect(this.gainOscillator);
12-
this.gainOscillator.connect(this.oscillatorContext.destination);
13-
this.oscillator.start();
14-
this.stop();
3+
this.initializated=false;
4+
}
5+
initialize(){
6+
if(!this.initializated){
7+
this.initializated=true;
8+
this.minF = 100;
9+
this.maxF = 700;
10+
this.oscillatorContext = new AudioContext();
11+
this.oscillatorContext.resume();
12+
this.oscillator = this.oscillatorContext.createOscillator();
13+
this.oscillator.type = "sine";
14+
this.gainOscillator = this.oscillatorContext.createGain();
15+
this.gainOscillator.gain.value = 0.005;
16+
this.oscillator.connect(this.gainOscillator);
17+
this.gainOscillator.connect(this.oscillatorContext.destination);
18+
this.oscillator.start();
19+
this.stop();
20+
}
1521
}
16-
1722
stop() {
23+
this.initialize();
1824
this.gainOscillator.disconnect();
1925
}
2026

2127
resume() {
28+
this.initialize();
2229
this.gainOscillator.connect(this.oscillatorContext.destination);
2330
}
2431

@@ -27,4 +34,4 @@ class Oscillator {
2734
}
2835
}
2936

30-
let osciallator = null;
37+
let oscillator = new Oscillator();

scripts/randomSort.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ class RandomSort extends Sort {
2626
cnt.fillRect(index * sizeOfBlock + sizeOfBlock * 0.025, 0, sizeOfBlock * 0.99, cnt.canvas.height * element);
2727
});
2828
}
29+
static sound(){
30+
return false;
31+
}
2932
}

scripts/sort.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ class Sort {
77

88
draw(cnt) {
99
}
10+
11+
/*
12+
* Default value for sound is true implement this function and return false
13+
* if your sorting algorithm does not use sound
14+
* */
15+
static sound(){
16+
return true;
17+
}
1018
}

0 commit comments

Comments
 (0)