Ver Fonte

ported to cpal, also broke sound

thajohns há 4 anos atrás
pai
commit
d2c63db0e4
2 ficheiros alterados com 41 adições e 62 exclusões
  1. 1 25
      Cargo.toml
  2. 40 37
      src/main.rs

+ 1 - 25
Cargo.toml

@@ -8,35 +8,11 @@ authors = ["Graham Northup <grissess@nexusg.org>"]
 name = "synfone"
 doc = false
 
-#[features]
-#default = ['graphics']
-#
-## The following feature will always refer ultimately to whatever backend
-## graphics library is chosen--which is subject to change.
-#graphics = ['glium', 'piston2d-graphics', 'piston2d-glium_graphics', 'palette']
 
 [dependencies]
 byteorder = "1.1.0"
 rand = "0.3"
+cpal = "0.13"
 unicode-xid = "0.1.0"
-portaudio = "0.7.0"
 xml-rs = "0.8.0"
 failure = "0.1"
-
-#[dependencies.glium]
-##version = "0.17.1"
-#git = "https://github.com/cmr/glium.git"
-#optional = true
-#features = ["glutin"]
-#
-#[dependencies.'piston2d-graphics']
-#version = "0.22.0"
-#optional = true
-#
-#[dependencies.'piston2d-glium_graphics']
-#version = "0.43.0"
-#optional = true
-#
-#[dependencies.palette]
-#version = "0.2"
-#optional = true

+ 40 - 37
src/main.rs

@@ -5,8 +5,8 @@ use std::net::*;
 use std::sync::*;
 use std::{env, ffi, iter, thread};
 
-extern crate portaudio;
-use portaudio as pa;
+use cpal::traits::{HostTrait, DeviceTrait, StreamTrait};
+
 use synfone::client::*;
 use synfone::lang::*;
 use synfone::proto::*;
@@ -64,46 +64,49 @@ fn main_client(args: Vec<ffi::OsString>) -> Result<(), std::io::Error> {
         .expect("Failed to init shared buffer")
         .append(&mut iter::repeat(0.0f32).take(last_buffer_lim).collect());
 
-    let pa_inst = pa::PortAudio::new().expect("Failed to create PortAudio interface");
-    let settings = pa_inst
-        .default_output_stream_settings(1, env.sample_rate as f64, env.default_buffer_size as u32)
-        .expect("Failed to instantiate stream settings");
-    let mut stream;
+    let host = cpal::default_host();
+    let device = host.default_output_device().expect("no default host audio device!");
+    let mut conf_range = device.supported_output_configs().expect("could not query audio device capabilities -- audio device disconnected?");
+    let conf = conf_range.next().expect("audio device has no supported configs!").with_max_sample_rate().config();
+    let stream;
     {
-        let client = client.clone();
-        let last_buffer = last_buffer.clone();
-        let mut ring: VecDeque<Sample> = VecDeque::new();
-        ring.reserve_exact(2 * env.default_buffer_size);
-        stream = pa_inst
-            .open_non_blocking_stream(
-                settings,
-                move |pa::OutputStreamCallbackArgs { buffer, frames, .. }| {
-                    while frames > ring.len() {
-                        let mut cli = client.lock().unwrap();
-                        cli.next_frames();
-                        {
-                            let mut buf = last_buffer
-                                .lock()
-                                .expect("Failed to acquire shared buffer in audio callback");
-                            buf.append(&mut cli.buffer().samples.iter().map(|&x| x).collect());
-                            let len = buf.len();
-                            if len > last_buffer_lim {
-                                buf.drain(..(len - last_buffer_lim));
-                            }
-                        }
-                        ring.append(&mut cli.buffer().iter().map(|&x| x).collect());
-                    }
-                    let samps = ring.drain(..frames).collect::<Vec<f32>>();
-                    buffer.copy_from_slice(&samps);
-                    pa::Continue
-                },
-            )
-            .expect("Failed to create stream");
+      let client = client.clone();
+      let last_buffer = last_buffer.clone();
+      let mut ring: VecDeque<Sample> = VecDeque::new();
+      ring.reserve_exact(2 * env.default_buffer_size);
+      stream = device.build_output_stream(
+          &conf,
+          move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
+              let frames = data.len();
+              while frames > ring.len() {
+                  let mut cli = client.lock().unwrap();
+                  cli.next_frames();
+                  {
+                      let mut buf = last_buffer
+                          .lock()
+                          .expect("Failed to acquire shared buffer in audio callback");
+                      buf.append(&mut cli.buffer().samples.iter().map(|&x| x).collect());
+                      let len = buf.len();
+                      if len > last_buffer_lim {
+                          buf.drain(..(len - last_buffer_lim));
+                      }
+                  }
+                  ring.append(&mut cli.buffer().iter().map(|&x| x).collect());
+              }
+              let mut drain = ring.drain(..frames);
+              for i in 0..frames {
+                data[i] = drain.next().unwrap();
+              }
+          },
+          move |err| {
+            println!("audio stream error: {}", err);
+          }
+      ).expect("could not create audio stream!");
     }
 
     eprintln!("Starting.");
 
-    stream.start().expect("Failed to start stream");
+    stream.play().expect("Failed to start stream");
 
     eprintln!("Audio stream started.");