Skip to content

Commit

Permalink
Better error messages if Kit/track contains nested composite sound
Browse files Browse the repository at this point in the history
I.e., a composite sound such as [a, [b, c]] is not valid. Previously,
this would result in a cryptic "no implicit conversion of Array to
String" error. Now it results in a more helpful message indicating
which track/Kit sound has the error.
  • Loading branch information
jstrait committed Sep 9, 2017
1 parent 5a2a5e0 commit 3baa1ba
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/beats/kit_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ def add_item(label, filenames)
raise SoundFileNotFoundError, "Kit has an empty composite pattern (i.e. \"[]\"), which is not valid."
end

@composite_replacements[label] = filenames.map {|filename| "#{label}-#{File.basename(filename, ".*")}" }

filenames.each do |filename|
unless filename.is_a?(String)
raise SoundFileNotFoundError, "The Kit sound '#{label}' contains an invalid file: '#{filename}'"
end
@labels_to_filenames["#{label}-#{File.basename(filename, ".*")}"] = absolute_file_name(filename)
end

@composite_replacements[label] = filenames.map {|filename| "#{label}-#{File.basename(filename, ".*")}" }
else
@labels_to_filenames[label] = absolute_file_name(filenames)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/beats/songparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def add_patterns_to_song(song, kit_builder, raw_patterns)
end

track_names.map! do |track_name|
unless track_name.is_a?(String)
raise ParseError, "'#{track_name}' in pattern '#{pattern_name}' is not a valid track sound"
end
kit_builder.composite_replacements[track_name] || track_name
end
track_names.flatten!
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/invalid/kit_with_composite_nested_sounds.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Song:
Tempo: 100
Kit:
- bass: test/sounds/bass_mono_8.wav
- snare: test/sounds/snare_mono_8.wav
- hh_closed: [test/sounds/hh_closed_mono_8.wav, [test/sounds/hh_open_mono_8.wav]]
Flow:
- Verse: x2
- Chorus: x2

Verse:
- bass: X...X...
- snare: ..X...X.
Chorus:
- bass: XXXXXXXX
- hh_closed: .X.X.X.X
16 changes: 16 additions & 0 deletions test/fixtures/invalid/track_with_composite_nested_sounds.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Song:
Tempo: 100
Kit:
- bass: test/sounds/bass_mono_8.wav
- snare: test/sounds/snare_mono_8.wav
- hh_closed: test/sounds/hh_closed_mono_8.wav
Flow:
- Verse: x2
- Chorus: x2

Verse:
- [bass, [hh_closed]]: X...X...
- snare: ..X...X.
Chorus:
- bass: XXXXXXXX
- [snare, [bass, hh_closed]]: .X.X.X.X
2 changes: 2 additions & 0 deletions test/songparser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class SongParserTest < Minitest::Test
:kit_with_composite_non_existent_sound,
:track_with_composite_empty_sound,
:kit_with_composite_empty_sound,
:track_with_composite_nested_sounds,
:kit_with_composite_nested_sounds,
:leading_bar_line,
:sound_in_kit_not_found,
:sound_in_track_not_found,
Expand Down

0 comments on commit 3baa1ba

Please sign in to comment.