# @tonaljs/interval

> Parse and manipulate music intervals

Latest version **5.1.0** (published 2024-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tonaljs/interval
pnpm add @tonaljs/interval
yarn add @tonaljs/interval
bun add @tonaljs/interval
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.1.0 |
| Published | 2024-07-23 |
| First published | 2019-06-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 33.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | danigb@gmail.com |
| Maintainers | danigb |
| Keywords | interval, music, theory, tonal |

## Links

- npm: https://www.npmjs.com/package/@tonaljs/interval
- npm.io page: https://npm.io/package/@tonaljs/interval

## Dependencies (3)

- [@tonaljs/pitch](https://npm.io/package/@tonaljs/pitch.md) ^5.0.2
- [@tonaljs/pitch-distance](https://npm.io/package/@tonaljs/pitch-distance.md) ^5.0.4
- [@tonaljs/pitch-interval](https://npm.io/package/@tonaljs/pitch-interval.md) ^6.0.0

## Recent versions

- 5.1.0 (latest) — 2024-07-23
- 5.0.0 — 2024-02-03
- 4.8.2 — 2024-02-03
- 4.8.1 — 2024-01-04
- 4.8.0 — 2022-11-29
- 4.7.3 — 2022-11-29
- 4.7.2 — 2022-11-29
- 4.7.1 — 2022-11-29
- 4.7.0 — 2022-11-28
- 4.6.10 — 2022-11-18
- 4.6.5 — 2021-09-05
- 4.6.2 — 2021-07-12
- 4.5.1 — 2020-11-05
- 4.5.0 — 2020-11-04
- 3.5.4 — 2020-06-28
- … 12 more at https://npm.io/package/@tonaljs/interval/versions

## README

# @tonaljs/interval ![tonal](https://img.shields.io/badge/@tonaljs-interval-yellow.svg?style=flat-square) [![npm version](https://img.shields.io/npm/v/@tonaljs/interval.svg?style=flat-square)](https://www.npmjs.com/package/@tonaljs/interval)

> A collection of functions to create and manipulate music intervals

## Usage

ES6:

```js
import { Interval } from "tonal";
```

nodejs:

```js
const { Interval } = require("tonal");
```

## API

#### `Interval.get(name: string)`

Get properties of an interval:

- name: the interval name (number + quality)
- type: "perfectable" | "majorable"
- dir: direction: 1 | -1
- num: the interval number
- q: quality (...| 'dd' | 'd' | 'm' | 'M' | 'A' | ...)
- alt: the quality number as a number
- oct: the number of octaves it spans
- semitones: the number of semitones it spans
- simple: the simplified number

```js
Interval.get("5P"); // => { name: "5P", num: 5, ...}
```

There are some shorthand functions (`name`, `num`, `semitones`, `quality`):

```js
Interval.name("d4"); // => "4d"
Interval.num("5P"); // => 5
Interval.quality("5P"); // => "P"
Interval.semitones("P4"); // => 5
```

#### `distance(from: string, to: string) => string`

Find the interval between two notes.

```js
Interval.distance("C4", "G4"); // => "5P"
```

#### `names() => string[]`

Return a list of (natural) interval names:

```js
Interval.names(); // => ["1P", "2M", "3M", "4P", "5P", "6m", "7m"]
```

#### `fromSemitones(semitones: number) => string`

Given a number of semitones, returns the interval name:

```js
Interval.fromSemitones(7); // => "5P"
Interval.fromSemitones(-7); // => "-5P"

[0, 1, 2, 3, 4].map(Interval.fromSemitones);
```

#### `simplify(interval: string) => string`

Simplify an interval:

```js
Interval.simplify("9M"); // => "2M"
Interval.simplify("2M"); // => "2M"
Interval.simplify("-2M"); // => "7m"
["8P", "9M", "10M", "11P", "12P", "13M", "14M", "15P"].map(Interval.simplify);
// => [ "8P", "2M", "3M", "4P", "5P", "6M", "7M", "8P" ]
```

#### `invert(interval: string) => string`

Get the interval inversion:

```js
Interval.invert("3m"); // => "6M"
Interval.invert("2M"); // => "7m"
```

#### `add(a: string, b: string) => string`

Add two intervals:

```js
Interval.add("3m", "5P"); // => "7m"
```

#### `subtract(min: string, sub: string) => string`

Substract two intervals:

```js
subtract("5P", "3M"); // => '3m'
subtract("3M", "5P"); // => '-3m'
```

---
_Source: https://npm.io/package/@tonaljs/interval · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
