multi channels

This commit is contained in:
Tom Gowan 2018-11-04 21:23:24 +11:00 committed by mitchmindtree
parent 6e0eb073d1
commit ffcbc02e52
3 changed files with 41 additions and 27 deletions

View File

@ -1,4 +1,5 @@
#include "helpers.hpp"
#include <stdio.h>
extern "C" ASIOError get_sample_rate(double * rate){
return ASIOGetSampleRate(reinterpret_cast<ASIOSampleRate *>(rate));

View File

@ -71,7 +71,6 @@ enum AsioState {
Running,
}
pub struct AsioStreams {
pub input: Option<AsioStream>,
pub output: Option<AsioStream>,
@ -282,14 +281,15 @@ impl Drivers {
}
pub fn prepare_input_stream(&self, output: Option<AsioStream>, num_channels: usize) -> Result<AsioStreams, AsioDriverError> {
let buffer_infos = vec![
let buffer_infos = (0..num_channels)
.map(|i| {
AsioBufferInfo {
is_input: 1,
channel_num: 0,
channel_num: i as c_long,
buffers: [std::ptr::null_mut(); 2],
};
num_channels
];
}
})
.collect();
let streams = AsioStreams{input: Some(AsioStream{buffer_infos, buffer_size: 0}), output};
self.create_streams(streams)
@ -298,14 +298,15 @@ impl Drivers {
/// Creates the output stream
pub fn prepare_output_stream(&self, input: Option<AsioStream>, num_channels: usize) -> Result<AsioStreams, AsioDriverError> {
// Initialize data for FFI
let buffer_infos = vec![
let buffer_infos = (0..num_channels)
.map(|i| {
AsioBufferInfo {
is_input: 0,
channel_num: 0,
channel_num: i as c_long,
buffers: [std::ptr::null_mut(); 2],
};
num_channels
];
}
})
.collect();
let streams = AsioStreams{output: Some(AsioStream{buffer_infos, buffer_size: 0}), input};
self.create_streams(streams)
}
@ -397,10 +398,13 @@ impl Drivers {
&mut grans,
).expect("Failed getting buffers");
if pref_b_size > 0 {
/*
let mut buffer_info_convert: Vec<ai::ASIOBufferInfo> = buffer_infos
.into_iter()
.map(|bi| mem::transmute::<AsioBufferInfo, ai::ASIOBufferInfo>(bi))
.collect();
*/
let mut buffer_info_convert = mem::transmute::<Vec<AsioBufferInfo>, Vec<ai::ASIOBufferInfo>>(buffer_infos);
let mut callbacks_convert =
mem::transmute::<AsioCallbacks, ai::ASIOCallbacks>(callbacks);
drivers.asio_create_buffers(
@ -409,10 +413,7 @@ impl Drivers {
pref_b_size,
&mut callbacks_convert,
).map(|_|{
let buffer_infos: Vec<AsioBufferInfo> = buffer_info_convert
.into_iter()
.map(|bi| mem::transmute::<ai::ASIOBufferInfo, AsioBufferInfo>(bi))
.collect();
let buffer_infos = mem::transmute::<Vec<ai::ASIOBufferInfo>, Vec<AsioBufferInfo>>(buffer_info_convert);
for d in &buffer_infos {
println!("after {:?}", d);
}

View File

@ -75,7 +75,14 @@ impl EventLoop {
/// Create a new CPAL Input Stream
/// If there is no ASIO Input Stream
/// it will be created
fn get_input_stream(&self, drivers: &sys::Drivers, num_channels: usize, sample_rate: u32) -> Result<usize, CreationError> {
fn get_input_stream(&self, drivers: &sys::Drivers, format: &Format) -> Result<usize, CreationError> {
let Format {
channels,
sample_rate,
..
} = format;
let num_channels = *channels as usize;
let sample_rate = sample_rate.0;
let ref mut streams = *self.asio_streams.lock().unwrap();
if sample_rate != drivers.get_sample_rate().rate {
if drivers.can_sample_rate(sample_rate) {
@ -105,7 +112,14 @@ impl EventLoop {
}
}
fn get_output_stream(&self, drivers: &sys::Drivers, num_channels: usize, sample_rate: u32) -> Result<usize, CreationError> {
fn get_output_stream(&self, drivers: &sys::Drivers, format: &Format) -> Result<usize, CreationError> {
let Format {
channels,
sample_rate,
..
} = format;
let num_channels = *channels as usize;
let sample_rate = sample_rate.0;
let ref mut streams = *self.asio_streams.lock().unwrap();
if sample_rate != drivers.get_sample_rate().rate {
if drivers.can_sample_rate(sample_rate) {
@ -146,8 +160,7 @@ impl EventLoop {
} = device;
let num_channels = format.channels.clone();
let stream_type = drivers.get_data_type().expect("Couldn't load data type");
let sample_rate = format.sample_rate.0;
self.get_input_stream(&drivers, num_channels as usize, sample_rate).map(|stream_buffer_size| {
self.get_input_stream(&drivers, format).map(|stream_buffer_size| {
let cpal_num_samples = stream_buffer_size * num_channels as usize;
let count = self.stream_count.load(Ordering::SeqCst);
self.stream_count.store(count + 1, Ordering::SeqCst);
@ -361,8 +374,7 @@ pub fn build_output_stream(
} = device;
let num_channels = format.channels.clone();
let stream_type = drivers.get_data_type().expect("Couldn't load data type");
let sample_rate = format.sample_rate.0;
self.get_output_stream(&drivers, num_channels as usize, sample_rate).map(|stream_buffer_size| {
self.get_output_stream(&drivers, format).map(|stream_buffer_size| {
let cpal_num_samples = stream_buffer_size * num_channels as usize;
let count = self.stream_count.load(Ordering::SeqCst);
self.stream_count.store(count + 1, Ordering::SeqCst);