voxudio 0.7.1

A real-time audio processing library with ONNX runtime support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use {
    crate::{GenericSample, OperationError, samples_to_f32},
    cpal::{
        BufferSize, Device, HostId, Stream, StreamConfig, SupportedStreamConfig, default_host,
        traits::{DeviceTrait, HostTrait, StreamTrait},
    },
    rodio::{ChannelCount, SampleRate, buffer::SamplesBuffer, source::UniformSourceIterator},
    std::{
        fmt::{Debug, Error as FmtError, Formatter, Result as FmtResult},
        io::Error as IoError,
        mem::replace,
    },
    tokio::sync::{
        mpsc::{Sender as MpscSender, channel as mpsc_channel},
        watch::{Sender as WatchSender, channel as watch_channel},
    },
};

/// 音频播放器结构体,用于管理和控制音频播放
///
/// # 字段说明
/// - `device`: 音频输出设备
/// - `host_id`: 音频主机ID
/// - `stream_config`: 音频流配置
/// - `supported_stream_config`: 支持的音频流配置
/// - `stream`: 音频流实例
/// - `sender`: 音频数据发送通道
///
/// # 示例
/// ```
/// use voxudio::AudioPlayer;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let mut player = AudioPlayer::new()?;
/// player.play()?;
/// player.write::<44100, f32>(&[0f32; 88200], 2).await?;
///
/// Ok(())
/// }
/// ```
///
/// # 错误
/// 可能返回`OperationError`类型的错误,包括设备不可用、配置错误等情况
pub struct AudioPlayer {
    device: Device,
    host_id: HostId,
    stream_config: StreamConfig,
    supported_stream_config: SupportedStreamConfig,
    stream: Stream,
    write_sender: MpscSender<f32>,
    stop_sender: WatchSender<bool>,
}

impl AudioPlayer {
    fn create_stream(
        device: &Device,
        stream_config: &StreamConfig,
    ) -> Result<(MpscSender<f32>, WatchSender<bool>, Stream), OperationError> {
        let buffer_size = match stream_config.buffer_size {
            BufferSize::Default => 8192,
            BufferSize::Fixed(size) => size as _,
        };
        let (write_tx, mut write_rx) = mpsc_channel(buffer_size);
        let (stop_tx, stop_rx) = watch_channel(false);

        Ok((
            write_tx,
            stop_tx,
            device.build_output_stream(
                stream_config.clone(),
                move |buffer: &mut [f32], _| {
                    let buf = buffer.iter_mut();
                    if *stop_rx.borrow() {
                        buf.for_each(|i| *i = Default::default());
                        while let Ok(_) = write_rx.try_recv() {}
                    } else {
                        buf.for_each(|i| *i = write_rx.try_recv().unwrap_or_default());
                    }
                },
                |e| eprintln!("{}", e),
                None,
            )?,
        ))
    }

    fn update_stream(&mut self) -> Result<(), OperationError> {
        let (write_sender, stop_sender, stream) =
            Self::create_stream(&self.device, &self.stream_config)?;
        drop(replace(&mut self.stream, stream));
        drop(replace(&mut self.write_sender, write_sender));
        drop(replace(&mut self.stop_sender, stop_sender));

        Ok(())
    }

    /// 创建一个新的音频播放器实例
    ///
    /// # 返回值
    /// 返回`Result<Self, OperationError>`,成功时包含初始化的音频播放器实例
    ///
    /// # 错误
    /// 可能返回以下错误:
    /// - `OperationError::NoDevice`: 当没有默认音频输出设备时
    /// - `OperationError::StreamError`: 当创建音频流失败时
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn new() -> Result<Self, OperationError> {
        let host = default_host();
        let device = host
            .default_output_device()
            .ok_or(OperationError::NoDevice(
                "No default audio output device.".to_owned(),
            ))?;
        let host_id = host.id();
        let supported_stream_config = device.default_output_config()?;
        let stream_config = supported_stream_config.config();
        let (write_sender, stop_sender, stream) = Self::create_stream(&device, &stream_config)?;

        Ok(Self {
            device,
            host_id,
            stream_config,
            supported_stream_config,
            stream,
            write_sender,
            stop_sender,
        })
    }

    /// 获取音频输出设备名称
    ///
    /// # 返回值
    /// 返回`Result<String, OperationError>`,成功时包含设备名称字符串
    ///
    /// # 错误
    /// 可能返回`OperationError::DeviceError`,当获取设备名称失败时
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    /// let name = player.get_name()?;
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn get_name(&self) -> Result<String, OperationError> {
        Ok(self.device.description()?.name().to_owned())
    }

    /// 获取支持的音频流通道数
    ///
    /// # 返回值
    /// 返回支持的音频通道数量(usize)
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    /// let channels = player.get_supported_stream_channels();
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn get_supported_stream_channels(&self) -> usize {
        self.supported_stream_config.channels() as _
    }

    /// 获取支持的音频流采样率
    ///
    /// # 返回值
    /// 返回支持的音频采样率(usize)
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    /// let sample_rate = player.get_supported_stream_sample_rate();
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn get_supported_stream_sample_rate(&self) -> usize {
        self.supported_stream_config.sample_rate() as _
    }

    /// 设置音频流通道数
    ///
    /// # 参数
    /// - `channels`: 要设置的通道数(usize)
    ///
    /// # 返回值
    /// 返回`Result<(), OperationError>`,成功时表示设置完成
    ///
    /// # 错误
    /// 可能返回`OperationError::StreamError`,当更新音频流失败时
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let mut player = AudioPlayer::new()?;
    /// player.set_stream_channels(2)?;
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn set_stream_channels(&mut self, channels: usize) -> Result<(), OperationError> {
        self.stream_config.channels = channels as _;
        self.update_stream()
    }

    /// 设置音频流采样率
    ///
    /// # 参数
    /// - `sample_rate`: 要设置的采样率(usize)
    ///
    /// # 返回值
    /// 返回`Result<(), OperationError>`,成功时表示设置完成
    ///
    /// # 错误
    /// 可能返回`OperationError::StreamError`,当更新音频流失败时
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let mut player = AudioPlayer::new()?;
    /// if let Ok(_) = player.set_stream_sample_rate(44100) {
    /// println!("Set sample rate successfully.");
    /// }
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn set_stream_sample_rate(&mut self, sample_rate: usize) -> Result<(), OperationError> {
        self.stream_config.sample_rate = sample_rate as _;
        self.update_stream()
    }

    /// 获取当前音频流的通道数
    ///
    /// # 返回值
    /// 返回当前音频流的通道数量(usize)
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    /// let channels = player.get_stream_channels();
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn get_stream_channels(&self) -> usize {
        self.stream_config.channels as _
    }

    /// 获取当前音频流的采样率
    ///
    /// # 返回值
    /// 返回当前音频流的采样率(usize)
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    /// let sample_rate = player.get_stream_sample_rate();
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn get_stream_sample_rate(&self) -> usize {
        self.stream_config.sample_rate as _
    }

    /// 开始播放或恢复暂停的音频
    ///
    /// # 返回值
    /// 返回`Result<(), OperationError>`,成功时表示播放已开始
    ///
    /// # 错误
    /// 可能返回`OperationError::StreamError`,当启动播放失败时
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    /// player.play()?;
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn play(&self) -> Result<(), OperationError> {
        self.stop_sender.send(false)?;

        Ok(self.stream.play()?)
    }

    /// 暂停播放音频(可使用`play()`恢复)
    ///
    /// # 返回值
    /// 返回`Result<(), OperationError>`,成功时表示播放已暂停
    ///
    /// # 错误
    /// 可能返回`OperationError::StreamError`,当暂停播放失败时
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    /// fn main() -> anyhow::Result<()> {
    /// let player = AudioPlayer::new()?;
    /// player.pause()?;
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn pause(&self) -> Result<(), OperationError> {
        Ok(self.stream.pause()?)
    }

    pub fn stop(&self) -> Result<(), OperationError> {
        self.stop_sender.send(true)?;
        self.stream.pause()?;

        Ok(())
    }

    /// 写入音频样本数据
    /// 当流通道已满,此方法会异步等待,直到播放器消耗一部分数据
    ///
    /// # 参数
    /// - `samples`: 音频样本数据切片,类型必须实现 [`GenericSample`] 特征
    /// - `channels`: 音频通道数
    ///
    /// # 返回值
    /// 返回`Result<(), OperationError>`,成功时表示数据已写入
    ///
    /// # 错误
    /// 可能返回以下错误:
    /// - `OperationError::StreamError`: 当写入数据失败时
    /// - `OperationError::ChannelError`: 当通道发送失败时
    ///
    /// # 示例
    /// ```
    /// use voxudio::AudioPlayer;
    ///
    /// #[tokio::main]
    /// async fn main() -> anyhow::Result<()> {
    ///     let mut player = AudioPlayer::new()?;
    ///     player.play()?;
    ///     player.write::<44100, f32>(&[0f32; 88200], 2).await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn write<const SR: usize, S>(
        &mut self,
        samples: &[S],
        channels: usize,
    ) -> Result<(), OperationError>
    where
        S: GenericSample,
    {
        if self.get_stream_channels() != channels || self.get_stream_sample_rate() != SR {
            let f32_samples = samples_to_f32(samples);
            let mut iter = UniformSourceIterator::new(
                SamplesBuffer::new(
                    ChannelCount::new(channels as _)
                        .ok_or(IoError::other("invalid channel count"))?,
                    SampleRate::new(SR as _).ok_or(IoError::other("invalid sample rate"))?,
                    f32_samples,
                ),
                ChannelCount::new(self.stream_config.channels)
                    .ok_or(IoError::other("invalid channel count"))?,
                SampleRate::new(self.stream_config.sample_rate)
                    .ok_or(IoError::other("invalid sample rate"))?,
            );
            while !*self.stop_sender.borrow() {
                let mut p = self.write_sender.reserve_many(256).await?;
                while let Some(w) = p.next()
                    && let Some(i) = iter.next()
                {
                    w.send(i);
                }
            }

            return Ok(());
        }

        for s in samples {
            self.write_sender.send(s.to_f32()).await?;
        }

        Ok(())
    }
}

impl Debug for AudioPlayer {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "AudioPlayer({}, {})",
            self.host_id.name(),
            self.get_name().map_err(|_| FmtError)?
        )
    }
}