Class: MTK::IO::Notation

Inherits:
Object
  • Object
show all
Defined in:
lib/mtk/io/notation.rb

Overview

Note:

This class is optional and only available if you require ‘mtk/io/lilypond’.

Note:

To make notation graphics, Lilypond must be installed and you must follow the “Running on the command-line” instructions (found on the download page for your operating system). If the lilypond command is not on your PATH, set the environment variable LILYPOND_PATH

Uses Events::Timelines to generates music notation graphics with / Lilypond.

Constant Summary collapse

LILYPOND_PATH =
ENV['LILYPOND_PATH'] || 'lilypond'
VALID_FORMATS =
%w( png pdf ps )

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Notation

Returns a new instance of Notation.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
# File 'lib/mtk/io/notation.rb', line 20

def initialize(file, options={})
  @file = file
  @options = options

  @format = File.extname(file)[1..-1].downcase
  raise ArgumentError.new("Invalid file format '#{@format}'") unless VALID_FORMATS.include? @format

  @dpi = options[:dpi]
end

Class Method Details

.open(file, options = {}) ⇒ Object



30
31
32
# File 'lib/mtk/io/notation.rb', line 30

def self.open(file, options={})
  new(file,options)
end

Instance Method Details

#write(timeline) ⇒ Object



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
# File 'lib/mtk/io/notation.rb', line 35

def write(timeline)
  lilypond_syntax = syntax_for_timeline(timeline)
  puts lilypond_syntax
  puts "_____________________________"
  Tempfile.open('mtk_lilypond') do |lilypond_file|
    Dir.mktmpdir do |tmpdir|
      # use the directory...
      #open("#{dir}/foo", "w") { ... }

      lilypond_file.write(lilypond_syntax)
      lilypond_file.flush

      cmd = ['lilypond', '-dbackend=eps', "-f#{@format}", "--output=\"#{tmpdir}\""]
      cmd << "-dresolution=#{@dpi}" if @dpi
      cmd << lilypond_file.path
      cmd = cmd.join(' ')

      puts cmd if $DEBUG
      lilypond_command_output = `#{cmd}`
      puts lilypond_command_output if $DEBUG

      output_file = Dir["#{tmpdir}/*.#{@format}"].first

      FileUtils.cp output_file, @file

      puts "Wrote #{@file}"
    end
  end
end