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

Commit

Permalink
fix(tokens): add Promise rejection for not supporting browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Feb 10, 2020
1 parent 8d4d3d7 commit b341d49
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
41 changes: 23 additions & 18 deletions projects/demo/src/app/app.component.html
@@ -1,20 +1,25 @@
<section *ngIf="notes$ | async as notes" class="piano">
<ng-container
*ngFor="let note of notes | keyvalue; trackBy: noteKey"
waOscillatorNode
type="sawtooth"
autoplay
[frequency]="toFrequency(note.key) | waAudioParam: 0.2"
>
<ng-container waGainNode gain="0" [gain]="note.value | waAudioParam: 0.01">
<ng-container waAudioDestinationNode></ng-container>
<button *ngIf="!started; else piano" class="start" (click)="start()">
Start AudioContext
</button>
<ng-template #piano>
<section *ngIf="notes$ | async as notes" class="piano">
<ng-container
*ngFor="let note of notes | keyvalue; trackBy: noteKey"
waOscillatorNode
type="sawtooth"
autoplay
[frequency]="toFrequency(note.key) | waAudioParam: 0.2"
>
<ng-container waGainNode gain="0" [gain]="note.value | waAudioParam: 0.01">
<ng-container waAudioDestinationNode></ng-container>
</ng-container>
</ng-container>
</ng-container>

<div
*ngFor="let key of octaves"
[class]="getClass(notes, key)"
(mousedown)="onMouseDown(key)"
(touchstart)="onMouseDown(key)"
></div>
</section>
<div
*ngFor="let key of octaves"
[class]="getClass(notes, key)"
(mousedown)="onMouseDown(key)"
(touchstart)="onMouseDown(key)"
></div>
</section>
</ng-template>
5 changes: 5 additions & 0 deletions projects/demo/src/app/app.component.less
Expand Up @@ -3,6 +3,11 @@
user-select: none;
}

.start {
align-self: flex-start;
height: 30px;
}

.piano {
margin: -12.5vw auto 12.5vw;
height: 50vw;
Expand Down
18 changes: 14 additions & 4 deletions projects/demo/src/app/app.component.ts
@@ -1,7 +1,7 @@
import {ChangeDetectionStrategy, Component, HostListener, Inject} from '@angular/core';
import {MIDI_MESSAGES, notes, toData, toFrequency} from '@ng-web-apis/midi';
import {merge, Observable, Subject} from 'rxjs';
import {map, scan, startWith, switchMap} from 'rxjs/operators';
import {MIDI_MESSAGES, MIDI_SUPPORT, notes, toData, toFrequency} from '@ng-web-apis/midi';
import {EMPTY, merge, Observable, Subject} from 'rxjs';
import {catchError, map, scan, startWith, switchMap} from 'rxjs/operators';

import MIDIMessageEvent = WebMidi.MIDIMessageEvent;

Expand All @@ -12,6 +12,8 @@ import MIDIMessageEvent = WebMidi.MIDIMessageEvent;
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
started = false;

readonly octaves = Array.from({length: 24}, (_, i) => i + 48);

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

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

constructor(@Inject(MIDI_MESSAGES) messages$: Observable<MIDIMessageEvent>) {
constructor(
@Inject(MIDI_SUPPORT) readonly supported: boolean,
@Inject(MIDI_MESSAGES) messages$: Observable<MIDIMessageEvent>,
) {
const mouseInitiated$ = this.mousedown$.pipe(
switchMap(down =>
this.mouseup$.pipe(
Expand All @@ -32,6 +37,7 @@ export class AppComponent {

this.notes$ = merge(
messages$.pipe(
catchError(() => EMPTY),
notes(),
toData(),
),
Expand All @@ -42,6 +48,10 @@ export class AppComponent {
);
}

start() {
this.started = true;
}

noteKey({key}: {key: number}): number {
return key;
}
Expand Down
4 changes: 3 additions & 1 deletion projects/midi/src/tokens/midi-access.ts
Expand Up @@ -12,7 +12,9 @@ export const MIDI_ACCESS = new InjectionToken<Promise<MIDIAccess>>(
const navigatorRef = inject(NAVIGATOR);
const sysex = inject(SYSEX);

return navigatorRef.requestMIDIAccess({sysex});
return navigatorRef.requestMIDIAccess
? navigatorRef.requestMIDIAccess({sysex})
: Promise.reject(new Error('Web MIDI API is not supported'));
},
},
);

0 comments on commit b341d49

Please sign in to comment.