Notex

Hex.pmDocs

A music theory library for Elixir — work with notes, scales, and chords(currently building).

Why did i build this

I wanted to build an ear training webapp using mainly elixir.

To make the browser sing, i chose to work with tone.js.

A note in tone.js is a string representation of Scientific pitch notation.

Thus i decided to use that as my foundation to build a music theory library in elixir, so that a web application can sing all sorts of lovely notes, scales and chords.

Installation

Add notex to your list of dependencies in mix.exs:

def deps do
  [
    {:notex, "~> 0.1"}
  ]
end

Quick Start

Notes

Create notes with Note.new/2 or the ~n sigil:

use Notex

note = ~n[C4]
# => ~n[C4]

{:ok, note} = Note.new("G#", 5)
# => {:ok, ~n[G#5]}

Parse note strings:

{:ok, note} = Note.parse("Ab3")
# => {:ok, ~n[G#3]}  (flats are normalized to sharps)

Transpose notes by semitones:

use Notex

Note.transpose!(~n[C4], 7)
# => ~n[G4]

Note.transpose!(~n[B4], 1)
# => ~n[C5]  (crosses octave boundary)

Scales

Build scales from a tonic note and a scale type:

use Notex

Scale.notes!(~n[C4], :major)
# => [~n[C4], ~n[D4], ~n[E4], ~n[F4], ~n[G4], ~n[A4], ~n[B4]]

Scale.notes!(~n[A4], :minor)
# => [~n[A4], ~n[B4], ~n[C5], ~n[D5], ~n[E5], ~n[F5], ~n[G5]]

Chords

Build chord notes from a base note with Chord.notes/2:

use Notex

Chord.notes(Chord.major7(), ~n[C4])
# => {:ok, [~n[C4], ~n[E4], ~n[G4], ~n[B4]]}

c9sus4 =
  Chord.dominant7()
  |> Chord.sus4()
  |> Chord.add9()

Chord.notes(c9sus4, ~n[C4])
# => {:ok, [~n[C4], ~n[F4], ~n[G4], ~n[A#4], ~n[D5]]}

Custom Scale Types

Define your own scale types by implementing the Notex.ScaleType behaviour:

defmodule MyApp.MinorPentatonic do
  @behaviour Notex.ScaleType

  def name, do: "minor pentatonic"
  def intervals, do: [:one, :flat_three, :four, :five, :flat_seven]
end

use Notex

Scale.notes!(~n[A4], MyApp.MinorPentatonic)
# => [~n[A4], ~n[C5], ~n[D5], ~n[E5], ~n[G5]]

Documentation

Full documentation is available on HexDocs.

Development

i use mise to manage elixir and erlang version. if you have mise installed on your machine, to get started simply run

mise install

mix deps.get

mix test

You can run the full CI suite with either:

just ci