Class: Rhythmmml::Scene::Main

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rhythmmml/scene.rb

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Main

Returns a new instance of Main.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rhythmmml/scene.rb', line 83

def initialize(window)
  super
  @rhythms = []
  @sampling_rate = @window.options[:sampling_rate] || 22050
  rhythm_infos = Parser.new(@window.mml, @sampling_rate, @window.options).parse
  y = 0
  rhythm_infos.each do |rhythm_info|
    scale = rhythm_info[0]
    x_space = @window.width / 8
    case scale
    when /r/i
      y -= rhythm_info[1] * 60
      next
    when /c/i
      x = x_space * 1
    when /d/i
      x = x_space * 2
    when /e/i
      x = x_space * 3
    when /f/i
      x = x_space * 4
    when /g/i
      x = x_space * 5
    when /a/i
      x = x_space * 6
    when /b/i
      x = x_space * 7
    end
    rhythm = Object::Rhythm.new(@window, x, y, rhythm_info)
    @rhythms << rhythm
    @objects << rhythm
    y -= rhythm_info[1] * 60
  end

  @info = Object::Info.new(@window, @window.width * 0.7, 0)
  @objects << @info

  @bar_y = @window.height * 0.8
  @bar = Figure::Bar.new(@window,
                         0, @bar_y,
                         @window.width, @bar_y)
  @figures << @bar
  @format = WaveFile::Format.new(:mono, :pcm_8, @sampling_rate)
  @buffer_format = WaveFile::Format.new(:mono, :float, @sampling_rate)

  @guide = Gosu::Image.from_text(@window,
                                 "press space key",
                                 @font_path,
                                 20,
                                 4,
                                 @window.width,
                                 :center)
end

Instance Method Details

#button_down(id) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rhythmmml/scene.rb', line 146

def button_down(id)
  case id
  when Gosu::KbQ
    @window.scenes.shift
  when Gosu::KbSpace
    @rhythms.each do |rhythm|
      distance = (@bar_y - rhythm.y).abs
      if distance < 10
        @info.score += 10 - distance
        Dir.mktmpdir("rhythmmml") do |dir|
          path = File.join(dir, "a.wav")
          WaveFile::Writer.new(path, @format) do |writer|
            samples = sine_wave(*rhythm.info[2])
            buffer = WaveFile::Buffer.new(samples, @buffer_format)
            writer.write(buffer)
          end
          Gosu::Sample.new(@window, path).play
        end
        @objects.delete(rhythm)
        @rhythms.delete(rhythm)
        break
      end
    end
  end
end

#drawObject



141
142
143
144
# File 'lib/rhythmmml/scene.rb', line 141

def draw
  super
  @guide.draw(0, @window.height * 0.9, ZOrder::TEXT)
end

#updateObject



137
138
139
# File 'lib/rhythmmml/scene.rb', line 137

def update
  super
end