From 56bc3421803728ca5c50b3d167e54ab9653c3582 Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Thu, 20 Oct 2016 19:57:43 -0700 Subject: [PATCH] working wholestep_up method back in --- spec/note_spec.cr | 27 +++++++++++++++++++++++++++ src/medley/note.cr | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/spec/note_spec.cr b/spec/note_spec.cr index d2083ea..0396fa9 100644 --- a/spec/note_spec.cr +++ b/spec/note_spec.cr @@ -56,4 +56,31 @@ describe Medley::Note do end end + describe ".wholestep_up" do + it "returns E when given D" do + note = Medley::Note.new("D") + note.wholestep_up.should eq "E" + end + + it "returns G when given F" do + note = Medley::Note.new("F") + note.wholestep_up.should eq "G" + end + + it "returns D# when given C#" do + note = Medley::Note.new("C#") + note.wholestep_up.should eq "D#" + end + + it "returns Bb when given Ab" do + note = Medley::Note.new("Ab") + note.wholestep_up.should eq "Bb" + end + + it "returns Dbb when given Cbb" do + note = Medley::Note.new("Cbb") + note.wholestep_up.should eq "Dbb" + end + end + end diff --git a/src/medley/note.cr b/src/medley/note.cr index 45dc203..fd82c37 100644 --- a/src/medley/note.cr +++ b/src/medley/note.cr @@ -4,16 +4,12 @@ module Medley ALIASES = {"A##": "B", "B##": "C#", "C##": "D", "D##": "E", "E##": "F#", "F##": "G", "G##": "A", "Abb": "G", "Bbb": "A", "Cbb": "Bb", "Dbb": "C", "Ebb": "D", - "Fbb": "Eb", "Gbb": "F"} + "Fbb": "Eb", "Gbb": "F", "A#": "Bb", "B#": "C", + "C#": "Db", "D#": "Eb", "E#": "F", "F#": "G", + "G#": "Ab"} def initialize(current_note : String) @current_note = current_note - @previous_note = "" - end - - def initialize(current_note : String, previous_note : String) - initialize(current_note) - @previous_note = previous_note end # true if it's a valid note letter with no modifiers @@ -46,6 +42,21 @@ module Medley @current_note end + # Returns the root of the note + def root + @current_note[0].to_s + end + + def next_root + idx = NOTE_NAMES.index(root) || -1 + NOTE_NAMES[idx + 1] + end + + # true if the root name is equal to the other note root + def root_matches?(other_note : Note) + root == other_note.root + end + # Returns the same note, but up a halfstep in most cases # Without context of the Key, there's no telling if # the note E should be E# or F. Since there's no triple sharps, @@ -61,6 +72,7 @@ module Medley when .double_sharp? aliased = ALIASES[@current_note] return "#{aliased}#" + else "" end end @@ -75,12 +87,20 @@ module Medley when .double_flat? aliased = ALIASES[@current_note] return "#{aliased}b" + else "" end end def wholestep_up - new_note = Note.new(halfstep_up, @current_note) - new_note.halfstep_up + up_halfstep = Note.new(halfstep_up) + new_note = Note.new(up_halfstep.halfstep_up) + if new_note.double_sharp? + ALIASES[new_note.name] + elsif new_note.root_matches?(self) + new_note.next_root + name[1..-1] + else + new_note.name + end end end end