mod xapcm;

use super::Arguments;
use super::raw_audio::CDAudioSamples;
use cdtypes::types::sector::{Mode2Form2, SECTOR_SIZE};
use std::io::Write;
use tool_helper::Error;

pub const HIGH_FREQUENCY:u32 = 37_800;
pub const  LOW_FREQUENCY:u32 = 18_900;

pub fn encode(input: CDAudioSamples, output: &mut dyn Write, arguments: &Arguments) -> Result<(), Error> {
    let mut encoder = xapcm::Encoder::new(&input, arguments.frequency, arguments.sample_depth);

    while let Some(xa_sector) = encoder.encode_next_xa_sector()? {
        let xa_sector = unsafe {std::mem::transmute::<Mode2Form2, [u8; SECTOR_SIZE]>(xa_sector)};
        output.write(&xa_sector)?;
    }
    Ok(())
}