Class: Scissor::Chunk
- Inherits:
-
Object
show all
- Defined in:
- lib/scissor/chunk.rb
Defined Under Namespace
Classes: CommandFailed, EmptyFragment, Error, FileExists, OutOfDuration
Class Attribute Summary collapse
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(filename = nil) ⇒ Chunk
Returns a new instance of Chunk.
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/scissor/chunk.rb', line 23
def initialize(filename = nil)
@fragments = []
if filename
@fragments << Fragment.new(
filename,
0,
SoundFile.new(filename).length)
end
end
|
Class Attribute Details
.logger ⇒ Object
Returns the value of attribute logger.
12
13
14
|
# File 'lib/scissor/chunk.rb', line 12
def logger
@logger
end
|
Instance Attribute Details
#fragments ⇒ Object
Returns the value of attribute fragments.
21
22
23
|
# File 'lib/scissor/chunk.rb', line 21
def fragments
@fragments
end
|
Instance Method Details
#+(other) ⇒ Object
85
86
87
88
89
|
# File 'lib/scissor/chunk.rb', line 85
def +(other)
new_instance = Scissor()
new_instance.add_fragments(@fragments + other.fragments)
new_instance
end
|
#>>(filename) ⇒ Object
246
247
248
|
# File 'lib/scissor/chunk.rb', line 246
def >>(filename)
to_file(filename, :overwrite => true)
end
|
#add_fragment(fragment) ⇒ Object
34
35
36
|
# File 'lib/scissor/chunk.rb', line 34
def add_fragment(fragment)
@fragments << fragment
end
|
#add_fragments(fragments) ⇒ Object
38
39
40
41
42
|
# File 'lib/scissor/chunk.rb', line 38
def add_fragments(fragments)
fragments.each do |fragment|
add_fragment(fragment)
end
end
|
#concat(other) ⇒ Object
Also known as:
<<
77
78
79
80
81
|
# File 'lib/scissor/chunk.rb', line 77
def concat(other)
add_fragments(other.fragments)
self
end
|
#duration ⇒ Object
44
45
46
47
48
|
# File 'lib/scissor/chunk.rb', line 44
def duration
@fragments.inject(0) do |memo, fragment|
memo += fragment.duration
end
end
|
#fill(filled_duration) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/scissor/chunk.rb', line 116
def fill(filled_duration)
if @fragments.empty?
raise EmptyFragment
end
remain = filled_duration
new_instance = self.class.new
while !remain.zero? && filled_duration > new_instance.duration
if remain < duration
added = slice(0, remain)
else
added = self
end
new_instance += added
remain -= added.duration
end
new_instance
end
|
#logger ⇒ Object
270
271
272
|
# File 'lib/scissor/chunk.rb', line 270
def logger
self.class.logger
end
|
#loop(count) ⇒ Object
Also known as:
*
91
92
93
94
95
96
97
98
99
|
# File 'lib/scissor/chunk.rb', line 91
def loop(count)
orig_fragments = @fragments.clone
(count - 1).times do
add_fragments(orig_fragments)
end
self
end
|
#replace(start, length, replaced) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/scissor/chunk.rb', line 139
def replace(start, length, replaced)
new_instance = self.class.new
offset = start + length
if offset > duration
raise OutOfDuration
end
if start > 0
new_instance += slice(0, start)
end
new_instance += replaced
new_instance += slice(offset, duration - offset)
new_instance
end
|
#reverse ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/scissor/chunk.rb', line 157
def reverse
new_instance = self.class.new
@fragments.reverse.each do |fragment|
new_instance.add_fragment(Fragment.new(
fragment.filename,
fragment.start,
fragment.duration,
!fragment.reversed?))
end
new_instance
end
|
#run_command(cmd) ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/scissor/chunk.rb', line 254
def run_command(cmd)
logger.debug("run_command: #{cmd}")
result = ''
status = Open4.popen4(cmd) do |pid, stdin, stdout, stderr|
logger.debug(stderr.read)
result = stdout.read
end
if status.exitstatus != 0
raise CommandFailed.new(cmd)
end
return result
end
|
#slice(start, length) ⇒ Object
Also known as:
[]
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/scissor/chunk.rb', line 50
def slice(start, length)
if start + length > duration
length = duration - start
end
new_instance = self.class.new
remaining_start = start.to_f
remaining_length = length.to_f
@fragments.each do |fragment|
new_fragment, remaining_start, remaining_length =
fragment.create(remaining_start, remaining_length)
if new_fragment
new_instance.add_fragment(new_fragment)
end
if remaining_length == 0
break
end
end
new_instance
end
|
#split(count) ⇒ Object
Also known as:
/
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/scissor/chunk.rb', line 103
def split(count)
splitted_duration = duration / count.to_f
results = []
count.times do |i|
results << slice(i * splitted_duration, splitted_duration)
end
results
end
|
#to_file(filename, options = {}) ⇒ Object
Also known as:
>
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
|
# File 'lib/scissor/chunk.rb', line 171
def to_file(filename, options = {})
filename = Pathname.new(filename)
if @fragments.empty?
raise EmptyFragment
end
which('ecasound')
which('ffmpeg')
options = {
:overwrite => false
}.merge(options)
filename = Pathname.new(filename)
if filename.exist?
if options[:overwrite]
filename.unlink
else
raise FileExists
end
end
position = 0.0
tmpdir = Pathname.new('/tmp/scissor-' + $$.to_s)
tmpdir.mkpath
tmpfile = tmpdir + 'tmp.wav'
cmd = %w/ecasound/
begin
@fragments.each_with_index do |fragment, index|
fragment_filename = fragment.filename
fragment_duration = fragment.duration
if !index.zero? && (index % 80).zero?
run_command(cmd.join(' '))
cmd = %w/ecasound/
end
fragment_tmpfile =
fragment_filename.extname.downcase == '.wav' ? fragment_filename :
tmpdir + (Digest::MD5.hexdigest(fragment_filename) + '.wav')
unless fragment_tmpfile.exist?
run_command("ffmpeg -i \"#{fragment_filename}\" \"#{fragment_tmpfile}\"")
end
cmd <<
"-a:#{index} " +
"-i:" +
(fragment.reversed? ? 'reverse,' : '') +
"select,#{fragment.start},#{fragment_duration},\"#{fragment_tmpfile}\" " +
"-o:#{tmpfile} " +
"-y:#{position}"
position += fragment_duration
end
run_command(cmd.join(' '))
if filename.extname == '.wav'
File.rename(tmpfile, filename)
else
run_command("ffmpeg -i \"#{tmpfile}\" \"#{filename}\"")
end
ensure
tmpdir.rmtree
end
self.class.new(filename)
end
|
#which(command) ⇒ Object
250
251
252
|
# File 'lib/scissor/chunk.rb', line 250
def which(command)
run_command("which #{command}")
end
|