Browse Source

WIP Error fixing on the Linear Allocator

pull/83/head
Andrea Ciliberti 2 years ago
parent
commit
1a283eb420
  1. 1
      ctru-rs/src/linear.rs
  2. 46
      ctru-rs/src/services/ndsp.rs

1
ctru-rs/src/linear.rs

@ -17,6 +17,7 @@ use std::ptr::NonNull;
/// [`std::alloc::Allocator`] struct for LINEAR memory /// [`std::alloc::Allocator`] struct for LINEAR memory
/// To use this struct the main crate must activate the `allocator_api` unstable feature. /// To use this struct the main crate must activate the `allocator_api` unstable feature.
#[derive(Copy, Clone, Default, Debug)]
pub struct LinearAllocator; pub struct LinearAllocator;
impl LinearAllocator { impl LinearAllocator {

46
ctru-rs/src/services/ndsp.rs

@ -1,8 +1,6 @@
use crate::error::ResultCode; use crate::error::ResultCode;
use crate::linear::LinearAllocator; use crate::linear::LinearAllocator;
use arr_macro::arr;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[repr(u32)] #[repr(u32)]
pub enum OutputMode { pub enum OutputMode {
@ -49,6 +47,10 @@ pub struct WaveInfo<'b> {
raw_data: ctru_sys::ndspWaveBuf, raw_data: ctru_sys::ndspWaveBuf,
} }
pub struct Channel {
id: i32,
}
#[non_exhaustive] #[non_exhaustive]
pub struct Ndsp(()); pub struct Ndsp(());
@ -56,8 +58,6 @@ impl Ndsp {
pub fn init() -> crate::Result<Self> { pub fn init() -> crate::Result<Self> {
ResultCode(unsafe { ctru_sys::ndspInit() })?; ResultCode(unsafe { ctru_sys::ndspInit() })?;
let mut i = 0;
Ok(Self(())) Ok(Self(()))
} }
@ -66,12 +66,12 @@ impl Ndsp {
/// # Errors /// # Errors
/// ///
/// An error will be returned if the channel id is not between 0 and 23. /// An error will be returned if the channel id is not between 0 and 23.
pub fn channel(&self, id: usize) -> crate::Result<Channel> { pub fn channel(&self, id: u8) -> crate::Result<Channel> {
if id > 23 { if id > 23 {
return Err(crate::Error::InvalidChannel); return Err(crate::Error::InvalidChannel);
} }
Ok(self.channels[id]) Ok(Channel { id: id.into() })
} }
/// Set the audio output mode. Defaults to `OutputMode::Stereo`. /// Set the audio output mode. Defaults to `OutputMode::Stereo`.
@ -168,7 +168,11 @@ impl Channel {
} }
impl AudioFormat { impl AudioFormat {
pub fn size(self) -> u8 { /// Returns the amount of bytes needed to store one sample
/// Eg.
/// 8 bit formats return 1 (byte)
/// 16 bit formats return 2 (bytes)
pub fn bytes_size(self) -> u8 {
match self { match self {
AudioFormat::PCM16Mono | AudioFormat::PCM16Stereo => 2, AudioFormat::PCM16Mono | AudioFormat::PCM16Stereo => 2,
AudioFormat::SurroundPreprocessed => { AudioFormat::SurroundPreprocessed => {
@ -181,10 +185,10 @@ impl AudioFormat {
impl WaveBuffer { impl WaveBuffer {
pub fn new(data: Box<[u8], LinearAllocator>, audio_format: AudioFormat) -> crate::Result<Self> { pub fn new(data: Box<[u8], LinearAllocator>, audio_format: AudioFormat) -> crate::Result<Self> {
let nsamples = data.len() / format.size(); let nsamples: usize = data.len() / (audio_format.bytes_size() as usize);
unsafe { unsafe {
ResultCode(ctru_sys::DSP_FlushDataCache(data.as_ptr(), data.len()))?; ResultCode(ctru_sys::DSP_FlushDataCache(data.as_ptr().cast(), data.len().try_into().unwrap()))?;
} }
Ok(WaveBuffer { Ok(WaveBuffer {
@ -198,27 +202,29 @@ impl WaveBuffer {
&mut self.data &mut self.data
} }
pub fn format(&self) -> AudioFormat { pub fn get_format(&self) -> AudioFormat {
self.audio_format self.audio_format
} }
pub fn len(&self) -> usize { pub fn get_sample_amount(&self) -> usize {
self.length self.nsamples
} }
} }
impl<'b> WaveInfo<'b> { impl<'b> WaveInfo<'b> {
pub fn new(buffer: &'b mut WaveBuffer, looping: bool) -> Self { pub fn new(buffer: &'b mut WaveBuffer, looping: bool) -> Self {
let address = ctru_sys::tag_ndspWaveBuf__bindgen_ty_1{ data_vaddr: buffer.data.as_ptr().cast() };
let raw_data = ctru_sys::ndspWaveBuf { let raw_data = ctru_sys::ndspWaveBuf {
__bindgen_anon_1: buffer.data.as_ptr(), // Buffer data virtual address __bindgen_anon_1: address, // Buffer data virtual address
nsamples: buffer.length, nsamples: buffer.nsamples.try_into().unwrap(),
adpcm_data: std::ptr::null(), adpcm_data: std::ptr::null_mut(),
offset: 0, offset: 0,
looping, looping,
// The ones after this point aren't supposed to be setup by the user // The ones after this point aren't supposed to be setup by the user
status: 0, status: 0,
sequence_id: 0, sequence_id: 0,
next: std::ptr::null(), next: std::ptr::null_mut(),
}; };
Self { buffer, raw_data } Self { buffer, raw_data }
@ -236,3 +242,11 @@ impl Drop for Ndsp {
} }
} }
} }
impl Drop for WaveBuffer {
fn drop(&mut self) {
unsafe {
ctru_sys::DSP_InvalidateDataCache(self.data.as_ptr().cast(), self.data.len().try_into().unwrap());
}
}
}

Loading…
Cancel
Save