Is there a faster way to read and process an aiff files sound data

⚓ Rust    📅 2026-06-18    👤 surdeus    👁️ 3      

surdeus

I have a program that reads an aiff format music file. It works but I wonder is there a faster way to read and process the sound data. An aiff file is broken up into chunks and the SSND chunk holds the music data. I have a loop that steps through the file and uses a match statement to process each chunk. The code associated with the match arm for SSND is below. The code reads the lower half of the data and the upper half of the data into separate buffers then passes each one into a separate thread. Each thread processes the data then returns two vectors, one for the left channel and one for the right channel (the music is 2 channel stereo). The associated vectors are appended and I end up with 2 vectors, one for each channel. Any thoughts on a better/faster approach will be appreciated.

println!("----- SSND Chunk -----");
println!("ckSize (chunk size) = {:?}", ck_size);
// ckSize (chunk size) =  64,585,928 bytes

let mut ck_data = [0u8; 8];
file.read_exact(&mut ck_data)?;
let offset = u32::from_be_bytes(ck_data[0..4].try_into().unwrap_or_default());
let _block_size = u32::from_be_bytes(ck_data[4..8].try_into().unwrap_or_default());

if offset > 0 {
    file.seek(SeekFrom::Current(offset as i64))?;
}

let half_sound_bytes = (ck_size - 8 - offset) / 2;

println!("half sound bytes = {:?}", half_sound_bytes);
// half sound bytes =     32,292,960 bytes = ckSize - 8

let mut raw_bytes1 = vec![0u8; half_sound_bytes as usize];
file.read_exact(&mut raw_bytes1)?;

let mut raw_bytes2 = vec![0u8; half_sound_bytes as usize];
file.read_exact(&mut raw_bytes2)?;

let channel_data1 = Arc::new(raw_bytes1);
let cloned_channel_data1 = Arc::clone(&channel_data1);

let channel_data2 = Arc::new(raw_bytes2);
let cloned_channel_data2 = Arc::clone(&channel_data2);

let channel_handle1 = thread::spawn( move || {

    let mut left_chnl = Vec::new();
    let mut right_chnl = Vec::new();

    for chunk in cloned_channel_data1.chunks_exact(4) {

    let left_sample = i16::from_be_bytes(chunk[0..2].try_into().unwrap_or_default());
    let right_sample = i16::from_be_bytes(chunk[2..4].try_into().unwrap_or_default());

    left_chnl.push(left_sample);
    right_chnl.push(right_sample);
    
    }
    (left_chnl, right_chnl)
});

let channel_handle2 = thread::spawn( move || {

    let mut left_chnl = Vec::new();
    let mut right_chnl = Vec::new();

    for chunk in cloned_channel_data2.chunks_exact(4) {

    let left_sample = i16::from_be_bytes(chunk[0..2].try_into().unwrap_or_default());
    let right_sample = i16::from_be_bytes(chunk[2..4].try_into().unwrap_or_default());

    left_chnl.push(left_sample);
    right_chnl.push(right_sample);
    
    }
    (left_chnl, right_chnl)
});

let channels_lower = channel_handle1.join().unwrap();
let channels_upper = channel_handle2.join().unwrap();

let mut left1 = channels_lower.0;
let mut right1 = channels_lower.1;
let mut left2 = channels_upper.0;
let mut right2 = channels_upper.1;

left1.append(&mut left2);
right1.append(&mut right2);

println!("left channel length = {:?}", left1.len());
println!("right channel length = {:?}", right1.len());
// left channel length =  16,146,480 bytes
// right channel length = 16,146,480 bytes

1 post - 1 participant

Read full topic

🏷️ Rust_feed