Fix sample rate decimal precision for bit-perfect playback (CRITICAL)

Problem:
- sample_rate stored as INTEGER in DB and u32 in Rust
- Fractional rates (44.1, 88.2, 176.4 kHz) stored as 44100, 88200, 176400 Hz
- Integer division (44100 / 1000 = 44) lost decimals
- DAC badge showed "resampling 44 → 44.1 kHz" false positive
- Breaks bit-perfect guarantee - user requirement: "no es negociable"

Root Cause:
- DB schema: sample_rate INTEGER (44100 Hz)
- Frontend: Math.round(44100 / 1000) = 44 kHz (lost .1)
- Badge: Compared 44 kHz vs 44.1 kHz → false resampling warning

Solution:
1. Changed LocalTrack.sample_rate from u32 to f64 (models.rs)
2. Changed AudioProperties.sample_rate from u32 to f64 (models.rs)
3. Changed DB schema from INTEGER to REAL (database.rs)
4. Added migration to convert existing data (database.rs)
5. Updated metadata extraction to cast to f64 (metadata.rs)
6. Fixed frontend to use float division without rounding (+page.svelte)

Migration:
- Checks if sample_rate is INTEGER
- Creates temp table with REAL, copies data, drops old, renames
- Preserves all data and indexes
- Runs automatically on app start

Result:
- 44100 Hz stored as 44100.0
- Division: 44100.0 / 1000.0 = 44.1 kHz (exact)
- Badge: 44.1 kHz = 44.1 kHz → no resampling (correct)
- Bit-perfect guarantee restored

Files modified:
- src-tauri/src/library/models.rs - Changed sample_rate to f64 in structs
- src-tauri/src/library/metadata.rs - Cast sample_rate to f64
- src-tauri/src/library/database.rs - Changed schema to REAL + migration
- src/routes/+page.svelte - Fixed division to preserve decimals
This commit is contained in:
vicrodh committed 2026-01-19 19:49:34 -06:00
1 parent 61fa05da93
commit 1e7475e308
4 files changed
+84 -8

No files matched your search

+77 -1
View File
@@ -54,7 +54,7 @@ impl LibraryDatabase {
duration_secs INTEGER NOT NULL,
format TEXT NOT NULL,
bit_depth INTEGER,
sample_rate INTEGER NOT NULL,
sample_rate REAL NOT NULL,
channels INTEGER NOT NULL,
file_size_bytes INTEGER NOT NULL,
cue_file_path TEXT,
@@ -345,6 +345,82 @@ impl LibraryDatabase {
.map_err(|e| LibraryError::Database(format!("Migration failed: {}", e)))?;
}
// Migration: Change sample_rate from INTEGER to REAL for decimal precision (44.1kHz, 88.2kHz, etc.)
// Check if sample_rate is currently INTEGER
let sample_rate_type: String = self.conn
.query_row(
"SELECT type FROM pragma_table_info('local_tracks') WHERE name = 'sample_rate'",
[],
|row| row.get(0),
)
.unwrap_or_else(|_| "REAL".to_string());
if sample_rate_type == "INTEGER" {
log::info!("Running migration: changing sample_rate from INTEGER to REAL for decimal precision");
// SQLite doesn't support ALTER COLUMN type change, need to recreate table
self.conn.execute_batch(
r#"
-- Create new table with REAL sample_rate
CREATE TABLE local_tracks_new (
id INTEGER PRIMARY KEY,
file_path TEXT NOT NULL,
title TEXT NOT NULL,
artist TEXT NOT NULL,
album TEXT NOT NULL,
album_artist TEXT,
track_number INTEGER,
disc_number INTEGER,
year INTEGER,
genre TEXT,
catalog_number TEXT,
duration_secs INTEGER NOT NULL,
format TEXT NOT NULL,
bit_depth INTEGER,
sample_rate REAL NOT NULL,
channels INTEGER NOT NULL,
file_size_bytes INTEGER NOT NULL,
cue_file_path TEXT,
cue_start_secs REAL,
cue_end_secs REAL,
artwork_path TEXT,
last_modified INTEGER NOT NULL,
indexed_at INTEGER NOT NULL,
album_group_key TEXT,
album_group_title TEXT,
source TEXT DEFAULT 'user',
qobuz_track_id INTEGER,
UNIQUE(file_path, cue_start_secs)
);
-- Copy all data (sample_rate will be auto-converted from INTEGER to REAL)
INSERT INTO local_tracks_new
SELECT * FROM local_tracks;
-- Drop old table
DROP TABLE local_tracks;
-- Rename new table
ALTER TABLE local_tracks_new RENAME TO local_tracks;
-- Recreate indexes
CREATE INDEX IF NOT EXISTS idx_tracks_artist ON local_tracks(artist);
CREATE INDEX IF NOT EXISTS idx_tracks_album ON local_tracks(album);
CREATE INDEX IF NOT EXISTS idx_tracks_album_artist ON local_tracks(album_artist);
CREATE INDEX IF NOT EXISTS idx_tracks_file_path ON local_tracks(file_path);
CREATE INDEX IF NOT EXISTS idx_tracks_title ON local_tracks(title);
CREATE INDEX IF NOT EXISTS idx_tracks_source ON local_tracks(source);
CREATE INDEX IF NOT EXISTS idx_tracks_qobuz_id ON local_tracks(qobuz_track_id);
CREATE INDEX IF NOT EXISTS idx_tracks_album_group ON local_tracks(album_group_key);
CREATE UNIQUE INDEX IF NOT EXISTS idx_tracks_file_nocue
ON local_tracks(file_path)
WHERE cue_file_path IS NULL;
"#
).map_err(|e| LibraryError::Database(format!("sample_rate migration failed: {}", e)))?;
log::info!("Migration completed: sample_rate is now REAL");
}
Ok(())
}
+2 -2
View File
@@ -268,7 +268,7 @@ impl MetadataExtractor {
// Get audio properties
let properties = tagged_file.properties();
let duration_secs = properties.duration().as_secs();
let sample_rate = properties.sample_rate().unwrap_or(44100);
let sample_rate = properties.sample_rate().unwrap_or(44100) as f64; // Convert to f64 for decimal precision
let bit_depth = properties.bit_depth().map(|b| b as u32);
let channels = properties.channels().unwrap_or(2) as u8;
@@ -398,7 +398,7 @@ impl MetadataExtractor {
Ok(AudioProperties {
duration_secs: properties.duration().as_secs(),
bit_depth: properties.bit_depth().map(|b| b as u32),
sample_rate: properties.sample_rate().unwrap_or(44100),
sample_rate: properties.sample_rate().unwrap_or(44100) as f64, // Convert to f64
channels: properties.channels().unwrap_or(2) as u8,
})
}
+3 -3
View File
@@ -57,7 +57,7 @@ pub struct LocalTrack {
pub duration_secs: u64,
pub format: AudioFormat,
pub bit_depth: Option<u32>,
pub sample_rate: u32,
pub sample_rate: f64, // Changed from u32 to f64 to support fractional rates (44.1kHz = 44100Hz)
pub channels: u8,
pub file_size_bytes: u64,
@@ -93,7 +93,7 @@ impl Default for LocalTrack {
duration_secs: 0,
format: AudioFormat::Unknown,
bit_depth: None,
sample_rate: 44100,
sample_rate: 44100.0, // Now f64
channels: 2,
file_size_bytes: 0,
cue_file_path: None,
@@ -184,7 +184,7 @@ pub struct ScanError {
pub struct AudioProperties {
pub duration_secs: u64,
pub bit_depth: Option<u32>,
pub sample_rate: u32,
pub sample_rate: f64, // Changed from u32 to f64 for decimal precision
pub channels: u8,
}