Skip to main content

horizon_lattice_multimedia/
lib.rs

1//! Multimedia module for Horizon Lattice.
2//!
3//! This crate provides multimedia capabilities for Horizon Lattice applications:
4
5#![warn(missing_docs)]
6//!
7//! - **Audio Playback**: Load and play audio files with signal-based state notifications
8//! - **Sound Effects**: Low-latency, pre-loaded sounds with concurrent playback
9//! - **High-Precision Timers** (feature `high-precision-timers`): Sub-millisecond accurate timing
10//!
11//! # Audio Playback
12//!
13//! The audio player provides a high-level API for playing audio files:
14//!
15//! ```ignore
16//! use horizon_lattice_multimedia::AudioPlayer;
17//!
18//! // Create a player
19//! let player = AudioPlayer::new()?;
20//!
21//! // Connect to state changes
22//! player.on_state_changed(|state| {
23//!     println!("State: {:?}", state);
24//! });
25//!
26//! // Load and play
27//! player.load_file("music.mp3")?;
28//! player.play();
29//!
30//! // Control playback
31//! player.set_volume(0.8);
32//! player.pause();
33//! player.set_looping(true);
34//! player.play();
35//! ```
36//!
37//! ## Supported Formats
38//!
39//! - WAV
40//! - MP3
41//! - OGG Vorbis
42//! - FLAC
43//! - AAC/M4A (via Symphonia backend)
44//!
45//! # Sound Effects
46//!
47//! For low-latency, fire-and-forget sounds (like game audio), use `SoundPool`:
48//!
49//! ```ignore
50//! use horizon_lattice_multimedia::SoundPool;
51//!
52//! // Create a pool and pre-load sounds
53//! let mut pool = SoundPool::new()?;
54//! pool.load("explosion", "assets/explosion.wav")?;
55//! pool.load("laser", "assets/laser.ogg")?;
56//!
57//! // Play sounds (can overlap - up to 8 instances by default)
58//! pool.play("explosion")?;
59//! pool.play("laser")?;
60//! pool.play("laser")?; // Multiple lasers at once
61//!
62//! // Control volume
63//! pool.set_volume(0.8);
64//! pool.set_sound_volume("explosion", 1.2);
65//!
66//! // Limit concurrent instances
67//! pool.set_max_instances("laser", 4);
68//! ```
69//!
70//! # High-Precision Timers
71//!
72//! When the `high-precision-timers` feature is enabled, this crate provides
73//! sub-millisecond accurate timing using native sleep combined with spin-waiting:
74//!
75//! ```ignore
76//! use horizon_lattice_multimedia::timers::{HighPrecisionTimer, precise_sleep};
77//! use std::time::Duration;
78//!
79//! // One-shot precise sleep
80//! precise_sleep(Duration::from_micros(500));
81//!
82//! // Interval timer for game loops, A/V sync, etc.
83//! let timer = HighPrecisionTimer::new(Duration::from_millis(16))?; // ~60 FPS
84//! timer.on_tick(|event| {
85//!     println!("Tick {}, drift: {:?}", event.tick_count, event.drift);
86//! });
87//! timer.start()?;
88//! ```
89
90pub mod audio;
91mod error;
92pub mod sound_effects;
93
94#[cfg(feature = "high-precision-timers")]
95pub mod timers;
96
97pub use error::{MultimediaError, Result};
98
99// Re-export commonly used types at the crate root
100pub use audio::{AudioMetadata, AudioPlayer, PlaybackState};
101pub use sound_effects::SoundPool;
102
103// Re-export timer types when feature is enabled
104#[cfg(feature = "high-precision-timers")]
105pub use timers::{
106    HighPrecisionTimer, PreciseSleeper, SpinStrategyConfig, TimerConfig, TimerEvent, precise_sleep,
107    precise_sleep_ns, precise_sleep_s,
108};