Class: Ramekin::Renderer

Inherits:
Object
  • Object
show all
Includes:
Error::Helpers
Defined in:
lib/ramekin/renderer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, track) ⇒ Renderer



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ramekin/renderer.rb', line 9

def initialize(filename, track)
  @filename = File.basename(filename)
  @track = track

  @channel = 0
  @tick = 0
  @octave = 4
  @volume = 0xFF

  # TODO: add @smwxxx special instruments and automatically
  # optimize out unused sfx samples
  @instrument_index = {}

  (0...30).each do |i|
    @instrument_index[i.to_s] = i
  end

  SMW::BUILTINS.each do |name, id|
    @instrument_index[name] = id
  end
end

Class Method Details

.render(fname, track, &b) ⇒ Object



5
6
7
# File 'lib/ramekin/renderer.rb', line 5

def self.render(fname, track, &b)
  new(fname, track).render(&b)
end

Instance Method Details

#fade_duration(tok) ⇒ Object



300
301
302
303
304
305
306
307
308
309
# File 'lib/ramekin/renderer.rb', line 300

def fade_duration(tok)
  return 0 unless tok.meta[:fade]
  out = tok.meta[:fade].ticks / @track.meta.divisor

  if out > 0xFF
    error! "fade too long, cannot exceed 255 ticks (was #{out} ticks)"
  end

  out
end

#hex(*args) ⇒ Object



326
327
328
# File 'lib/ramekin/renderer.rb', line 326

def hex(*args)
  args.map { |a| sprintf("$%02x", a) }.join
end

#next!Object



113
114
115
116
117
118
119
# File 'lib/ramekin/renderer.rb', line 113

def next!
  if @peek
    return @peek.tap { @peek = nil }
  end

  @current_channel.next
end

#pan_command(pan, time = 0) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/ramekin/renderer.rb', line 276

def pan_command(pan, time=0)
  error! 'invalid pan: must be R10 to L10 (0 to 20)' unless (0..20).include?(pan)

  if time.to_i == 0
    "y#{pan}"
  else
    hex(0xDC, time, pan)
  end
end

#peekObject



121
122
123
# File 'lib/ramekin/renderer.rb', line 121

def peek
  @peek ||= @current_channel.next
end

#render(&b) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ramekin/renderer.rb', line 31

def render(&b)
  return render_string unless block_given?

  if ENV['RAMEKIN_DEBUG'] == '1'
    old_b = b
    @so_far = StringIO.new
    b = lambda { |chunk| @so_far << chunk; old_b.call(chunk) }
  end

  render_preamble(&b)

  @track.channels.compact.each { |c| render_channel(c, &b) }
end

#render_channel(channel) {|"\n\n"| ... } ⇒ Object

Yields:

  • ("\n\n")


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ramekin/renderer.rb', line 125

def render_channel(channel, &b)
  @tick = 0
  yield "\n\n"
  @current_channel = channel.each
  loop do
    @current = el = next!

    case el
    when Bend
      old_tick = @tick
      @tick += el.ticks
      if (old_tick-1) / 192 != (@tick-1) / 192
        yield "\n"
      end

      yield el.to_amk(@octave, @track.meta.divisor)

      @octave = el.ending_octave unless el.tie?
    when NoteEvent
      old_tick = @tick
      @tick += el.ticks
      if (old_tick-1) / 192 != (@tick-1) / 192
        yield "\n"
      end
      divisor = @track.meta.divisor

      yield el.to_amk(@octave, @track.meta.divisor)
      @octave = el.octave unless el.tie? || el.rest?
    when MacroDefinition
      # pass
    when LegatoStart, LegatoLastNote
      yield el.to_amk(@octave, @track.meta.divisor)
    when Instrument
      yield "@#{@instrument_index[el.name.value]}"
    when Token
      render_token(el, &b)
    else
      error! "unexpected element type #{el.inspect}"
    end
  end
end

#render_directive(token) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/ramekin/renderer.rb', line 311

def render_directive(token)
  case token.value
  when 'SPC'
    # pass
  when 'legato', 'legato/toggle'
    yield hex(0xF4, 0x01)
  when 'sustain/global-toggle'
    yield hex(0xF4, 0x02)
  when 'echo/toggle'
    yield hex(0xF4, 0x03)
  else
    error! "unexpected directive ##{token.value}"
  end
end

#render_preamble {|"#SPC\n{\n"| ... } ⇒ Object

Yields:

  • ("#SPC\n{\n")


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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ramekin/renderer.rb', line 45

def render_preamble(&b)
  m = @track.meta
  yield "#SPC\n{\n"
  yield "  #title #{m.title.value.inspect}\n" if m.title
  yield "  #author #{m.author.value.inspect}\n" if m.author
  yield "  #game #{m.game.value.inspect}\n" if m.game
  yield "  #comment #{m.comment.value.inspect}\n" if m.comment
  yield "}\n\n"

  yield "; generated from #{@filename} by Ramekin v#{RAMEKIN_VERSION}\n"
  yield "; https://codeberg.org/jneen/ramekin\n\n"
  if m.instruments.any?
    yield "; Sample Sources:\n"
    m.pack_info.split("\n").each do |line|
      yield "; #{line}\n"
    end
    yield "\n"
  end

  amk = @track.meta.amk&.value || 4
  yield "#amk #{amk}\n"

  if amk.to_i >= 4
    yield "#option amk109hotpatch\n"
  end

  yield "\n"

  if m.instruments.any?
    yield "#path #{(m.sample_path || @filename.chomp('.rmk')).inspect}\n"
    yield "#samples {\n"
    if m.sample_groups.empty?
      yield "  #default\n"
    else
      m.sample_groups.each do |group|
        yield "  ##{group.value}\n"
      end
    end

    m.instruments.map(&:sample_name).sort.uniq.each do |sample|
      yield "  #{sample.inspect}\n"
    end
    yield "}\n\n"

    yield "#instruments {\n"
    m.instruments.each_with_index do |inst, i|
      @instrument_index[inst.name.value] = 30 + i
      yield "  #{inst.to_amk}\n"
    end
    yield "}\n\n"
  end

  yield "t#{m.t_value} ; main tempo (0-60)\n" if m.tempo
  yield "w#{m.volume.value} ; main volume (0-255)\n" if m.volume
  yield "l16\n\n"

  yield "; echo settings\n"
  yield m.echo_hex
  # binding.pry
end

#render_stringObject



330
331
332
333
334
# File 'lib/ramekin/renderer.rb', line 330

def render_string
  out = StringIO.new
  render { |chunk| out << chunk }
  out.string
end

#render_token(token, &b) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/ramekin/renderer.rb', line 167

def render_token(token, &b)
  case token.type
  when :t, :bpm
    tempo = tempo_of(token)
    fade = fade_duration(token)

    if fade == 0
      yield "t#{tempo_of(token)}"
    else
      yield hex(0xE3, fade, tempo)
    end
  when :directive
    render_directive(token, &b)
  when :channel
    yield "##{token.value}\n\n"
  when :hex
    yield "$#{token.value.downcase} "
  when :slash
    yield "\n/\n"
    # reset tick counter for newlines
    @tick = 0
  when :transpose
    yield hex(0xFA, 0x02, token.value.to_i & 0x7f)
  when :instrument
    case token.value
    when /\A\d+\z/ then yield "@#{token.value}"
    else
      @octave = nil
      unless @instrument_index.key?(token.value)
        return error! "undeclared instrument @#{token.value}"
      end

      yield "@#{@instrument_index[token.value]}"
    end
  when :v
    vol = token.value.to_i

    unless Token === peek && [:v, :relv].include?(peek.type)
      yield velocity_command(vol, fade_duration(token))
    end

    @volume = vol
  when :relv
    unless Token === peek && [:v, :relv].include?(peek.type)
      yield velocity_command(@volume + token.value.to_i, fade_duration(token))
    end
  when :q
    token.value =~ /(\d),?(\h)?/
    qval = $1
    gain = $2 || 'F'
    yield "q#{qval}#{gain}"
  when :w
    fade = fade_duration(token)

    if fade.to_i == 0
      yield "w#{token.value}"
    else
      yield hex(0xE1, fade, token.value.to_i)
    end
  when :adsr
    a, d, s, r = Util.adsr_value(token)

    yield hex(0xED, (7-d) * 16 + (15-a), s * 32 + (31-r))
  when :y, :rely
    pan_number = case token.type
    when :y then token.value.to_i
    when :rely
      token.value =~ /\A([LRC])(\d*)\z/
      pan = case $1
      when 'L' then 10 + $2.to_i
      when 'R' then 10 - $2.to_i
      when 'C' then 10
      end
    end

    yield pan_command(pan_number, fade_duration(token))
  when :p
    # it's free aram
    if token.value == '0,0'
      yield hex(0xDF)
    else
      yield "p#{token.value} "
    end
  when :n
    yield "n#{token.value}"
  when :superloop
    yield " [[ "
  when :loop
    if token.value
      yield " (#{token.meta[:number]})[ "
    else
      yield " [ "
    end
  when :loop_end
    yield " ]#{token.value} "
  when :loop_call
    yield " (#{token.meta[:number]})#{token.values[1]} "
  when :star
    yield "*#{token.value}"
  when :superloop_end
    yield " ]]#{token.value} "
  # triplets are handled in NoteAggregator
  when :lbrace, :rbrace
    # pass
  else
    error! "unexpected token type: #{token.type}"
  end
end

#tempo_of(el) ⇒ Object



106
107
108
109
110
111
# File 'lib/ramekin/renderer.rb', line 106

def tempo_of(el)
  case el.type
  when :t then el.value
  when :bpm then (el.value.to_i * 8192 / 20025.0).round
  end
end

#velocity_command(vol, time = 0) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/ramekin/renderer.rb', line 286

def velocity_command(vol, time=0)
  vol = 255 if vol > 255
  vol = 0 if vol < 0

  if time.to_i == 0
    "v#{vol}"
  else
    time = time.to_i
    time = 255 if time > 255
    time = 0 if time < 0
    hex(0xE8, time, vol)
  end
end