Skip to content

Commit 244ceb7

Browse files
committed
volume control
1 parent 797e865 commit 244ceb7

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "emulators-ui",
3-
"version": "0.73.4",
3+
"version": "0.73.5",
44
"description": "Emulators UI",
55
"main": "dist/emulators-ui.js",
66
"types": "dist/types/emulators-ui.d.ts",

src/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
export const Build = {
77
short: "0.73.4",
8-
version: "0.73.4 (187fc112dd3c66ac30885526e9cdd0cb)",
9-
buildSeed: 1654072258715,
8+
version: "0.73.4 (6a0cb8b5df676c0070a37160bcdc0521)",
9+
buildSeed: 1654074868008,
1010
};

src/js-dos.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export class DosInstance {
5151
private onScaleChanged: ((scale: number) => void)[] = [];
5252
private onVolumeChanged: ((scale: number) => void)[] = [];
5353

54+
setVolumeImplFn: (volume: number) => void = () => {};
55+
5456
constructor(root: HTMLDivElement, emulatorsUi: EmulatorsUi, options: DosOptions) {
5557
this.options = options;
5658
this.emulatorsUi = emulatorsUi;
@@ -117,7 +119,8 @@ export class DosInstance {
117119
console.error("Unable to create webgl canvas, fallback to 2d rendering");
118120
emulatorsUi.graphics._2d(this.layers, ci);
119121
}
120-
emulatorsUi.sound.audioNode(ci);
122+
this.setVolumeImplFn = emulatorsUi.sound.audioNode(ci);
123+
this.setVolumeImplFn(this.volume);
121124
}
122125

123126
emulatorsUi.dom.lifecycle(ci);
@@ -248,6 +251,8 @@ export class DosInstance {
248251

249252
public async setVolume(volume: number) {
250253
this.volume = volume;
254+
this.storage.setItem("volume", volume + "");
255+
this.setVolumeImplFn(volume);
251256
for (const next of this.onVolumeChanged) {
252257
next(this.volume);
253258
}

src/sound/audio-node.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ class SamplesQueue {
4141
}
4242
}
4343

44-
export function audioNode(ci: CommandInterface) {
44+
export function audioNode(ci: CommandInterface): (volume: number) => void {
4545
const sampleRate = ci.soundFrequency();
4646
const channels = 1;
4747

4848
if (sampleRate === 0) {
4949
console.warn("Can't create audio node with sampleRate === 0, ingnoring");
50-
return;
50+
return () => {};
5151
}
5252

5353
let audioContext: AudioContext | null = null;
@@ -66,7 +66,7 @@ export function audioNode(ci: CommandInterface) {
6666
}
6767

6868
if (audioContext == null) {
69-
return;
69+
return () => {};
7070
}
7171

7272
const samplesQueue = new SamplesQueue();
@@ -148,7 +148,12 @@ export function audioNode(ci: CommandInterface) {
148148
};
149149

150150
audioNode.onaudioprocess = ci.directSound !== undefined ? onDirectProcess : onQueueProcess;
151-
audioNode.connect(audioContext.destination);
151+
152+
const gainNode = audioContext.createGain();
153+
gainNode.connect(audioContext.destination);
154+
audioNode.connect(gainNode);
155+
156+
gainNode.gain.value = 1.0;
152157

153158
const resumeWebAudio = () => {
154159
if (audioContext !== null && audioContext.state === "suspended") {
@@ -163,11 +168,16 @@ export function audioNode(ci: CommandInterface) {
163168
ci.events().onExit(() => {
164169
if (audioContext !== null) {
165170
audioNode.disconnect();
171+
gainNode.disconnect();
166172
audioContext.close();
167173
}
168174

169175
document.removeEventListener("click", resumeWebAudio);
170176
document.removeEventListener("touchstart", resumeWebAudio);
171177
document.removeEventListener("keydown", resumeWebAudio);
172178
});
179+
180+
return (volume: number) => {
181+
gainNode.gain.value = volume;
182+
};
173183
}

0 commit comments

Comments
 (0)