Class: Beats::SongParser

Inherits:
Object
  • Object
show all
Defined in:
lib/beats/songparser.rb

Overview

This class is used to parse a raw YAML song definition into domain objects (i.e. Song, Pattern, Track, and Kit). These domain objects can then be used by AudioEngine to generate the actual audio data that is saved to disk.

The sole public method is parse(). It takes a raw YAML string and returns a Song and Kit object (or raises an error if the YAML string couldn’t be parsed correctly).

Constant Summary collapse

DONT_USE_STRUCTURE_WARNING =
"\n" +
"WARNING! This song contains a 'Structure' section in the header.\n" +
"As of BEATS 1.2.1, the 'Structure' section should be renamed 'Flow'.\n" +
"You should change your song file, in a future version using 'Structure' will cause an error.\n"
NO_SONG_HEADER_ERROR_MSG =
"Song must have a header. Here's an example:

    Song:
      Tempo: 120
      Flow:
- Verse: x2
- Chorus: x2"

Instance Method Summary collapse

Constructor Details

#initializeSongParser

Returns a new instance of SongParser.



27
28
# File 'lib/beats/songparser.rb', line 27

def initialize
end

Instance Method Details

#parse(base_path, raw_yaml_string) ⇒ Object

Parses a raw YAML song definition and converts it into a Song and Kit object.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/beats/songparser.rb', line 32

def parse(base_path, raw_yaml_string)
  raw_song_components = hashify_raw_yaml(raw_yaml_string)

  unless raw_song_components[:folder].nil?
    base_path = raw_song_components[:folder]
  end

  song = Song.new

  # 1.) Set tempo
  begin
    unless raw_song_components[:tempo].nil?
      song.tempo = raw_song_components[:tempo]
    end
  rescue InvalidTempoError => detail
    raise SongParseError, "#{detail}"
  end

  # 2.) Build the kit
  begin
    kit = build_kit(base_path, raw_song_components[:kit], raw_song_components[:patterns])
  rescue SoundFileNotFoundError => detail
    raise SongParseError, "#{detail}"
  rescue InvalidSoundFormatError => detail
    raise SongParseError, "#{detail}"
  end

  # 3.) Load patterns
  add_patterns_to_song(song, raw_song_components[:patterns])

  # 4.) Set flow
  if raw_song_components[:flow].nil?
    raise SongParseError, "Song must have a Flow section in the header."
  else
    set_song_flow(song, raw_song_components[:flow])
  end

  # 5.) Swing, if swing flag is set
  if raw_song_components[:swing]
    begin
      song = Transforms::SongSwinger.transform(song, raw_song_components[:swing])
    rescue Transforms::InvalidSwingRateError => detail
      raise SongParseError, "#{detail}"
    end
  end

  return song, kit
end