encode_from_audio

Function encode_from_audio 

Source
pub fn encode_from_audio(
    audio_bytes: &[u8],
    options: EncodeOptions,
) -> Result<Vec<u8>>
Expand description

Encode audio file bytes to flo™ format

§Arguments

  • audio_bytes - Raw bytes of an audio file (MP3, WAV, FLAC, OGG, etc.)
  • options - Encoding options

§Returns

Raw bytes of the flo™ file

Examples found in repository?
examples/convert_audio.rs (line 34)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let args: Vec<String> = env::args().collect();
11
12    if args.len() < 3 {
13        eprintln!("Usage: {} <input-audio> <output-flo>", args[0]);
14        std::process::exit(1);
15    }
16
17    let input_path = &args[1];
18    let output_path = &args[2];
19
20    println!("Reading {}...", input_path);
21    let audio_bytes = fs::read(input_path)?;
22
23    // Get audio info
24    let info = reflo::get_audio_info(&audio_bytes)?;
25    println!("  Sample rate: {} Hz", info.sample_rate);
26    println!("  Channels: {}", info.channels);
27    println!("  Duration: {:.2}s", info.duration_secs);
28
29    // Encode with lossy compression (high quality)
30    println!("\nEncoding to flo (lossy, high quality)...");
31    let options = EncodeOptions::lossy(0.6) // 0.0 = low, 1.0 = transparent
32        .with_level(5);
33
34    let flo_bytes = encode_from_audio(&audio_bytes, options)?;
35
36    // Show compression stats
37    let original_size = audio_bytes.len();
38    let compressed_size = flo_bytes.len();
39    let ratio = original_size as f32 / compressed_size as f32;
40
41    println!("  Original: {} bytes", original_size);
42    println!("  Compressed: {} bytes", compressed_size);
43    println!("  Ratio: {:.1}x", ratio);
44
45    // Write to file
46    fs::write(output_path, &flo_bytes)?;
47    println!("\nWrote flo file to {}", output_path);
48
49    // Get flo file info
50    let flo_info =
51        get_flo_info(&flo_bytes).map_err(|e| anyhow::anyhow!("Failed to get flo info: {:?}", e))?;
52    println!("\nflo File Info:");
53    println!("  Sample rate: {} Hz", flo_info.sample_rate);
54    println!("  Channels: {}", flo_info.channels);
55    println!("  Duration: {:.2}s", flo_info.duration_secs);
56    println!("  Lossy: {}", if flo_info.is_lossy { "yes" } else { "no" });
57    println!(
58        "  CRC valid: {}",
59        if flo_info.crc_valid { "yes" } else { "no" }
60    );
61
62    // Decode back to WAV for verification
63    println!("\nDecoding back to WAV for verification...");
64    let wav_bytes = decode_to_wav(&flo_bytes)?;
65    let wav_path = output_path.replace(".flo", "_decoded.wav");
66    fs::write(&wav_path, wav_bytes)?;
67    println!("Wrote decoded WAV to {}", wav_path);
68
69    Ok(())
70}