-
Notifications
You must be signed in to change notification settings - Fork 2
/
special_commands.rb
103 lines (93 loc) · 2.78 KB
/
special_commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
95
96
97
98
99
100
101
102
103
module Sounds::SpecialCommands
# load all the files in lib/special_commands and make their constants available here
Dir.glob("./lib/special_commands/*.rb").each do |path|
require path
const_name = path.file_identifier.camelize
self.const_set(const_name, Object.const_get("Sounds::#{const_name}"))
end
# When creating a new command make sure to add it to this list
SpecialCommandList = [:k, :t, :h, :u, :s, :r, :p]
# keymap
def k
puts File.read 'keymap.yml'
end
# tempo
def t
original_tempo = $tempo_bpm
next_input = get_char(true)
cmd = { u: :up, d: :down, '=': :set }[next_input.to_sym]
result = cmd ? Tempo.send(cmd) : (err && return)
result || (err && return)
puts "Changed tempo from #{original_tempo} to #{$tempo_bpm}"
end
# help
def h
puts introduction
end
# metronome
def u
$is_metronome_playing = ! $is_metronome_playing
Thread.new do
while $is_metronome_playing
single_beat_duration = 60.0 / $tempo_bpm
play "metronome/snare" # accent for the measure's beat 1
($time_signature).to_i.times do |i|
sleep single_beat_duration
play "metronome/kick"
end
end
end
end
# time signature
def s
puts "\nenter new time signature followed by new line"
input = gets.chomp
if input.is_number?
$time_signature = input.to_f
puts "changed time signature to #{$time_signature}"
else
puts "invalid time signature"
end
end
# record
def r
$is_recording = ! $is_recording
if $is_recording
puts "recording".green
puts "\n you're live\n\n\n".yellow
Thread.new do
@file_id = Time.now.to_i
`arecord -f cd -t raw | oggenc - -r -o recording/#{@file_id}.ogg > /dev/null 2>&1`
end
else
`pkill arecord`
puts "please check #{`pwd`.chomp}/recording and ".white_on_red
puts "press enter when ogg file is ready".white_on_red
input = gets.chomp
puts "finished recording to ogg. converting to mp3 ...".white_on_red
`ffmpeg -i recording/#{@file_id}.ogg recording/#{@file_id}.mp3 > /dev/null 2>&1`
`rm recording/#{@file_id}.ogg`
puts "created recording/#{@file_id}.mp3 and removed ogg source".white_on_red
puts " - - - - ".blue
end
end
# play the last recorded sample, if any
def p
$is_last_recording_playing = !$is_last_recording_playing
if $is_last_recording_playing
Thread.new do
path = File.most_recently_edited("./recording/*.mp3")
Thread.new do
unless path.blank?
while $is_last_recording_playing
Sounds::Base.play_file_with_mpg123 path
end
end
end
puts "playing #{path}"
end
else
puts "stopped playback"
end
end
end