Skip to content

Commit

Permalink
Change percent input to decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmacphersonmusic committed Jun 20, 2015
1 parent 23f42e3 commit 6aa37b7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/music_theory/envelope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ class Envelope

def initialize(options = {})
@duration = options[:duration]
@attack = options[:attack] # in seconds ex: 0.15
@sustain = options[:sustain] # percentage ex: 30
@decay = options[:decay] # percentage ex: 60
@attack = options[:attack] # in seconds ex: 0.15
@sustain = options[:sustain] # decimal between 0 and 1 ex: 0.2
@decay = options[:decay] # decimal between 0 and 1 ex: 0.6
@sample_rate = 22050
@total_frames = @sample_rate * @duration
end
Expand All @@ -18,13 +18,13 @@ def attack_frames
end

def sustain_frames
sustain_frames_count = (@total_frames - attack_frames.length) * (@sustain.to_f / 100.0)
sustain_frames_count = (@total_frames - attack_frames.length) * @sustain
sustain_frames = Array.new(sustain_frames_count) { 1 }
sustain_frames
end

def decay_frames
decay_frames_count = (@total_frames - attack_frames.length) * (@decay.to_f / 100.0)
decay_frames_count = (@total_frames - attack_frames.length) * @decay
decay_rate = (1.0 / decay_frames_count)
decay_frames = Array.new(decay_frames_count){ |index| index * decay_rate }.reverse
decay_frames
Expand All @@ -34,7 +34,7 @@ def envelope_frames
envelope_frames = attack_frames + sustain_frames + decay_frames
if envelope_frames.length < @total_frames
remainder = @total_frames - envelope_frames.length
filler = Array.new(remainder){ 0 }
filler = Array.new(remainder) { 0 }
envelope_frames += filler
end
envelope_frames
Expand Down
4 changes: 2 additions & 2 deletions test/envelope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def setup
duration: duration,
envelope: Envelope.new(duration: duration,
attack: 0.15,
sustain: 10,
decay: 70),
sustain: 0.2,
decay: 0.6),
output_file_name: 'delete_me')
end

Expand Down

0 comments on commit 6aa37b7

Please sign in to comment.