-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsong.rb
227 lines (183 loc) · 4.24 KB
/
song.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
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
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
require 'tmpdir.rb'
require 'fileutils.rb'
$PyFolder = "./pykaraoke-0.7.2/"
def pykarplay filename
if not $pykarprocess
$pykarprocess = IO.popen($PyFolder + "control.py", "w+")
end
#for buggy files we do a simple retry
10.times do
startedsong = Time.now
$pykarprocess.puts(filename)
while line = $pykarprocess.gets.chomp
if line == "finished"
break
end
end
break if Time.now - startedsong > 4
sleep 1
end
end
def pykarstop
$pykarprocess.puts("#stop#")
end
def pykarrestart
$pykarprocess.puts("#restart#")
end
def vidplay filename
$vidprocess = IO.popen("mplayer -ao jack:port=renoise -slave \"#{filename}\"", "w+")
#$vidprocess.puts(filename)
while line = $vidprocess.gets.chomp
if line.include?("Exiting")
return
end
end
$vidprocess = nil
end
def vidstop
if $vidprocess
$vidprocess.puts "stop"
end
end
def vidrestart
if $vidprocess
$vidprocess.puts "seek -999999999999999"
end
end
def midival val
if val > 127
127
elsif val < 0
0
else
val
end
end
class Song
attr_reader :filename
attr_reader :hashid
attr_reader :sid
@@nextsid = 0
def initialize filename, title
case filename[-3,3].upcase
when "ZIP"
@type = :Zip
when "CDG"
@type = :Cdg
when "AVI"
@type = :Vid
when "MPG"
@type = :Vid
when "FLV"
@type = :Vid
when "MP4"
@type = :Vid
else
puts "ERROR, no type found for #{filename}"
@type = :Vid
end
@filename = filename
@upper = filename.gsub("/home/harald", "").upcase
@title = title.clone
[".cdg", ".CDG",
".zip", ".ZIP",
".avi", ".AVI",
".mpg", ".MPG",
".flv", ".FLV"].each do |ext|
@title.gsub!(ext, "")
end
@title.gsub!("_", " ")
@title.gsub!(/^[a-zA-z]{2,4}\d{2,5}-\d{2,4}(\s*-*\s)?/, "")
@title.gsub!("[video][jap] カラオケ", "")
@title.gsub!("[video] ", "")
@title.gsub!("[korea] ", "")
@title.gsub!("[korean] ", "")
@title.gsub!(/\b\w/){$&.upcase} #titlecase
@title.strip!
generate_hashid
@sid = @@nextsid
@@nextsid += 1
end
def to_s
@title
end
def generate_hashid
@hashid = 0
@title.upcase.each_byte do |byte|
@hashid += byte
@hashid *= byte * 105701
@hashid %= 8803
end
@hashid += 1
while @hashid < 999
@hashid *= @filename[0] * 105701
@hashid %= 8803
end
@hashid = @hashid.to_s
end
def unzip_to_tmp
dir = Dir.mktmpdir
system("unzip -jn \"#{@filename}\" -d #{dir}")
return dir
end
def play
puts "play" + @filename
@pitch = 64
@volume = 55
#reset gain
`midisend -a "Renoise MIDI Input:2" --control 01 02 #{@volume}`
#disable pitchshift
`midisend -a "Renoise MIDI Input:2" --control 01 25 0`
case @type
when :Zip
tmpdir = unzip_to_tmp
cdgfile = Dir.glob(tmpdir + "/*.cdg")[0]
if not cdgfile
cdgfile = Dir.glob(tmpdir + "/*.CDG")[0]
end
pykarplay(cdgfile)
FileUtils.rm_rf(tmpdir)
puts "removing " + tmpdir
when :Cdg
pykarplay(@filename)
when :Vid
vidplay(@filename)
end
end
def stop
pykarstop if @type == :Zip or @type == :Cdg
vidstop if @type == :Vid
end
def restart
pykarrestart if @type == :Zip or @type == :Cdg
vidrestart if @type == :Vid
end
def updatepitch
if (@pitch-64).abs <= 15
`midisend -a "Renoise MIDI Input:2" --control 01 25 0`
@pitch = 64
else
`midisend -a "Renoise MIDI Input:2" --control 01 25 127`
end
`midisend -a "Renoise MIDI Input:2" --control 01 04 #{@pitch}`
end
def pitch_up
@pitch = midival(@pitch + 20)
updatepitch
end
def pitch_down
@pitch = midival(@pitch - 20)
updatepitch
end
def volume_up
@volume = midival(@volume + 20)
`midisend -a "Renoise MIDI Input:2" --control 01 02 #{@volume}`
end
def volume_down
@volume = midival(@volume - 20)
`midisend -a "Renoise MIDI Input:2" --control 01 02 #{@volume}`
end
def match term
@upper.include?(term) or @hashid.include?(term)
end
end