Adjusted buffer length so buffer ends at the end of the ramp.
This commit is contained in:
parent
4eb15ae29f
commit
8c134edc26
2 changed files with 13 additions and 9 deletions
|
@ -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)
|
play(wave)
|
||||||
sleep(2)
|
sleep(2)
|
||||||
stop(wave)
|
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)
|
play(wave)
|
||||||
sleep(4)
|
sleep(4)
|
||||||
stop(wave)
|
stop(wave)
|
16
src/nodes.jl
16
src/nodes.jl
|
@ -282,6 +282,7 @@ type LinRampRenderer <: AudioRenderer
|
||||||
key_samples::Array{AudioSample}
|
key_samples::Array{AudioSample}
|
||||||
key_durations::Array{Float32}
|
key_durations::Array{Float32}
|
||||||
|
|
||||||
|
duration::Float32
|
||||||
buf::AudioBuf
|
buf::AudioBuf
|
||||||
|
|
||||||
LinRampRenderer(start, finish, dur) = LinRampRenderer([start,finish], [dur])
|
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})
|
function LinRampRenderer(key_samples::Array{AudioSample}, key_durations::Array{Float32})
|
||||||
@assert length(key_samples) == length(key_durations) + 1
|
@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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -302,22 +303,25 @@ typealias LinRamp AudioNode{LinRampRenderer}
|
||||||
export LinRamp
|
export LinRamp
|
||||||
|
|
||||||
function render(node::LinRampRenderer, device_input::AudioBuf, info::DeviceInfo)
|
function render(node::LinRampRenderer, device_input::AudioBuf, info::DeviceInfo)
|
||||||
# Grow buffer if it's too small
|
# Resize buffer if (1) it's too small or (2) we've hit the end of the ramp
|
||||||
if length(node.buf) < info.buf_size
|
ramp_samples = int(node.duration * info.sample_rate)
|
||||||
resize!(node.buf, info.buf_size)
|
block_samples = min(ramp_samples, info.buf_size)
|
||||||
|
if length(node.buf) != block_samples
|
||||||
|
resize!(node.buf, block_samples)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Fill the buffer as long as there are more segments
|
# Fill the buffer as long as there are more segments
|
||||||
dt::Float32 = 1/info.sample_rate
|
dt::Float32 = 1/info.sample_rate
|
||||||
i::Int = 1
|
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
|
# 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
|
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.buf[i] = node.key_samples[1]
|
||||||
node.key_samples[1] += ds
|
node.key_samples[1] += ds
|
||||||
node.key_durations[1] -= dt
|
node.key_durations[1] -= dt
|
||||||
|
node.duration -= dt
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
# Discard segment if we're finished
|
# Discard segment if we're finished
|
||||||
|
|
Loading…
Reference in a new issue