-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic attribute generation with Gens.
- Loading branch information
Jeremy Voorhis
committed
Nov 1, 2008
1 parent
8c47183
commit c63f455
Showing
5 changed files
with
101 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Music | ||
class Gen | ||
def initialize(&fn) | ||
@fn = fn | ||
end | ||
|
||
def apply(name, val, context) | ||
@fn.call(context) | ||
end | ||
end | ||
|
||
class Tr < Gen | ||
def apply(name, val, context) | ||
@fn.call(val) | ||
end | ||
end | ||
|
||
class Env < Gen | ||
def apply(name, val, context) | ||
@fn.call(val, context.phase) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require File.join( File.dirname(__FILE__), 'spec_helper') | ||
|
||
describe Gen do | ||
before(:all) do | ||
@score = s( n(c4, 1), | ||
:amp => gen { |c| 0.5 }) | ||
end | ||
|
||
it "should generate values from the given context" do | ||
timeline = @score.to_timeline | ||
timeline[0].amp.should == 0.5 | ||
end | ||
end | ||
|
||
describe Tr do | ||
before(:all) do | ||
@score = s( n(c4, 1, :amp => 0.5), | ||
:amp => tr { |amp| amp * 0.5 } ) | ||
end | ||
|
||
it "should transform attribute values" do | ||
timeline = @score.to_timeline | ||
timeline[0].amp.should == 0.25 | ||
end | ||
end | ||
|
||
describe Env do | ||
it "should transform attribute values based on their phase" do | ||
score = s( seq(n([c4, e4, g4], 1, :amp => 0.5)), | ||
:amp => env { |value, phase| value * Math.sin(phase) } ) | ||
timeline = score.to_timeline | ||
timeline[0].amp.should == 0.0 | ||
timeline[1].amp.should be_close(0.1636, 0.0001) | ||
timeline[2].amp.should be_close(0.3092, 0.0001) | ||
end | ||
|
||
it "can be used to implement various transforms" do | ||
score = cresc(2, | ||
seq(n([c4, e4, g4], 1, :velocity => 64))) | ||
timeline = score.to_timeline | ||
timeline[0].velocity.should == 64 | ||
timeline[1].velocity.should == 85 | ||
timeline[2].velocity.should == 106 | ||
end | ||
|
||
def cresc(factor, score) | ||
s(score, :velocity => env { |vel, ph| | ||
multiplier = (factor - 1.0) * (1.0 + ph) | ||
(vel * multiplier).to_i | ||
}) | ||
end | ||
end |