Support playing sound from stereo files

This commit is contained in:
Joris Kraak 2014-08-01 14:00:24 +02:00
parent 18f0cee2e5
commit 289d7a56cd

View file

@ -86,11 +86,7 @@ end
# through an arbitrary render chain and returns the result as a vector # through an arbitrary render chain and returns the result as a vector
function Base.read(file::AudioFile, nframes::Integer, dtype::Type) function Base.read(file::AudioFile, nframes::Integer, dtype::Type)
@assert file.sfinfo.channels <= 2 @assert file.sfinfo.channels <= 2
if file.sfinfo.channels == 2 arr = zeros(dtype, file.sfinfo.channels, nframes)
arr = zeros(dtype, 2, nframes)
else
arr = zeros(dtype, nframes)
end
if dtype == Int16 if dtype == Int16
nread = ccall((:sf_readf_short, libsndfile), Int64, nread = ccall((:sf_readf_short, libsndfile), Int64,
@ -110,7 +106,7 @@ function Base.read(file::AudioFile, nframes::Integer, dtype::Type)
file.filePtr, arr, nframes) file.filePtr, arr, nframes)
end end
return arr[1:nread] return arr[:, 1:nread]
end end
Base.read(file::AudioFile, dtype::Type) = Base.read(file, file.sfinfo.frames, dtype) Base.read(file::AudioFile, dtype::Type) = Base.read(file, file.sfinfo.frames, dtype)
@ -157,11 +153,10 @@ FilePlayer(path::String) = FilePlayer(af_open(path))
function render(node::FileRenderer, device_input::AudioBuf, info::DeviceInfo) function render(node::FileRenderer, device_input::AudioBuf, info::DeviceInfo)
@assert node.file.sfinfo.samplerate == info.sample_rate @assert node.file.sfinfo.samplerate == info.sample_rate
frames_read = 0 # Keep reading data from the file until the output buffer is full
audio = AudioSample[] audio = Array(AudioSample, node.file.sfinfo.channels, 0)
while size(audio, 2) < info.buf_size while size(audio, 2) < info.buf_size
append!(audio, read(node.file, info.buf_size-size(audio, 2), AudioSample)) audio = hcat(audio, read(node.file, info.buf_size-size(audio, 2), AudioSample))
println("read $(size(audio, 2)) frames, requested $(info.buf_size-size(audio, 2))")
end end
if audio == Nothing if audio == Nothing
@ -171,10 +166,10 @@ function render(node::FileRenderer, device_input::AudioBuf, info::DeviceInfo)
# if the file is stereo, mix the two channels together # if the file is stereo, mix the two channels together
if node.file.sfinfo.channels == 2 if node.file.sfinfo.channels == 2
return (audio[1, :] / 2) + (audio[2, :] / 2) return (audio[1, :] / 2) + (audio[2, :] / 2)
end else
return audio return audio
end end
end
function play(filename::String, args...) function play(filename::String, args...)
player = FilePlayer(filename) player = FilePlayer(filename)