Skip to content

Commit 77fe75e

Browse files
committed
Soundpill bugfix and Systemwide default device
1 parent 070e540 commit 77fe75e

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

apps/frontend/src/app/actions/playsound/audio.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EventEmitter } from "events";
2+
import { settingsLabels } from '@lunchpad/types';
23

34
export class Sound extends EventEmitter {
45
public audioContext: AudioContext;
@@ -59,7 +60,10 @@ export class Sound extends EventEmitter {
5960
this.audioElement.play();
6061
this.bufferSource.start(0, begin, duration);
6162
//@ts-ignore //IT DOES EXIST
62-
this.audioElement.setSinkId(this.sinkId);
63+
console.log(this.sinkId);
64+
if (this.sinkId === "inherit") {
65+
this.audioElement.setSinkId(localStorage.getItem(settingsLabels.soundOutput) || "default")
66+
} else this.audioElement.setSinkId(this.sinkId);
6367
})
6468
}
6569

apps/frontend/src/app/actions/playsound/classes.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,29 @@ export class PlaySound extends Action {
3737
this.start = 0;
3838
this.end = 1;
3939
this.outputDevice = outputDevice;
40-
}
4140

41+
this.InitSound();
42+
}
43+
private async InitSound() {
44+
if (!this.soundfile) return;
45+
try {
46+
const response = await fetch(this.soundfile);
47+
const arrayBuffer = await response.arrayBuffer();
48+
const buffer = await this.audioContext.decodeAudioData(arrayBuffer);
49+
this.duration = buffer.duration
50+
this.sound = new Sound(
51+
this.soundfile,
52+
this.outputDevice,
53+
this.volume,
54+
this.start,
55+
this.end
56+
)
57+
this.sound.audioContext = this.audioContext;
58+
} catch (ex) {}
59+
}
4260
public setAudioContext = (audio: AudioContext) => this.audioContext = audio;
4361
public async Run(): Promise<unknown> {
44-
console.log("Running")
45-
const response = await fetch(this.soundfile);
46-
const arrayBuffer = await response.arrayBuffer();
47-
const buffer = await this.audioContext.decodeAudioData(arrayBuffer);
48-
this.duration = buffer.duration
49-
this.sound = new Sound(
50-
this.soundfile,
51-
this.outputDevice,
52-
this.volume,
53-
this.start,
54-
this.end
55-
)
56-
this.sound.audioContext = this.audioContext;
62+
await this.InitSound();
5763
await this.sound.Init()
5864
if (this.sound) {
5965
return this.sound.Play();

apps/frontend/src/app/actions/playsound/component.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as React from 'react';
22
import lodash from 'lodash';
33

4-
import { IMediaDevice } from '@lunchpad/types';
5-
import { useAnimationFrame } from '@lunchpad/hooks';
4+
import { IMediaDevice, settingsLabels } from '@lunchpad/types';
5+
import { useAnimationFrame, useLocalStorage } from '@lunchpad/hooks';
66
import { Icon, TriangleRight, Rectangle, Sound } from '@lunchpad/icons';
77
import { FileURI } from '@lunchpad/types';
88

@@ -33,6 +33,7 @@ interface AudioBufferSourceNodeExtra extends AudioBufferSourceNode {
3333
export const PlaySoundPill: React.SFC<IPlaySoundPill> = (props) => {
3434
const [ showBody, setExpanded ] = React.useState<boolean>(props.expanded);
3535
const { audio } = React.useContext(AudioContext.Context);
36+
const [ defaultOutput ] = useLocalStorage<string>(settingsLabels.soundOutput, "default");
3637

3738
const [ audioBuffer, setAudioBuffer ] = React.useState<AudioBuffer>();
3839
const [ player, setPlayer ] = React.useState<AudioBufferSourceNodeExtra>();
@@ -160,6 +161,7 @@ export const PlaySoundPill: React.SFC<IPlaySoundPill> = (props) => {
160161
value={props.action.outputDevice}
161162
onChange={e => setProp({ outputDevice: e.target.value })}
162163
>
164+
<option value="inherit">Lunchpad Default ({props.outputDevices.find(d => d.deviceId === defaultOutput)?.label})</option>
163165
{props.outputDevices.map(e => <option key={e.deviceId} value={e.deviceId}>{e.label}</option>)}
164166
</Select>
165167
</Row>

apps/frontend/src/app/components/Controller/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as Devices from '../../controller';
1717
import { Playground } from '../../contexts/playground';
1818
import { MacroContext } from '../../contexts/macro/index';
1919
import { LayoutContext } from '../../contexts/layout';
20-
import { LaunchpadButton, LaunchpadButtonLookText, LaunchpadButtonLookType, LaunchpadSolidButtonColor } from '../../contexts/layout/classes';
20+
import { LaunchpadButton, LaunchpadButtonLookText, LaunchpadButtonLookType, LaunchpadSolidButtonColor, Page } from '../../contexts/layout/classes';
2121
import { PushToTalkEnd, PushToTalkStart } from '../../actions/pushtotalk';
2222
import { ActionType } from '../../actions';
2323
import { PlaySound } from '../../actions/playsound';
@@ -86,7 +86,9 @@ export default () => {
8686
const editButton = (x: number, y: number, limitedColor = false) => {
8787
const pageId = activePage.id
8888

89-
const button = lodash.get(activePage, `buttons.${x}.${y}`, new LaunchpadButton());
89+
const button: LaunchpadButton = lodash.get<Page, any, LaunchpadButton>(activePage, `buttons.${x}.${y}`, new LaunchpadButton());
90+
button.look = new LaunchpadButtonLookText("New");
91+
9092
stopSpecific(x, y);
9193
showConfigDialog(
9294
<ConfigDialog

libs/contexts/src/audio/useMediaDevices.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { useEffect, useState } from 'react';
22

33
const noop = () => {};
44

5+
const updateName = (device: Partial<MediaDeviceInfo>) => {
6+
if (device.deviceId === "default" || device.deviceId === "communications") {
7+
return `System ${device.label}`
8+
} else return device.label
9+
}
510
const useMediaDevices = (filter: 'all' | 'audiooutput' | 'audioinput' = "all") => {
611
const [state, setState] = useState([]);
712

@@ -13,7 +18,7 @@ const useMediaDevices = (filter: 'all' | 'audiooutput' | 'audioinput' = "all") =
1318
.enumerateDevices()
1419
.then(devices => {
1520
if (mounted) {
16-
let dev = devices.map(({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label }))
21+
let dev = devices.map(({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label: updateName({ deviceId, groupId, kind, label }) }))
1722
if (filter !== "all") {
1823
dev = [...dev].filter(({ kind }) => kind === filter);
1924
}

0 commit comments

Comments
 (0)