> For AI agents: the complete documentation index is available at https://anyformat.swc.rs/llms.txt, the full documentation bundle is available at https://anyformat.swc.rs/llms-full.txt.

# Audio

The audio family identifies audio formats from the leading bytes of a file.

| Language | Package                | Module                 |
| -------- | ---------------------- | ---------------------- |
| Node.js  | `@swc/anyformat-audio` | `@swc/anyformat-audio` |
| Python   | `anyformat-audio`      | `anyformat_audio`      |

## Supported formats

| Identifier | MIME type    | Format                                           |
| ---------- | ------------ | ------------------------------------------------ |
| `mp3`      | `audio/mpeg` | MPEG-1/2 Audio Layer III                         |
| `aac`      | `audio/aac`  | Advanced Audio Coding in an ADTS stream          |
| `m4a`      | `audio/mp4`  | MPEG-4 audio (`.m4a`, `.m4b`)                    |
| `flac`     | `audio/flac` | Free Lossless Audio Codec                        |
| `wav`      | `audio/wav`  | Waveform Audio File Format                       |
| `aiff`     | `audio/aiff` | Audio Interchange File Format, including AIFF-C  |
| `opus`     | `audio/opus` | Opus in an Ogg container                         |
| `ogg`      | `audio/ogg`  | Ogg with a codec other than Opus, usually Vorbis |
| `midi`     | `audio/midi` | Standard MIDI File                               |
| `amr`      | `audio/amr`  | Adaptive Multi-Rate audio                        |

## `detect`

Identifies the audio format of `data` from its leading bytes. Returns `null` in
Node.js and `None` in Python when the format is not recognized.

`detect` skips a leading ID3v2 tag. Give it the whole tag and a few bytes of the
audio stream after the tag.

### Node.js

```ts
function detect(data: Uint8Array): DetectedFormat | null
```

```js
import { readFile } from 'node:fs/promises'
import { detect } from '@swc/anyformat-audio'

detect(await readFile('song.flac'))
// => { format: 'flac', mimeType: 'audio/flac' }
```

### Python

```python
def detect(data: bytes) -> DetectedFormat | None
```

```python
from pathlib import Path

import anyformat_audio

anyformat_audio.detect(Path("song.flac").read_bytes())
# => DetectedFormat(format='flac', mime_type='audio/flac')
```

## `DetectedFormat`

A format identified by `detect`.

| Node.js    | Python      | Type     | Description                                                         |
| ---------- | ----------- | -------- | ------------------------------------------------------------------- |
| `format`   | `format`    | `string` | The stable lowercase identifier of the format, for example `"mp3"`. |
| `mimeType` | `mime_type` | `string` | The most common MIME type of the format.                            |

In Python, `DetectedFormat` is a final class with read-only properties, and the
type of each property is `str`.

## `supportedFormats`

Returns the identifier of each format that `detect` can return.

### Node.js

```ts
function supportedFormats(): string[]
```

```js
import { supportedFormats } from '@swc/anyformat-audio'

supportedFormats()
// => ['mp3', 'aac', 'm4a', 'flac', 'wav', 'aiff', 'opus', 'ogg', 'midi', 'amr']
```

### Python

```python
def supported_formats() -> list[str]
```

```python
import anyformat_audio

anyformat_audio.supported_formats()
# => ['mp3', 'aac', 'm4a', 'flac', 'wav', 'aiff', 'opus', 'ogg', 'midi', 'amr']
```
