Skip to content

Commit

Permalink
Increase precision of note matcher regexp and
Browse files Browse the repository at this point in the history
customise exception.
  • Loading branch information
Sam Aaron committed Jun 24, 2014
1 parent 145f29e commit b707c8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/server/sonicpi/lib/sonicpi/note.rb
Expand Up @@ -13,6 +13,8 @@
module SonicPi
class Note

class InvalidNoteError < ArgumentError ; end ;

NOTES_TO_INTERVALS =
{c: 0, C: 0,
cs: 1, CS: 1, cS: 1, Cs: 1,
Expand Down Expand Up @@ -59,7 +61,7 @@ class Note

DEFAULT_OCTAVE = 4

MIDI_NOTE_RE = /(([a-gA-G])([sSbBfF]?))([-]?[0-9]*)/
MIDI_NOTE_RE = /\A(([a-gA-G])([sSbBfF]?))([-]?[0-9]*)\Z/

attr_reader :pitch_class, :octave, :interval, :midi_note, :midi_string

Expand Down Expand Up @@ -97,7 +99,7 @@ def initialize(n, o=nil)

m = MIDI_NOTE_RE.match n

raise "Invalid note: #{n}" unless m
raise InvalidNoteError, "Invalid note: #{n}" unless m
@pitch_class = "#{m[2].capitalize}#{unify_sharp_flat_modifier(m[3])}".to_sym

if o
Expand Down
8 changes: 8 additions & 0 deletions app/server/sonicpi/test/test_note.rb
Expand Up @@ -119,5 +119,13 @@ def test_init_Fs3_7
assert_equal(6, n.interval)
assert_equal(102, n.midi_note)
end

def test_init_error_sam
assert_raise Note::InvalidNoteError do
Note.new(:sam)
end
end
end


end

0 comments on commit b707c8f

Please sign in to comment.