Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for flat notes, fix wrong position and frequency #39

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions __tests__/Note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test('Can create C#2 note', () => {
test('Can create Cb2 note', () => {
const note = new Note('Cb2')

expect(note.frequency()).toBe('65.41')
expect(note.frequency()).toBe('61.74')
expect(note.name()).toBe('Cb')
expect(note.sciName()).toBe('Cb2')
expect(note.octave()).toBe(2)
Expand All @@ -61,7 +61,7 @@ test('Can create Cb2 note', () => {
test('Can create Cb note', () => {
const note = new Note('Cb')

expect(note.frequency()).toBe('16.35')
expect(note.frequency()).toBe('15.43')
expect(note.name()).toBe('Cb')
expect(note.sciName()).toBe('Cb0')
expect(note.octave()).toBe(0)
Expand Down Expand Up @@ -109,6 +109,12 @@ test('Octaves frequency', () => {
['B6', '1975.53'],
['B7', '3951.07'],
['B8', '7902.13'],

// Accidentals
['B1', '61.74'],
['Cb2', '61.74'],
['F#4', '369.99'],
['Gb4', '369.99']
]

notes.forEach(item => {
Expand Down Expand Up @@ -178,16 +184,16 @@ test('Diatonic position of two octaves', () => {
test('Chromatic position of two octave', () => {
const noteNumbers = [
['C0', 1],
['C#0', 2],
['C#0', 2], ['Db0', 2],
['D0', 3],
['D#0', 4],
['E0', 5],
['D#0', 4], ['Eb0', 4],
['E0', 5], ['Fb0', 5],
['F0', 6],
['F#0', 7],
['F#0', 7], ['Gb0', 7],
['G0', 8],
['G#0', 9],
['G#0', 9], ['Ab0', 9],
['A0', 10],
['A#0', 11],
['A#0', 11], ['Bb0', 11],
['B0', 12],

['C1', 1],
Expand Down
11 changes: 9 additions & 2 deletions src/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ export class Note {

chromaticPosition(): number {
const diatonicPosition = this.diatonicPosition()
const addSharp = this.accidental.isSharp() ? 1 : 0
const passingEnote = diatonicPosition >= 4 ? 1 : 0
return 2 * diatonicPosition + addSharp - 1 - passingEnote
let position = 2 * diatonicPosition - 1 - passingEnote

if(this.accidental.isSharp()) {
position += 1
} else if(this.accidental.isFlat()) {
position -= 1
}

return position
}

distance(note: Note): number {
Expand Down
Loading