Complete la crate pmoaudio
This commit is contained in:
@@ -47,6 +47,12 @@ pub struct AudioChunk {
|
||||
///
|
||||
/// Valeurs typiques: 44100, 48000, 96000, 192000
|
||||
pub sample_rate: u32,
|
||||
|
||||
/// Gain multiplicatif appliqué au flux audio
|
||||
///
|
||||
/// Valeur par défaut: 1.0 (aucun changement)
|
||||
/// Valeurs typiques: 0.0 (silence) à 1.0 (volume max)
|
||||
pub gain: f32,
|
||||
}
|
||||
|
||||
impl AudioChunk {
|
||||
@@ -79,6 +85,18 @@ impl AudioChunk {
|
||||
left: Arc::new(left),
|
||||
right: Arc::new(right),
|
||||
sample_rate,
|
||||
gain: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un nouveau chunk audio avec un gain spécifique
|
||||
pub fn with_gain(order: u64, left: Vec<f32>, right: Vec<f32>, sample_rate: u32, gain: f32) -> Self {
|
||||
Self {
|
||||
order,
|
||||
left: Arc::new(left),
|
||||
right: Arc::new(right),
|
||||
sample_rate,
|
||||
gain,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +114,24 @@ impl AudioChunk {
|
||||
left,
|
||||
right,
|
||||
sample_rate,
|
||||
gain: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un chunk à partir de données déjà wrappées dans Arc avec gain
|
||||
pub fn from_arc_with_gain(
|
||||
order: u64,
|
||||
left: Arc<Vec<f32>>,
|
||||
right: Arc<Vec<f32>>,
|
||||
sample_rate: u32,
|
||||
gain: f32,
|
||||
) -> Self {
|
||||
Self {
|
||||
order,
|
||||
left,
|
||||
right,
|
||||
sample_rate,
|
||||
gain,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,6 +176,48 @@ impl AudioChunk {
|
||||
pub fn clone_data(&self) -> (Vec<f32>, Vec<f32>) {
|
||||
((*self.left).clone(), (*self.right).clone())
|
||||
}
|
||||
|
||||
/// Applique le gain et retourne un nouveau chunk avec les données modifiées
|
||||
///
|
||||
/// Cette méthode crée un nouveau chunk avec les samples multipliés par le gain.
|
||||
/// Utile pour les nodes qui doivent matérialiser le gain avant la sortie.
|
||||
///
|
||||
/// # Exemples
|
||||
///
|
||||
/// ```
|
||||
/// use pmoaudio::AudioChunk;
|
||||
///
|
||||
/// let chunk = AudioChunk::with_gain(0, vec![1.0, 2.0], vec![3.0, 4.0], 48000, 0.5);
|
||||
/// let applied = chunk.apply_gain();
|
||||
///
|
||||
/// assert_eq!(applied.left[0], 0.5);
|
||||
/// assert_eq!(applied.left[1], 1.0);
|
||||
/// assert_eq!(applied.gain, 1.0); // Gain réinitialisé après application
|
||||
/// ```
|
||||
pub fn apply_gain(&self) -> Self {
|
||||
if (self.gain - 1.0).abs() < f32::EPSILON {
|
||||
// Pas de gain à appliquer, retourner un clone
|
||||
return self.clone();
|
||||
}
|
||||
|
||||
let left: Vec<f32> = self.left.iter().map(|&s| s * self.gain).collect();
|
||||
let right: Vec<f32> = self.right.iter().map(|&s| s * self.gain).collect();
|
||||
|
||||
Self::new(self.order, left, right, self.sample_rate)
|
||||
}
|
||||
|
||||
/// Modifie le gain de ce chunk (retourne un nouveau chunk avec le même Arc mais gain différent)
|
||||
///
|
||||
/// Cette méthode est très peu coûteuse car elle ne clone que la structure, pas les données audio.
|
||||
pub fn with_modified_gain(&self, new_gain: f32) -> Self {
|
||||
Self {
|
||||
order: self.order,
|
||||
left: self.left.clone(),
|
||||
right: self.right.clone(),
|
||||
sample_rate: self.sample_rate,
|
||||
gain: self.gain * new_gain, // Multiplication des gains
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
233
pmoaudio/src/events.rs
Normal file
233
pmoaudio/src/events.rs
Normal file
@@ -0,0 +1,233 @@
|
||||
//! Système d'événements et d'abonnements générique pour les nodes
|
||||
//!
|
||||
//! Ce module fournit une infrastructure d'abonnement type-safe permettant
|
||||
//! à chaque node d'émettre et de recevoir différents types d'événements.
|
||||
|
||||
use crate::AudioChunk;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
/// Trait de base pour tous les événements de node
|
||||
///
|
||||
/// Chaque type d'événement doit implémenter ce trait pour pouvoir
|
||||
/// être utilisé dans le système d'abonnement.
|
||||
pub trait NodeEvent: Send + Sync + Clone + 'static {}
|
||||
|
||||
/// Événement : données audio disponibles
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AudioDataEvent {
|
||||
pub chunk: Arc<AudioChunk>,
|
||||
}
|
||||
|
||||
impl NodeEvent for AudioDataEvent {}
|
||||
|
||||
/// Événement : changement de volume
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct VolumeChangeEvent {
|
||||
pub volume: f32,
|
||||
pub source_node_id: String,
|
||||
}
|
||||
|
||||
impl NodeEvent for VolumeChangeEvent {}
|
||||
|
||||
/// Événement : mise à jour du nom de la source
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SourceNameUpdateEvent {
|
||||
pub source_name: String,
|
||||
pub device_name: Option<String>,
|
||||
}
|
||||
|
||||
impl NodeEvent for SourceNameUpdateEvent {}
|
||||
|
||||
/// Trait pour les listeners d'événements
|
||||
///
|
||||
/// Les nodes qui souhaitent recevoir des événements d'un type particulier
|
||||
/// doivent implémenter ce trait pour ce type.
|
||||
#[async_trait::async_trait]
|
||||
pub trait NodeListener<E: NodeEvent>: Send + Sync {
|
||||
/// Appelé lorsqu'un événement est reçu
|
||||
async fn on_event(&self, event: E);
|
||||
}
|
||||
|
||||
/// Gestionnaire d'abonnements pour un type d'événement spécifique
|
||||
///
|
||||
/// Permet d'enregistrer des listeners et de broadcaster des événements.
|
||||
#[derive(Clone)]
|
||||
pub struct EventPublisher<E: NodeEvent> {
|
||||
subscribers: Vec<mpsc::Sender<E>>,
|
||||
}
|
||||
|
||||
impl<E: NodeEvent> EventPublisher<E> {
|
||||
/// Crée un nouveau publisher vide
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
subscribers: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Ajoute un subscriber via un channel
|
||||
pub fn subscribe(&mut self, tx: mpsc::Sender<E>) {
|
||||
self.subscribers.push(tx);
|
||||
}
|
||||
|
||||
/// Publie un événement à tous les subscribers
|
||||
pub async fn publish(&self, event: E) {
|
||||
for tx in &self.subscribers {
|
||||
// Utiliser try_send pour éviter de bloquer si un subscriber est lent
|
||||
let _ = tx.try_send(event.clone());
|
||||
}
|
||||
}
|
||||
|
||||
/// Publie un événement de manière bloquante (attend que tous les subscribers reçoivent)
|
||||
pub async fn publish_blocking(&self, event: E) {
|
||||
for tx in &self.subscribers {
|
||||
let _ = tx.send(event.clone()).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Retourne le nombre de subscribers actifs
|
||||
pub fn subscriber_count(&self) -> usize {
|
||||
self.subscribers.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: NodeEvent> Default for EventPublisher<E> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper pour créer un listener basé sur une closure
|
||||
pub struct ClosureListener<E: NodeEvent, F>
|
||||
where
|
||||
F: Fn(E) + Send + Sync + 'static,
|
||||
{
|
||||
callback: Arc<F>,
|
||||
_phantom: std::marker::PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: NodeEvent, F> ClosureListener<E, F>
|
||||
where
|
||||
F: Fn(E) + Send + Sync + 'static,
|
||||
{
|
||||
pub fn new(callback: F) -> Self {
|
||||
Self {
|
||||
callback: Arc::new(callback),
|
||||
_phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<E: NodeEvent, F> NodeListener<E> for ClosureListener<E, F>
|
||||
where
|
||||
F: Fn(E) + Send + Sync + 'static,
|
||||
{
|
||||
async fn on_event(&self, event: E) {
|
||||
(self.callback)(event);
|
||||
}
|
||||
}
|
||||
|
||||
/// Receiver helper pour consommer des événements depuis un channel
|
||||
pub struct EventReceiver<E: NodeEvent> {
|
||||
rx: mpsc::Receiver<E>,
|
||||
}
|
||||
|
||||
impl<E: NodeEvent> EventReceiver<E> {
|
||||
/// Crée un nouveau receiver
|
||||
pub fn new(rx: mpsc::Receiver<E>) -> Self {
|
||||
Self { rx }
|
||||
}
|
||||
|
||||
/// Attend le prochain événement
|
||||
pub async fn recv(&mut self) -> Option<E> {
|
||||
self.rx.recv().await
|
||||
}
|
||||
|
||||
/// Tente de recevoir un événement sans bloquer
|
||||
pub fn try_recv(&mut self) -> Result<E, mpsc::error::TryRecvError> {
|
||||
self.rx.try_recv()
|
||||
}
|
||||
}
|
||||
|
||||
/// Macro pour faciliter la création de publishers multiples dans un node
|
||||
///
|
||||
/// # Exemple
|
||||
///
|
||||
/// ```ignore
|
||||
/// struct MyNode {
|
||||
/// audio_publisher: EventPublisher<AudioDataEvent>,
|
||||
/// volume_publisher: EventPublisher<VolumeChangeEvent>,
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! publishers {
|
||||
($($field:ident: $event_type:ty),* $(,)?) => {
|
||||
$(
|
||||
pub $field: $crate::events::EventPublisher<$event_type>,
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_event_publisher_basic() {
|
||||
let mut publisher = EventPublisher::<VolumeChangeEvent>::new();
|
||||
let (tx, mut rx) = mpsc::channel(10);
|
||||
|
||||
publisher.subscribe(tx);
|
||||
|
||||
let event = VolumeChangeEvent {
|
||||
volume: 0.5,
|
||||
source_node_id: "test".to_string(),
|
||||
};
|
||||
|
||||
publisher.publish(event.clone()).await;
|
||||
|
||||
let received = rx.recv().await.unwrap();
|
||||
assert_eq!(received.volume, 0.5);
|
||||
assert_eq!(received.source_node_id, "test");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_multiple_subscribers() {
|
||||
let mut publisher = EventPublisher::<VolumeChangeEvent>::new();
|
||||
let (tx1, mut rx1) = mpsc::channel(10);
|
||||
let (tx2, mut rx2) = mpsc::channel(10);
|
||||
|
||||
publisher.subscribe(tx1);
|
||||
publisher.subscribe(tx2);
|
||||
|
||||
let event = VolumeChangeEvent {
|
||||
volume: 0.7,
|
||||
source_node_id: "test".to_string(),
|
||||
};
|
||||
|
||||
publisher.publish(event.clone()).await;
|
||||
|
||||
let received1 = rx1.recv().await.unwrap();
|
||||
let received2 = rx2.recv().await.unwrap();
|
||||
|
||||
assert_eq!(received1.volume, 0.7);
|
||||
assert_eq!(received2.volume, 0.7);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_event_receiver() {
|
||||
let (tx, rx) = mpsc::channel(10);
|
||||
let mut receiver = EventReceiver::new(rx);
|
||||
|
||||
let event = VolumeChangeEvent {
|
||||
volume: 0.3,
|
||||
source_node_id: "test".to_string(),
|
||||
};
|
||||
|
||||
tx.send(event.clone()).await.unwrap();
|
||||
|
||||
let received = receiver.recv().await.unwrap();
|
||||
assert_eq!(received.volume, 0.3);
|
||||
}
|
||||
}
|
||||
@@ -77,14 +77,23 @@
|
||||
|
||||
mod audio_chunk;
|
||||
mod nodes;
|
||||
pub mod events;
|
||||
|
||||
pub use audio_chunk::AudioChunk;
|
||||
pub use events::{
|
||||
AudioDataEvent, EventPublisher, EventReceiver, NodeEvent, NodeListener,
|
||||
SourceNameUpdateEvent, VolumeChangeEvent,
|
||||
};
|
||||
pub use nodes::{
|
||||
buffer_node::BufferNode,
|
||||
chromecast_sink::{ChromecastConfig, ChromecastSink, ChromecastStats, StreamEncoding},
|
||||
decoder_node::DecoderNode,
|
||||
disk_sink::{AudioFileFormat, DiskSink, DiskSinkConfig, DiskSinkStats},
|
||||
dsp_node::DspNode,
|
||||
mpd_sink::{MpdAudioFormat, MpdConfig, MpdHandle, MpdSink, MpdStats},
|
||||
sink_node::{SinkNode, SinkStats},
|
||||
source_node::SourceNode,
|
||||
timer_node::{TimerHandle, TimerNode},
|
||||
volume_node::{HardwareVolumeNode, VolumeHandle, VolumeNode},
|
||||
AudioError, AudioNode, MultiSubscriberNode, SingleSubscriberNode,
|
||||
};
|
||||
|
||||
289
pmoaudio/src/nodes/chromecast_sink.rs
Normal file
289
pmoaudio/src/nodes/chromecast_sink.rs
Normal file
@@ -0,0 +1,289 @@
|
||||
//! ChromecastSink - Diffuse le flux audio vers un périphérique Chromecast
|
||||
//!
|
||||
//! Ce module fournit un sink qui envoie le flux audio à un Chromecast.
|
||||
//! Note: Cette implémentation est une version mock/skeleton. Une vraie implémentation
|
||||
//! nécessiterait une bibliothèque comme `rust-cast` ou similaire.
|
||||
|
||||
use crate::{nodes::AudioError, AudioChunk};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
/// Configuration pour le ChromecastSink
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChromecastConfig {
|
||||
/// Nom ou adresse IP du Chromecast
|
||||
pub device_address: String,
|
||||
|
||||
/// Nom amical du device
|
||||
pub device_name: String,
|
||||
|
||||
/// Port de communication (défaut: 8009)
|
||||
pub port: u16,
|
||||
|
||||
/// Taille du buffer de streaming
|
||||
pub buffer_size: usize,
|
||||
|
||||
/// Format d'encodage pour le streaming
|
||||
pub encoding: StreamEncoding,
|
||||
}
|
||||
|
||||
impl Default for ChromecastConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
device_address: "192.168.1.100".to_string(),
|
||||
device_name: "Living Room".to_string(),
|
||||
port: 8009,
|
||||
buffer_size: 50,
|
||||
encoding: StreamEncoding::Mp3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats d'encodage supportés pour le streaming
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum StreamEncoding {
|
||||
/// MP3 (compatible avec la plupart des Chromecasts)
|
||||
Mp3,
|
||||
/// AAC
|
||||
Aac,
|
||||
/// Opus
|
||||
Opus,
|
||||
/// PCM non compressé (haute qualité, bande passante élevée)
|
||||
Pcm,
|
||||
}
|
||||
|
||||
/// ChromecastSink - Diffuse vers un périphérique Chromecast
|
||||
///
|
||||
/// Ce sink encode le flux audio et le streame vers un Chromecast.
|
||||
/// La connexion est établie lors de l'initialisation et maintenue pendant toute la durée.
|
||||
///
|
||||
/// # Implémentation actuelle
|
||||
///
|
||||
/// Cette version est un mock qui simule l'envoi au Chromecast.
|
||||
/// Pour une vraie implémentation, il faudrait:
|
||||
/// - Utiliser une bibliothèque comme `rust-cast`
|
||||
/// - Établir une connexion TLS avec le device
|
||||
/// - Lancer une application de récepteur sur le Chromecast
|
||||
/// - Encoder l'audio dans le format approprié
|
||||
/// - Streamer via HTTP ou WebSocket
|
||||
///
|
||||
/// # Exemples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use pmoaudio::{ChromecastSink, ChromecastConfig};
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() {
|
||||
/// let config = ChromecastConfig {
|
||||
/// device_address: "192.168.1.100".to_string(),
|
||||
/// device_name: "Living Room".to_string(),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
///
|
||||
/// let (sink, sink_tx) = ChromecastSink::new("chromecast1".to_string(), config, 10);
|
||||
///
|
||||
/// tokio::spawn(async move {
|
||||
/// sink.run().await.unwrap()
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub struct ChromecastSink {
|
||||
/// Identifiant du sink
|
||||
node_id: String,
|
||||
|
||||
/// Channel pour recevoir les chunks audio
|
||||
rx: mpsc::Receiver<Arc<AudioChunk>>,
|
||||
|
||||
/// Configuration
|
||||
config: ChromecastConfig,
|
||||
|
||||
/// État de la connexion (mock)
|
||||
connected: bool,
|
||||
}
|
||||
|
||||
impl ChromecastSink {
|
||||
/// Crée un nouveau ChromecastSink
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `node_id` - Identifiant unique du sink
|
||||
/// * `config` - Configuration du Chromecast
|
||||
/// * `channel_size` - Taille du buffer du channel
|
||||
pub fn new(
|
||||
node_id: String,
|
||||
config: ChromecastConfig,
|
||||
channel_size: usize,
|
||||
) -> (Self, mpsc::Sender<Arc<AudioChunk>>) {
|
||||
let (tx, rx) = mpsc::channel(channel_size);
|
||||
|
||||
let sink = Self {
|
||||
node_id,
|
||||
rx,
|
||||
config,
|
||||
connected: false,
|
||||
};
|
||||
|
||||
(sink, tx)
|
||||
}
|
||||
|
||||
/// Établit la connexion avec le Chromecast (mock)
|
||||
async fn connect(&mut self) -> Result<(), AudioError> {
|
||||
println!(
|
||||
"[{}] Connecting to Chromecast '{}' at {}:{}...",
|
||||
self.node_id, self.config.device_name, self.config.device_address, self.config.port
|
||||
);
|
||||
|
||||
// Simuler une connexion
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
|
||||
|
||||
self.connected = true;
|
||||
|
||||
println!(
|
||||
"[{}] Connected to Chromecast '{}' successfully",
|
||||
self.node_id, self.config.device_name
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Envoie un chunk au Chromecast (mock)
|
||||
async fn send_chunk(&self, _chunk: &AudioChunk) -> Result<(), AudioError> {
|
||||
if !self.connected {
|
||||
return Err(AudioError::ProcessingError(
|
||||
"Not connected to Chromecast".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
// Dans une vraie implémentation:
|
||||
// 1. Appliquer le gain
|
||||
// 2. Encoder dans le format approprié (MP3, AAC, etc.)
|
||||
// 3. Envoyer via le protocole Chromecast
|
||||
|
||||
// Pour l'instant, simplement simuler un délai d'envoi
|
||||
tokio::time::sleep(tokio::time::Duration::from_micros(50)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Déconnecte proprement du Chromecast (mock)
|
||||
async fn disconnect(&mut self) -> Result<(), AudioError> {
|
||||
if self.connected {
|
||||
println!(
|
||||
"[{}] Disconnecting from Chromecast '{}'...",
|
||||
self.node_id, self.config.device_name
|
||||
);
|
||||
|
||||
// Simuler la déconnexion
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
|
||||
|
||||
self.connected = false;
|
||||
|
||||
println!("[{}] Disconnected successfully", self.node_id);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Démarre la boucle de traitement du ChromecastSink
|
||||
pub async fn run(mut self) -> Result<ChromecastStats, AudioError> {
|
||||
// Établir la connexion
|
||||
self.connect().await?;
|
||||
|
||||
let mut stats = ChromecastStats::new(
|
||||
self.node_id.clone(),
|
||||
self.config.device_name.clone(),
|
||||
);
|
||||
|
||||
// Boucle principale
|
||||
while let Some(chunk) = self.rx.recv().await {
|
||||
// Appliquer le gain si nécessaire
|
||||
let chunk_to_send = if (chunk.gain - 1.0).abs() > f32::EPSILON {
|
||||
chunk.apply_gain()
|
||||
} else {
|
||||
(*chunk).clone()
|
||||
};
|
||||
|
||||
// Envoyer au Chromecast
|
||||
self.send_chunk(&chunk_to_send).await?;
|
||||
|
||||
stats.record_chunk(&chunk_to_send);
|
||||
}
|
||||
|
||||
// Déconnexion propre
|
||||
self.disconnect().await?;
|
||||
|
||||
stats.finalize();
|
||||
Ok(stats)
|
||||
}
|
||||
}
|
||||
|
||||
/// Statistiques du ChromecastSink
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChromecastStats {
|
||||
pub node_id: String,
|
||||
pub device_name: String,
|
||||
pub chunks_sent: u64,
|
||||
pub total_samples: u64,
|
||||
pub total_duration_sec: f64,
|
||||
}
|
||||
|
||||
impl ChromecastStats {
|
||||
pub fn new(node_id: String, device_name: String) -> Self {
|
||||
Self {
|
||||
node_id,
|
||||
device_name,
|
||||
chunks_sent: 0,
|
||||
total_samples: 0,
|
||||
total_duration_sec: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_chunk(&mut self, chunk: &AudioChunk) {
|
||||
self.chunks_sent += 1;
|
||||
self.total_samples += chunk.len() as u64;
|
||||
self.total_duration_sec += chunk.len() as f64 / chunk.sample_rate as f64;
|
||||
}
|
||||
|
||||
pub fn finalize(&mut self) {
|
||||
// Calculs finaux si nécessaire
|
||||
}
|
||||
|
||||
pub fn display(&self) {
|
||||
println!("\n=== Chromecast Statistics: {} ===", self.node_id);
|
||||
println!("Device: {}", self.device_name);
|
||||
println!("Chunks sent: {}", self.chunks_sent);
|
||||
println!("Total samples: {}", self.total_samples);
|
||||
println!("Total duration: {:.3} sec", self.total_duration_sec);
|
||||
println!("==================================\n");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_chromecast_sink_basic() {
|
||||
let config = ChromecastConfig {
|
||||
device_address: "127.0.0.1".to_string(),
|
||||
device_name: "Test Device".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (sink, tx) = ChromecastSink::new("test".to_string(), config, 10);
|
||||
|
||||
let handle = tokio::spawn(async move { sink.run().await });
|
||||
|
||||
// Envoyer quelques chunks
|
||||
for i in 0..5 {
|
||||
let chunk = AudioChunk::new(i, vec![0.5; 1000], vec![0.5; 1000], 48000);
|
||||
tx.send(Arc::new(chunk)).await.unwrap();
|
||||
}
|
||||
|
||||
drop(tx);
|
||||
|
||||
let stats = handle.await.unwrap().unwrap();
|
||||
assert_eq!(stats.chunks_sent, 5);
|
||||
assert_eq!(stats.device_name, "Test Device");
|
||||
}
|
||||
}
|
||||
481
pmoaudio/src/nodes/disk_sink.rs
Normal file
481
pmoaudio/src/nodes/disk_sink.rs
Normal file
@@ -0,0 +1,481 @@
|
||||
//! DiskSink - Écrit le flux audio dans un fichier
|
||||
//!
|
||||
//! Ce module fournit un sink qui écrit les chunks audio sur disque,
|
||||
//! avec support de la dérivation automatique du nom de fichier depuis la source.
|
||||
|
||||
use crate::{
|
||||
events::SourceNameUpdateEvent,
|
||||
nodes::AudioError,
|
||||
AudioChunk,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::sync::{mpsc, RwLock};
|
||||
|
||||
/// Configuration pour le DiskSink
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DiskSinkConfig {
|
||||
/// Chemin racine où écrire les fichiers
|
||||
pub output_dir: PathBuf,
|
||||
|
||||
/// Nom de fichier explicite (optionnel)
|
||||
/// Si None, sera dérivé du nom de la source
|
||||
pub filename: Option<String>,
|
||||
|
||||
/// Format d'écriture
|
||||
pub format: AudioFileFormat,
|
||||
|
||||
/// Taille du buffer d'écriture (en chunks)
|
||||
pub buffer_size: usize,
|
||||
}
|
||||
|
||||
impl Default for DiskSinkConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
output_dir: PathBuf::from("."),
|
||||
filename: None,
|
||||
format: AudioFileFormat::Wav,
|
||||
buffer_size: 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats de fichiers audio supportés
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum AudioFileFormat {
|
||||
/// Format WAV (non compressé)
|
||||
Wav,
|
||||
/// Format FLAC (compressé sans perte)
|
||||
Flac,
|
||||
/// Format brut PCM
|
||||
Raw,
|
||||
}
|
||||
|
||||
impl AudioFileFormat {
|
||||
/// Retourne l'extension de fichier appropriée
|
||||
pub fn extension(&self) -> &str {
|
||||
match self {
|
||||
AudioFileFormat::Wav => "wav",
|
||||
AudioFileFormat::Flac => "flac",
|
||||
AudioFileFormat::Raw => "pcm",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// DiskSink - Écrit le flux audio dans un fichier sur disque
|
||||
///
|
||||
/// Ce sink consomme les chunks audio et les écrit dans un fichier.
|
||||
/// Le nom du fichier peut être dérivé automatiquement du nom de la source
|
||||
/// via les événements `SourceNameUpdateEvent`.
|
||||
///
|
||||
/// # Caractéristiques
|
||||
///
|
||||
/// - Écriture asynchrone avec buffer
|
||||
/// - Dérivation automatique du nom de fichier depuis la source
|
||||
/// - Support de plusieurs formats (WAV, FLAC, PCM brut)
|
||||
/// - Gestion du gain : applique le gain avant l'écriture
|
||||
///
|
||||
/// # Exemples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use pmoaudio::{DiskSink, DiskSinkConfig};
|
||||
/// use std::path::PathBuf;
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() {
|
||||
/// let config = DiskSinkConfig {
|
||||
/// output_dir: PathBuf::from("/tmp/audio"),
|
||||
/// filename: Some("output.wav".to_string()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
///
|
||||
/// let (sink, sink_tx) = DiskSink::new("disk1".to_string(), config, 10);
|
||||
///
|
||||
/// tokio::spawn(async move {
|
||||
/// sink.run().await.unwrap()
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub struct DiskSink {
|
||||
/// Identifiant du sink
|
||||
node_id: String,
|
||||
|
||||
/// Channel pour recevoir les chunks audio
|
||||
rx: mpsc::Receiver<Arc<AudioChunk>>,
|
||||
|
||||
/// Configuration
|
||||
config: DiskSinkConfig,
|
||||
|
||||
/// Nom de fichier résolu (partagé)
|
||||
resolved_filename: Arc<RwLock<Option<PathBuf>>>,
|
||||
|
||||
/// Receiver pour les événements de nom de source (optionnel)
|
||||
source_name_rx: Option<mpsc::Receiver<SourceNameUpdateEvent>>,
|
||||
|
||||
/// Writer pour le fichier
|
||||
writer: Option<AudioFileWriter>,
|
||||
}
|
||||
|
||||
impl DiskSink {
|
||||
/// Crée un nouveau DiskSink
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `node_id` - Identifiant unique du sink
|
||||
/// * `config` - Configuration du sink
|
||||
/// * `channel_size` - Taille du buffer du channel
|
||||
pub fn new(
|
||||
node_id: String,
|
||||
config: DiskSinkConfig,
|
||||
channel_size: usize,
|
||||
) -> (Self, mpsc::Sender<Arc<AudioChunk>>) {
|
||||
let (tx, rx) = mpsc::channel(channel_size);
|
||||
|
||||
let sink = Self {
|
||||
node_id,
|
||||
rx,
|
||||
config,
|
||||
resolved_filename: Arc::new(RwLock::new(None)),
|
||||
source_name_rx: None,
|
||||
writer: None,
|
||||
};
|
||||
|
||||
(sink, tx)
|
||||
}
|
||||
|
||||
/// Configure la source des événements de nom de source
|
||||
pub fn set_source_name_source(&mut self, rx: mpsc::Receiver<SourceNameUpdateEvent>) {
|
||||
self.source_name_rx = Some(rx);
|
||||
}
|
||||
|
||||
/// Résout le nom du fichier de sortie
|
||||
///
|
||||
/// Si un filename explicite est fourni dans la config, l'utilise.
|
||||
/// Sinon, utilise le source_name avec l'extension appropriée.
|
||||
fn resolve_filename(&self, source_name: Option<&str>) -> PathBuf {
|
||||
let filename = if let Some(ref explicit_name) = self.config.filename {
|
||||
explicit_name.clone()
|
||||
} else if let Some(name) = source_name {
|
||||
// Nettoyer le nom de la source pour en faire un nom de fichier valide
|
||||
let clean_name = name
|
||||
.chars()
|
||||
.map(|c| if c.is_alphanumeric() || c == '_' || c == '-' { c } else { '_' })
|
||||
.collect::<String>();
|
||||
|
||||
format!("{}.{}", clean_name, self.config.format.extension())
|
||||
} else {
|
||||
// Fallback sur un nom par défaut
|
||||
format!("{}.{}", self.node_id, self.config.format.extension())
|
||||
};
|
||||
|
||||
self.config.output_dir.join(filename)
|
||||
}
|
||||
|
||||
/// Initialise le writer pour le fichier de sortie
|
||||
async fn initialize_writer(&mut self, source_name: Option<&str>) -> Result<(), AudioError> {
|
||||
let path = self.resolve_filename(source_name);
|
||||
*self.resolved_filename.write().await = Some(path.clone());
|
||||
|
||||
// Créer le répertoire parent si nécessaire
|
||||
if let Some(parent) = path.parent() {
|
||||
tokio::fs::create_dir_all(parent)
|
||||
.await
|
||||
.map_err(|e| AudioError::ProcessingError(format!("Failed to create directory: {}", e)))?;
|
||||
}
|
||||
|
||||
// Créer le writer approprié selon le format
|
||||
let writer = match self.config.format {
|
||||
AudioFileFormat::Wav => AudioFileWriter::new_wav(path).await?,
|
||||
AudioFileFormat::Flac => {
|
||||
// FLAC nécessiterait une bibliothèque externe, pour l'instant utiliser WAV
|
||||
AudioFileWriter::new_wav(path).await?
|
||||
}
|
||||
AudioFileFormat::Raw => AudioFileWriter::new_raw(path).await?,
|
||||
};
|
||||
|
||||
self.writer = Some(writer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Démarre la boucle de traitement du DiskSink
|
||||
pub async fn run(mut self) -> Result<DiskSinkStats, AudioError> {
|
||||
let mut stats = DiskSinkStats::new(self.node_id.clone());
|
||||
let mut source_name: Option<String> = None;
|
||||
let mut initialized = false;
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
// Recevoir les chunks audio
|
||||
chunk_opt = self.rx.recv() => {
|
||||
match chunk_opt {
|
||||
Some(chunk) => {
|
||||
// Initialiser le writer à la réception du premier chunk
|
||||
if !initialized {
|
||||
self.initialize_writer(source_name.as_deref()).await?;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
// Appliquer le gain avant l'écriture
|
||||
let chunk_with_gain = if (chunk.gain - 1.0).abs() > f32::EPSILON {
|
||||
chunk.apply_gain()
|
||||
} else {
|
||||
(*chunk).clone()
|
||||
};
|
||||
|
||||
// Écrire le chunk
|
||||
if let Some(ref mut writer) = self.writer {
|
||||
writer.write_chunk(&chunk_with_gain).await?;
|
||||
stats.record_chunk(&chunk_with_gain);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// Channel fermé, terminer
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recevoir les mises à jour du nom de source
|
||||
source_event_opt = async {
|
||||
if let Some(ref mut rx) = self.source_name_rx {
|
||||
rx.recv().await
|
||||
} else {
|
||||
std::future::pending().await
|
||||
}
|
||||
} => {
|
||||
if let Some(event) = source_event_opt {
|
||||
source_name = Some(event.source_name.clone());
|
||||
|
||||
// Si on n'a pas encore initialisé, le nom sera utilisé plus tard
|
||||
// Sinon, on pourrait décider de fermer le fichier actuel et d'en créer un nouveau
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fermer le fichier proprement
|
||||
if let Some(writer) = self.writer {
|
||||
writer.close().await?;
|
||||
}
|
||||
|
||||
stats.finalize();
|
||||
Ok(stats)
|
||||
}
|
||||
}
|
||||
|
||||
/// Writer pour fichiers audio
|
||||
struct AudioFileWriter {
|
||||
file: File,
|
||||
format: AudioFileFormat,
|
||||
sample_rate: Option<u32>,
|
||||
total_samples: usize,
|
||||
}
|
||||
|
||||
impl AudioFileWriter {
|
||||
/// Crée un writer WAV
|
||||
async fn new_wav(path: PathBuf) -> Result<Self, AudioError> {
|
||||
let file = File::create(path)
|
||||
.await
|
||||
.map_err(|e| AudioError::ProcessingError(format!("Failed to create file: {}", e)))?;
|
||||
|
||||
Ok(Self {
|
||||
file,
|
||||
format: AudioFileFormat::Wav,
|
||||
sample_rate: None,
|
||||
total_samples: 0,
|
||||
})
|
||||
}
|
||||
|
||||
/// Crée un writer pour PCM brut
|
||||
async fn new_raw(path: PathBuf) -> Result<Self, AudioError> {
|
||||
let file = File::create(path)
|
||||
.await
|
||||
.map_err(|e| AudioError::ProcessingError(format!("Failed to create file: {}", e)))?;
|
||||
|
||||
Ok(Self {
|
||||
file,
|
||||
format: AudioFileFormat::Raw,
|
||||
sample_rate: None,
|
||||
total_samples: 0,
|
||||
})
|
||||
}
|
||||
|
||||
/// Écrit un chunk audio
|
||||
async fn write_chunk(&mut self, chunk: &AudioChunk) -> Result<(), AudioError> {
|
||||
// Enregistrer le sample rate du premier chunk
|
||||
if self.sample_rate.is_none() {
|
||||
self.sample_rate = Some(chunk.sample_rate);
|
||||
|
||||
// Pour WAV, écrire l'en-tête (simplifié)
|
||||
if matches!(self.format, AudioFileFormat::Wav) {
|
||||
self.write_wav_header(chunk.sample_rate).await?;
|
||||
}
|
||||
}
|
||||
|
||||
// Entrelacer les canaux gauche et droit
|
||||
let mut interleaved = Vec::with_capacity(chunk.len() * 2);
|
||||
for i in 0..chunk.len() {
|
||||
interleaved.push(chunk.left[i]);
|
||||
interleaved.push(chunk.right[i]);
|
||||
}
|
||||
|
||||
// Convertir en bytes (little-endian 16-bit PCM)
|
||||
let mut bytes = Vec::with_capacity(interleaved.len() * 2);
|
||||
for &sample in &interleaved {
|
||||
let sample_i16 = (sample.clamp(-1.0, 1.0) * 32767.0) as i16;
|
||||
bytes.extend_from_slice(&sample_i16.to_le_bytes());
|
||||
}
|
||||
|
||||
self.file
|
||||
.write_all(&bytes)
|
||||
.await
|
||||
.map_err(|e| AudioError::ProcessingError(format!("Failed to write audio data: {}", e)))?;
|
||||
|
||||
self.total_samples += chunk.len();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Écrit un en-tête WAV simplifié
|
||||
async fn write_wav_header(&mut self, sample_rate: u32) -> Result<(), AudioError> {
|
||||
// En-tête WAV basique (sera mis à jour à la fermeture)
|
||||
let mut header = Vec::new();
|
||||
|
||||
// RIFF chunk
|
||||
header.extend_from_slice(b"RIFF");
|
||||
header.extend_from_slice(&0u32.to_le_bytes()); // Taille (à mettre à jour)
|
||||
header.extend_from_slice(b"WAVE");
|
||||
|
||||
// fmt chunk
|
||||
header.extend_from_slice(b"fmt ");
|
||||
header.extend_from_slice(&16u32.to_le_bytes()); // Taille du fmt chunk
|
||||
header.extend_from_slice(&1u16.to_le_bytes()); // Format PCM
|
||||
header.extend_from_slice(&2u16.to_le_bytes()); // 2 canaux (stéréo)
|
||||
header.extend_from_slice(&sample_rate.to_le_bytes());
|
||||
header.extend_from_slice(&(sample_rate * 4).to_le_bytes()); // Byte rate
|
||||
header.extend_from_slice(&4u16.to_le_bytes()); // Block align
|
||||
header.extend_from_slice(&16u16.to_le_bytes()); // Bits per sample
|
||||
|
||||
// data chunk header
|
||||
header.extend_from_slice(b"data");
|
||||
header.extend_from_slice(&0u32.to_le_bytes()); // Taille des données (à mettre à jour)
|
||||
|
||||
self.file
|
||||
.write_all(&header)
|
||||
.await
|
||||
.map_err(|e| AudioError::ProcessingError(format!("Failed to write WAV header: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ferme le fichier et met à jour l'en-tête si nécessaire
|
||||
async fn close(mut self) -> Result<(), AudioError> {
|
||||
if matches!(self.format, AudioFileFormat::Wav) {
|
||||
// Mettre à jour les tailles dans l'en-tête WAV
|
||||
let data_size = (self.total_samples * 4) as u32; // 2 bytes per sample * 2 channels
|
||||
let file_size = data_size + 36;
|
||||
|
||||
// Positionner au début et réécrire les tailles
|
||||
use tokio::io::AsyncSeekExt;
|
||||
self.file.seek(std::io::SeekFrom::Start(4)).await.map_err(|e| {
|
||||
AudioError::ProcessingError(format!("Failed to seek in file: {}", e))
|
||||
})?;
|
||||
self.file.write_all(&file_size.to_le_bytes()).await.map_err(|e| {
|
||||
AudioError::ProcessingError(format!("Failed to update file size: {}", e))
|
||||
})?;
|
||||
|
||||
self.file.seek(std::io::SeekFrom::Start(40)).await.map_err(|e| {
|
||||
AudioError::ProcessingError(format!("Failed to seek in file: {}", e))
|
||||
})?;
|
||||
self.file.write_all(&data_size.to_le_bytes()).await.map_err(|e| {
|
||||
AudioError::ProcessingError(format!("Failed to update data size: {}", e))
|
||||
})?;
|
||||
}
|
||||
|
||||
self.file.flush().await.map_err(|e| {
|
||||
AudioError::ProcessingError(format!("Failed to flush file: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Statistiques du DiskSink
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DiskSinkStats {
|
||||
pub node_id: String,
|
||||
pub chunks_written: u64,
|
||||
pub total_samples: u64,
|
||||
pub total_duration_sec: f64,
|
||||
}
|
||||
|
||||
impl DiskSinkStats {
|
||||
pub fn new(node_id: String) -> Self {
|
||||
Self {
|
||||
node_id,
|
||||
chunks_written: 0,
|
||||
total_samples: 0,
|
||||
total_duration_sec: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_chunk(&mut self, chunk: &AudioChunk) {
|
||||
self.chunks_written += 1;
|
||||
self.total_samples += chunk.len() as u64;
|
||||
self.total_duration_sec += chunk.len() as f64 / chunk.sample_rate as f64;
|
||||
}
|
||||
|
||||
pub fn finalize(&mut self) {
|
||||
// Pourrait effectuer des calculs finaux ici
|
||||
}
|
||||
|
||||
pub fn display(&self) {
|
||||
println!("\n=== DiskSink Statistics: {} ===", self.node_id);
|
||||
println!("Chunks written: {}", self.chunks_written);
|
||||
println!("Total samples: {}", self.total_samples);
|
||||
println!("Total duration: {:.3} sec", self.total_duration_sec);
|
||||
println!("============================\n");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_disk_sink_basic() {
|
||||
let temp_dir = std::env::temp_dir().join("pmoaudio_test");
|
||||
tokio::fs::create_dir_all(&temp_dir).await.unwrap();
|
||||
|
||||
let config = DiskSinkConfig {
|
||||
output_dir: temp_dir.clone(),
|
||||
filename: Some("test_output.wav".to_string()),
|
||||
format: AudioFileFormat::Wav,
|
||||
buffer_size: 10,
|
||||
};
|
||||
|
||||
let (sink, tx) = DiskSink::new("test".to_string(), config, 10);
|
||||
|
||||
let handle = tokio::spawn(async move { sink.run().await });
|
||||
|
||||
// Envoyer quelques chunks
|
||||
for i in 0..5 {
|
||||
let chunk = AudioChunk::new(i, vec![0.5; 1000], vec![0.5; 1000], 48000);
|
||||
tx.send(Arc::new(chunk)).await.unwrap();
|
||||
}
|
||||
|
||||
drop(tx);
|
||||
|
||||
let stats = handle.await.unwrap().unwrap();
|
||||
assert_eq!(stats.chunks_written, 5);
|
||||
|
||||
// Vérifier que le fichier existe
|
||||
let output_path = temp_dir.join("test_output.wav");
|
||||
assert!(output_path.exists());
|
||||
|
||||
// Nettoyage
|
||||
tokio::fs::remove_file(output_path).await.ok();
|
||||
tokio::fs::remove_dir(temp_dir).await.ok();
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,15 @@ use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
pub mod buffer_node;
|
||||
pub mod chromecast_sink;
|
||||
pub mod decoder_node;
|
||||
pub mod disk_sink;
|
||||
pub mod dsp_node;
|
||||
pub mod mpd_sink;
|
||||
pub mod sink_node;
|
||||
pub mod source_node;
|
||||
pub mod timer_node;
|
||||
pub mod volume_node;
|
||||
|
||||
/// Trait de base pour tous les nodes audio
|
||||
///
|
||||
|
||||
389
pmoaudio/src/nodes/mpd_sink.rs
Normal file
389
pmoaudio/src/nodes/mpd_sink.rs
Normal file
@@ -0,0 +1,389 @@
|
||||
//! MpdSink - Envoie le flux audio à un démon MPD (Music Player Daemon)
|
||||
//!
|
||||
//! Ce module fournit un sink qui streame l'audio vers un démon MPD distant ou local.
|
||||
//! Note: Cette implémentation est une version mock/skeleton. Une vraie implémentation
|
||||
//! nécessiterait le protocole MPD complet et l'utilisation de bibliothèques comme `mpd`.
|
||||
|
||||
use crate::{nodes::AudioError, AudioChunk};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
/// Configuration pour le MpdSink
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MpdConfig {
|
||||
/// Adresse du serveur MPD
|
||||
pub host: String,
|
||||
|
||||
/// Port du serveur MPD (défaut: 6600)
|
||||
pub port: u16,
|
||||
|
||||
/// Mot de passe optionnel
|
||||
pub password: Option<String>,
|
||||
|
||||
/// Nom de l'output MPD à utiliser (optionnel)
|
||||
pub output_name: Option<String>,
|
||||
|
||||
/// Taille du buffer
|
||||
pub buffer_size: usize,
|
||||
|
||||
/// Format d'envoi
|
||||
pub format: MpdAudioFormat,
|
||||
}
|
||||
|
||||
impl Default for MpdConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
host: "localhost".to_string(),
|
||||
port: 6600,
|
||||
password: None,
|
||||
output_name: None,
|
||||
buffer_size: 50,
|
||||
format: MpdAudioFormat::S16Le,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats audio supportés par MPD
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum MpdAudioFormat {
|
||||
/// Signed 16-bit Little Endian
|
||||
S16Le,
|
||||
/// Signed 24-bit Little Endian
|
||||
S24Le,
|
||||
/// Signed 32-bit Little Endian
|
||||
S32Le,
|
||||
/// Float 32-bit
|
||||
F32,
|
||||
}
|
||||
|
||||
impl MpdAudioFormat {
|
||||
/// Retourne le nom du format pour le protocole MPD
|
||||
pub fn as_mpd_string(&self) -> &str {
|
||||
match self {
|
||||
MpdAudioFormat::S16Le => "16:16:2",
|
||||
MpdAudioFormat::S24Le => "24:24:2",
|
||||
MpdAudioFormat::S32Le => "32:32:2",
|
||||
MpdAudioFormat::F32 => "f:32:2",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// MpdSink - Streame vers un démon MPD
|
||||
///
|
||||
/// Ce sink se connecte à un serveur MPD et lui envoie le flux audio.
|
||||
/// MPD peut ensuite router l'audio vers différents outputs (ALSA, PulseAudio, HTTP, etc.).
|
||||
///
|
||||
/// # Implémentation actuelle
|
||||
///
|
||||
/// Cette version est un mock qui simule la communication avec MPD.
|
||||
/// Pour une vraie implémentation, il faudrait:
|
||||
/// - Implémenter le protocole MPD (commandes textuelles sur TCP)
|
||||
/// - S'authentifier si nécessaire
|
||||
/// - Configurer le format audio
|
||||
/// - Envoyer les données PCM via le protocole approprié
|
||||
/// - Gérer les commandes de contrôle (play, pause, stop)
|
||||
///
|
||||
/// # Exemples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use pmoaudio::{MpdSink, MpdConfig};
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() {
|
||||
/// let config = MpdConfig {
|
||||
/// host: "localhost".to_string(),
|
||||
/// port: 6600,
|
||||
/// password: None,
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
///
|
||||
/// let (sink, sink_tx) = MpdSink::new("mpd1".to_string(), config, 10);
|
||||
///
|
||||
/// tokio::spawn(async move {
|
||||
/// sink.run().await.unwrap()
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub struct MpdSink {
|
||||
/// Identifiant du sink
|
||||
node_id: String,
|
||||
|
||||
/// Channel pour recevoir les chunks audio
|
||||
rx: mpsc::Receiver<Arc<AudioChunk>>,
|
||||
|
||||
/// Configuration
|
||||
config: MpdConfig,
|
||||
|
||||
/// État de la connexion (mock)
|
||||
connected: bool,
|
||||
|
||||
/// Version du serveur MPD (mock)
|
||||
mpd_version: Option<String>,
|
||||
}
|
||||
|
||||
impl MpdSink {
|
||||
/// Crée un nouveau MpdSink
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `node_id` - Identifiant unique du sink
|
||||
/// * `config` - Configuration MPD
|
||||
/// * `channel_size` - Taille du buffer du channel
|
||||
pub fn new(
|
||||
node_id: String,
|
||||
config: MpdConfig,
|
||||
channel_size: usize,
|
||||
) -> (Self, mpsc::Sender<Arc<AudioChunk>>) {
|
||||
let (tx, rx) = mpsc::channel(channel_size);
|
||||
|
||||
let sink = Self {
|
||||
node_id,
|
||||
rx,
|
||||
config,
|
||||
connected: false,
|
||||
mpd_version: None,
|
||||
};
|
||||
|
||||
(sink, tx)
|
||||
}
|
||||
|
||||
/// Établit la connexion avec le serveur MPD (mock)
|
||||
async fn connect(&mut self) -> Result<(), AudioError> {
|
||||
println!(
|
||||
"[{}] Connecting to MPD at {}:{}...",
|
||||
self.node_id, self.config.host, self.config.port
|
||||
);
|
||||
|
||||
// Simuler une connexion TCP
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(300)).await;
|
||||
|
||||
// Dans une vraie implémentation:
|
||||
// 1. Établir connexion TCP
|
||||
// 2. Lire la bannière de version
|
||||
// 3. S'authentifier si password fourni
|
||||
// 4. Configurer le format audio
|
||||
|
||||
self.mpd_version = Some("0.23.0".to_string());
|
||||
self.connected = true;
|
||||
|
||||
println!(
|
||||
"[{}] Connected to MPD v{} successfully",
|
||||
self.node_id,
|
||||
self.mpd_version.as_ref().unwrap()
|
||||
);
|
||||
|
||||
// Configurer le format audio
|
||||
self.configure_audio_format().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Configure le format audio sur MPD (mock)
|
||||
async fn configure_audio_format(&self) -> Result<(), AudioError> {
|
||||
println!(
|
||||
"[{}] Configuring audio format: {}",
|
||||
self.node_id,
|
||||
self.config.format.as_mpd_string()
|
||||
);
|
||||
|
||||
// Dans une vraie implémentation:
|
||||
// Envoyer une commande MPD pour configurer le format
|
||||
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Envoie un chunk au serveur MPD (mock)
|
||||
async fn send_chunk(&self, _chunk: &AudioChunk) -> Result<(), AudioError> {
|
||||
if !self.connected {
|
||||
return Err(AudioError::ProcessingError("Not connected to MPD".to_string()));
|
||||
}
|
||||
|
||||
// Dans une vraie implémentation:
|
||||
// 1. Appliquer le gain
|
||||
// 2. Convertir dans le format approprié (S16LE, etc.)
|
||||
// 3. Envoyer via le protocole MPD (probablement via une commande `sendmessage` ou pipe)
|
||||
|
||||
// Simuler un délai d'envoi
|
||||
tokio::time::sleep(tokio::time::Duration::from_micros(50)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Déconnecte proprement du serveur MPD (mock)
|
||||
async fn disconnect(&mut self) -> Result<(), AudioError> {
|
||||
if self.connected {
|
||||
println!("[{}] Disconnecting from MPD...", self.node_id);
|
||||
|
||||
// Dans une vraie implémentation:
|
||||
// Envoyer la commande "close"
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
|
||||
self.connected = false;
|
||||
|
||||
println!("[{}] Disconnected successfully", self.node_id);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Démarre la boucle de traitement du MpdSink
|
||||
pub async fn run(mut self) -> Result<MpdStats, AudioError> {
|
||||
// Établir la connexion
|
||||
self.connect().await?;
|
||||
|
||||
let mut stats = MpdStats::new(
|
||||
self.node_id.clone(),
|
||||
format!("{}:{}", self.config.host, self.config.port),
|
||||
);
|
||||
|
||||
// Boucle principale
|
||||
while let Some(chunk) = self.rx.recv().await {
|
||||
// Appliquer le gain si nécessaire
|
||||
let chunk_to_send = if (chunk.gain - 1.0).abs() > f32::EPSILON {
|
||||
chunk.apply_gain()
|
||||
} else {
|
||||
(*chunk).clone()
|
||||
};
|
||||
|
||||
// Envoyer au serveur MPD
|
||||
self.send_chunk(&chunk_to_send).await?;
|
||||
|
||||
stats.record_chunk(&chunk_to_send);
|
||||
}
|
||||
|
||||
// Déconnexion propre
|
||||
self.disconnect().await?;
|
||||
|
||||
stats.finalize();
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
/// Retourne un handle pour contrôler le sink (mock)
|
||||
pub fn get_handle(&self) -> MpdHandle {
|
||||
MpdHandle {
|
||||
node_id: self.node_id.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle pour contrôler le MpdSink
|
||||
///
|
||||
/// Permet d'envoyer des commandes de contrôle au serveur MPD
|
||||
#[derive(Clone)]
|
||||
pub struct MpdHandle {
|
||||
node_id: String,
|
||||
}
|
||||
|
||||
impl MpdHandle {
|
||||
/// Commande play (mock)
|
||||
pub async fn play(&self) -> Result<(), AudioError> {
|
||||
println!("[{}] MPD command: play", self.node_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Commande pause (mock)
|
||||
pub async fn pause(&self) -> Result<(), AudioError> {
|
||||
println!("[{}] MPD command: pause", self.node_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Commande stop (mock)
|
||||
pub async fn stop(&self) -> Result<(), AudioError> {
|
||||
println!("[{}] MPD command: stop", self.node_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Change le volume MPD (0-100) (mock)
|
||||
pub async fn set_volume(&self, volume: u8) -> Result<(), AudioError> {
|
||||
let clamped = volume.min(100);
|
||||
println!("[{}] MPD command: setvol {}", self.node_id, clamped);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Statistiques du MpdSink
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MpdStats {
|
||||
pub node_id: String,
|
||||
pub server_address: String,
|
||||
pub chunks_sent: u64,
|
||||
pub total_samples: u64,
|
||||
pub total_duration_sec: f64,
|
||||
}
|
||||
|
||||
impl MpdStats {
|
||||
pub fn new(node_id: String, server_address: String) -> Self {
|
||||
Self {
|
||||
node_id,
|
||||
server_address,
|
||||
chunks_sent: 0,
|
||||
total_samples: 0,
|
||||
total_duration_sec: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_chunk(&mut self, chunk: &AudioChunk) {
|
||||
self.chunks_sent += 1;
|
||||
self.total_samples += chunk.len() as u64;
|
||||
self.total_duration_sec += chunk.len() as f64 / chunk.sample_rate as f64;
|
||||
}
|
||||
|
||||
pub fn finalize(&mut self) {
|
||||
// Calculs finaux si nécessaire
|
||||
}
|
||||
|
||||
pub fn display(&self) {
|
||||
println!("\n=== MPD Sink Statistics: {} ===", self.node_id);
|
||||
println!("Server: {}", self.server_address);
|
||||
println!("Chunks sent: {}", self.chunks_sent);
|
||||
println!("Total samples: {}", self.total_samples);
|
||||
println!("Total duration: {:.3} sec", self.total_duration_sec);
|
||||
println!("===============================\n");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_mpd_sink_basic() {
|
||||
let config = MpdConfig {
|
||||
host: "localhost".to_string(),
|
||||
port: 6600,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (sink, tx) = MpdSink::new("test".to_string(), config, 10);
|
||||
|
||||
let handle = tokio::spawn(async move { sink.run().await });
|
||||
|
||||
// Envoyer quelques chunks
|
||||
for i in 0..5 {
|
||||
let chunk = AudioChunk::new(i, vec![0.5; 1000], vec![0.5; 1000], 48000);
|
||||
tx.send(Arc::new(chunk)).await.unwrap();
|
||||
}
|
||||
|
||||
drop(tx);
|
||||
|
||||
let stats = handle.await.unwrap().unwrap();
|
||||
assert_eq!(stats.chunks_sent, 5);
|
||||
assert_eq!(stats.server_address, "localhost:6600");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_mpd_handle() {
|
||||
let config = MpdConfig::default();
|
||||
let (sink, _tx) = MpdSink::new("test".to_string(), config, 10);
|
||||
|
||||
let handle = sink.get_handle();
|
||||
|
||||
// Tester les commandes (mock)
|
||||
handle.play().await.unwrap();
|
||||
handle.pause().await.unwrap();
|
||||
handle.set_volume(75).await.unwrap();
|
||||
handle.stop().await.unwrap();
|
||||
}
|
||||
}
|
||||
358
pmoaudio/src/nodes/volume_node.rs
Normal file
358
pmoaudio/src/nodes/volume_node.rs
Normal file
@@ -0,0 +1,358 @@
|
||||
//! Volume nodes - Contrôle du volume audio
|
||||
//!
|
||||
//! Ce module fournit des nodes pour ajuster le volume du flux audio,
|
||||
//! avec support du volume master/secondaire et notification des changements.
|
||||
|
||||
use crate::{
|
||||
events::{EventPublisher, VolumeChangeEvent},
|
||||
nodes::{AudioError, MultiSubscriberNode},
|
||||
AudioChunk,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{mpsc, RwLock};
|
||||
|
||||
/// VolumeNode - Applique un gain au flux audio (contrôle software)
|
||||
///
|
||||
/// Ce node modifie le champ `gain` de chaque `AudioChunk` qui le traverse.
|
||||
/// Le gain est multiplié avec le gain existant du chunk, permettant ainsi
|
||||
/// une chaîne de contrôles de volume.
|
||||
///
|
||||
/// # Caractéristiques
|
||||
///
|
||||
/// - Thread-safe : le volume peut être modifié pendant l'exécution via `set_volume`
|
||||
/// - Notification : émet des événements `VolumeChangeEvent` lors des changements
|
||||
/// - Master/Slave : peut s'abonner à un volume master pour synchronisation
|
||||
///
|
||||
/// # Exemples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use pmoaudio::VolumeNode;
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() {
|
||||
/// let (volume_node, volume_tx) = VolumeNode::new("Room 1".to_string(), 0.8, 10);
|
||||
///
|
||||
/// // Modifier le volume pendant l'exécution
|
||||
/// let handle = volume_node.get_handle();
|
||||
/// tokio::spawn(async move {
|
||||
/// tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
||||
/// handle.set_volume(0.5).await;
|
||||
/// });
|
||||
///
|
||||
/// tokio::spawn(async move { volume_node.run().await.unwrap() });
|
||||
/// }
|
||||
/// ```
|
||||
pub struct VolumeNode {
|
||||
/// Channel pour recevoir les chunks audio
|
||||
rx: mpsc::Receiver<Arc<AudioChunk>>,
|
||||
|
||||
/// Subscribers pour les chunks modifiés
|
||||
subscribers: MultiSubscriberNode,
|
||||
|
||||
/// Volume courant (partagé via RwLock pour lecture/écriture thread-safe)
|
||||
volume: Arc<RwLock<f32>>,
|
||||
|
||||
/// Publisher pour les événements de changement de volume
|
||||
volume_publisher: EventPublisher<VolumeChangeEvent>,
|
||||
|
||||
/// Identifiant unique du node (pour traçabilité)
|
||||
node_id: String,
|
||||
|
||||
/// Receiver pour les événements de volume master (optionnel)
|
||||
master_volume_rx: Option<mpsc::Receiver<VolumeChangeEvent>>,
|
||||
}
|
||||
|
||||
impl VolumeNode {
|
||||
/// Crée un nouveau VolumeNode
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `node_id` - Identifiant unique du node
|
||||
/// * `initial_volume` - Volume initial (0.0 à 1.0)
|
||||
/// * `channel_size` - Taille du buffer du channel
|
||||
pub fn new(
|
||||
node_id: String,
|
||||
initial_volume: f32,
|
||||
channel_size: usize,
|
||||
) -> (Self, mpsc::Sender<Arc<AudioChunk>>) {
|
||||
let (tx, rx) = mpsc::channel(channel_size);
|
||||
|
||||
let node = Self {
|
||||
rx,
|
||||
subscribers: MultiSubscriberNode::new(),
|
||||
volume: Arc::new(RwLock::new(initial_volume)),
|
||||
volume_publisher: EventPublisher::new(),
|
||||
node_id,
|
||||
master_volume_rx: None,
|
||||
};
|
||||
|
||||
(node, tx)
|
||||
}
|
||||
|
||||
/// Ajoute un subscriber pour recevoir les chunks audio modifiés
|
||||
pub fn add_subscriber(&mut self, tx: mpsc::Sender<Arc<AudioChunk>>) {
|
||||
self.subscribers.add_subscriber(tx);
|
||||
}
|
||||
|
||||
/// Ajoute un subscriber pour les événements de changement de volume
|
||||
pub fn subscribe_volume_events(&mut self, tx: mpsc::Sender<VolumeChangeEvent>) {
|
||||
self.volume_publisher.subscribe(tx);
|
||||
}
|
||||
|
||||
/// Configure ce node pour écouter un volume master
|
||||
///
|
||||
/// Le node appliquera à la fois son volume local ET le volume master reçu.
|
||||
pub fn set_master_volume_source(&mut self, rx: mpsc::Receiver<VolumeChangeEvent>) {
|
||||
self.master_volume_rx = Some(rx);
|
||||
}
|
||||
|
||||
/// Retourne un handle pour contrôler le volume depuis un autre contexte
|
||||
pub fn get_handle(&self) -> VolumeHandle {
|
||||
VolumeHandle {
|
||||
volume: self.volume.clone(),
|
||||
node_id: self.node_id.clone(),
|
||||
publisher: Arc::new(RwLock::new(self.volume_publisher.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Démarre la boucle de traitement du VolumeNode
|
||||
pub async fn run(mut self) -> Result<(), AudioError> {
|
||||
let mut master_volume = 1.0f32;
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
// Recevoir les chunks audio
|
||||
chunk_opt = self.rx.recv() => {
|
||||
match chunk_opt {
|
||||
Some(chunk) => {
|
||||
let local_volume = *self.volume.read().await;
|
||||
let total_volume = local_volume * master_volume;
|
||||
|
||||
// Créer un nouveau chunk avec le gain modifié
|
||||
let modified_chunk = chunk.with_modified_gain(total_volume);
|
||||
|
||||
// Envoyer aux subscribers
|
||||
self.subscribers.push(Arc::new(modified_chunk)).await?;
|
||||
}
|
||||
None => {
|
||||
// Channel fermé, terminer
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recevoir les mises à jour du volume master (si configuré)
|
||||
master_event_opt = async {
|
||||
if let Some(ref mut rx) = self.master_volume_rx {
|
||||
rx.recv().await
|
||||
} else {
|
||||
// Bloquer indéfiniment si pas de master
|
||||
std::future::pending().await
|
||||
}
|
||||
} => {
|
||||
if let Some(event) = master_event_opt {
|
||||
master_volume = event.volume;
|
||||
|
||||
// Optionnel : re-publier l'événement combiné
|
||||
let local_volume = *self.volume.read().await;
|
||||
let combined_event = VolumeChangeEvent {
|
||||
volume: local_volume * master_volume,
|
||||
source_node_id: self.node_id.clone(),
|
||||
};
|
||||
self.volume_publisher.publish(combined_event).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle pour contrôler un VolumeNode depuis un autre contexte
|
||||
///
|
||||
/// Ce handle permet de modifier le volume et de notifier les subscribers
|
||||
/// sans avoir accès direct au node.
|
||||
#[derive(Clone)]
|
||||
pub struct VolumeHandle {
|
||||
volume: Arc<RwLock<f32>>,
|
||||
node_id: String,
|
||||
publisher: Arc<RwLock<EventPublisher<VolumeChangeEvent>>>,
|
||||
}
|
||||
|
||||
impl VolumeHandle {
|
||||
/// Modifie le volume
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `new_volume` - Nouveau volume (0.0 à 1.0)
|
||||
pub async fn set_volume(&self, new_volume: f32) {
|
||||
let clamped = new_volume.clamp(0.0, 1.0);
|
||||
*self.volume.write().await = clamped;
|
||||
|
||||
// Publier l'événement de changement
|
||||
let event = VolumeChangeEvent {
|
||||
volume: clamped,
|
||||
source_node_id: self.node_id.clone(),
|
||||
};
|
||||
|
||||
self.publisher.read().await.publish(event).await;
|
||||
}
|
||||
|
||||
/// Obtient le volume courant
|
||||
pub async fn get_volume(&self) -> f32 {
|
||||
*self.volume.read().await
|
||||
}
|
||||
|
||||
/// Augmente le volume de manière relative
|
||||
pub async fn adjust_volume(&self, delta: f32) {
|
||||
let current = *self.volume.read().await;
|
||||
self.set_volume(current + delta).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// HardwareVolumeNode - Contrôle matériel du volume
|
||||
///
|
||||
/// Ce node simule un contrôle hardware du volume. Dans une implémentation réelle,
|
||||
/// il communiquerait avec le driver audio pour ajuster le volume matériel.
|
||||
///
|
||||
/// Pour cette version, il agit de manière similaire à `VolumeNode` mais pourrait
|
||||
/// être étendu pour utiliser des APIs système spécifiques.
|
||||
pub struct HardwareVolumeNode {
|
||||
inner: VolumeNode,
|
||||
}
|
||||
|
||||
impl HardwareVolumeNode {
|
||||
/// Crée un nouveau HardwareVolumeNode
|
||||
pub fn new(
|
||||
node_id: String,
|
||||
initial_volume: f32,
|
||||
channel_size: usize,
|
||||
) -> (Self, mpsc::Sender<Arc<AudioChunk>>) {
|
||||
let (inner, tx) = VolumeNode::new(node_id, initial_volume, channel_size);
|
||||
|
||||
(Self { inner }, tx)
|
||||
}
|
||||
|
||||
/// Ajoute un subscriber
|
||||
pub fn add_subscriber(&mut self, tx: mpsc::Sender<Arc<AudioChunk>>) {
|
||||
self.inner.add_subscriber(tx);
|
||||
}
|
||||
|
||||
/// Obtient un handle pour contrôler le volume
|
||||
pub fn get_handle(&self) -> VolumeHandle {
|
||||
self.inner.get_handle()
|
||||
}
|
||||
|
||||
/// Démarre la boucle de traitement
|
||||
pub async fn run(self) -> Result<(), AudioError> {
|
||||
// Dans une vraie implémentation, on communiquerait avec le hardware ici
|
||||
// Pour l'instant, délègue au VolumeNode standard
|
||||
self.inner.run().await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_volume_node_basic() {
|
||||
let (mut node, tx) = VolumeNode::new("test".to_string(), 0.5, 10);
|
||||
let (out_tx, mut out_rx) = mpsc::channel(10);
|
||||
|
||||
node.add_subscriber(out_tx);
|
||||
|
||||
let handle = tokio::spawn(async move { node.run().await });
|
||||
|
||||
// Envoyer un chunk avec gain 1.0
|
||||
let chunk = AudioChunk::with_gain(0, vec![1.0; 100], vec![1.0; 100], 48000, 1.0);
|
||||
tx.send(Arc::new(chunk)).await.unwrap();
|
||||
|
||||
// Recevoir le chunk modifié
|
||||
let modified = out_rx.recv().await.unwrap();
|
||||
assert!((modified.gain - 0.5).abs() < f32::EPSILON);
|
||||
|
||||
drop(tx);
|
||||
handle.await.unwrap().unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_volume_handle() {
|
||||
let (node, tx) = VolumeNode::new("test".to_string(), 1.0, 10);
|
||||
let handle = node.get_handle();
|
||||
|
||||
tokio::spawn(async move { node.run().await });
|
||||
|
||||
// Modifier le volume via le handle
|
||||
handle.set_volume(0.3).await;
|
||||
|
||||
let volume = handle.get_volume().await;
|
||||
assert!((volume - 0.3).abs() < f32::EPSILON);
|
||||
|
||||
drop(tx);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_volume_events() {
|
||||
let (mut node, tx) = VolumeNode::new("test".to_string(), 1.0, 10);
|
||||
let (event_tx, mut event_rx) = mpsc::channel(10);
|
||||
|
||||
node.subscribe_volume_events(event_tx);
|
||||
let handle = node.get_handle();
|
||||
|
||||
tokio::spawn(async move { node.run().await });
|
||||
|
||||
// Changer le volume
|
||||
handle.set_volume(0.7).await;
|
||||
|
||||
// Vérifier l'événement
|
||||
let event = event_rx.recv().await.unwrap();
|
||||
assert!((event.volume - 0.7).abs() < f32::EPSILON);
|
||||
assert_eq!(event.source_node_id, "test");
|
||||
|
||||
drop(tx);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_master_slave_volume() {
|
||||
// Créer le master
|
||||
let (mut master, master_tx) = VolumeNode::new("master".to_string(), 1.0, 10);
|
||||
let (master_event_tx, master_event_rx) = mpsc::channel(10);
|
||||
master.subscribe_volume_events(master_event_tx);
|
||||
let master_handle = master.get_handle();
|
||||
|
||||
// Créer le slave
|
||||
let (mut slave, slave_tx) = VolumeNode::new("slave".to_string(), 0.8, 10);
|
||||
slave.set_master_volume_source(master_event_rx);
|
||||
let (out_tx, mut out_rx) = mpsc::channel(10);
|
||||
slave.add_subscriber(out_tx);
|
||||
|
||||
tokio::spawn(async move { master.run().await });
|
||||
tokio::spawn(async move { slave.run().await });
|
||||
|
||||
// Envoyer un chunk au slave
|
||||
let chunk = AudioChunk::with_gain(0, vec![1.0; 100], vec![1.0; 100], 48000, 1.0);
|
||||
slave_tx.send(Arc::new(chunk)).await.unwrap();
|
||||
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
|
||||
|
||||
// Modifier le volume master
|
||||
master_handle.set_volume(0.5).await;
|
||||
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
|
||||
|
||||
// Envoyer un autre chunk
|
||||
let chunk2 = AudioChunk::with_gain(1, vec![1.0; 100], vec![1.0; 100], 48000, 1.0);
|
||||
slave_tx.send(Arc::new(chunk2)).await.unwrap();
|
||||
|
||||
// Le deuxième chunk devrait avoir un gain de 0.8 * 0.5 = 0.4
|
||||
let _first = out_rx.recv().await.unwrap(); // gain = 0.8
|
||||
let second = out_rx.recv().await.unwrap(); // gain = 0.4
|
||||
|
||||
assert!((second.gain - 0.4).abs() < 0.01);
|
||||
|
||||
drop(master_tx);
|
||||
drop(slave_tx);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user