2023-10-07 11:45:05 +00:00
|
|
|
use std::io::{BufReader, Read, Seek};
|
2023-10-04 14:49:35 +00:00
|
|
|
use std::path::Path;
|
2023-10-12 11:50:47 +00:00
|
|
|
use std::time::Duration;
|
2023-10-04 14:49:35 +00:00
|
|
|
|
2024-04-03 09:45:54 +00:00
|
|
|
use rodio::{Decoder, Source};
|
2024-04-04 14:26:42 +00:00
|
|
|
use rstest::rstest;
|
|
|
|
use rstest_reuse::{self, *};
|
|
|
|
|
|
|
|
#[template]
|
|
|
|
#[rstest]
|
|
|
|
// note: disabled, broken decoder see issue: #516
|
|
|
|
// #[cfg_attr(feature = "symphonia-vorbis"), case("ogg", true, "symphonia")],
|
|
|
|
#[cfg_attr(
|
|
|
|
all(feature = "minimp3", not(feature = "symphonia-mp3")),
|
|
|
|
case("mp3", false, "minimp3")
|
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
all(feature = "wav", not(feature = "symphonia-wav")),
|
|
|
|
case("wav", true, "hound")
|
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
all(feature = "flac", not(feature = "symphonia-flac")),
|
|
|
|
case("flac", false, "claxon")
|
|
|
|
)]
|
|
|
|
#[cfg_attr(feature = "symphonia-mp3", case("mp3", true, "symphonia"))]
|
|
|
|
#[cfg_attr(feature = "symponia-isomp4", case("m4a", true, "symphonia"))]
|
|
|
|
#[cfg_attr(feature = "symphonia-wav", case("wav", true, "symphonia"))]
|
|
|
|
#[cfg_attr(feature = "symphonia-flac", case("flac", true, "symphonia"))]
|
|
|
|
fn all_decoders(
|
|
|
|
#[case] format: &'static str,
|
|
|
|
#[case] supports_seek: bool,
|
|
|
|
#[case] decoder_name: &'static str,
|
|
|
|
) {
|
2023-10-12 11:39:16 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
#[template]
|
|
|
|
#[rstest]
|
|
|
|
// note: disabled, broken decoder see issue: #516
|
|
|
|
// #[cfg_attr(feature = "symphonia-vorbis"), case("ogg", true, "symphonia")],
|
|
|
|
#[cfg_attr(
|
|
|
|
all(feature = "wav", not(feature = "symphonia-wav")),
|
|
|
|
case("wav", "hound")
|
|
|
|
)]
|
|
|
|
#[cfg_attr(feature = "symphonia-mp3", case("mp3", "symphonia"))]
|
|
|
|
#[cfg_attr(feature = "symponia-isomp4", case("m4a", "symphonia"))]
|
|
|
|
#[cfg_attr(feature = "symphonia-wav", case("wav", "symphonia"))]
|
|
|
|
#[cfg_attr(feature = "symphonia-flac", case("flac", "symphonia"))]
|
|
|
|
fn supported_decoders(#[case] format: &'static str, #[case] decoder_name: &'static str) {}
|
|
|
|
|
|
|
|
#[apply(all_decoders)]
|
|
|
|
#[trace]
|
|
|
|
fn seek_returns_err_if_unsupported(
|
|
|
|
#[case] format: &'static str,
|
|
|
|
#[case] supports_seek: bool,
|
|
|
|
#[case] decoder_name: &'static str,
|
|
|
|
) {
|
|
|
|
let mut decoder = get_music(format);
|
|
|
|
let res = decoder.try_seek(Duration::from_millis(2500));
|
|
|
|
assert_eq!(res.is_ok(), supports_seek, "decoder: {decoder_name}");
|
2023-10-07 11:45:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
#[apply(supported_decoders)]
|
|
|
|
#[trace]
|
|
|
|
fn seek_beyond_end_saturates(#[case] format: &'static str, #[case] decoder_name: &'static str) {
|
|
|
|
let mut decoder = get_music(format);
|
|
|
|
println!("seeking beyond end for: {format}\t decoded by: {decoder_name}");
|
|
|
|
let res = decoder.try_seek(Duration::from_secs(999));
|
2023-10-05 11:28:59 +00:00
|
|
|
|
2024-04-05 11:59:40 +00:00
|
|
|
assert!(res.is_ok(), "err: {res:?}");
|
2024-04-04 14:26:42 +00:00
|
|
|
assert!(time_remaining(decoder) < Duration::from_secs(1));
|
2023-10-04 14:49:35 +00:00
|
|
|
}
|
2023-10-07 11:45:05 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
#[apply(supported_decoders)]
|
|
|
|
#[trace]
|
|
|
|
fn seek_results_in_correct_remaining_playtime(
|
|
|
|
#[case] format: &'static str,
|
|
|
|
#[case] decoder_name: &'static str,
|
|
|
|
) {
|
|
|
|
println!("checking seek duration for: {format}\t decoded by: {decoder_name}");
|
2023-10-10 00:25:34 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
let decoder = get_music(format);
|
|
|
|
let total_duration = time_remaining(decoder);
|
|
|
|
dbg!(total_duration);
|
2023-10-10 00:25:34 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
const SEEK_BEFORE_END: Duration = Duration::from_secs(5);
|
|
|
|
let mut decoder = get_music(format);
|
|
|
|
decoder.try_seek(total_duration - SEEK_BEFORE_END).unwrap();
|
|
|
|
|
|
|
|
let after_seek = time_remaining(decoder);
|
|
|
|
let expected = SEEK_BEFORE_END;
|
|
|
|
|
|
|
|
if after_seek.as_millis().abs_diff(expected.as_millis()) > 250 {
|
|
|
|
panic!(
|
|
|
|
"Seek did not result in expected leftover playtime
|
|
|
|
leftover time: {after_seek:?}
|
|
|
|
expected time left in source: {SEEK_BEFORE_END:?}"
|
|
|
|
);
|
2023-10-10 00:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
#[apply(supported_decoders)]
|
|
|
|
#[trace]
|
|
|
|
fn seek_possible_after_exausting_source(
|
|
|
|
#[case] format: &'static str,
|
|
|
|
#[case] _decoder_name: &'static str,
|
|
|
|
) {
|
|
|
|
let mut source = get_music(format);
|
|
|
|
while source.next().is_some() {}
|
|
|
|
assert!(source.next().is_none());
|
2023-10-08 12:05:34 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
source.try_seek(Duration::from_secs(0)).unwrap();
|
|
|
|
assert!(source.next().is_some());
|
|
|
|
}
|
2023-10-08 12:05:34 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
#[apply(supported_decoders)]
|
|
|
|
#[trace]
|
|
|
|
fn seek_does_not_break_channel_order(
|
|
|
|
#[case] format: &'static str,
|
|
|
|
#[case] _decoder_name: &'static str,
|
|
|
|
) {
|
|
|
|
let mut source = get_rl(format).convert_samples();
|
|
|
|
let channels = source.channels();
|
|
|
|
assert_eq!(channels, 2, "test needs a stereo beep file");
|
2023-10-08 12:05:34 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
let beep_range = second_channel_beep_range(&mut source);
|
|
|
|
let beep_start = Duration::from_secs_f32(
|
|
|
|
beep_range.start as f32 / source.channels() as f32 / source.sample_rate() as f32,
|
|
|
|
);
|
2023-10-08 12:05:34 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
let mut source = get_rl(format).convert_samples();
|
|
|
|
|
|
|
|
let mut channel_offset = 0;
|
|
|
|
for offset in [1, 4, 7, 40, 41, 120, 179]
|
|
|
|
.map(|offset| offset as f32 / (source.sample_rate() as f32))
|
|
|
|
.map(Duration::from_secs_f32)
|
|
|
|
{
|
|
|
|
source.next(); // WINDOW is even, make the amount of calls to next
|
|
|
|
// uneven to force issues with channels alternating
|
|
|
|
// between seek to surface
|
|
|
|
channel_offset = (channel_offset + 1) % 2;
|
|
|
|
|
|
|
|
source.try_seek(beep_start + offset).unwrap();
|
2024-04-05 10:16:06 +00:00
|
|
|
let samples: Vec<_> = source.by_ref().take(100).collect();
|
2024-04-04 14:26:42 +00:00
|
|
|
let channel0 = 0 + channel_offset;
|
|
|
|
assert!(
|
|
|
|
is_silent(&samples, source.channels(), channel0),
|
|
|
|
"channel0 should be silent,
|
|
|
|
channel0 starts at idx: {channel0}
|
|
|
|
seek: {beep_start:?} + {offset:?}
|
|
|
|
samples: {samples:?}"
|
|
|
|
);
|
|
|
|
let channel1 = (1 + channel_offset) % 2;
|
|
|
|
assert!(
|
|
|
|
!is_silent(&samples, source.channels(), channel1),
|
|
|
|
"channel1 should not be silent,
|
|
|
|
channel1; starts at idx: {channel1}
|
|
|
|
seek: {beep_start:?} + {offset:?}
|
|
|
|
samples: {samples:?}"
|
|
|
|
);
|
2023-10-08 12:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 10:08:51 +00:00
|
|
|
|
2024-04-02 10:59:22 +00:00
|
|
|
fn second_channel_beep_range<R: rodio::Source>(source: &mut R) -> std::ops::Range<usize>
|
|
|
|
where
|
|
|
|
R: Iterator<Item = f32>,
|
|
|
|
{
|
2024-04-02 10:08:51 +00:00
|
|
|
let channels = source.channels() as usize;
|
2024-04-02 10:59:22 +00:00
|
|
|
let samples: Vec<f32> = source.by_ref().collect();
|
2024-04-02 10:08:51 +00:00
|
|
|
|
2024-04-02 10:29:55 +00:00
|
|
|
const WINDOW: usize = 50;
|
2024-04-02 10:08:51 +00:00
|
|
|
let beep_starts = samples
|
2024-04-02 10:29:55 +00:00
|
|
|
.chunks_exact(WINDOW)
|
2024-04-02 10:08:51 +00:00
|
|
|
.enumerate()
|
2024-04-02 10:29:55 +00:00
|
|
|
.map(|(idx, chunk)| (idx * WINDOW, chunk))
|
2024-04-02 10:59:22 +00:00
|
|
|
.find(|(_, s)| {
|
|
|
|
s.iter()
|
|
|
|
.skip(1)
|
|
|
|
.step_by(channels)
|
|
|
|
.map(|s| s.abs())
|
|
|
|
.sum::<f32>()
|
|
|
|
> 0.1
|
|
|
|
})
|
2024-04-02 10:08:51 +00:00
|
|
|
.expect("track should not be silent")
|
2024-04-05 10:16:06 +00:00
|
|
|
.0
|
|
|
|
.next_multiple_of(channels);
|
2024-04-02 10:08:51 +00:00
|
|
|
|
2024-04-05 10:16:06 +00:00
|
|
|
const BASICALLY_ZERO: f32 = 0.0001;
|
2024-04-02 10:08:51 +00:00
|
|
|
let beep_ends = samples
|
2024-04-02 10:29:55 +00:00
|
|
|
.chunks_exact(WINDOW)
|
2024-04-02 10:08:51 +00:00
|
|
|
.enumerate()
|
2024-04-02 10:29:55 +00:00
|
|
|
.map(|(idx, chunk)| (idx * WINDOW, chunk))
|
|
|
|
.skip(beep_starts / WINDOW)
|
|
|
|
.find(|(_, s)| {
|
|
|
|
s.iter()
|
|
|
|
.skip(1)
|
2024-04-02 10:59:22 +00:00
|
|
|
.step_by(channels)
|
2024-04-02 10:29:55 +00:00
|
|
|
.all(|s| s.abs() < BASICALLY_ZERO)
|
|
|
|
})
|
2024-04-02 10:08:51 +00:00
|
|
|
.expect("beep should end")
|
2024-04-05 10:16:06 +00:00
|
|
|
.0
|
|
|
|
.next_multiple_of(channels);
|
|
|
|
|
|
|
|
let samples = &samples[beep_starts..beep_starts + 100];
|
|
|
|
assert!(is_silent(samples, channels as u16, 0), "{samples:?}");
|
|
|
|
assert!(!is_silent(samples, channels as u16, 1), "{samples:?}");
|
2024-04-02 10:59:22 +00:00
|
|
|
|
|
|
|
beep_starts..beep_ends
|
|
|
|
}
|
|
|
|
|
2024-04-03 09:45:54 +00:00
|
|
|
fn is_silent(samples: &[f32], channels: u16, channel: usize) -> bool {
|
|
|
|
assert_eq!(samples.len(), 100);
|
|
|
|
let channel = samples.iter().skip(channel).step_by(channels as usize);
|
|
|
|
let volume =
|
|
|
|
channel.map(|s| s.abs()).sum::<f32>() as f32 / samples.len() as f32 * channels as f32;
|
2024-04-02 10:59:22 +00:00
|
|
|
|
2024-04-03 09:45:54 +00:00
|
|
|
const BASICALLY_ZERO: f32 = 0.0001;
|
|
|
|
volume < BASICALLY_ZERO
|
2024-04-02 10:59:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
fn time_remaining(decoder: Decoder<impl Read + Seek>) -> Duration {
|
|
|
|
let rate = decoder.sample_rate() as f64;
|
|
|
|
let n_channels = decoder.channels() as f64;
|
|
|
|
let n_samples = decoder.into_iter().count() as f64;
|
|
|
|
Duration::from_secs_f64(n_samples / rate / n_channels)
|
2024-04-02 10:59:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
fn get_music(format: &str) -> Decoder<impl Read + Seek> {
|
|
|
|
let asset = Path::new("assets/music").with_extension(format);
|
|
|
|
let file = std::fs::File::open(asset).unwrap();
|
|
|
|
let decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
|
|
|
|
decoder
|
|
|
|
}
|
2024-04-02 10:08:51 +00:00
|
|
|
|
2024-04-04 14:26:42 +00:00
|
|
|
fn get_rl(format: &str) -> Decoder<impl Read + Seek> {
|
|
|
|
let asset = Path::new("assets/RL").with_extension(format);
|
|
|
|
println!("opening: {}", asset.display());
|
|
|
|
let file = std::fs::File::open(asset).unwrap();
|
|
|
|
let decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
|
|
|
|
decoder
|
2024-04-02 10:08:51 +00:00
|
|
|
}
|