Class: Ramekin::CLI

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ramekin/cli.rb

Constant Summary

Constants included from Util

Util::KNOWN_LENGTHS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#adsr_value, #extra_nice_length_amk, #nice_length_amk

Class Method Details

.main(*argv) ⇒ Object



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

def self.main(*argv)
  new.main(*argv)
end

Instance Method Details

#compile(*argv) ⇒ Object



117
118
119
120
121
122
123
124
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
166
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
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/ramekin/cli.rb', line 117

def compile(*argv)
  warn_unless_setup!
  @force = false
  @play = false

  while (h = argv.shift)
    case h
    when '--version', '-V'
      puts RAMEKIN_VERSION
      return 0
    when '--help', '-help', '-h', '-?'
      usage
      return 0
    when '--sample-path'
      @sample_path = argv.shift or usage! 'missing path for --sample-path'
    when '--txt'
      @output_txt = argv.shift or usage! 'missing filename after --txt'
    when '--vis'
      @output_vis = argv.shift or usage! 'missing filename after --vis'
    when '--spc'
      @output_spc = argv.shift or usage! 'missing filename after --spc'
    when '--package'
      @output_package = argv.shift or usage! 'missing dirname after --package'
    when '--play'
      if argv.any? && argv[0] =~ /\A\d+\z/
        @play_offset = argv.shift
      else
        @play_offset = 0
      end
      @play = true
    when '--wav'
      arg = argv.shift or usage! 'missing wav file after --render'
      @wav_file, @wav_seconds = arg.split(':')
      @wav_seconds ||= ENV['SECONDS'] || 60
      @wav_seconds = @wav_seconds.to_i or usage! 'invalid number of seconds'
    when '--force', '-f'
      @force = true
    when '-i'
      @infile = argv.shift or usage! 'missing filename after -i'
    when '-v', '--verbose'
      @verbose = true
    when /\A-/
      usage! "unknown argument #{h}"
    else
      @infile = h
    end
  end

  # if we want to play or render wav, we have to make sure we
  # have a path to render the SPC file to.
  if @play || @wav_file
    if @output_package
      @play_spc = "#@output_package/#{File.basename(@infile, '.rmk')}.spc"
    elsif @output_spc
      @play_spc = @output_spc
    else
      @output_spc = @play_spc = tempfile("#{File.basename(@infile)}.spc")
    end
  end

  usage! 'missing infile (use -i)' if @infile.nil?
  @infile = File.expand_path(@infile)
  fail! "can't find a file at #{@infile}" unless File.exist?(@infile)

  source = begin
    File.read(@infile)
  rescue
    fail! "couldn't read the file at #{@infile}"
  end

  tokens = Tokenizer.tokenize(source).to_a

  outer_chain = Processor.compose(
    MacroExpander,
    ScanForL,
    LoopAllocator,
  )

  expanded = outer_chain.call(tokens)

  channels = ChannelSeparator.parse(expanded, @sample_path)

  inner_chain = Processor.compose(
    NoteAggregator,
    Fades,
    Bends,
    RestAggregator,
    Legato,
    Inspector,
  )

  channels.channels.map! do |c|
    next if c.nil?

    inner_chain.call(c)
  end


  txt = Renderer.render(@infile, channels)

  if Error.any?
    puts "errors:"
    puts
    Error.all.each do |error|
      puts error.present(source)
    end

    exit 1
  end

  unless @output_txt || @output_package || @output_spc || @output_vis
    warn "      no output specified. use either:\n        --txt my_cool_file.txt\n        --spc my_cool_file.spc\n        --package my_cool_output_dir\n        --vis my_cool_png.png\n        --play\n        --wav my_cool_file.wav\n    WARN\n  end\n\n  if @output_txt == '-'\n    $stdout.puts(txt)\n  elsif @output_txt\n    File.write(@output_txt, txt)\n  end\n\n  if @output_spc || @output_package || @output_vis\n    runner = AMKRunner.new(@infile, channels.meta, txt)\n    result = runner.compile\n    unless result && result.success?\n      $stderr.puts \"AddmusicK did not exit successfully.\"\n      exit result&.to_i || 1\n    end\n  end\n\n  FileUtils.cp(runner.spc_file, @output_spc) if @output_spc\n  if @output_package\n    clear_dir(@output_package, force: @force) or exit 1\n    runner.export(@output_package)\n\n    $stderr.puts <<~MSG\n      Package directory generated at \#{@output_package}\n    MSG\n  end\n\n  if @output_vis\n    FileUtils.cp(runner.vis_file, @output_vis)\n  end\n\n  if @wav_file\n    SPCPlayer.instance.setup!\n    SPCPlayer.instance.render(@play_spc, @wav_file, @wav_seconds)\n  end\n\n  if @play\n    SPCPlayer.instance.setup!\n    SPCPlayer.instance.play(@play_spc, @play_offset)\n  end\n\n  if @verbose\n    aggregated.each do |a|\n      puts a.inspect\n    end\n  end\n\n  tempfiles.each { |path| FileUtils.rm_r(path) if path && !path.empty? }\n\n  return 0\nend\n"

#dump_brrs(pack, brrs) ⇒ Object



404
405
406
407
408
409
410
411
# File 'lib/ramekin/cli.rb', line 404

def dump_brrs(pack, brrs)
  brrs.each do |brr|
    tunings = pack.tunings_for(brr)
    out = "  #{brr.inspect}"
    out << " (#{tunings.size} tunings)" if tunings.size != 1
    puts out
  end
end

#fail!(message) ⇒ Object



104
105
106
107
108
109
# File 'lib/ramekin/cli.rb', line 104

def fail!(message)
  $stderr.puts(message)
  $stderr.puts

  exit 1
end

#main(*argv) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/ramekin/cli.rb', line 413

def main(*argv)
  if argv.empty?
    usage
    return 0
  end

  while argv.any?
    case (arg = argv.shift)
    when '--pack'
      pack = argv.shift or fail! '--pack missing a pack definition NAME=PATH'
      pack =~ /\A(.*?)=(.*)\z/ or fail! 'invalid --pack, must be of the form NAME=PATH'
      SamplePack.add_custom($1, File.expand_path($2))
    when 'compile'
      return compile(*argv)
    when 'package', 'pack'
      return package(*argv)
    when 'setup'
      return setup(*argv)
    else
      return compile(arg, *argv)
    end
  end
end

#package(*argv) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/ramekin/cli.rb', line 289

def package(*argv)
  warn_unless_setup!

  @mode = nil
  @update = false
  @search = nil
  while (h = argv.shift)
    case h
    when '--list-smw'
      @list_smw = true
    when '--update'
      @update = true
    when '--search'
      @mode = :search
      @term = argv.shift || ''
    when '--list'
      @mode = :list
      @term = argv.shift || ''
    else
      usage!("unknown argument: #{h.inspect}")
    end
  end

  if @list_smw
    width = SMW::BUILTINS.keys.map(&:size).max
    $stderr.puts ";;; List of builtin instruments from SMW: ;;;"
    SMW::BUILTINS.each do |name, id|
      puts "@#{name.ljust(width)} ; @#{id}"
    end
  end

  if @update
    $stderr.puts "=== updating packages... ==="
    SamplePack.download_all!
  end

  case @mode
  when :search
    search(@term, list_matched: false, search_brr: true)
  when :list
    search(@term, list_matched: true, search_brr: false)
  end
end

#search(term, list_matched: false, search_brr: false) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/ramekin/cli.rb', line 380

def search(term, list_matched: false, search_brr: false)
  term = term.downcase

  SamplePack.each do |package|
    matched_pack = package.name.downcase.include?(term)

    matched_brr = package.unprefixed_brrs.select do |brr|
      brr.downcase.include?(term)
    end if search_brr

    if search_brr && matched_brr.any?
      puts "#pack #{package.name.inspect}"
      dump_brrs(package, matched_brr)
    elsif matched_pack
      puts "#pack #{package.name.inspect}"
      if list_matched
        dump_brrs(package, package.unprefixed_brrs)
      end
    end
  end

  return 0
end

#setup(*argv) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/ramekin/cli.rb', line 333

def setup(*argv)
  @force = false
  @setup_packages = false
  @setup_amk = false
  @default_action = true
  while (h = argv.shift)
    case h
    when '--all'
      @setup_packages = true
      @setup_amk = true
      @setup_spc = true
      @default_action = false
    when '--amk'
      @setup_amk = true
      @default_action = false
    when '--packages', '--packs'
      @setup_packages = true
      @default_action = false
    when '--spc'
      @setup_spc = true
      @default_action = false
    when '--force'
      @force = true
    end
  end

  if @default_action
    @setup_packages = true
    @setup_amk = true
    @setup_spc = true
  end

  if @setup_amk
    AMKSetup.setup!(force: @force)
  end

  if @setup_spc
    SPCPlayer.instance.setup!
  end

  if @setup_packages
    SamplePack.download_all!
  end

  return 0
end

#tempfile(fname) ⇒ Object



13
14
15
16
17
# File 'lib/ramekin/cli.rb', line 13

def tempfile(fname)
  dir = Dir.mktmpdir
  tempfiles << dir
  "#{dir}/#{fname}"
end

#tempfilesObject



9
10
11
# File 'lib/ramekin/cli.rb', line 9

def tempfiles
  @tempfiles ||= []
end

#usageObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
# File 'lib/ramekin/cli.rb', line 19

def usage
  $stderr.puts "Ramekin version #{RAMEKIN_VERSION}"
  $stderr.puts ""
  $stderr.puts "usage: ramekin [--pack NAME=PATH] [command] [flags]"
  $stderr.puts "    (default command: compile)"
  $stderr.puts ""
  $stderr.puts "ramekin --pack my_cool_pack=/path/to/my_cool_pack [...]"
  $stderr.puts "    Specifies an additional custom sample location."
  $stderr.puts "    Files compiled this way should use #pack \"my_cool_pack\","
  $stderr.puts "    and sample paths relative to the deepest common path that"
  $stderr.puts "    contains .brr files. Use with `package --list my_cool_pack` to"
  $stderr.puts "    see details."
  $stderr.puts ""
  $stderr.puts "ramekin compile -i filename.rmk [flags]"
  $stderr.puts "ramekin compile filename.rmk [flags]"
  $stderr.puts "ramekin filename.rmk [flags]"
  $stderr.puts "    flags:"
  $stderr.puts "        -h --help"
  $stderr.puts "            display this message"
  $stderr.puts "        -i filename.rmk"
  $stderr.puts "            set the input file"
  $stderr.puts "        --txt filename.txt"
  $stderr.puts "            output amk txt to this file."
  $stderr.puts "            use a single dash (-) to print txt to stdout."
  $stderr.puts "        --spc filename.spc"
  $stderr.puts "            attempt to invoke AddmusicK to output an SPC."
  $stderr.puts "        --play"
  $stderr.puts "            play the generated SPC file. will automatically"
  $stderr.puts "            set up an SPC player if none is detected."
  default_text = if Ramekin.config.windows?
    '(installs spcplay.exe, which can render WAVs)'
  else
    '(compiles and installs spct - requires git,cmake,make)'
  end
  $stderr.puts "            #{default_text}"
  $stderr.puts "        --package my-cool-folder/"
  $stderr.puts "            create a package directory suitable for submission"
  $stderr.puts "            to SMWC, complete with packaged samples."
  unless Ramekin.config.windows?
    $stderr.puts "        --wav filename.wav"
    $stderr.puts "        --wav filename.wav:N"
    $stderr.puts "            render N seconds of the file to WAV. Requires spct."
  end
  $stderr.puts "        --vis filename.png"
  $stderr.puts "            outputs AddmusicK's ARAM visualization to a specified file"
  $stderr.puts ""
  $stderr.puts "ramekin package [flags]"
  $stderr.puts "ramekin pack [flags]"
  $stderr.puts "    flags:"
  $stderr.puts "        --update"
  $stderr.puts "            download BRR packs from SMW Central."
  $stderr.puts "        --search [text]"
  $stderr.puts "            search for BRR filenames."
  $stderr.puts "        --list [text]"
  $stderr.puts "            list all samples in packs matching [text]."
  $stderr.puts "        --list-smw"
  $stderr.puts "            list the builtin instruments from SMW that can be used"
  $stderr.puts "            without a pack."
  $stderr.puts ""
  $stderr.puts "ramekin setup [flags]"
  $stderr.puts "    flags:"
  $stderr.puts "        --amk"
  $stderr.puts "            downloads and sets up AddMusicK and asar."
  unless Ramekin.config.windows?
    $stderr.puts "            (requires cmake and make to be available)"
  end
  $stderr.puts "        --packages"
  $stderr.puts "            downloads packages from SMW Central."
  $stderr.puts "            (equivalent to package --update)"
  $stderr.puts "        --spc"
  $stderr.puts "            downloads and sets up a default SPC player."
  $stderr.puts "        --all (default)"
  $stderr.puts "            runs all of the above setup steps"
  $stderr.puts "        --force"
  $stderr.puts "            deletes and redownloads files instead of skipping."
end

#usage!(message) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/ramekin/cli.rb', line 96

def usage!(message)
  $stderr.puts(message)
  $stderr.puts
  usage

  exit 1
end

#warn_unless_setup!Object



111
112
113
114
115
# File 'lib/ramekin/cli.rb', line 111

def warn_unless_setup!
  unless AMKSetup.setup_ok?
    $stderr.puts "WARNING: ramekin is not set up. Have you run `ramekin setup`?"
  end
end