#! Contains the tests of `audio_output`: one of no device, mixed as the test #! asks; the system's default device, where there is one; and the system's #! devices listed, one opened by its name, and a name of none refused. import std::testing; import std::thread; import std::time; import malunal::audio; @test def mixes_for_no_device_as_asked(t: &mut tester): void { var out := audio_output::headless(44100); t.expects(!out.has_device && out.rate() == 44100); val id := out.add(sound::tone(440, 0.1, 44100)); val v := out.play(id, playback{ pan = -1 }); var frames := vector::::new(); frames.resize(2 * 441); out.render(frames.slice); t.expects(out.played == 441 && frames[2 * 200] != 0 && frames[2 * 200 + 1] == 0); t.expects(out.playing(v)); out.stop(v); t.expects(!out.playing(v)); } @test def plays_to_the_default_device_where_there_is_one(t: &mut tester): void { var out := audio_output::open() else (e) { # A machine of no audio device, as a build server, passes. t.expects(e == audio_error::no_device); return; }; t.expects(out.has_device && out.rate() >= 8000); # Silent, so the test isn't heard. val id := out.add(sound::tone(440, 0.05, out.rate(), 0)); _ = out.play(id); val started := time::instant::now(); while out.played < 2000 && started.elapsed().compare(&time::seconds(2)) < 0 { thread::sleep(time::milliseconds(10)); } t.expects(out.played >= 2000); } @test def opens_a_device_by_its_name(t: &mut tester): void { val devices := audio_output::devices(); var defaults: u32 = 0; for &each in devices { t.expects(each.name.length > 0 && each.id.length > 0); if each.is_default { defaults++; } } t.expects(defaults <= 1); t.expects(fails(audio_output::open(0.02, "no device is named this"), audio_error::no_device)); # A machine of no audio device, as a build server, has none to open. return if devices.empty; var out := audio_output::open(0.02, devices[0].name.span) else (e) { t.expects(false); return; }; val id := out.add(sound::tone(440, 0.05, out.rate(), 0)); _ = out.play(id); val started := time::instant::now(); while out.played < 2000 && started.elapsed().compare(&time::seconds(2)) < 0 { thread::sleep(time::milliseconds(10)); } t.expects(out.played >= 2000); }