Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Scissor#fill and Scissor.silence

  • Loading branch information...
commit 5679a3b7f75b4d9835302d5b4809cb8b66900368 1 parent 5962617
youpy authored
Showing with 45 additions and 2 deletions.
  1. +1 −1  Rakefile
  2. +29 −1 lib/scissor.rb
  3. +15 −0 spec/scissor_spec.rb
2  Rakefile
View
@@ -61,7 +61,7 @@ spec = Gem::Specification.new do |s|
#s.required_ruby_version = '>= 1.8.2'
s.files = %w(README.rdoc ChangeLog Rakefile) +
- Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
+ Dir.glob("{bin,doc,test,lib,data,templates,generator,extras,website,script}/**/*") +
Dir.glob("ext/**/*.{h,c,rb}") +
Dir.glob("examples/**/*.rb") +
Dir.glob("tools/*.rb")
30 lib/scissor.rb
View
@@ -34,7 +34,7 @@ def duration
end
def slice(start, length)
- new_mp3 = self.class.new()
+ new_mp3 = self.class.new
remain = length
@fragments.each do |fragment|
@@ -102,6 +102,28 @@ def split(count)
alias / split
+ def fill(filled_duration)
+ if @fragments.empty?
+ raise EmptyFragment
+ end
+
+ remain = filled_duration
+ new_mp3 = self.class.new
+
+ while filled_duration > new_mp3.duration
+ if remain < duration
+ added = slice(0, remain)
+ else
+ added = self
+ end
+
+ new_mp3 += added
+ remain -= added.duration
+ end
+
+ new_mp3
+ end
+
def to_file(filename, options = {})
if @fragments.empty?
raise EmptyFragment
@@ -162,6 +184,12 @@ def run_command(cmd)
end
end
+ class << self
+ def silence(duration)
+ new(File.dirname(__FILE__) + '/../data/silence.mp3').fill(duration)
+ end
+ end
+
class Fragment
attr_reader :filename, :start, :duration
15 spec/scissor_spec.rb
View
@@ -36,6 +36,11 @@
new_mp3.duration.should eql(140)
end
+ it "should concat silence" do
+ new_mp3 = @mp3.slice(0, 12).concat(Scissor.silence(3))
+ new_mp3.duration.to_s.should eql('15.0')
+ end
+
it "should slice concatenated one" do
new_mp3 = @mp3.slice(0.33, 1).concat(@mp3.slice(0.2, 0.1)).slice(0.9, 0.2)
@@ -85,6 +90,16 @@
splits[4].fragments.size.should eql(2)
end
+ it "should fill" do
+ new_mp3 = (@mp3.slice(0, 6) + @mp3.slice(0, 2)).fill(15)
+ new_mp3.duration.should eql(15)
+ new_mp3.fragments.size.should eql(4)
+ new_mp3.fragments[0].duration.should eql(6)
+ new_mp3.fragments[1].duration.should eql(2)
+ new_mp3.fragments[2].duration.should eql(6)
+ new_mp3.fragments[3].duration.should eql(1)
+ end
+
it "should write to file and return new instance of Scissor" do
new_mp3 = @mp3.slice(0, 120) + @mp3.slice(150, 20)
result = new_mp3.to_file('/tmp/scissor-test/out.mp3')
Please sign in to comment.
Something went wrong with that request. Please try again.