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

Commit

Permalink
Merge pull request #2 from ng-web-apis/proper-reject
Browse files Browse the repository at this point in the history
fix(tokens): add Promise rejection for not supporting browsers
  • Loading branch information
waterplea committed Feb 11, 2020
2 parents 8d4d3d7 + e840490 commit 2ac2242
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ![ng-web-apis logo](https://github.com/ng-web-apis/midi/blob/master/projects/demo/src/assets/logo.svg) Web MIDI API for Angular
# ![ng-web-apis logo](projects/demo/src/assets/logo.svg) Web MIDI API for Angular

> Part of <img src="projects/demo/src/assets/web-api.svg" align="top"> [Web APIs for Angular](https://ng-web-apis.github.io/)
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"@angular/platform-browser-dynamic": "^7.2.15",
"@angular/platform-server": "^7.2.15",
"@angular/router": "^7.2.15",
"@ng-web-apis/audio": "latest",
"@ng-web-apis/common": "latest",
"@ng-web-apis/audio": "^1.2.1",
"@ng-web-apis/common": "^1.0.1",
"@nguniversal/common": "^7.1.1",
"@nguniversal/express-engine": "^7.1.1",
"@nguniversal/module-map-ngfactory-loader": "^7.1.1",
Expand Down
44 changes: 26 additions & 18 deletions projects/demo/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<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>
<p *ngIf="!supported">
Web MIDI API is not supported by your browser
</p>
<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)"
>
<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>
6 changes: 6 additions & 0 deletions projects/demo/src/app/app.component.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
:host {
perspective: 150vw;
user-select: none;
flex-direction: column;
align-items: center;
}

.start {
height: 30px;
}

.piano {
Expand Down
18 changes: 14 additions & 4 deletions projects/demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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'));
},
},
);
22 changes: 12 additions & 10 deletions projects/midi/src/tokens/midi-messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {inject, InjectionToken} from '@angular/core';
import {from, fromEvent, merge, Observable} from 'rxjs';
import {from, fromEvent, merge, Observable, throwError} from 'rxjs';
import {FromEventTarget} from 'rxjs/internal/observable/fromEvent';
import {switchMap} from 'rxjs/operators';
import {MIDI_ACCESS} from './midi-access';
Expand All @@ -11,16 +11,18 @@ export const MIDI_MESSAGES = new InjectionToken<Observable<MIDIMessageEvent>>(
{
providedIn: 'root',
factory: () =>
from(inject(MIDI_ACCESS)).pipe(
from(inject(MIDI_ACCESS).catch((e: Error) => e)).pipe(
switchMap(access =>
merge(
...Array.from(access.inputs).map(([_, input]) =>
fromEvent(
input as FromEventTarget<MIDIMessageEvent>,
'midimessage',
),
),
),
access instanceof Error
? throwError(access)
: merge(
...Array.from(access.inputs).map(([_, input]) =>
fromEvent(
input as FromEventTarget<MIDIMessageEvent>,
'midimessage',
),
),
),
),
),
},
Expand Down
16 changes: 16 additions & 0 deletions projects/midi/src/tokens/tests/midi-access.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,20 @@ describe('MIDI_ACCESS', () => {
sysex: true,
});
});

it('Promise is rejected when Web MIDI API is not supported', done => {
TestBed.configureTestingModule({
providers: [
{
provide: NAVIGATOR,
useValue: {},
},
],
});

TestBed.get(MIDI_ACCESS).catch((e: any) => {
expect(e instanceof Error).toBe(true);
done();
});
});
});

0 comments on commit 2ac2242

Please sign in to comment.