|
| 1 | +// Mostly copied from symphonia-play |
| 2 | + |
| 3 | +// Copyright (c) 2019-2022 The Project Symphonia Developers. |
| 4 | +// |
| 5 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 8 | + |
| 9 | +//! Platform-dependant Audio Outputs |
| 10 | +
|
| 11 | +use std::{result, time}; |
| 12 | + |
| 13 | +use super::resampler::Resampler; |
| 14 | +use symphonia::core::audio::{AudioBufferRef, RawSample, SampleBuffer, SignalSpec}; |
| 15 | +use symphonia::core::conv::{ConvertibleSample, IntoSample}; |
| 16 | +use symphonia::core::units::Duration; |
| 17 | + |
| 18 | +use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; |
| 19 | +use rb::*; |
| 20 | + |
| 21 | +use log::{error, info}; |
| 22 | + |
| 23 | +pub trait AudioOutput { |
| 24 | + fn write(&mut self, decoded: AudioBufferRef<'_>) -> Result<()>; |
| 25 | + fn flush(&mut self); |
| 26 | +} |
| 27 | + |
| 28 | +#[allow(dead_code)] |
| 29 | +#[allow(clippy::enum_variant_names)] |
| 30 | +#[derive(Debug)] |
| 31 | +pub enum AudioOutputError { |
| 32 | + OpenStreamError, |
| 33 | + PlayStreamError, |
| 34 | + StreamClosedError, |
| 35 | +} |
| 36 | + |
| 37 | +pub type Result<T> = result::Result<T, AudioOutputError>; |
| 38 | + |
| 39 | +pub struct CpalAudioOutput; |
| 40 | + |
| 41 | +trait AudioOutputSample: |
| 42 | + cpal::SizedSample + ConvertibleSample + IntoSample<f32> + RawSample + std::marker::Send + 'static |
| 43 | +{ |
| 44 | +} |
| 45 | + |
| 46 | +impl AudioOutputSample for f32 {} |
| 47 | +impl AudioOutputSample for i16 {} |
| 48 | +impl AudioOutputSample for u16 {} |
| 49 | + |
| 50 | +impl CpalAudioOutput { |
| 51 | + pub fn try_open(spec: SignalSpec, duration: Duration) -> Result<Box<dyn AudioOutput>> { |
| 52 | + // Get default host. |
| 53 | + let host = cpal::default_host(); |
| 54 | + |
| 55 | + // Get the default audio output device. |
| 56 | + let device = match host.default_output_device() { |
| 57 | + Some(device) => device, |
| 58 | + _ => { |
| 59 | + error!("failed to get default audio output device"); |
| 60 | + return Err(AudioOutputError::OpenStreamError); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + let config = match device.default_output_config() { |
| 65 | + Ok(config) => config, |
| 66 | + Err(err) => { |
| 67 | + error!("failed to get default audio output device config: {}", err); |
| 68 | + return Err(AudioOutputError::OpenStreamError); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + // Select proper playback routine based on sample format. |
| 73 | + match config.sample_format() { |
| 74 | + cpal::SampleFormat::F32 => { |
| 75 | + CpalAudioOutputImpl::<f32>::try_open(spec, duration, &device) |
| 76 | + } |
| 77 | + cpal::SampleFormat::I16 => { |
| 78 | + CpalAudioOutputImpl::<i16>::try_open(spec, duration, &device) |
| 79 | + } |
| 80 | + cpal::SampleFormat::U16 => { |
| 81 | + CpalAudioOutputImpl::<u16>::try_open(spec, duration, &device) |
| 82 | + } |
| 83 | + _ => unimplemented!(), |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +struct CpalAudioOutputImpl<T: AudioOutputSample> |
| 89 | +where |
| 90 | + T: AudioOutputSample, |
| 91 | +{ |
| 92 | + ring_buf_producer: rb::Producer<T>, |
| 93 | + sample_buf: SampleBuffer<T>, |
| 94 | + stream: cpal::Stream, |
| 95 | + resampler: Option<Resampler<T>>, |
| 96 | +} |
| 97 | + |
| 98 | +impl<T: AudioOutputSample> CpalAudioOutputImpl<T> { |
| 99 | + pub fn try_open( |
| 100 | + spec: SignalSpec, |
| 101 | + duration: Duration, |
| 102 | + device: &cpal::Device, |
| 103 | + ) -> Result<Box<dyn AudioOutput>> { |
| 104 | + let num_channels = spec.channels.count(); |
| 105 | + |
| 106 | + // Output audio stream config. |
| 107 | + let config = if cfg!(not(target_os = "windows")) { |
| 108 | + cpal::StreamConfig { |
| 109 | + channels: num_channels as cpal::ChannelCount, |
| 110 | + sample_rate: cpal::SampleRate(spec.rate), |
| 111 | + buffer_size: cpal::BufferSize::Default, |
| 112 | + } |
| 113 | + } else { |
| 114 | + // Use the default config for Windows. |
| 115 | + device |
| 116 | + .default_output_config() |
| 117 | + .expect("Failed to get the default output config.") |
| 118 | + .config() |
| 119 | + }; |
| 120 | + |
| 121 | + // Create a ring buffer with a capacity for up-to 200ms of audio. |
| 122 | + let ring_len = ((200 * config.sample_rate.0 as usize) / 1000) * num_channels; |
| 123 | + |
| 124 | + let ring_buf = SpscRb::new(ring_len); |
| 125 | + let (ring_buf_producer, ring_buf_consumer) = (ring_buf.producer(), ring_buf.consumer()); |
| 126 | + |
| 127 | + let stream_result = device.build_output_stream( |
| 128 | + &config, |
| 129 | + move |data: &mut [T], _: &cpal::OutputCallbackInfo| { |
| 130 | + // Write out as many samples as possible from the ring buffer to the audio |
| 131 | + // output. |
| 132 | + let written = ring_buf_consumer.read(data).unwrap_or(0); |
| 133 | + |
| 134 | + // Mute any remaining samples. |
| 135 | + data[written..].iter_mut().for_each(|s| *s = T::MID); |
| 136 | + }, |
| 137 | + move |err| error!("audio output error: {}", err), |
| 138 | + Some(time::Duration::from_secs(1)), |
| 139 | + ); |
| 140 | + |
| 141 | + if let Err(err) = stream_result { |
| 142 | + error!("audio output stream open error: {}", err); |
| 143 | + |
| 144 | + return Err(AudioOutputError::OpenStreamError); |
| 145 | + } |
| 146 | + |
| 147 | + let stream = stream_result.unwrap(); |
| 148 | + |
| 149 | + // Start the output stream. |
| 150 | + if let Err(err) = stream.play() { |
| 151 | + error!("audio output stream play error: {}", err); |
| 152 | + |
| 153 | + return Err(AudioOutputError::PlayStreamError); |
| 154 | + } |
| 155 | + |
| 156 | + let sample_buf = SampleBuffer::<T>::new(duration, spec); |
| 157 | + |
| 158 | + let resampler = if spec.rate != config.sample_rate.0 { |
| 159 | + info!("resampling {} Hz to {} Hz", spec.rate, config.sample_rate.0); |
| 160 | + Some(Resampler::new( |
| 161 | + spec, |
| 162 | + config.sample_rate.0 as usize, |
| 163 | + duration, |
| 164 | + )) |
| 165 | + } else { |
| 166 | + None |
| 167 | + }; |
| 168 | + |
| 169 | + Ok(Box::new(CpalAudioOutputImpl { |
| 170 | + ring_buf_producer, |
| 171 | + sample_buf, |
| 172 | + stream, |
| 173 | + resampler, |
| 174 | + })) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +impl<T: AudioOutputSample> AudioOutput for CpalAudioOutputImpl<T> { |
| 179 | + fn write(&mut self, decoded: AudioBufferRef<'_>) -> Result<()> { |
| 180 | + // Do nothing if there are no audio frames. |
| 181 | + if decoded.frames() == 0 { |
| 182 | + return Ok(()); |
| 183 | + } |
| 184 | + |
| 185 | + let mut samples = if let Some(resampler) = &mut self.resampler { |
| 186 | + // Resampling is required. The resampler will return interleaved samples in the |
| 187 | + // correct sample format. |
| 188 | + match resampler.resample(decoded) { |
| 189 | + Some(resampled) => resampled, |
| 190 | + None => return Ok(()), |
| 191 | + } |
| 192 | + } else { |
| 193 | + // Resampling is not required. Interleave the sample for cpal using a sample buffer. |
| 194 | + self.sample_buf.copy_interleaved_ref(decoded); |
| 195 | + |
| 196 | + self.sample_buf.samples() |
| 197 | + }; |
| 198 | + |
| 199 | + // Write all samples to the ring buffer. |
| 200 | + while let Some(written) = self.ring_buf_producer.write_blocking(samples) { |
| 201 | + samples = &samples[written..]; |
| 202 | + } |
| 203 | + |
| 204 | + Ok(()) |
| 205 | + } |
| 206 | + |
| 207 | + fn flush(&mut self) { |
| 208 | + // If there is a resampler, then it may need to be flushed |
| 209 | + // depending on the number of samples it has. |
| 210 | + if let Some(resampler) = &mut self.resampler { |
| 211 | + let mut remaining_samples = resampler.flush().unwrap_or_default(); |
| 212 | + |
| 213 | + while let Some(written) = self.ring_buf_producer.write_blocking(remaining_samples) { |
| 214 | + remaining_samples = &remaining_samples[written..]; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + // Flush is best-effort, ignore the returned result. |
| 219 | + let _ = self.stream.pause(); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +pub fn try_open(spec: SignalSpec, duration: Duration) -> Result<Box<dyn AudioOutput>> { |
| 224 | + CpalAudioOutput::try_open(spec, duration) |
| 225 | +} |
0 commit comments