use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::format::SampleFormat;
use super::unit::SignalUnit;
use crate::buffer::MemoryOrder;
use crate::time::{RationalTime, SampleRate};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChannelInfo {
pub name: String,
pub gain: f32,
pub offset: f32,
pub unit: SignalUnit,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RecordingInfo {
pub name: String,
pub channels: Vec<ChannelInfo>,
pub samples: u64,
pub sample_rate: SampleRate,
pub format: SampleFormat,
pub order: MemoryOrder,
pub start_time: RationalTime,
pub metadata: BTreeMap<String, String>,
}
impl RecordingInfo {
pub fn new(
name: impl Into<String>,
channel_count: usize,
samples: u64,
sample_rate: SampleRate,
format: SampleFormat,
order: MemoryOrder,
) -> Self {
let channels = (0..channel_count)
.map(|i| ChannelInfo { name: format!("ch{i}"), gain: 1.0, offset: 0.0, unit: SignalUnit::Dimensionless })
.collect();
Self {
name: name.into(),
channels,
samples,
sample_rate,
format,
order,
start_time: RationalTime::ZERO,
metadata: BTreeMap::new(),
}
}
pub fn with_gain(mut self, gain: f32, unit: SignalUnit) -> Self {
for c in &mut self.channels {
c.gain = gain;
c.unit = unit.clone();
}
self
}
pub fn channel_count(&self) -> usize {
self.channels.len()
}
pub fn sample_rate_hz(&self) -> f64 {
self.sample_rate.rate_hz()
}
pub fn duration_sec(&self) -> f64 {
self.samples as f64 / self.sample_rate.rate_hz()
}
pub fn data_bytes(&self) -> u64 {
self.samples * self.channels.len() as u64 * self.format.bytes() as u64
}
}