amberjs 1.16.1

JavaScript/TypeScript runtime built with Rust and V8
Documentation
/// 空间音频系统
use std::collections::HashMap;
use std::collections::{BTreeMap};
/// HRTF 配置文件
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HRTFProfile {
    /// 通用配置
    Generic,
    /// 小头型
    SmallHead,
    /// 中头型
    MediumHead,
    /// 大头型
    LargeHead,
    /// 自定义
    Custom,
}
impl Default for HRTFProfile {
    fn default() -> Self {
        Self::Generic
    }
}
/// 音频配置
#[derive(Debug, Clone)]
pub struct AudioConfig {
    /// 采样率 (Hz)
    pub sample_rate: u32,
    /// 声道数
    pub channels: u32,
    /// HRTF 配置
    pub hrtf_profile: HRTFProfile,
    /// 启用混响
    pub enable_reverb: bool,
    /// 启用遮挡
    pub enable_occlusion: bool,
}
impl Default for AudioConfig {
    fn default() -> Self {
        Self {
            sample_rate: 48000,
            channels: 2,
            hrtf_profile: HRTFProfile::Generic,
            enable_reverb: true,
            enable_occlusion: false,
        }
    }
}
/// 音频源
#[derive(Debug, Clone)]
pub struct AudioSource {
    /// ID
    pub id: String,
    /// 位置
    pub position: [f32; 3],
    /// 音量 0.0 - 1.0
    pub volume: f32,
    /// 是否循环
    pub loop_audio: bool,
}
/// 空间音频系统
pub struct SpatialAudioSystem {
    /// 配置
    config: AudioConfig,
    /// 音频源
    sources: HashMap<String, AudioSource>,
    /// 监听者位置
    listener_position: [f32; 3],
    /// 监听者旋转
    listener_rotation: [f32; 4],
    /// 主音量
    master_volume: f32,
}
impl SpatialAudioSystem {
    /// 创建空间音频系统
    pub fn new(config: AudioConfig) -> Result<Self, AudioError> {
        Ok(Self {
            config,
            sources: HashMap::new(),
            listener_position: [0.0, 0.0, 0.0],
            listener_rotation: [0.0, 0.0, 0.0, 1.0],
            master_volume: 1.0,
        })
    }
    /// 获取配置
    pub fn config(&self) -> &AudioConfig {
        &self.config
    }
    /// 添加音频源
    pub fn add_source(&mut self, source: AudioSource) -> Result<(), AudioError> {
        self.sources.insert(source.id.clone(), source);
        Ok(())
    }
    /// 移除音频源
    pub fn remove_source(&mut self, id: &str) -> Option<AudioSource> {
        self.sources.remove(id)
    }
    /// 获取音频源
    pub fn get_source(&self, id: &str) -> Option<&AudioSource> {
        self.sources.get(id)
    }
    /// 更新监听者位置
    pub fn update_listener(&mut self, position: [f32; 3], rotation: [f32; 4]) {
        self.listener_position = position;
        self.listener_rotation = rotation;
    }
    /// 设置主音量
    pub fn set_master_volume(&mut self, volume: f32) {
        self.master_volume = volume.clamp(0.0, 1.0);
    }
    /// 获取主音量
    pub fn master_volume(&self) -> f32 {
        self.master_volume
    }
    /// 处理音频帧
    pub fn process_frame(&self, buffer: &mut [f32]) -> Result<(), AudioError> {
        // 空间音频处理逻辑
        Ok(())
    }
    /// 获取音频源数量
    pub fn source_count(&self) -> usize {
        self.sources.len()
    }
}
/// 音频错误
#[derive(Debug, Clone)]
pub enum AudioError {
    /// 初始化失败
    InitializationFailed(String),
    /// 音频源错误
    SourceError(String),
    /// 播放失败
    PlaybackFailed(String),
}
impl std::fmt::Display for AudioError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InitializationFailed(msg) => write!(f, "音频初始化失败: {}", msg),
            Self::SourceError(msg) => write!(f, "音频源错误: {}", msg),
            Self::PlaybackFailed(msg) => write!(f, "播放失败: {}", msg),
        }
    }
}
impl std::error::Error for AudioError {}