Skip to content

Commit

Permalink
Use Accidentals on Note class
Browse files Browse the repository at this point in the history
  • Loading branch information
simonppg committed Jun 2, 2024
1 parent 4171e02 commit 4c8fd99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
18 changes: 18 additions & 0 deletions __tests__/Note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ test('Can create C#2 note', () => {
expect(note.octave()).toBe(2)
})

test('Can create Cb2 note', () => {
const note = new Note('Cb2')

expect(note.frequency()).toBe('65.41')
expect(note.name()).toBe('Cb')
expect(note.sciName()).toBe('Cb2')
expect(note.octave()).toBe(2)
})

test('Can create Cb note', () => {
const note = new Note('Cb')

expect(note.frequency()).toBe('16.35')
expect(note.name()).toBe('Cb')
expect(note.sciName()).toBe('Cb0')
expect(note.octave()).toBe(0)
})

test('C0 and D0 are in the same octave', () => {
const c0 = new Note('C0')
const d0 = new Note('D0')
Expand Down
2 changes: 2 additions & 0 deletions src/Accidental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ export class Accidental {
switch (accidentalString) {
case "#":
this.type = AccidentalType.SHARP;
break
case "b":
this.type = AccidentalType.FLAT;
break
default:
this.type = AccidentalType.NATURAL;
}
Expand Down
26 changes: 12 additions & 14 deletions src/Note.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
import {Frequency} from "./Frequency"
import {Math} from "./Math"
import {NoteValidator} from "./NoteValidator"
import {NoteParser} from "./NoteParser"
import { Accidental } from "./Accidental"

export class Note {
private noteName: string
private aOctave: number
private isSharp: boolean
private accidental: Accidental
private static DIATONIC_NOTES = 7
private static CHROMATIC_NOTES = 12
private static A4 = '440.00'
private static noteValidator = new NoteValidator()
private static NOTES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
private parser = new NoteParser()

constructor(note: string = 'A4') {
const length = note.length
if(length < 2 || length > 3)
throw new Error('InvalidArgumentException')

this.noteName = note.charAt(0)
const noteDTO = this.parser.parseNote(note)
this.accidental = noteDTO.accidental
this.aOctave = noteDTO.octave || 0
this.noteName = noteDTO.name

if(!Note.noteValidator.isValid(this.noteName))
throw new Error('InvalidArgumentException')

const numberIndex = length === 2 ? 1 : 2

this.aOctave = parseInt(note.charAt(numberIndex))
this.isSharp = length === 3
}

sciName(): string {
if(this.isSharp)
return this.noteName +"#"+ this.aOctave
return this.noteName + this.aOctave
return this.name() + this.aOctave
}

name(): string {
if(this.isSharp)
return this.noteName +"#"
return this.noteName
return this.noteName + this.accidental.toString()
}

frequency(): string {
Expand Down Expand Up @@ -66,7 +64,7 @@ export class Note {

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

0 comments on commit 4c8fd99

Please sign in to comment.