Skip to content

Commit

Permalink
Merge pull request #1 from firefly-zero/file-source
Browse files Browse the repository at this point in the history
File source
  • Loading branch information
orsinium authored Oct 20, 2024
2 parents 704ae77 + 8f89c3a commit 304433d
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "firefly-audio"
version = "0.1.0"
version = "0.2.0"
rust-version = "1.82.0"
edition = "2021"
authors = ["Firefly Zero team"]
description = "Tree-based generator and processor for sound. Powers audio in Firefly Zero."
Expand Down
10 changes: 5 additions & 5 deletions src/basic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ pub struct Frame {
}

impl Frame {
pub(crate) fn zero() -> Self {
pub(crate) const fn zero() -> Self {
Self {
left: Sample::ZERO,
right: None,
}
}

#[must_use]
pub fn mono(s: Sample) -> Self {
pub const fn mono(s: Sample) -> Self {
Self {
left: s,
right: None,
}
}

#[must_use]
pub fn stereo(l: Sample, r: Sample) -> Self {
pub const fn stereo(l: Sample, r: Sample) -> Self {
Self {
left: l,
right: Some(r),
}
}
}

impl Add<&Frame> for Frame {
impl Add<&Self> for Frame {
type Output = Self;

fn add(self, rhs: &Frame) -> Self {
fn add(self, rhs: &Self) -> Self {
let left = self.left + rhs.left;
let right = match (self.right, rhs.right) {
(None, None) => None,
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub enum NodeError {
impl fmt::Display for NodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NodeError::TooManyChildren => write!(f, "the node has too many children"),
NodeError::TooManyNodes => write!(f, "the tree has too many nodes"),
NodeError::UnknownID(id) => write!(f, "there is no node with id {id}"),
Self::TooManyChildren => write!(f, "the node has too many children"),
Self::TooManyNodes => write!(f, "the tree has too many nodes"),
Self::UnknownID(id) => write!(f, "there is no node with id {id}"),
}
}
}
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#![cfg_attr(not(test), no_std)]
#![forbid(unsafe_code)]
#![deny(clippy::pedantic)]
#![deny(
clippy::all,
clippy::pedantic,
clippy::nursery,
clippy::allow_attributes
)]
#![allow(clippy::wildcard_imports)]
// TODO: fix casting warning
#![expect(
// TODO: fix casting warning
clippy::cast_precision_loss,
clippy::module_name_repetitions,
clippy::new_without_default
clippy::new_without_default,
)]
extern crate alloc;

Expand All @@ -15,6 +20,7 @@ mod error;
mod manager;
pub mod modulators;
mod node;
mod pcm;
mod processor;
mod processors;
mod sources;
Expand All @@ -23,6 +29,7 @@ pub use basic_types::*;
pub use error::*;
pub use manager::*;
pub use node::*;
pub use pcm::*;
pub use processor::*;
pub use processors::*;
pub use sources::*;
11 changes: 4 additions & 7 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl Manager {
/// If the parent node is not present, returns [`NodeError::UnknownID`].
/// If there are too many nodes, returns [`NodeError::TooManyChildren`]
/// or [`NodeError::TooManyNodes`].
#[allow(clippy::cast_possible_truncation)]
pub fn add_node(&mut self, parent_id: u32, b: Box<dyn Processor>) -> Result<u32, NodeError> {
const MAX_NODES: usize = 32;
if self.paths.len() >= MAX_NODES {
Expand All @@ -51,6 +50,7 @@ impl Manager {
};
let parent_node = self.root.get_node(parent_path);
let sub_id = parent_node.add(b)?;
#[expect(clippy::cast_possible_truncation)]
let id = self.paths.len() as u32;
let mut path = Vec::new();
path.extend_from_slice(parent_path);
Expand Down Expand Up @@ -134,10 +134,7 @@ impl Manager {
/// Write the given frame (starting from skip index) into the beginning of the buffer.
fn fill_buf(buf: &mut [i16], frame: &Frame, skip: usize) -> usize {
// make iterators over left and right channels
let right = match frame.right {
Some(right) => right,
None => frame.left,
};
let right = frame.right.unwrap_or(frame.left);
let mut left = frame.left.as_array_ref().iter();
let mut right = right.as_array_ref().iter();

Expand All @@ -153,7 +150,7 @@ fn fill_buf(buf: &mut [i16], frame: &Frame, skip: usize) -> usize {
}

let mut written = 0;
#[allow(clippy::cast_possible_truncation)]
#[expect(clippy::cast_possible_truncation)]
for tar in buf.iter_mut() {
let chan = if even { &mut left } else { &mut right };
let Some(s) = chan.next() else { break };
Expand Down Expand Up @@ -202,7 +199,7 @@ mod tests {
assert_eq!(&buf[16..], &[0, 0, 0, 0]);
}

#[allow(clippy::cast_lossless)]
#[expect(clippy::cast_lossless)]
fn u2f(u: u16) -> f32 {
u as f32 / 100.
}
Expand Down
10 changes: 5 additions & 5 deletions src/modulators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Hold {

impl Hold {
#[must_use]
pub fn new(v1: f32, v2: f32, time: u32) -> Self {
pub const fn new(v1: f32, v2: f32, time: u32) -> Self {
Self { v1, v2, time }
}
}
Expand All @@ -54,7 +54,7 @@ pub struct Linear {

impl Linear {
#[must_use]
pub fn new(start: f32, end: f32, start_at: u32, end_at: u32) -> Self {
pub const fn new(start: f32, end: f32, start_at: u32, end_at: u32) -> Self {
Self {
start,
end,
Expand All @@ -78,11 +78,11 @@ impl Modulator for Linear {
}
let elapsed = now - self.start_at;
let ratio = elapsed as f32 / duration as f32;
self.start + (self.end - self.start) * ratio
(self.end - self.start).mul_add(ratio, self.start)
}
}

// Sine wave low-frequency oscillator.
/// Sine wave low-frequency oscillator.
pub struct Sine {
s: f32,
mid: f32,
Expand All @@ -103,7 +103,7 @@ impl Sine {
impl Modulator for Sine {
fn get(&self, now: u32) -> f32 {
let s = F32Ext::sin(self.s * now as f32);
self.mid + self.amp * s
self.amp.mul_add(s, self.mid)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::vec::Vec;

const MODULATE_EVERY: u32 = SAMPLE_RATE / 60;

// A modulator connected to a parameter of a node.
/// A modulator connected to a parameter of a node.
struct WiredModulator {
param: u8,
modulator: Box<dyn Modulator>,
Expand All @@ -28,11 +28,11 @@ impl Node {
}

/// Add a child node.
#[allow(clippy::cast_possible_truncation)]
pub(crate) fn add(&mut self, proc: Box<dyn Processor>) -> Result<u8, NodeError> {
if self.children.len() >= 4 {
return Err(NodeError::TooManyChildren);
}
#[expect(clippy::cast_possible_truncation)]
let child_id = self.children.len() as u8;
let child = Self {
children: Vec::new(),
Expand Down
Loading

0 comments on commit 304433d

Please sign in to comment.