fixes imprecise SinOsc

This commit is contained in:
Spencer Russell 2014-06-23 17:58:32 -04:00
parent f240f3fbf6
commit afe4e0d8be
2 changed files with 14 additions and 8 deletions

View file

@ -29,10 +29,15 @@ SinOsc() = SinOsc(440)
export SinOsc export SinOsc
function render(node::SinOscRenderer, device_input::AudioBuf, info::DeviceInfo) function render(node::SinOscRenderer, device_input::AudioBuf, info::DeviceInfo)
phase = AudioSample[0:(info.buf_size-1)] * 2pi * node.freq / info.sample_rate outbuf = Array(AudioSample, info.buf_size)
phase .+= node.phase phase = node.phase
node.phase = phase[end] + 2pi * node.freq / info.sample_rate dt = 1/info.sample_rate
return sin(phase) for i in 1:info.buf_size
outbuf[i] = sin(2pi*node.freq*phase)
phase += dt
end
node.phase = phase
return outbuf
end end
#### AudioMixer #### #### AudioMixer ####

View file

@ -56,13 +56,14 @@ render_output = render(mix, dev_input, test_info)
info("Testing SinOSC...") info("Testing SinOSC...")
freq = 440 freq = 440
t = linspace(0, # note that this range includes the end, which is why there are sample_rate+1 samples
(test_info.buf_size-1) / test_info.sample_rate, t = linspace(0, 1, test_info.sample_rate+1)
test_info.buf_size)
test_vect = convert(AudioBuf, sin(2pi * t * freq)) test_vect = convert(AudioBuf, sin(2pi * t * freq))
osc = SinOsc(freq) osc = SinOsc(freq)
render_output = render(osc, dev_input, test_info) 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) stop(osc)
render_output = render(osc, dev_input, test_info) render_output = render(osc, dev_input, test_info)
@test render_output == AudioSample[] @test render_output == AudioSample[]