Skip to content

Commit

Permalink
feat: add functions to convert between types of note representations …
Browse files Browse the repository at this point in the history
…- freqToMidi, pitchToMidi, etc
  • Loading branch information
Seanitzel committed Aug 19, 2023
1 parent bcdee5b commit 4e257d7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/utilities/ScientificFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export function freqFromPitch(note: Note): number {
) * PITCH_STANDARD.frequency;
}

export function freqToPitch(frequency: number): Note {
const midi = freqToMidi(frequency);
const pitchClass = midi % NUMBER_OF_PITCH_CLASSES;
const octave = Math.floor(midi / NUMBER_OF_PITCH_CLASSES) - 1;
const note = `${FLAT_CLASS_NOTES[pitchClass]}${octave}`;
return note as Note;
}

/**
* Turns a midi value to frequency.
* @param {Number} midi
Expand Down Expand Up @@ -62,3 +70,23 @@ export function centsOffFromFreq(frequency: number, midi: number): number {
export function midiToFreq(realNumber: number): number {
return PITCH_STANDARD.frequency * (Math.pow(2, (realNumber - PITCH_STANDARD.midi) / NUMBER_OF_PITCH_CLASSES));
}

/**
* Convert MIDI value to pitch.
* @param {Number} midi
* @returns {Note}
*/
export function midiToPitch(midi: number): Note {
const freq = freqFromMidi(midi);
return freqToPitch(freq);
}

/**
* Convert pitch to MIDI value.
* @param {Note} pitch
* @returns {number}
*/
export function pitchToMidi(note: Note): number {
const freq = freqFromPitch(note);
return freqToMidi(freq);
}
38 changes: 32 additions & 6 deletions test/utilities/scientific-functions.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { freqFromPitch, freqToMidi, midiToFreq } from '../../lib/index.js';
import { freqFromPitch, freqToMidi, midiToFreq, freqToPitch, midiToPitch, pitchToMidi } from '../../lib/index.js';

describe('Scientific functions', () => {
describe.only('Scientific functions', () => {
it('#freqFromPitch', () => {
const note = 'A4';
const pitch2 = 'E5';
expect(freqFromPitch(note)).to.eql(440);
expect(Math.ceil(freqFromPitch(pitch2))).to.eql(660);
expect(freqFromPitch('A4')).to.eql(440);
expect(Math.ceil(freqFromPitch('E5'))).to.eql(660);
expect(Math.ceil(freqFromPitch('F4'))).to.eql(350);
expect(Math.ceil(freqFromPitch('C4'))).to.eql(262);
expect(Math.ceil(freqFromPitch('Db4'))).to.eql(278);
expect(Math.ceil(freqFromPitch('C#4'))).to.eql(278);
});

it('#freqToMidi', () => {
Expand All @@ -17,4 +19,28 @@ describe('Scientific functions', () => {
expect(midiToFreq(69)).to.eql(440);
expect(midiToFreq(57)).to.eql(220);
});
it('#freqToPitch', () => {
expect(freqToPitch(69.3)).to.eql('Db2');
expect(freqToPitch(440)).to.eql('A4');
expect(freqToPitch(220)).to.eql('A3');
expect(freqToPitch(660)).to.eql('E5');
expect(freqToPitch(349.23)).to.eql('F4');
});
it('#midiToPitch', () => {
expect(midiToPitch(69)).to.eql('A4');
expect(midiToPitch(57)).to.eql('A3');
expect(midiToPitch(64)).to.eql('E4');
expect(midiToPitch(53)).to.eql('F3');
expect(midiToPitch(60)).to.eql('C4');
expect(midiToPitch(61)).to.eql('Db4');
});
it('#pitchToMidi', () => {
expect(pitchToMidi('A4')).to.eql(69);
expect(pitchToMidi('A3')).to.eql(57);
expect(pitchToMidi('E4')).to.eql(64);
expect(pitchToMidi('F3')).to.eql(53);
expect(pitchToMidi('C4')).to.eql(60);
expect(pitchToMidi('Db4')).to.eql(61);
expect(pitchToMidi('C#4')).to.eql(61);
});
});

0 comments on commit 4e257d7

Please sign in to comment.