From afe4e0d8be50abf23ac700356805e6d43a4d0712 Mon Sep 17 00:00:00 2001 From: Spencer Russell Date: Mon, 23 Jun 2014 17:58:32 -0400 Subject: [PATCH] fixes imprecise SinOsc --- src/nodes.jl | 13 +++++++++---- test/test_nodes.jl | 9 +++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/nodes.jl b/src/nodes.jl index f83a1ba..95607a7 100644 --- a/src/nodes.jl +++ b/src/nodes.jl @@ -29,10 +29,15 @@ SinOsc() = SinOsc(440) export SinOsc function render(node::SinOscRenderer, device_input::AudioBuf, info::DeviceInfo) - phase = AudioSample[0:(info.buf_size-1)] * 2pi * node.freq / info.sample_rate - phase .+= node.phase - node.phase = phase[end] + 2pi * node.freq / info.sample_rate - return sin(phase) + outbuf = Array(AudioSample, info.buf_size) + phase = node.phase + dt = 1/info.sample_rate + for i in 1:info.buf_size + outbuf[i] = sin(2pi*node.freq*phase) + phase += dt + end + node.phase = phase + return outbuf end #### AudioMixer #### diff --git a/test/test_nodes.jl b/test/test_nodes.jl index 93442e0..64bddf9 100644 --- a/test/test_nodes.jl +++ b/test/test_nodes.jl @@ -56,13 +56,14 @@ render_output = render(mix, dev_input, test_info) info("Testing SinOSC...") freq = 440 -t = linspace(0, - (test_info.buf_size-1) / test_info.sample_rate, - test_info.buf_size) +# note that this range includes the end, which is why there are sample_rate+1 samples +t = linspace(0, 1, test_info.sample_rate+1) test_vect = convert(AudioBuf, sin(2pi * t * freq)) osc = SinOsc(freq) render_output = render(osc, dev_input, test_info) -@test_approx_eq(render_output, test_vect) +@test render_output == test_vect[1:test_info.buf_size] +render_output = render(osc, dev_input, test_info) +@test render_output == test_vect[test_info.buf_size+1:2*test_info.buf_size] stop(osc) render_output = render(osc, dev_input, test_info) @test render_output == AudioSample[]