From 8c134edc263579f21565d8bd8885cf9d3e0a64fe Mon Sep 17 00:00:00 2001 From: Calder Coalson Date: Sat, 28 Jun 2014 16:33:34 -0500 Subject: [PATCH] Adjusted buffer length so buffer ends at the end of the ramp. --- examples/ramp.jl | 6 +++--- src/nodes.jl | 16 ++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/ramp.jl b/examples/ramp.jl index 9562541..f1c5b5c 100644 --- a/examples/ramp.jl +++ b/examples/ramp.jl @@ -12,13 +12,13 @@ println(""" * * * * * * * * * * * """) -wave = SinOsc(440) * LinRamp(0.0,1.0,2.0) +wave = SinOsc(440) * LinRamp(0.0, 1.0, 2.0) play(wave) sleep(2) stop(wave) -print(""" +println(""" * * * * * * * * * @@ -26,7 +26,7 @@ print(""" * * * * * * * * * * * * * * * * * * * * """) -wave = SinOsc(440) * LinRamp([0.0,1.0,0.0], [2.0,2.0]) +wave = SinOsc(440) * LinRamp([0.0, 1.0, 0.0], [2.0, 2.0]) play(wave) sleep(4) stop(wave) \ No newline at end of file diff --git a/src/nodes.jl b/src/nodes.jl index 711e53e..d524f44 100644 --- a/src/nodes.jl +++ b/src/nodes.jl @@ -282,6 +282,7 @@ type LinRampRenderer <: AudioRenderer key_samples::Array{AudioSample} key_durations::Array{Float32} + duration::Float32 buf::AudioBuf LinRampRenderer(start, finish, dur) = LinRampRenderer([start,finish], [dur]) @@ -294,7 +295,7 @@ type LinRampRenderer <: AudioRenderer function LinRampRenderer(key_samples::Array{AudioSample}, key_durations::Array{Float32}) @assert length(key_samples) == length(key_durations) + 1 - new(key_samples, key_durations, AudioSample[]) + new(key_samples, key_durations, sum(key_durations), AudioSample[]) end end @@ -302,22 +303,25 @@ typealias LinRamp AudioNode{LinRampRenderer} export LinRamp function render(node::LinRampRenderer, device_input::AudioBuf, info::DeviceInfo) - # Grow buffer if it's too small - if length(node.buf) < info.buf_size - resize!(node.buf, info.buf_size) + # Resize buffer if (1) it's too small or (2) we've hit the end of the ramp + ramp_samples = int(node.duration * info.sample_rate) + block_samples = min(ramp_samples, info.buf_size) + if length(node.buf) != block_samples + resize!(node.buf, block_samples) end # Fill the buffer as long as there are more segments dt::Float32 = 1/info.sample_rate i::Int = 1 - while i <= info.buf_size && length(node.key_samples) > 1 + while i <= length(node.buf) && length(node.key_samples) > 1 # Fill as much of the buffer as we can with the current segment ds::Float32 = (node.key_samples[2] - node.key_samples[1]) / node.key_durations[1] / info.sample_rate - while i <= info.buf_size + while i <= length(node.buf) node.buf[i] = node.key_samples[1] node.key_samples[1] += ds node.key_durations[1] -= dt + node.duration -= dt i += 1 # Discard segment if we're finished