Skip to content

Commit

Permalink
* add Scissor::Sequence, Scissor.sequence
Browse files Browse the repository at this point in the history
 * update rdoc
 * version0.0.14
  • Loading branch information
youpy committed Apr 5, 2009
1 parent 43e2840 commit de68ca8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.rdoc
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -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']
Expand Down
32 changes: 32 additions & 0 deletions lib/scissor.rb
Expand Up @@ -267,6 +267,10 @@ def silence(duration)
slice(0, 1).
fill(duration)
end

def sequence(*args)
Sequence.new(*args)
end
end

class SoundFile
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion 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"]
Expand Down
49 changes: 49 additions & 0 deletions 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

0 comments on commit de68ca8

Please sign in to comment.