From de68ca89d2196cda17030303eaf137fb105cbbe7 Mon Sep 17 00:00:00 2001 From: youpy Date: Mon, 6 Apr 2009 07:55:35 +0900 Subject: [PATCH] * add Scissor::Sequence, Scissor.sequence * update rdoc * version0.0.14 --- README.rdoc | 6 +++--- Rakefile | 2 +- lib/scissor.rb | 32 ++++++++++++++++++++++++++++ scissor.gemspec | 2 +- spec/sequence_spec.rb | 49 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 spec/sequence_spec.rb diff --git a/README.rdoc b/README.rdoc index b57665a..db790cb 100644 --- a/README.rdoc +++ b/README.rdoc @@ -48,9 +48,9 @@ supported file format: # replace first 10 seconds with 30 seconds of silence foo.replace(0, 10, Scissor.silence(30)).to_file('replace.mp3') - # reverse + concat + loop - beat = foo.slice(0, 1) - ((beat * 3) + beat.reverse) * 80 + # sequence + loop + seq = Scissor.sequence('x y xyz', 0.2) + seq.apply(:x => foo, :y => bar, :z => foo.reverse) * 4 > 'sequence.wav' == Copyright diff --git a/Rakefile b/Rakefile index ffb7040..4b9c8cc 100644 --- a/Rakefile +++ b/Rakefile @@ -17,7 +17,7 @@ DESCRIPTION = "utility to chop sound files" RUBYFORGE_PROJECT = "scissor" HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org" BIN_FILES = %w( ) -VERS = "0.0.13" +VERS = "0.0.14" REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil CLEAN.include ['**/.*.sw?', '*.gem', '.config'] diff --git a/lib/scissor.rb b/lib/scissor.rb index 0ef51b8..2b32804 100644 --- a/lib/scissor.rb +++ b/lib/scissor.rb @@ -267,6 +267,10 @@ def silence(duration) slice(0, 1). fill(duration) end + + def sequence(*args) + Sequence.new(*args) + end end class SoundFile @@ -314,4 +318,32 @@ def reversed? @reverse end end + + class Sequence + def initialize(pattern, duration_per_step) + @pattern = pattern + @duration_per_step = duration_per_step + end + + def apply(scissors) + result = Scissor.new + + @pattern.split(//).each do |c| + if scissors.include?(c.to_sym) + scissor = scissors[c.to_sym] + + if @duration_per_step > scissor.duration + result += scissor + result += Scissor.silence(@duration_per_step - scissor.duration) + else + result += scissors[c.to_sym].slice(0, @duration_per_step) + end + else + result += Scissor.silence(@duration_per_step) + end + end + + result + end + end end diff --git a/scissor.gemspec b/scissor.gemspec index 16ea3f4..5c779e2 100644 --- a/scissor.gemspec +++ b/scissor.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = %q{scissor} - s.version = "0.0.13" + s.version = "0.0.14" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["youpy"] diff --git a/spec/sequence_spec.rb b/spec/sequence_spec.rb new file mode 100644 index 0000000..b7cb835 --- /dev/null +++ b/spec/sequence_spec.rb @@ -0,0 +1,49 @@ +$:.unshift File.dirname(__FILE__) + +require 'spec_helper' + +describe Scissor::Sequence do + before do + @foo = Scissor.new(fixture('sample.mp3')) + @bar = Scissor.new(fixture('sine.wav')) + end + + it "should instantiated" do + seq = Scissor.sequence('ababaab', 1.5) + seq.should be_an_instance_of(Scissor::Sequence) + end + + it "should apply" do + seq = Scissor.sequence('ababaab ab', 0.5) + scissor = seq.apply(:a => @foo, :b => @bar) + + scissor.should be_an_instance_of(Scissor) + scissor.duration.should eql(5.0) + scissor.fragments.size.should eql(10) + + scissor.fragments.each do |fragment| + fragment.duration.should eql(0.5) + end + + scissor.fragments[0].filename.to_s.should eql(fixture('sample.mp3')) + scissor.fragments[1].filename.to_s.should eql(fixture('sine.wav')) + scissor.fragments[2].filename.to_s.should eql(fixture('sample.mp3')) + scissor.fragments[3].filename.to_s.should eql(fixture('sine.wav')) + scissor.fragments[4].filename.to_s.should eql(fixture('sample.mp3')) + scissor.fragments[5].filename.to_s.should eql(fixture('sample.mp3')) + scissor.fragments[6].filename.to_s.should eql(fixture('sine.wav')) + scissor.fragments[8].filename.to_s.should eql(fixture('sample.mp3')) + scissor.fragments[9].filename.to_s.should eql(fixture('sine.wav')) + end + + it "should append silence when applied instance does not have enough duration" do + seq = Scissor.sequence('ba', 1.5) + scissor = seq.apply(:a => @foo, :b => @bar) + + scissor.duration.should eql(3.0) + scissor.fragments.size.should eql(3) + scissor.fragments[0].filename.to_s.should eql(fixture('sine.wav')) + scissor.fragments[1].filename.to_s.should match(/silence\.mp3/) + scissor.fragments[2].filename.to_s.should eql(fixture('sample.mp3')) + end +end