class Alda::Score

Includes Alda::EventList and provides methods to play, parse, or export.

Public Class Methods

new(&block) → Alda::Score click to toggle source

Creates an Alda::Score.

Calls superclass method Alda::EventList::new
# File lib/alda-rb/event_list.rb, line 315
def initialize(...)
  super
  on_contained
end

Public Instance Methods

clear() → nil click to toggle source

Clears all the events and variables.

# File lib/alda-rb/event_list.rb, line 325
def clear
  @events.clear
  @variables.clear
  nil
end
export(**opts) → String click to toggle source

Exports the score.

Returns the command line output of the alda command.

Run command alda help to see available options that can be specified in opts.

Alda::Score.new { piano_; c }.export output: 'temp.mid'
# (outputs a midi file called temp.mid)
# File lib/alda-rb/event_list.rb, line 275
def export **opts
  Alda.export code: self, **opts
end
l(head, *args) → Alda::EventContainer click to toggle source

Adds an Alda::EventContainer containing an Alda::InlineLisp event to the event list. In most cases, method_misssing is a more convenient way to add an inline Lisp event. However, sometimes you may want to programmatically control which Lisp function to be called, or the function name is already a valid Ruby method name (for example, you want to use f or p as the dynamics but f would be interpreted as a note and p is already a Ruby method for printing) so that it cannot trigger method_missing, then you should use this method.

Alda::Score.new { piano_; l :p; c }.to_s # => "piano: (p ) c"
# File lib/alda-rb/event_list.rb, line 357
def l head, *args
  Alda::EventContainer.new(Alda::InlineLisp.new(head, *args), self).tap { @events.push _1 }
end
load(filename) → Alda::Raw click to toggle source

Loads alda codes from a file.

Actually appends a Alda::Raw event with the contents in the file filename.

# File lib/alda-rb/event_list.rb, line 295
def load filename
  event = Alda::Raw.new File.read filename
  @events.push event
  event
end
parse(**opts) → String click to toggle source

Parses the score.

Returns the JSON string of the parse result.

Run command alda help to see available options that can be specified in opts.

Alda::Score.new { piano_; c }.parse output: :events
# => "[{\"event-type\":...}]\n"
# File lib/alda-rb/event_list.rb, line 258
def parse **opts
  Alda.parse code: self, **opts
end
play(**opts) → String click to toggle source

Plays the score.

Returns the command line output of the alda command.

Run command alda help to see available options that can be specified in opts.

Alda::Score.new { piano_; c; d; e }.play
# => "[27713] Parsing/evaluating...\n[27713] Playing...\n"
# (and plays the sound)
Alda::Score.new { piano_; c; d; e }.play from: 1
# (plays only an E note)
# File lib/alda-rb/event_list.rb, line 241
def play **opts
  Alda.env(ALDA_DISABLE_SPAWNING: :no) { Alda.play code: self, **opts }
end
raw(contents) → Alda::Raw click to toggle source

Adds an Alda::Raw event to the event list and returns it. The event is not contained by a container.

Alda::Score.new { raw 'piano: c d e' }.to_s # => "piano: c d e"
# File lib/alda-rb/event_list.rb, line 339
def raw contents
  Alda::Raw.new(contents).tap { @events.push _1 }
end
save(filename) → nil click to toggle source

Saves the alda codes into a file.

# File lib/alda-rb/event_list.rb, line 284
def save filename
  File.open(filename, 'w') { _1.puts to_s }
end
to_s() → String click to toggle source

Returns a String containing the alda codes representing the score.

# File lib/alda-rb/event_list.rb, line 306
def to_s
  events_alda_codes
end