From ae76badebbc5af0db9e4a331d38b5f5d7299afe6 Mon Sep 17 00:00:00 2001 From: Spencer Russell Date: Wed, 27 Aug 2014 10:57:55 -0400 Subject: [PATCH] adds method to get rate of a file --- src/sndfile.jl | 4 +++- test/test_sndfile.jl | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/sndfile.jl b/src/sndfile.jl index 21664a6..b8ac331 100644 --- a/src/sndfile.jl +++ b/src/sndfile.jl @@ -1,4 +1,4 @@ -export af_open, FilePlayer, rewind +export af_open, FilePlayer, rewind, samplerate const SFM_READ = int32(0x10) const SFM_WRITE = int32(0x20) @@ -41,6 +41,8 @@ type AudioFile sfinfo::SF_INFO end +samplerate(f::AudioFile) = f.sfinfo.samplerate + # AudioIO.open is part of the public API, but is not exported so that it # doesn't conflict with Base.open function open(path::String, mode::String = "r", diff --git a/test/test_sndfile.jl b/test/test_sndfile.jl index 95c673e..75ab80d 100644 --- a/test/test_sndfile.jl +++ b/test/test_sndfile.jl @@ -9,9 +9,9 @@ import AudioIO: DeviceInfo, render, AudioSample, AudioBuf facts("WAV file write/read") do fname = Pkg.dir("AudioIO", "test", "sinwave.wav") - samplerate = 44100 + srate = 44100 freq = 440 - t = [0 : 2 * samplerate - 1] / samplerate + t = [0 : 2 * srate - 1] / srate phase = 2 * pi * freq * t reference = int16((2 ^ 15 - 1) * sin(phase)) @@ -22,10 +22,11 @@ facts("WAV file write/read") do # test basic reading AudioIO.open(fname) do f @fact f.sfinfo.channels => 1 - @fact f.sfinfo.frames => 2 * samplerate + @fact f.sfinfo.frames => 2 * srate actual = read(f) @fact length(reference) => length(actual) @fact reference => actual[:, 1] + @fact samplerate(f) => srate end # test seeking @@ -35,7 +36,7 @@ facts("WAV file write/read") do # pretend we have a stream at the same rate as the file bufsize = 1024 input = zeros(AudioSample, bufsize) - test_info = DeviceInfo(samplerate, bufsize) + test_info = DeviceInfo(srate, bufsize) node = FilePlayer(f) # convert to floating point because that's what AudioIO uses natively expected = convert(AudioBuf, reference ./ (2^15)) @@ -48,8 +49,8 @@ end facts("Stereo file reading") do fname = Pkg.dir("AudioIO", "test", "440left_880right.wav") - samplerate = 44100 - t = [0 : 2 * samplerate - 1] / samplerate + srate = 44100 + t = [0 : 2 * srate - 1] / srate expected = int16((2^15-1) * hcat(sin(2pi*t*440), sin(2pi*t*880))) AudioIO.open(fname) do f @@ -62,11 +63,11 @@ end # new-fangled stereo sound stuff facts("Stereo file rendering") do fname = Pkg.dir("AudioIO", "test", "440left_880right.wav") - samplerate = 44100 + srate = 44100 bufsize = 1024 input = zeros(AudioSample, bufsize) - test_info = DeviceInfo(samplerate, bufsize) - t = [0 : 2 * samplerate - 1] / samplerate + test_info = DeviceInfo(srate, bufsize) + t = [0 : 2 * srate - 1] / srate expected = convert(AudioBuf, 0.5 * (sin(2pi*t*440) + sin(2pi*t*880))) AudioIO.open(fname) do f