Merge pull request #1165 from JasonLG1979/norm-data

Read Normalisation Data in one go
This commit is contained in:
Roderick van Domburg 2023-06-01 20:49:25 +02:00 committed by GitHub
commit cb8a9bc931
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,7 +15,6 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use byteorder::{LittleEndian, ReadBytesExt};
use futures_util::{ use futures_util::{
future, future::FusedFuture, stream::futures_unordered::FuturesUnordered, StreamExt, future, future::FusedFuture, stream::futures_unordered::FuturesUnordered, StreamExt,
TryFutureExt, TryFutureExt,
@ -306,29 +305,35 @@ impl Default for NormalisationData {
impl NormalisationData { impl NormalisationData {
fn parse_from_ogg<T: Read + Seek>(mut file: T) -> io::Result<NormalisationData> { fn parse_from_ogg<T: Read + Seek>(mut file: T) -> io::Result<NormalisationData> {
const SPOTIFY_NORMALIZATION_HEADER_START_OFFSET: u64 = 144; const SPOTIFY_NORMALIZATION_HEADER_START_OFFSET: u64 = 144;
const NORMALISATION_DATA_SIZE: usize = 16;
let newpos = file.seek(SeekFrom::Start(SPOTIFY_NORMALIZATION_HEADER_START_OFFSET))?; let newpos = file.seek(SeekFrom::Start(SPOTIFY_NORMALIZATION_HEADER_START_OFFSET))?;
if newpos != SPOTIFY_NORMALIZATION_HEADER_START_OFFSET { if newpos != SPOTIFY_NORMALIZATION_HEADER_START_OFFSET {
error!( error!(
"NormalisationData::parse_from_file seeking to {} but position is now {}", "NormalisationData::parse_from_file seeking to {} but position is now {}",
SPOTIFY_NORMALIZATION_HEADER_START_OFFSET, newpos SPOTIFY_NORMALIZATION_HEADER_START_OFFSET, newpos
); );
error!("Falling back to default (non-track and non-album) normalisation data."); error!("Falling back to default (non-track and non-album) normalisation data.");
return Ok(NormalisationData::default()); return Ok(NormalisationData::default());
} }
let track_gain_db = file.read_f32::<LittleEndian>()? as f64; let mut buf = [0u8; NORMALISATION_DATA_SIZE];
let track_peak = file.read_f32::<LittleEndian>()? as f64;
let album_gain_db = file.read_f32::<LittleEndian>()? as f64;
let album_peak = file.read_f32::<LittleEndian>()? as f64;
let r = NormalisationData { file.read_exact(&mut buf)?;
let track_gain_db = f32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) as f64;
let track_peak = f32::from_le_bytes([buf[4], buf[5], buf[6], buf[7]]) as f64;
let album_gain_db = f32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]) as f64;
let album_peak = f32::from_le_bytes([buf[12], buf[13], buf[14], buf[15]]) as f64;
Ok(Self {
track_gain_db, track_gain_db,
track_peak, track_peak,
album_gain_db, album_gain_db,
album_peak, album_peak,
}; })
Ok(r)
} }
fn get_factor(config: &PlayerConfig, data: NormalisationData) -> f64 { fn get_factor(config: &PlayerConfig, data: NormalisationData) -> f64 {