Browse Source

Use more appropriate types (unsiged integers for counts)

Signed 32-bit integers `i32` were used because `c_int` is defined as
this. There were also less type casts but more range checks, though.
fixes
Andreas Linz 5 years ago
parent
commit
5218884b0b
6 changed files with 57 additions and 66 deletions
  1. +2
    -2
      examples/sine.rs
  2. +41
    -49
      src/base.rs
  3. +2
    -2
      src/lib.rs
  4. +10
    -10
      src/stream.rs
  5. +1
    -2
      tests/basic.rs
  6. +1
    -1
      tests/stream.rs

+ 2
- 2
examples/sine.rs View File

@ -31,8 +31,8 @@ fn main() {
// register callbacks
out.register_write_callback(Box::new(|out: rsoundio::OutStream,
min_frame_count: i32,
max_frame_count: i32| {
min_frame_count: u32,
max_frame_count: u32| {
let l: Vec<f32> = samples.iter()
.cycle()
.take(max_frame_count as usize + pos)

+ 41
- 49
src/base.rs View File

@ -5,6 +5,8 @@ use std::ffi::CString;
use ffi;
use stream::OutStream;
const MAX_CHANNELS: u32 = 24;
/// Result wrapper that always contains a `ffi::enums::SioError` in error case.
pub type SioResult<T> = Result<T, ffi::enums::SioError>;
@ -23,13 +25,8 @@ impl SoundIo {
}
/// Returns the number builtin channel layouts.
pub fn channel_layout_builtin_count() -> i32 {
let cnt = unsafe { ffi::soundio_channel_layout_builtin_count() };
if cnt < 0 {
panic!("Negative # of builtin channel layouts!")
} else {
cnt as i32
}
pub fn channel_layout_builtin_count() -> u32 {
unsafe { ffi::soundio_channel_layout_builtin_count() as u32 }
}
// NOTE: Links to other types in rustdoc are not implemented
@ -69,20 +66,15 @@ impl SoundIo {
}
/// Returns the number of available backens.
pub fn backend_count(&self) -> i32 {
let cnt = unsafe { ffi::soundio_backend_count(self.context) } as i32;
if cnt < 0 {
panic!("Negative backend count!");
} else {
cnt
}
pub fn backend_count(&self) -> u32 {
unsafe { ffi::soundio_backend_count(self.context) as u32 }
}
/// Returns a backend at the specified index.
/// If the index is not in range [0, backend_count), then
/// `None` is returned.
pub fn backend(&self, idx: i32) -> Option<ffi::enums::SioBackend> {
match unsafe { ffi::soundio_get_backend(self.context, idx) } {
pub fn backend(&self, idx: u32) -> Option<ffi::enums::SioBackend> {
match unsafe { ffi::soundio_get_backend(self.context, idx as c_int) } {
ffi::enums::SioBackend::None => None,
backend @ _ => Some(backend),
}
@ -158,26 +150,26 @@ impl SoundIo {
/// `wait_events` to block until devices change. If an error occurs
/// scanning devices in a background thread, `backend_disconnect` is called
/// with the error code.
///
/// Get the number of input devices.
/// Returns -1 if you never called `flush_events`.
pub fn input_device_count(&self) -> Option<i32> {
/// Returns `None` if you never called `flush_events`.
pub fn input_device_count(&self) -> Option<u32> {
let cnt = unsafe { ffi::soundio_input_device_count(self.context) };
if cnt < 0 {
None
} else {
Some(cnt as i32)
Some(cnt as u32)
}
}
/// Get the number of output devices.
/// Returns -1 if you never called `flush_events`.
pub fn output_device_count(&self) -> Option<i32> {
/// Returns `None` if you never called `flush_events`.
pub fn output_device_count(&self) -> Option<u32> {
let cnt = unsafe { ffi::soundio_output_device_count(self.context) };
if cnt < 0 {
None
} else {
Some(cnt as i32)
Some(cnt as u32)
}
}
@ -185,8 +177,8 @@ impl SoundIo {
/// `idx` must be in [0, `input_device_count`)
/// Returns `None` if you never called `flush_events` or if you provide
/// invalid parameter values.
pub fn input_device(&self, idx: i32) -> Option<Device> {
let dev_ptr = unsafe { ffi::soundio_get_input_device(self.context, idx) };
pub fn input_device(&self, idx: u32) -> Option<Device> {
let dev_ptr = unsafe { ffi::soundio_get_input_device(self.context, idx as c_int) };
if dev_ptr.is_null() {
None
} else {
@ -198,8 +190,8 @@ impl SoundIo {
/// `idx` must be in [0, `output_device_count`)
/// Returns `None` if you never called `flush_events` or if you provide
/// invalid parameter values.
pub fn output_device(&self, idx: i32) -> Option<Device> {
let dev_ptr = unsafe { ffi::soundio_get_output_device(self.context, idx) };
pub fn output_device(&self, idx: u32) -> Option<Device> {
let dev_ptr = unsafe { ffi::soundio_get_output_device(self.context, idx as c_int) };
if dev_ptr.is_null() {
None
} else {
@ -207,23 +199,23 @@ impl SoundIo {
}
}
/// Returns the index of the default input device or `-1`
/// Returns the index of the default input device or `None`
/// if there are no devices or if you never called
/// `flush_events`.
pub fn default_input_device_index(&self) -> Option<i32> {
pub fn default_input_device_index(&self) -> Option<u32> {
match unsafe { ffi::soundio_default_input_device_index(self.context) } {
-1 => None,
idx @ _ => Some(idx as i32),
idx @ _ => Some(idx as u32),
}
}
/// Returns the index of the default output device or `-1`
/// Returns the index of the default output device or `None`
/// if there are no devices or if you never called
/// `flush_events`.
pub fn default_output_device_index(&self) -> Option<i32> {
pub fn default_output_device_index(&self) -> Option<u32> {
match unsafe { ffi::soundio_default_output_device_index(self.context) } {
-1 => None,
idx @ _ => Some(idx as i32),
idx @ _ => Some(idx as u32),
}
}
@ -280,8 +272,8 @@ impl ChannelLayout {
/// Returns a builtin channel layout or `None` if
/// `idx` *not* in [0, `SoundIo::channel_layout_builtin_count`).
pub fn builtin(idx: i32) -> Option<Self> {
if 0 <= idx && idx < SoundIo::channel_layout_builtin_count() {
pub fn builtin(idx: u32) -> Option<Self> {
if idx < SoundIo::channel_layout_builtin_count() as u32 {
Some(ChannelLayout::new(unsafe {
ffi::soundio_channel_layout_get_builtin(idx as c_int)
}))
@ -291,21 +283,21 @@ impl ChannelLayout {
}
/// Get the default builtin channel layout for the given number of channels.
pub fn default(channel_count: i32) -> Option<Self> {
if channel_count < 0 {
None
} else {
pub fn default(channel_count: u32) -> Option<Self> {
if channel_count < MAX_CHANNELS {
Some(ChannelLayout::new(unsafe {
ffi::soundio_channel_layout_get_default(channel_count as i32)
ffi::soundio_channel_layout_get_default(channel_count as c_int)
}))
} else {
None
}
}
/// Return the index of `channel` in the layout, or `None` if not found.
pub fn find_channel(&self, channel: ffi::enums::SioChannelId) -> Option<i32> {
pub fn find_channel(&self, channel: ffi::enums::SioChannelId) -> Option<u32> {
match unsafe { ffi::soundio_channel_layout_find_channel(self.layout, channel) } {
-1 => None,
idx @ _ => Some(idx),
idx @ _ => Some(idx as u32),
}
}
@ -346,8 +338,8 @@ impl ChannelLayout {
}
/// Returns the number of channels in the layout.
pub fn channel_count(&self) -> i32 {
unsafe { (*self.layout).channel_count as i32 }
pub fn channel_count(&self) -> u32 {
unsafe { (*self.layout).channel_count as u32 }
}
}
impl PartialEq for ChannelLayout {
@ -411,7 +403,7 @@ impl Device {
/// Convenience function.
/// Returns whether `sample_rate` is included in the
/// device's supported sample rates.
pub fn supports_sample_rate(&self, sample_rate: i32) -> bool {
pub fn supports_sample_rate(&self, sample_rate: u32) -> bool {
unsafe {
ffi::soundio_device_supports_sample_rate(self.device, sample_rate as c_int) == 1u8
}
@ -420,8 +412,8 @@ impl Device {
/// Convenience function.
/// Returns the available sample rate nearest to
/// `sample_rate`, rounding up.
pub fn nearest_sample_rate(&self, sample_rate: i32) -> i32 {
unsafe { ffi::soundio_device_nearest_sample_rate(self.device, sample_rate) as i32 }
pub fn nearest_sample_rate(&self, sample_rate: u32) -> u32 {
unsafe { ffi::soundio_device_nearest_sample_rate(self.device, sample_rate as c_int) as u32 }
}
/// Returns an OutStream struct with default settings.
@ -437,8 +429,8 @@ impl Device {
}
/// Returns the number of references on this device.
pub fn ref_count(&self) -> i32 {
unsafe { (*self.device).ref_count as i32 }
pub fn ref_count(&self) -> u32 {
unsafe { (*self.device).ref_count as u32 }
}
/// This is set to a `ffi::enums::SioError` representing the result of the device

+ 2
- 2
src/lib.rs View File

@ -13,8 +13,8 @@
//! let dev = sio.default_output_device().unwrap();
//! let mut out = dev.create_outstream().unwrap();
//! // register write_callback
//! out.register_write_callback(Box::new(|out: rsoundio::OutStream, min_frame_count: i32,
//! max_frame_count: i32| {
//! out.register_write_callback(Box::new(|out: rsoundio::OutStream, min_frame_count: u32,
//! max_frame_count: u32| {
//! let frames = vec![vec![], vec![]];
//! // frames must contain audio data for each channel
//! out.write_stream_f32(min_frame_count, &frames).unwrap();

+ 10
- 10
src/stream.rs View File

@ -19,7 +19,7 @@ macro_rules! write_stream {
/// than `min_frame_count`, or less buffers
/// as `channel_count` are provided,
/// then a `ffi::enums::SioError::Invalid` is returned.
pub fn $name(&self, min_frame_count: i32, buffers: &Vec<Vec<$t>>) -> SioResult<i32> {
pub fn $name(&self, min_frame_count: u32, buffers: &Vec<Vec<$t>>) -> SioResult<u32> {
let channel_count = self.layout().channel_count();
// check if buffer contains frames for all channels
if buffers.len() < channel_count as usize {
@ -48,12 +48,12 @@ macro_rules! write_stream {
}
extern "C" fn write_wrapper<W>(raw_out: *mut ffi::SoundIoOutStream, min: c_int, max: c_int)
where W: FnMut(OutStream, i32, i32)
where W: FnMut(OutStream, u32, u32)
{
let out = OutStream::new(raw_out);
let callbacks_ptr = unsafe { (*out.stream).userdata as *mut Box<OutStreamCallbacks> };
let callbacks: &mut Box<OutStreamCallbacks> = unsafe { &mut *callbacks_ptr };
callbacks.write.as_mut().map(|f| f(out, min as i32, max as i32));
callbacks.write.as_mut().map(|f| f(out, min as u32, max as u32));
}
extern "C" fn underflow_wrapper<U>(raw_out: *mut ffi::SoundIoOutStream)
@ -75,7 +75,7 @@ extern "C" fn error_wrapper(raw_out: *mut ffi::SoundIoOutStream, error: ffi::
}
struct OutStreamCallbacks<'a> {
write: Option<Box<FnMut(OutStream, i32, i32) + 'a>>,
write: Option<Box<FnMut(OutStream, u32, u32) + 'a>>,
underflow: Option<Box<FnMut(OutStream) + 'a>>,
error: Option<Box<FnMut(OutStream, ffi::enums::SioError) + 'a>>,
}
@ -175,7 +175,7 @@ impl<'a> OutStream<'a> {
/// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select,
/// pthread_join, pthread_cond_wait, etc.
pub fn register_write_callback<W>(&mut self, callback: Box<W>)
where W: FnMut(OutStream, i32, i32) + 'a
where W: FnMut(OutStream, u32, u32) + 'a
{
// stored box reference to callback closure
self.callbacks.write = Some(callback);
@ -235,15 +235,15 @@ impl<'a> OutStream<'a> {
fn begin_write(&self,
areas: *mut *mut ffi::SoundIoChannelArea,
frame_count: &i32)
-> SioResult<i32> {
frame_count: &c_int)
-> SioResult<u32> {
let mut actual_frame_count = *frame_count as c_int;
match unsafe {
ffi::soundio_outstream_begin_write(self.stream,
areas,
&mut actual_frame_count as *mut c_int)
} {
ffi::enums::SioError::None => Ok(actual_frame_count),
ffi::enums::SioError::None => Ok(actual_frame_count as u32),
err @ _ => Err(err),
}
}
@ -378,8 +378,8 @@ impl<'a> OutStream<'a> {
}
/// Returns the sample rate of the output stream.
pub fn sample_rate(&self) -> i32 {
unsafe { (*self.stream).sample_rate as i32 }
pub fn sample_rate(&self) -> u32 {
unsafe { (*self.stream).sample_rate as u32 }
}
/// Returns the underlying device of the output stream.

+ 1
- 2
tests/basic.rs View File

@ -18,7 +18,6 @@ fn test_soundio() {
.unwrap();
sio.disconnect();
}
assert!(rsoundio::SoundIo::channel_layout_builtin_count() >= 0);
sio.connect().unwrap();
sio.flush_events();
assert!(sio.output_device_count().unwrap() > 0);
@ -29,7 +28,7 @@ fn test_soundio() {
fn test_channel_layout() {
let cnt = rsoundio::SoundIo::channel_layout_builtin_count();
assert!(cnt > 0);
assert!(rsoundio::ChannelLayout::builtin(-1).is_none());
assert!(rsoundio::ChannelLayout::builtin(100).is_none());
assert_eq!(rsoundio::ChannelLayout::builtin(0),
rsoundio::ChannelLayout::builtin(0));
let mut layout = rsoundio::ChannelLayout::default(2).unwrap();

+ 1
- 1
tests/stream.rs View File

@ -23,7 +23,7 @@ fn test_outstream() {
assert!(stream.sample_rate() > 0);
let layout = stream.layout();
assert_eq!(layout.channel_count(), 2);
let cb = |out: rsoundio::OutStream, min_frame_count: i32, max_frame_count: i32| {
let cb = |out: rsoundio::OutStream, min_frame_count: u32, max_frame_count: u32| {
let l: Vec<f32> = (0..max_frame_count as usize)
.map(|i| {
match i % 2 == 0 {

Loading…
Cancel
Save