0.0.5 ā€¢ Published 4 years ago

ng-web-audio v0.0.5

Weekly downloads
4
License
WTFPL
Repository
github
Last release
4 years ago

Web Audio API for Angular

npm version npm bundle size Travis (.org) Coveralls github angular-open-source-starter

This is a library for declarative use of Web Audio API with Angular 6+. It is a complete conversion to declarative Angular directives, if you find any inconsistencies or errors, please file an issue. Watch out for šŸ’” emoji in this README for addition features and special use cases.

How to use

You can build audio graph with directives. For example, here's a typical echo feedback loop:

<audio src="/demo.wav" MediaElementAudioSourceNode>
    <ng-container #node="AudioNode" [DelayNode]="delayTime">
        <ng-container [GainNode]="gain">
            <ng-container [Output]="node"></ng-container>
            <ng-container AudioDestinationNode></ng-container>
        </ng-container>
    </ng-container>
    <ng-container AudioDestinationNode></ng-container>
</audio>

Note that single input nodes, such as GainNode or DelayNode use directive name as input alias for underlying gain or delayTime parameters. Whereas more complex nodes use respective parameters names.

šŸ’” AudioBufferService

This library has AudioBufferService with fetch method, returning Promise which allows you to easily turn your hosted audio file into AudioBuffer through GET requests. Result is stored in service's cache so same file is not requested again while application is running.

This service is also used within directives that have AudioBuffer inputs (such as AudioBufferSourceNode or ConvolverNode) so you can just pass string URL, as well as an actual AudioBuffer. For example:

<button
    #source="AudioNode"
    buffer="/demo.wav"
    AudioBufferSourceNode
    (click)="source.start()"
>
    Play
    <ng-container AudioDestinationNode></ng-container>
</button>

Supported nodes

You can use following audio nodes through directives of the same name:

Terminal nodes

Sources

  • AudioBufferSourceNode

    šŸ’” Additionally supports setting URL to media file as buffer so it will be fetched and turned into AudioBuffer

    šŸ’” Additionally supports empty autoplay attribute similar to audio tag so it would start playing immediately

  • MediaElementAudioSourceNode

  • OscillatorNode

    šŸ’” Additionally supports empty autoplay attribute similar to audio tag so it would start playing immediately

Processors

šŸ’” Special cases

šŸ’” Tokens

  • You can inject BaseAudioContext through AUDIO_CONTEXT token
  • AudioContext is created by default with default options when token is requested
  • You can also provide custom BaseAudioContext through that token
  • All node directives provide underlying AudioNode as AUDIO_NODE token

Browser support

12+31+34+9+

Note that some features (AudioWorklet etc.) were added later and are supported only by more recent versions

IMPORTANT: You must add ng-web-audio/polyfill to polyfills.ts used in angular.json if you want to support Safari

šŸ’” StereoPannerNode is emulated with PannerNode in browsers that do not support it yet

šŸ’” positionX (orientationX) and other similar properties of AudioListener and PannerNode fall back to setPosition (setOrientation) method if browser does not support it

Demo

You can try online demo here

TODO

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago