Skip to content

Instantly share code, notes, and snippets.

@danigb
Created August 13, 2018 09:31
Show Gist options
  • Save danigb/81a99cedfc2cf038546ccb94117a21e6 to your computer and use it in GitHub Desktop.
Save danigb/81a99cedfc2cf038546ccb94117a21e6 to your computer and use it in GitHub Desktop.
Pitch classes et al
// Duration
export type Duration = number;
const PPQ: Duration = 960;
export const wn = 4 * PPQ; // whole note duration
export const hn = (1 / 2) * wn; // half note duration
export const qn = (1 / 4) * wn; // quarter note duration
export const en = (1 / 8) * wn; // eight note duration
export const sn = (1 / 16) * wn; // sixteenth note duration
export const tn = (1 / 32) * wn; // thirty-second note duration
export const sfn = (1 / 64) * wn; // sixty-fourth note duration
export const dwn = (3 / 2) * wn; // dotted whole note duration
export const dhn = (3 / 4) * wn; // dotted half note duration
export const dqn = (3 / 8) * wn; // dotted quarter note duration
export const den = (3 / 16) * wn; // dotted eight note duration
export const dsn = (3 / 32) * wn; //dotted sixteenth note duration
export const dtn = (3 / 64) * wn; //dotted thirty-second note duration
export const ddhn = (7 / 8) * wn; // double-dotted half note duration
export const ddqn = (7 / 16) * wn; // double-dotted quarter note duration
export const dden = (7 / 32) * wn; // double-dotted eightnote duration
export type Midi = number;
export type Octave = number;
export enum PitchClass {
Cff = "Cbb",
Cf = "Cb",
C = "C",
Dff = "Dbb",
Cs = "C#",
Df = "Db",
Css = "C##",
D = "D",
Eff = "Ebb",
Ds = "D#",
Ef = "Eb",
Fff = "F##",
Dss = "D##",
E = "E",
Ff = "Fb",
Es = "E#",
F = "F",
Gff = "Gbb",
Ess = "E##",
Fs = "F#",
Gf = "Gb",
Fss = "F##",
G = "G",
Aff = "Abb",
Gs = "G#",
Af = "Ab",
Gss = "G##",
A = "A",
Bff = "Bbb",
As = "A#",
Bf = "Bb",
Ass = "A##",
B = "B",
Bs = "B#",
Bss = "B##"
}
export type Pitch = ["pitch", PitchClass, Octave];
const ACCIDENTALS = ["bb", "b", "", "#", "##"];
const pitch = (base: PitchClass) => (oct: Octave, alt: number = 0): Pitch => {
const acc = ACCIDENTALS[alt + 2];
return ["pitch", base + acc, oct] as Pitch;
};
export function isPitch(p: Pitch | Array<any>): p is Pitch {
return p[0] === "pitch";
}
export const C = pitch(PitchClass.C);
export const D = pitch(PitchClass.D);
export const E = pitch(PitchClass.E);
export const F = pitch(PitchClass.F);
export const G = pitch(PitchClass.G);
export const A = pitch(PitchClass.A);
export const B = pitch(PitchClass.B);
export interface Music {
duration: Duration;
}
export interface Primitive extends Music {
pitch: Pitch | null;
}
export interface Rest extends Primitive {
pitch: null;
}
export interface Note extends Primitive {
pitch: Pitch;
[propName: string]: any;
}
export function rest(duration = PPQ): Rest {
return { duration, pitch: null };
}
export const Nothing = rest(0);
export function note(pitch: Pitch, duration = PPQ): Note {
return { pitch, duration };
}
export function strToNote(str: string, duration = PPQ): Primitive {
// FIXME: needs implementation
return Nothing;
}
export function midiToNote(midi: Midi, duration = PPQ): Primitive {
// FIXME: needs implementation
return Nothing;
}
export function noteToMidi(note: Note): Midi {
return 0;
}
export type Tempo = number;
export type Transpose = number;
export enum Mode {
Major = "major",
Minor = "minor"
}
export type KeySig = [PitchClass, Mode];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment