record wav working v1
This commit is contained in:
parent
fe4d108477
commit
a1740a9282
|
@ -318,6 +318,109 @@ pub fn get_data_type(driver_name: &str) -> Result<AsioSampleType, ASIOError> {
|
||||||
data_type
|
data_type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn prepare_input_stream(driver_name: &str) -> Result<AsioStream, ASIOError> {
|
||||||
|
let mut buffer_infos = [
|
||||||
|
AsioBufferInfo {
|
||||||
|
is_input: 1,
|
||||||
|
channel_num: 0,
|
||||||
|
buffers: [std::ptr::null_mut(); 2],
|
||||||
|
},
|
||||||
|
AsioBufferInfo {
|
||||||
|
is_input: 1,
|
||||||
|
channel_num: 1,
|
||||||
|
buffers: [std::ptr::null_mut(); 2],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let num_channels = 2;
|
||||||
|
|
||||||
|
let mut callbacks = AsioCallbacks {
|
||||||
|
buffer_switch: buffer_switch,
|
||||||
|
sample_rate_did_change: sample_rate_did_change,
|
||||||
|
asio_message: asio_message,
|
||||||
|
buffer_switch_time_info: buffer_switch_time_info,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut min_b_size: c_long = 0;
|
||||||
|
let mut max_b_size: c_long = 0;
|
||||||
|
let mut pref_b_size: c_long = 0;
|
||||||
|
let mut grans: c_long = 0;
|
||||||
|
|
||||||
|
let mut driver_info = ai::ASIODriverInfo {
|
||||||
|
_bindgen_opaque_blob: [0u32; 43],
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make owned CString to send to load driver
|
||||||
|
let mut my_driver_name = CString::new(driver_name).expect("Can't go from str to CString");
|
||||||
|
let raw = my_driver_name.into_raw();
|
||||||
|
|
||||||
|
let mut result = Err(ASIOError::NoResult("not implimented".to_owned()));
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let mut asio_drivers = ai::AsioDrivers::new();
|
||||||
|
let load_result = asio_drivers.loadDriver(raw);
|
||||||
|
// Take back ownership
|
||||||
|
my_driver_name = CString::from_raw(raw);
|
||||||
|
if !load_result {
|
||||||
|
return Err(ASIOError::DriverLoadError);
|
||||||
|
}
|
||||||
|
|
||||||
|
for d in &buffer_infos {
|
||||||
|
println!("before {:?}", d);
|
||||||
|
}
|
||||||
|
|
||||||
|
ai::ASIOInit(&mut driver_info);
|
||||||
|
ai::ASIOGetBufferSize(
|
||||||
|
&mut min_b_size,
|
||||||
|
&mut max_b_size,
|
||||||
|
&mut pref_b_size,
|
||||||
|
&mut grans,
|
||||||
|
);
|
||||||
|
result = if pref_b_size > 0 {
|
||||||
|
let mut buffer_info_convert = [
|
||||||
|
mem::transmute::<AsioBufferInfo, ai::ASIOBufferInfo>(buffer_infos[0]),
|
||||||
|
mem::transmute::<AsioBufferInfo, ai::ASIOBufferInfo>(buffer_infos[1]),
|
||||||
|
];
|
||||||
|
let mut callbacks_convert =
|
||||||
|
mem::transmute::<AsioCallbacks, ai::ASIOCallbacks>(callbacks);
|
||||||
|
let buffer_result = ai::ASIOCreateBuffers(
|
||||||
|
buffer_info_convert.as_mut_ptr(),
|
||||||
|
num_channels,
|
||||||
|
pref_b_size,
|
||||||
|
&mut callbacks_convert,
|
||||||
|
);
|
||||||
|
if buffer_result == 0 {
|
||||||
|
let buffer_infos = [
|
||||||
|
mem::transmute::<ai::ASIOBufferInfo, AsioBufferInfo>(buffer_info_convert[0]),
|
||||||
|
mem::transmute::<ai::ASIOBufferInfo, AsioBufferInfo>(buffer_info_convert[1]),
|
||||||
|
];
|
||||||
|
for d in &buffer_infos {
|
||||||
|
println!("after {:?}", d);
|
||||||
|
}
|
||||||
|
println!("channels: {:?}", num_channels);
|
||||||
|
|
||||||
|
return Ok(AsioStream {
|
||||||
|
buffer_infos: buffer_infos,
|
||||||
|
driver: asio_drivers,
|
||||||
|
buffer_size: pref_b_size,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(ASIOError::BufferError(format!(
|
||||||
|
"failed to create buffers,
|
||||||
|
error code: {}",
|
||||||
|
buffer_result
|
||||||
|
)))
|
||||||
|
} else {
|
||||||
|
Err(ASIOError::BufferError(
|
||||||
|
"Failed to get buffer size".to_owned(),
|
||||||
|
))
|
||||||
|
};
|
||||||
|
|
||||||
|
asio_drivers.removeCurrentDriver();
|
||||||
|
ai::destruct_AsioDrivers(&mut asio_drivers);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
pub fn prepare_stream(driver_name: &str) -> Result<AsioStream, ASIOError> {
|
pub fn prepare_stream(driver_name: &str) -> Result<AsioStream, ASIOError> {
|
||||||
//let mut buffer_info = ai::ASIOBufferInfo{_bindgen_opaque_blob: [0u32; 6]};
|
//let mut buffer_info = ai::ASIOBufferInfo{_bindgen_opaque_blob: [0u32; 6]};
|
||||||
let mut buffer_infos = [
|
let mut buffer_infos = [
|
||||||
|
|
|
@ -45,7 +45,7 @@ impl EventLoop {
|
||||||
format: &Format,
|
format: &Format,
|
||||||
) -> Result<StreamId, CreationError> {
|
) -> Result<StreamId, CreationError> {
|
||||||
let stream_type = sys::get_data_type(&device.driver_name).expect("Couldn't load data type");
|
let stream_type = sys::get_data_type(&device.driver_name).expect("Couldn't load data type");
|
||||||
match sys::prepare_stream(&device.driver_name) {
|
match sys::prepare_input_stream(&device.driver_name) {
|
||||||
Ok(stream) => {
|
Ok(stream) => {
|
||||||
{
|
{
|
||||||
*self.asio_stream.lock().unwrap() = Some(stream);
|
*self.asio_stream.lock().unwrap() = Some(stream);
|
||||||
|
@ -76,9 +76,6 @@ impl EventLoop {
|
||||||
$AsioTypeIdent:ident) => {
|
$AsioTypeIdent:ident) => {
|
||||||
// Buffer that is filled by cpal.
|
// Buffer that is filled by cpal.
|
||||||
let mut cpal_buffer: Vec<$SampleType> = vec![0 as $SampleType; cpal_num_samples];
|
let mut cpal_buffer: Vec<$SampleType> = vec![0 as $SampleType; cpal_num_samples];
|
||||||
// Call in block because of mut borrow
|
|
||||||
{
|
|
||||||
}
|
|
||||||
// Function for deinterleaving because
|
// Function for deinterleaving because
|
||||||
// cpal writes to buffer interleaved
|
// cpal writes to buffer interleaved
|
||||||
fn interleave(channels: Vec<Vec<$SampleType>>) -> Vec<$SampleType>{
|
fn interleave(channels: Vec<Vec<$SampleType>>) -> Vec<$SampleType>{
|
||||||
|
@ -96,13 +93,14 @@ impl EventLoop {
|
||||||
// For each channel write the cpal data to
|
// For each channel write the cpal data to
|
||||||
// the asio buffer
|
// the asio buffer
|
||||||
// Also need to check for Endian
|
// Also need to check for Endian
|
||||||
|
|
||||||
for (i, channel) in channels.iter_mut().enumerate(){
|
for (i, channel) in channels.iter_mut().enumerate(){
|
||||||
let buff_ptr = (asio_stream
|
let buff_ptr = (asio_stream
|
||||||
.buffer_infos[i]
|
.buffer_infos[i]
|
||||||
.buffers[index as usize] as *mut $AsioType)
|
.buffers[index as usize] as *mut $AsioType);
|
||||||
.offset(asio_stream.buffer_size as isize * i as isize);
|
//.offset(asio_stream.buffer_size as isize * i as isize);
|
||||||
let asio_buffer: &'static mut [$AsioType] =
|
let asio_buffer: &'static [$AsioType] =
|
||||||
std::slice::from_raw_parts_mut(
|
std::slice::from_raw_parts(
|
||||||
buff_ptr,
|
buff_ptr,
|
||||||
asio_stream.buffer_size as usize);
|
asio_stream.buffer_size as usize);
|
||||||
for asio_s in asio_buffer.iter(){
|
for asio_s in asio_buffer.iter(){
|
||||||
|
@ -112,12 +110,13 @@ impl EventLoop {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// interleave all the channels
|
// interleave all the channels
|
||||||
let inter_buffer = interleave(channels);
|
let mut inter_buffer = interleave(channels);
|
||||||
|
|
||||||
|
|
||||||
let buff = InputBuffer{
|
let buff = InputBuffer{
|
||||||
buffer: &mut cpal_buffer
|
buffer: &mut inter_buffer
|
||||||
};
|
};
|
||||||
callback(
|
callback(
|
||||||
StreamId(count),
|
StreamId(count),
|
||||||
|
@ -264,7 +263,7 @@ pub fn build_output_stream(
|
||||||
sys::AsioSampleType::ASIOSTFloat32LSB => {
|
sys::AsioSampleType::ASIOSTFloat32LSB => {
|
||||||
try_callback!(F32, f32, f32, f32, f32);
|
try_callback!(F32, f32, f32, f32, f32);
|
||||||
}
|
}
|
||||||
sys::AsioSampleType::ASIOSTFloat32LSB => {
|
sys::AsioSampleType::ASIOSTFloat64LSB => {
|
||||||
try_callback!(F32, f32, f32, f64, f64);
|
try_callback!(F32, f32, f32, f64, f64);
|
||||||
}
|
}
|
||||||
_ => println!("unsupported format {:?}", stream_type),
|
_ => println!("unsupported format {:?}", stream_type),
|
||||||
|
|
Loading…
Reference in New Issue