-
Notifications
You must be signed in to change notification settings - Fork 2
/
jinglize.rb
84 lines (70 loc) · 1.82 KB
/
jinglize.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
require 'tempfile'
# Merges the resulting wav with the start and stop jingle.
#
# The jingle files have to have a sample rate of 44.1k and 2 channels!
#
# Prior to this strategy use MoveClean.
#
module Fidelity
module Strategy
class Jinglize < Base
class << self
def required_executables
%w( sox soxi mv )
end
end
def input
"#{name}.wav"
end
def backup
"#{name}-bak.wav"
end
def identify_cmd(file)
"soxi #{file}"
end
def classify(file)
stdout = identify(file)
@channels = stdout.match(/^Channels\s*: (.*)$/)[1]
@sample_rate = stdout.match(/^Sample Rate\s*: (.*)$/)[1]
end
def run
classify input
fu.mv input, backup
start = jingle_file(jingles.first)
stop = jingle_file(jingles.last)
merge_with_jingles start.path, stop.path
start.unlink
stop.unlink
outputs
end
def outputs
[ input, backup ]
end
def jingle_file(file)
raw = Tempfile.new(%w(raw_jingle .wav))
fetch file, raw.path
cooked = Tempfile.new(%w(cooked_jingle .wav))
transcode raw.path, cooked.path
raw.unlink
cooked
end
def fetch_cmd(source, destination)
if source.match(/^https?:/)
"wget --no-check-certificate -q -O #{destination} '#{source}'"
else
"cp #{source} #{destination}"
end
end
def transcode_cmd(source, destination)
"sox #{source} -c #{@channels} #{destination} rate -L #{@sample_rate}"
end
def merge_with_jingles_cmd(start, stop)
"sox -V1 #{start} #{backup} #{stop} #{input}"
end
def jingles
[ manifest.jingle_in,
manifest.jingle_out ]
end
end
end
end