2013-12-30 10:00:04 +01:00
|
|
|
using Base.Test
|
2014-01-03 16:41:00 +01:00
|
|
|
using AudioIO
|
2013-12-30 10:00:04 +01:00
|
|
|
|
|
|
|
const TEST_SAMPLERATE = 44100
|
|
|
|
const TEST_BUF_SIZE = 1024
|
|
|
|
|
2014-01-03 16:41:00 +01:00
|
|
|
type TestAudioStream <: AudioIO.AudioStream
|
2013-12-30 10:00:04 +01:00
|
|
|
mixer::AudioMixer
|
2014-01-03 16:41:00 +01:00
|
|
|
info::AudioIO.DeviceInfo
|
2013-12-30 10:00:04 +01:00
|
|
|
|
|
|
|
function TestAudioStream()
|
|
|
|
mixer = AudioMixer()
|
2014-01-03 16:41:00 +01:00
|
|
|
new(mixer, AudioIO.DeviceInfo(TEST_SAMPLERATE, TEST_BUF_SIZE))
|
2013-12-30 10:00:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# render the stream and return the next block of audio. This is used in testing
|
|
|
|
# to simulate the audio callback that's normally called by the device.
|
|
|
|
function process(stream::TestAudioStream)
|
2014-01-03 16:41:00 +01:00
|
|
|
in_array = zeros(AudioIO.AudioSample, stream.info.buf_size)
|
2014-01-03 22:02:25 +01:00
|
|
|
out_array, _ = AudioIO.render(stream.mixer, in_array, stream.info)
|
|
|
|
return out_array
|
2013-12-30 10:00:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#### Test playing back various vector types ####
|
|
|
|
|
|
|
|
# data shared between tests, for convenience
|
|
|
|
t = linspace(0, 2, 2 * 44100)
|
|
|
|
phase = 2pi * 100 * t
|
|
|
|
|
|
|
|
## Test Float32 arrays, this is currently the native audio playback format
|
2013-12-30 10:31:54 +01:00
|
|
|
info("Testing Playing Float32 arrays...")
|
2013-12-30 10:00:04 +01:00
|
|
|
f32 = convert(Array{Float32}, sin(phase))
|
|
|
|
test_stream = TestAudioStream()
|
|
|
|
player = play(f32, test_stream)
|
|
|
|
@test process(test_stream) == f32[1:TEST_BUF_SIZE]
|
|
|
|
|
|
|
|
|
2013-12-30 10:31:54 +01:00
|
|
|
info("Testing Playing Float64 arrays...")
|
2013-12-30 10:00:04 +01:00
|
|
|
f64 = convert(Array{Float64}, sin(phase))
|
|
|
|
test_stream = TestAudioStream()
|
|
|
|
player = play(f64, test_stream)
|
2014-01-03 16:41:00 +01:00
|
|
|
@test process(test_stream) == convert(AudioIO.AudioBuf, f64[1:TEST_BUF_SIZE])
|
2013-12-30 10:00:04 +01:00
|
|
|
|
2013-12-30 10:31:54 +01:00
|
|
|
info("Testing Playing Int8(Signed) arrays...")
|
2013-12-30 10:00:04 +01:00
|
|
|
i8 = Int8[-127:127]
|
|
|
|
test_stream = TestAudioStream()
|
|
|
|
player = play(i8, test_stream)
|
|
|
|
@test_approx_eq(process(test_stream)[1:255],
|
2014-01-03 16:41:00 +01:00
|
|
|
convert(AudioIO.AudioBuf, linspace(-1.0, 1.0, 255)))
|
2013-12-30 10:00:04 +01:00
|
|
|
|
2013-12-30 10:31:54 +01:00
|
|
|
info("Testing Playing Uint8(Unsigned) arrays...")
|
2013-12-30 10:00:04 +01:00
|
|
|
# for unsigned 8-bit audio silence is represented as 128, so the symmetric range
|
|
|
|
# is 1-255
|
|
|
|
ui8 = Uint8[1:255]
|
|
|
|
test_stream = TestAudioStream()
|
|
|
|
player = play(ui8, test_stream)
|
|
|
|
@test_approx_eq(process(test_stream)[1:255],
|
2014-01-03 16:41:00 +01:00
|
|
|
convert(AudioIO.AudioBuf, linspace(-1.0, 1.0, 255)))
|
2013-12-30 12:29:43 +01:00
|
|
|
|
|
|
|
|
2014-01-06 04:50:56 +01:00
|
|
|
info("Testing AudioNode Stopping...")
|
|
|
|
test_stream = TestAudioStream()
|
|
|
|
node = SinOsc(440)
|
|
|
|
@test !node.active
|
|
|
|
play(node, test_stream)
|
|
|
|
@test node.active
|
|
|
|
process(test_stream)
|
|
|
|
stop(node)
|
|
|
|
@test !node.active
|
|
|
|
# give the render task a chance to clean up
|
|
|
|
process(test_stream)
|
|
|
|
@test process(test_stream) == zeros(AudioIO.AudioSample, TEST_BUF_SIZE)
|
2014-01-06 17:24:53 +01:00
|
|
|
|
|
|
|
info("Testing libsndfile read")
|
|
|
|
samplerate = 44100
|
|
|
|
freq = 440
|
|
|
|
t = linspace(0, 2, 2 * samplerate)
|
|
|
|
phase = 2 * pi * freq * t
|
|
|
|
reference = int16((2 ^ 15 - 1) * sin(phase))
|
|
|
|
|
2014-01-11 04:30:52 +01:00
|
|
|
f = af_open("test/sinwave.flac")
|
2014-01-06 17:24:53 +01:00
|
|
|
@test f.sfinfo.channels == 1
|
|
|
|
@test f.sfinfo.frames == 2 * samplerate
|
|
|
|
actual = readFrames(f, 2 * samplerate)
|
|
|
|
@test_approx_eq(reference, actual)
|