From 7b0a4a5838194acbd9575111448844042916d5db Mon Sep 17 00:00:00 2001 From: Mike Welsh Date: Fri, 10 Jul 2020 15:24:05 -0700 Subject: [PATCH] chore: Bump to cpal 0.12 --- Cargo.lock | 102 +++++++++++++++++++++++++++++++++++++--- desktop/Cargo.toml | 2 +- desktop/src/audio.rs | 109 +++++++++++++++++++++---------------------- 3 files changed, 149 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0852ac3b4..1f94247ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,9 +18,20 @@ dependencies = [ "memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "alsa" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "alsa-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "alsa-sys" -version = "0.1.2" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", @@ -383,17 +394,21 @@ dependencies = [ [[package]] name = "cpal" -version = "0.11.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "alsa-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "alsa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "coreaudio-rs 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "js-sys 0.3.41 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "stdweb 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", + "web-sys 0.3.41 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1423,6 +1438,18 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nix" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "nom" version = "5.1.1" @@ -1589,6 +1616,16 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "parking_lot" version = "0.10.2" @@ -1598,6 +1635,20 @@ dependencies = [ "parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "parking_lot_core" version = "0.7.2" @@ -1961,7 +2012,7 @@ name = "ruffle_desktop" version = "0.1.0" dependencies = [ "clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "cpal 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cpal 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "embed-resource 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2110,6 +2161,14 @@ name = "rustc-hash" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rusttype" version = "0.7.9" @@ -2161,6 +2220,19 @@ name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "serde" version = "1.0.114" @@ -2207,6 +2279,14 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "smallvec" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "smallvec" version = "1.4.1" @@ -2891,7 +2971,8 @@ dependencies = [ "checksum adler 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ccc9a9dd069569f212bc4330af9f17c4afb5e8ce185e83dbb14f1349dda18b10" "checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2" "checksum aho-corasick 0.7.10 (registry+https://github.com/rust-lang/crates.io-index)" = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada" -"checksum alsa-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b0edcbbf9ef68f15ae1b620f722180b82a98b6f0628d30baa6b8d2a5abc87d58" +"checksum alsa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "44581add1add74ade32aca327b550342359ec00191672c23c1caa3d492b85930" +"checksum alsa-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5a0559bcd3f7a482690d98be41c08a43e92f669b179433e95ddf5e8b8fd36a3" "checksum andrew 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7f09f89872c2b6b29e319377b1fbe91c6f5947df19a25596e121cf19a7b35e" "checksum android_log-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b8052e2d8aabbb8d556d6abbcce2a22b9590996c5f849b9c7ce4544a2e3b984e" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" @@ -2936,7 +3017,7 @@ dependencies = [ "checksum core-video-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "34ecad23610ad9757664d644e369246edde1803fcb43ed72876565098a5d3828" "checksum coreaudio-rs 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f229761965dad3e9b11081668a6ea00f1def7aa46062321b5ec245b834f6e491" "checksum coreaudio-sys 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e81f1c165c33ffab90a03077ac3b03462b34d5947145dfa48102e063d581502c" -"checksum cpal 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6b55d55d69f403f62a95bd3c04b431e0aedf5120c70f15d07a8edd234443dd59" +"checksum cpal 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ba08f6cef6ecc4058a6c458b6b692ded275b5e80f67230387237ba3e187d593" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum crossbeam-deque 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" "checksum crossbeam-epoch 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" @@ -3048,6 +3129,7 @@ dependencies = [ "checksum ndk-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2b2820aca934aba5ed91c79acc72b6a44048ceacc5d36c035ed4e051f12d887d" "checksum net2 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7" "checksum nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" +"checksum nix 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3b2e0b4f3320ed72aaedb9a5ac838690a8047c7b275da22711fddff4f8a14229" "checksum nom 5.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b471253da97532da4b61552249c521e01e736071f71c1a4f7ebbfbf0a06aad6" "checksum num-derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0c8b15b261814f992e33760b1fca9fe8b693d8a65299f20c9901688636cfb746" "checksum num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" @@ -3068,6 +3150,8 @@ dependencies = [ "checksum ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "18869315e81473c951eb56ad5558bbc56978562d3ecfb87abb7a1e944cea4518" "checksum output_vt100 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" "checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" +"checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" +"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" "checksum parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" "checksum path-slash 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ff65715a17cba8979903db6294baef56c5d39e05c8b054cffa31e69e61f24c68" "checksum peek-poke 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d93fd6a575ebf1ac2668d08443c97a22872cfb463fd8b7ddd141e9f6be59af2f" @@ -3110,6 +3194,7 @@ dependencies = [ "checksum rle-decode-fast 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cabe4fa914dec5870285fa7f71f602645da47c486e68486d2b4ceb4a343e90ac" "checksum rust-argon2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" "checksum rustc-hash 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusttype 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)" = "310942406a39981bed7e12b09182a221a29e0990f3e7e0c971f131922ed135d5" "checksum rusttype 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9f61411055101f7b60ecf1041d87fb74205fb20b0c7a723f07ef39174cf6b4c0" "checksum ryu 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1" @@ -3118,12 +3203,15 @@ dependencies = [ "checksum scoped-tls 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)" = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3" "checksum serde_derive 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)" = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e" "checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" "checksum sid 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bd5ac56c121948b4879bba9e519852c211bcdd8f014efff766441deff0b91bdb" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum slice-deque 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "31ef6ee280cdefba6d2d0b4b78a84a1c1a3f3a4cec98c2d4231c8bc225de0f25" +"checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" "checksum smallvec 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3757cb9d89161a2f24e1cf78efa0c1fcff485d18e3f55e0aa3480824ddaa0f3f" "checksum smithay-client-toolkit 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "421c8dc7acf5cb205b88160f8b4cc2c5cfabe210e43b2f80f009f4c1ef910f1d" "checksum spirv_cross 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "946216f8793f7199e3ea5b995ee8dc20a0ace1fcf46293a0ef4c17e1d046dbde" diff --git a/desktop/Cargo.toml b/desktop/Cargo.toml index 8ac6b650d..89c1bdf2e 100644 --- a/desktop/Cargo.toml +++ b/desktop/Cargo.toml @@ -7,7 +7,7 @@ default-run = "ruffle_desktop" license = "MIT OR Apache-2.0" [dependencies] -cpal = "0.11.0" +cpal = "0.12.0" ruffle_core = { path = "../core" } ruffle_render_wgpu = { path = "../render/wgpu" } env_logger = "0.7.1" diff --git a/desktop/src/audio.rs b/desktop/src/audio.rs index bed29a96b..6290f56bd 100644 --- a/desktop/src/audio.rs +++ b/desktop/src/audio.rs @@ -1,4 +1,4 @@ -use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait}; +use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use generational_arena::Arena; use ruffle_core::backend::audio::decoders::{ self, AdpcmDecoder, Mp3Decoder, PcmDecoder, SeekableDecoder, @@ -14,13 +14,19 @@ use swf::AudioCompression; #[allow(dead_code)] pub struct CpalAudioBackend { device: cpal::Device, - output_format: cpal::Format, - audio_thread_handle: std::thread::JoinHandle<()>, - + output_config: cpal::StreamConfig, + stream: Stream, sounds: Arena, sound_instances: Arc>>, } +// Because of https://github.com/RustAudio/cpal/pull/348, we have to initialize cpal on a +// separate thread (see `new` below). Unfortunately `cpal::Stream` is marked `!Send`, but +// we know this should be safe (since we aren't accessing the stream at all after creation; +// we just want to keep it alive) +struct Stream(cpal::Stream); +unsafe impl Send for CpalAudioBackend {} + type Signal = Box>; type Error = Box; @@ -81,73 +87,64 @@ impl CpalAudioBackend { fn init() -> Result { // Create CPAL audio device. let host = cpal::default_host(); - let event_loop = host.event_loop(); let device = host .default_output_device() .ok_or("No audio devices available")?; // Create audio stream for device. - let mut supported_formats = device - .supported_output_formats() + let mut supported_configs = device + .supported_output_configs() .map_err(|_| "No supported audio format")?; - let format = supported_formats + let config = supported_configs .next() .ok_or("No supported audio formats")? .with_max_sample_rate(); - let stream_id = event_loop - .build_output_stream(&device, &format) - .map_err(|_| "Unable to create audio stream")?; - - let output_format = format.clone(); - // Start the stream. - event_loop - .play_stream(stream_id) - .map_err(|_| "Unable to start audio stream")?; + let sample_format = config.sample_format(); + let config = cpal::StreamConfig::from(config); let sound_instances: Arc>> = Arc::new(Mutex::new(Arena::new())); - // Start the audio thread. - let audio_thread_handle = { + // Start the audio stream. + let stream = { let sound_instances = Arc::clone(&sound_instances); - std::thread::spawn(move || { - event_loop.run(move |stream_id, stream_result| { - use cpal::{StreamData, UnknownTypeOutputBuffer}; + let error_handler = move |err| log::error!("Audio stream error: {}", err); + let output_config = config.clone(); - let stream_data = match stream_result { - Ok(data) => data, - Err(err) => { - eprintln!("an error occurred on stream {:?}: {}", stream_id, err); - return; - } - }; - - let mut sound_instances = sound_instances.lock().unwrap(); - match stream_data { - StreamData::Output { - buffer: UnknownTypeOutputBuffer::U16(buffer), - } => { - Self::mix_audio(&mut sound_instances, &output_format, buffer); - } - StreamData::Output { - buffer: UnknownTypeOutputBuffer::I16(buffer), - } => { - Self::mix_audio(&mut sound_instances, &output_format, buffer); - } - StreamData::Output { - buffer: UnknownTypeOutputBuffer::F32(buffer), - } => { - Self::mix_audio(&mut sound_instances, &output_format, buffer); - } - _ => (), - } - }); - }) + use cpal::SampleFormat; + match sample_format { + SampleFormat::F32 => device.build_output_stream( + &config, + move |buffer, _| { + let mut sound_instances = sound_instances.lock().unwrap(); + Self::mix_audio::(&mut sound_instances, &output_config, buffer) + }, + error_handler, + ), + SampleFormat::I16 => device.build_output_stream( + &config, + move |buffer, _| { + let mut sound_instances = sound_instances.lock().unwrap(); + Self::mix_audio::(&mut sound_instances, &output_config, buffer) + }, + error_handler, + ), + SampleFormat::U16 => device.build_output_stream( + &config, + move |buffer, _| { + let mut sound_instances = sound_instances.lock().unwrap(); + Self::mix_audio::(&mut sound_instances, &output_config, buffer) + }, + error_handler, + ), + }? }; + stream.play()?; + Ok(Self { device, - output_format: format, - audio_thread_handle, + output_config: config, + stream: Stream(stream), sounds: Arena::new(), sound_instances, }) @@ -200,7 +197,7 @@ impl CpalAudioBackend { signal, interpolator, format.sample_rate.into(), - self.output_format.sample_rate.0.into(), + self.output_config.sample_rate.0.into(), ) } @@ -266,8 +263,8 @@ impl CpalAudioBackend { /// and mixing in their output. fn mix_audio<'a, T>( sound_instances: &mut Arena, - output_format: &cpal::Format, - mut output_buffer: cpal::OutputBuffer<'a, T>, + output_format: &cpal::StreamConfig, + mut output_buffer: &mut [T], ) where T: 'a + cpal::Sample + Default + sample::Sample, T::Signed: sample::conv::FromSample,