Implement automatic format detection for ALSA Direct
PROBLEM: Hardcoding S32_LE only worked for Cambridge Audio DAC, not generic. SOLUTION: Auto-detect best format at runtime: - Try formats in priority order: FloatLE > S32LE > S24LE > S16LE - Store detected format in AlsaDirectStream struct - write() auto-converts from i16 based on detected format: * FloatLE: i16 -> f32 (normalize to -1.0..1.0) * S32LE: i16 -> i32 (bit-perfect: shift left 16 bits) * S16LE: i16 -> i16 (no conversion) Now works with any DAC regardless of supported formats.
This commit is contained in:
1 parent
dc9c7d0424
commit
c7a3737fb5
2 files changed
+108
-37
No files matched your search
@@ -90,7 +90,6 @@ impl PlaybackEngine {
|
||||
let chunk_samples = CHUNK_SIZE * channels as usize;
|
||||
|
||||
let mut buffer_i16 = Vec::with_capacity(chunk_samples);
|
||||
let mut buffer_i32 = Vec::with_capacity(chunk_samples);
|
||||
|
||||
let mut total_frames: u64 = 0;
|
||||
let mut source_iter = source.into_iter();
|
||||
@@ -117,20 +116,14 @@ impl PlaybackEngine {
|
||||
break;
|
||||
}
|
||||
|
||||
// Convert i16 to i32 (bit-perfect: shift left 16 bits to use full 32-bit range)
|
||||
buffer_i32.clear();
|
||||
for &sample in &buffer_i16 {
|
||||
buffer_i32.push((sample as i32) << 16);
|
||||
}
|
||||
|
||||
// Write to ALSA
|
||||
if let Err(e) = stream_clone.write(&buffer_i32) {
|
||||
// Write to ALSA (auto-converts based on detected format)
|
||||
if let Err(e) = stream_clone.write(&buffer_i16) {
|
||||
log::error!("[ALSA Direct Engine] Write failed: {}", e);
|
||||
break;
|
||||
}
|
||||
|
||||
// Update position
|
||||
let frames_written = buffer_i32.len() / channels as usize;
|
||||
let frames_written = buffer_i16.len() / channels as usize;
|
||||
total_frames += frames_written as u64;
|
||||
position_clone.store(total_frames, Ordering::SeqCst);
|
||||
duration_clone.store(total_frames, Ordering::SeqCst);
|
||||
|
||||
Reference in new issue
Block a user