## API

- [**Methods**](#methods)
- [**Properties**](#properties)
  - [**Advanced**](#advanced)

### Methods

#### `new VisualizerMicro(source, callback)`
The constructor that spawns an instance of the library.

For convenience, after initializing defaults, it acts as an alias to `load()`.

##### Params
See [`load()`](#loadsource-callback) method.

##### Returns
`VisualizerMicro` instance, an isolated process that loads a single audio source. Interface for the rest of the API.

##### Example
```js

import VisualizerMicro from 'visualizer-micro';

const vm = new VisualizerMicro();

vm.supported // returns bool

```

#### `.load(source, callback)`
Call that loads an audio source into the VisualizerMicro instance. Only 1 source per instance.

##### Params
`source`: Instance of HTML `<audio/>` element, `Audio` class, or `MediaStream` class.

`callback`: Function called when audio source has been ingested and is ready to play.

##### Returns
`boolean`

Returns `true` if load execution completes. Capturing this return isnt required, but can be helpful.

##### Example
```js

const audio = new Audio('some url');

vm.load(audio, () => {
  audio.play();
  vm.getWaveform(); // returns data
  // vm.loaded; // returns true
});

```

#### `.unload()`
Removes event listeners and resets the VisualizerMicro instance.

##### Returns
`boolean`

Returns `true` if unload execution completes.

##### Example
```js
// on unmount

if (vm.loaded) {
  vm.unload();
}

```

#### `.getSpectrum()`
Returns an easily consumable array of decibel values for the spread of frequencies from a playing audio source. Will throw if `.loaded` or `.supported` are falsy.

##### Returns
`array` of floats from 0 to 1.

For reference, values are relative to the data returned from the AnalyerNode's [`getByteFrequencyData()`][mdn-getSpectrum] call.

##### Example
```js
// loaded and playing

setInterval(() => {
  const frameData = vm.getSpectrum();

  frameData.map((value, index) => {
    // some animation based on value
  });
}, 60/1000);

```

#### `.getWaveform()`
Returns an easily consumable array of the current time-domain (waveform) of a playing audio source. Will throw if `.loaded` or `.supported` are falsy.

##### Returns
`array` of floats from -1 to 1.

For reference, values are relative to the data returned from the AnalyerNode's [`getByteTimeDomainData()`][mdn-getWaveform] call.

##### Example
```js
// loaded and playing

setInterval(() => {
  const frameData = vm.getWaveform();

  frameData.map((value, index) => {
    value = Math.abs(value);

    // some animation based on value
  });
}, 60/1000);

```

---

### Properties

#### .loaded
`boolean`

`false` unless an audio source has been loaded using the constructor or through a `load()` call. Is reset to `false` when `unload()` is called.

#### .loading
`boolean`

`true` once `load()` is called, during its execution, and until the audio source is ready to play.

Not required, but can be helpful in preventing duplicate `load()` without the need for more state.

#### .supported
`boolean`

Returns whether the Web Audio API is present in the current JS context. Checks for the `AudioContext` class globally.

### Advanced
These properties can be ignored in most cases, but are available if needed.

#### .analyser
`AnalyserNode`

The specific instance of the Web Audio API [AnalyserNode][mdn-AnalyserNode] used by this instance of VisualizerMicro.

#### .context
`AudioContext`

The specific instance of the Web Audio API [AudioContext][mdn-AudioContext] used by this instance of VisualizerMicro.

#### .isMediaStream
`boolean`

Returns whether the audio source loaded is a MediaStream. Mostly used internally, but can be helpful for consistent conditionals.

[mdn-getSpectrum]: https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData
[mdn-getWaveform]: https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteTimeDomainData
[mdn-AnalyserNode]: https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode
[mdn-AudioContext]: https://developer.mozilla.org/en-US/docs/Web/API/AudioContext
