Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.

Commit b341d49

Browse files
committed
fix(tokens): add Promise rejection for not supporting browsers
1 parent 8d4d3d7 commit b341d49

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed
+23-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
<section *ngIf="notes$ | async as notes" class="piano">
2-
<ng-container
3-
*ngFor="let note of notes | keyvalue; trackBy: noteKey"
4-
waOscillatorNode
5-
type="sawtooth"
6-
autoplay
7-
[frequency]="toFrequency(note.key) | waAudioParam: 0.2"
8-
>
9-
<ng-container waGainNode gain="0" [gain]="note.value | waAudioParam: 0.01">
10-
<ng-container waAudioDestinationNode></ng-container>
1+
<button *ngIf="!started; else piano" class="start" (click)="start()">
2+
Start AudioContext
3+
</button>
4+
<ng-template #piano>
5+
<section *ngIf="notes$ | async as notes" class="piano">
6+
<ng-container
7+
*ngFor="let note of notes | keyvalue; trackBy: noteKey"
8+
waOscillatorNode
9+
type="sawtooth"
10+
autoplay
11+
[frequency]="toFrequency(note.key) | waAudioParam: 0.2"
12+
>
13+
<ng-container waGainNode gain="0" [gain]="note.value | waAudioParam: 0.01">
14+
<ng-container waAudioDestinationNode></ng-container>
15+
</ng-container>
1116
</ng-container>
12-
</ng-container>
1317

14-
<div
15-
*ngFor="let key of octaves"
16-
[class]="getClass(notes, key)"
17-
(mousedown)="onMouseDown(key)"
18-
(touchstart)="onMouseDown(key)"
19-
></div>
20-
</section>
18+
<div
19+
*ngFor="let key of octaves"
20+
[class]="getClass(notes, key)"
21+
(mousedown)="onMouseDown(key)"
22+
(touchstart)="onMouseDown(key)"
23+
></div>
24+
</section>
25+
</ng-template>

projects/demo/src/app/app.component.less

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
user-select: none;
44
}
55

6+
.start {
7+
align-self: flex-start;
8+
height: 30px;
9+
}
10+
611
.piano {
712
margin: -12.5vw auto 12.5vw;
813
height: 50vw;

projects/demo/src/app/app.component.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ChangeDetectionStrategy, Component, HostListener, Inject} from '@angular/core';
2-
import {MIDI_MESSAGES, notes, toData, toFrequency} from '@ng-web-apis/midi';
3-
import {merge, Observable, Subject} from 'rxjs';
4-
import {map, scan, startWith, switchMap} from 'rxjs/operators';
2+
import {MIDI_MESSAGES, MIDI_SUPPORT, notes, toData, toFrequency} from '@ng-web-apis/midi';
3+
import {EMPTY, merge, Observable, Subject} from 'rxjs';
4+
import {catchError, map, scan, startWith, switchMap} from 'rxjs/operators';
55

66
import MIDIMessageEvent = WebMidi.MIDIMessageEvent;
77

@@ -12,6 +12,8 @@ import MIDIMessageEvent = WebMidi.MIDIMessageEvent;
1212
changeDetection: ChangeDetectionStrategy.OnPush,
1313
})
1414
export class AppComponent {
15+
started = false;
16+
1517
readonly octaves = Array.from({length: 24}, (_, i) => i + 48);
1618

1719
readonly notes$: Observable<Map<number, number>>;
@@ -20,7 +22,10 @@ export class AppComponent {
2022

2123
readonly mouseup$ = new Subject<void>();
2224

23-
constructor(@Inject(MIDI_MESSAGES) messages$: Observable<MIDIMessageEvent>) {
25+
constructor(
26+
@Inject(MIDI_SUPPORT) readonly supported: boolean,
27+
@Inject(MIDI_MESSAGES) messages$: Observable<MIDIMessageEvent>,
28+
) {
2429
const mouseInitiated$ = this.mousedown$.pipe(
2530
switchMap(down =>
2631
this.mouseup$.pipe(
@@ -32,6 +37,7 @@ export class AppComponent {
3237

3338
this.notes$ = merge(
3439
messages$.pipe(
40+
catchError(() => EMPTY),
3541
notes(),
3642
toData(),
3743
),
@@ -42,6 +48,10 @@ export class AppComponent {
4248
);
4349
}
4450

51+
start() {
52+
this.started = true;
53+
}
54+
4555
noteKey({key}: {key: number}): number {
4656
return key;
4757
}

projects/midi/src/tokens/midi-access.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const MIDI_ACCESS = new InjectionToken<Promise<MIDIAccess>>(
1212
const navigatorRef = inject(NAVIGATOR);
1313
const sysex = inject(SYSEX);
1414

15-
return navigatorRef.requestMIDIAccess({sysex});
15+
return navigatorRef.requestMIDIAccess
16+
? navigatorRef.requestMIDIAccess({sysex})
17+
: Promise.reject(new Error('Web MIDI API is not supported'));
1618
},
1719
},
1820
);

0 commit comments

Comments
 (0)