Skip to content

Commit 1f6405a

Browse files
Audio fixes (#75)
* Stop the oscillator on board stop. * Avoid divide by zero in the same way as CODAL. We're enquiring about the value.
1 parent 61c02c8 commit 1f6405a

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/board/audio/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,15 @@ export class Audio {
118118
}
119119

120120
setPeriodUs(periodUs: number) {
121-
this.frequency = 1000000 / periodUs;
121+
// CODAL defaults in this way:
122+
this.frequency = periodUs === 0 ? 6068 : 1000000 / periodUs;
122123
if (this.oscillator) {
123124
this.oscillator.frequency.value = this.frequency;
124125
}
125126
}
126127

127128
setAmplitudeU10(amplitudeU10: number) {
128-
if (this.oscillator) {
129-
this.oscillator.stop();
130-
this.oscillator = undefined;
131-
}
129+
this.stopOscillator();
132130
if (amplitudeU10) {
133131
this.oscillator = this.context!.createOscillator();
134132
this.oscillator.type = "sine";
@@ -138,7 +136,16 @@ export class Audio {
138136
}
139137
}
140138

141-
boardStopped() {}
139+
boardStopped() {
140+
this.stopOscillator();
141+
}
142+
143+
private stopOscillator() {
144+
if (this.oscillator) {
145+
this.oscillator.stop();
146+
this.oscillator = undefined;
147+
}
148+
}
142149
}
143150

144151
class BufferedAudio {

0 commit comments

Comments
 (0)