# Multitrack audio editor — library for developers

Engine of a multitrack audio editor: decoding, cutting and joining audio without clicks, fades and volume, live playback, mixdown and export to WAV or MP3, projects in the browser storage.

## Installation

```sh
npm install @openlibsdev/audio-editor
```

```js
import AudioEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/audio-editor@1.0.0/+esm";
```

## Usage

```js
import AudioEditor from "@openlibsdev/audio-editor";

const file = document.querySelector("input[type=file]").files[0];
const buffer = await AudioEditor.decodeAudio(await file.arrayBuffer());
const part = AudioEditor.sliceAudioBuffer(buffer, 0, Math.min(2, buffer.duration));
const wav = AudioEditor.audioBufferToWav(part);           // a Blob
const mp3 = await AudioEditor.encodeToFormat(part, "mp3", 192);
```

## API

### `AudioEditor.decodeAudio(arrayBuffer)`

Decodes an audio file (ArrayBuffer) into an AudioBuffer.

### `AudioEditor.sliceAudioBuffer(buffer, startSec, endSec)`

Cuts out a piece from start to end (seconds) as a new AudioBuffer; the edges get a short fade against clicks.

### `AudioEditor.spliceOutRange(buffer, startSec, endSec)`

Removes a piece and joins the rest.

### `AudioEditor.computePeak(buffer)`

Highest sample level, 0–1.

### `AudioEditor.audioBufferToWav(buffer)`

Encodes an AudioBuffer to a WAV Blob.

### `AudioEditor.audioBufferToMp3(buffer, kbps)`

Encodes an AudioBuffer to an MP3 Blob at the given bitrate.

### `AudioEditor.encodeToFormat(audioBuffer, format, bitrate)`

Encodes to "wav" or "mp3".

### `AudioEditor.renderMixdown(tracks, opts)`

Mixes tracks into one AudioBuffer, with their position, pan, volume, fades, mute and solo.

### `AudioEditor.renderSingleTrack(track, opts)`

Renders one track with its volume and fades.

### `AudioEditor.startAudio()`

Starts the audio context; call it from a user action (a click).

### `AudioEditor.startLive(track, opts)`

Plays a track live.

### `AudioEditor.stopLive(live)`

Stops live playback.

### `AudioEditor.saveProjectToDb(project)`

Saves a project to the browser storage (IndexedDB).

### `AudioEditor.loadProjectFromDb(id)`

Loads a saved project by its id.

### `AudioEditor.now()`

Current time of the audio clock in seconds (the time base for startLive and scheduleTrackFades).

### `AudioEditor.scheduleTrackFades(gainParam, track, startAtCtxTime, segmentStartOffsetInTrack, segmentDuration, audible, masterVolume)`

Schedules a track's volume on a gain parameter: fade in, fade out, track volume and master volume, or silence when the track is muted.

### `AudioEditor.APP`

Name and version of the library: {name, version}.

## Dependencies

- @breezystack/lamejs 1.2.7 (LGPL-3.0)
- tone 15 (MIT)
- wavesurfer.js 7 (BSD-3-Clause)
- wavesurfer.js 7 (BSD-3-Clause)
- wavesurfer.js 7 (BSD-3-Clause)

- GitHub: https://github.com/OpenLibsDev/audio-editor
- npm: https://www.npmjs.com/package/@openlibsdev/audio-editor
- Open the app: https://openlibs.dev/en/app/audio-editor/
