lib/xi/pattern/transforms.rb in xi-lang-0.1.0 vs lib/xi/pattern/transforms.rb in xi-lang-0.1.2

- old
+ new

@@ -10,13 +10,16 @@ # peek -[1, -2, 3].p #=> [-1, 2, -3] # # @return [Pattern] # def -@ - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:-@) ? -v : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:-@) ? -v : v), e.start, e.duration] + } + } end # Concatenate +object+ pattern or perform a scalar sum with +object+ # # If +object+ is a Pattern, concatenate the two patterns. @@ -34,18 +37,21 @@ # @param object [Pattern, Numeric] pattern or numeric # @return [Pattern] # def +(object) if object.is_a?(Pattern) - Pattern.new(self, size: size + object.size) do |y| + Pattern.new(self, size: size + object.size) { |y| each { |v| y << v } object.each { |v| y << v } - end + } else - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:+) ? v + object : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:+) ? v + object : v), e.start, e.duration] + } + } end end # Performs a scalar substraction with +numeric+ # @@ -58,13 +64,16 @@ # # @param numeric [Numeric] # @return [Pattern] # def -(numeric) - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:-) ? v - numeric : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:-) ? v - numeric : v), e.start, e.duration] + } + } end # Performs a scalar multiplication with +numeric+ # # For each value from pattern, multiplicate with +numeric+. @@ -76,13 +85,16 @@ # # @param numeric [Numeric] # @return [Pattern] # def *(numeric) - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:*) ? v * numeric : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:*) ? v * numeric : v), e.start, e.duration] + } + } end # Performs a scalar division by +numeric+ # # For each value from pattern, divide by +numeric+. @@ -94,13 +106,16 @@ # # @param numeric [Numeric] # @return [Pattern] # def /(numeric) - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:/) ? v / numeric : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:/) ? v / numeric : v), e.start, e.duration] + } + } end # Performs a scalar modulo against +numeric+ # # For each value from pattern, return modulo of value divided by +numeric+. @@ -112,13 +127,16 @@ # # @param numeric [Numeric] # @return [Pattern] # def %(numeric) - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:%) ? v % numeric : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:%) ? v % numeric : v), e.start, e.duration] + } + } end # Raises each value to the power of +numeric+, which may be negative or # fractional. # @@ -130,13 +148,16 @@ # # @param numeric [Numeric] # @return [Pattern] # def **(numeric) - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:**) ? v ** numeric : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:**) ? v ** numeric : v), e.start, e.duration] + } + } end alias_method :^, :** # Cycles pattern +repeats+ number of times, shifted by +offset+ # @@ -216,13 +237,16 @@ # @param min [Numeric] # @param max [Numeric] # @return [Pattern] # def normalize(min, max) - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:-) ? (v - min) / (max - min) : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:-) ? (v - min) / (max - min) : v), e.start, e.duration] + } + } end # Scales a pattern of normalized values (0..1) to a custom range # +min+..+max+ # @@ -238,13 +262,16 @@ # @param min [Numeric] # @param max [Numeric] # @return [Pattern] # def denormalize(min, max) - Pattern.new(self) do |y| - each { |v| y << (v.respond_to?(:*) ? (max - min) * v + min : v) } - end + Pattern.new(self) { |y| + each_event { |e| + v = e.value + y << E[(v.respond_to?(:*) ? (max - min) * v + min : v), e.start, e.duration] + } + } end # Scale from one range of values to another range of values # # @example @@ -260,19 +287,19 @@ normalize(min_from, max_from).denormalize(min_to, max_to) end # TODO Document def decelerate(num) - Pattern.new(self) do |y| + Pattern.new(self) { |y| each_event { |e| y << E[e.value, e.start * num, e.duration * num] } - end + } end # TODO Document def accelerate(num) - Pattern.new(self) do |y| + Pattern.new(self) { |y| each_event { |e| y << E[e.value, e.start / num, e.duration / num] } - end + } end # Based on +probability+, it yields original value or nil # TODO Document #