From 0cd33b82892818fbeaed0ea509e218b24fef5ce7 Mon Sep 17 00:00:00 2001 From: Joris Kraak Date: Fri, 1 Aug 2014 14:01:49 +0200 Subject: [PATCH] Stop trying to read from audio file if no data is available As soon as libsndfile has finished reading a file it no longer returns any data. This can send the `render` method for `FileRenderer` into an infinite loop. This way if not enough data is available a partial buffer is returned. --- src/sndfile.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/sndfile.jl b/src/sndfile.jl index 1b277be..9fc257b 100644 --- a/src/sndfile.jl +++ b/src/sndfile.jl @@ -153,10 +153,13 @@ FilePlayer(path::String) = FilePlayer(af_open(path)) function render(node::FileRenderer, device_input::AudioBuf, info::DeviceInfo) @assert node.file.sfinfo.samplerate == info.sample_rate - # Keep reading data from the file until the output buffer is full + # Keep reading data from the file until the output buffer is full, but stop + # as soon as no more data can be read from the file audio = Array(AudioSample, node.file.sfinfo.channels, 0) - while size(audio, 2) < info.buf_size - audio = hcat(audio, read(node.file, info.buf_size-size(audio, 2), AudioSample)) + read_audio = zeros(AudioSample, node.file.sfinfo.channels, 1) + while size(audio, 2) < info.buf_size && size(read_audio, 2) > 0 + read_audio = read(node.file, info.buf_size-size(audio, 2), AudioSample) + audio = hcat(audio, read_audio) end if audio == Nothing