Class: Alda::Score

Inherits:
Object
  • Object
show all
Includes:
EventList
Defined in:
lib/alda-rb/event_list.rb

Overview

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

Direct Known Subclasses

REPL::TempScore

Instance Attribute Summary

Attributes included from EventList

#events, #variables

Instance Method Summary collapse

Methods included from EventList

#==, #events_alda_codes, #has_variable?, #import, #method_missing, #on_contained, #to_a

Constructor Details

#initializeScore

:call-seq:

new(&block) -> Alda::Score

Creates an Alda::Score.



315
316
317
318
# File 'lib/alda-rb/event_list.rb', line 315

def initialize(...)
	super
	on_contained
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Alda::EventList

Instance Method Details

#clearObject

:call-seq:

clear() -> nil

Clears all the events and variables.



325
326
327
328
329
# File 'lib/alda-rb/event_list.rb', line 325

def clear
	@events.clear
	@variables.clear
	nil
end

#export(**opts) ⇒ Object

:call-seq:

export(**opts) -> String

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)


275
276
277
# File 'lib/alda-rb/event_list.rb', line 275

def export **opts
	Alda.export code: self, **opts
end

#l(head, *args) ⇒ Object

:call-seq:

l(head, *args) -> Alda::EventContainer

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"


357
358
359
# 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) ⇒ Object

:call-seq:

load(filename) -> Alda::Raw

Loads alda codes from a file.

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



295
296
297
298
299
# 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) ⇒ Object

:call-seq:

parse(**opts) -> String

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"


258
259
260
# File 'lib/alda-rb/event_list.rb', line 258

def parse **opts
	Alda.parse code: self, **opts
end

#play(**opts) ⇒ Object

:call-seq:

play(**opts) -> String

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)


241
242
243
# 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) ⇒ Object

:call-seq:

raw(contents) -> Alda::Raw

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"


339
340
341
# File 'lib/alda-rb/event_list.rb', line 339

def raw contents
	Alda::Raw.new(contents).tap { @events.push _1 }
end

#save(filename) ⇒ Object

:call-seq:

save(filename) -> nil

Saves the alda codes into a file.



284
285
286
# File 'lib/alda-rb/event_list.rb', line 284

def save filename
	File.open(filename, 'w') { _1.puts to_s }
end

#to_sObject

:call-seq:

to_s() -> String

Returns a String containing the alda codes representing the score.



306
307
308
# File 'lib/alda-rb/event_list.rb', line 306

def to_s
	events_alda_codes
end