8 unstable releases (3 breaking)

0.4.1 Aug 7, 2023
0.4.0 Aug 7, 2023
0.3.0 Jul 7, 2023
0.2.3 Apr 24, 2023
0.1.0 Apr 19, 2023

#240 in Audio

Download history 32/week @ 2024-07-19 78/week @ 2024-07-26 56/week @ 2024-08-02 59/week @ 2024-08-09 82/week @ 2024-08-16 47/week @ 2024-08-23 60/week @ 2024-08-30 48/week @ 2024-09-06 55/week @ 2024-09-13 101/week @ 2024-09-20 111/week @ 2024-09-27 97/week @ 2024-10-04 98/week @ 2024-10-11 110/week @ 2024-10-18 26/week @ 2024-10-25 43/week @ 2024-11-01

287 downloads per month
Used in uiua

MIT license

81KB
2K SLoC

github crates.io docs.rs

Hodaun

An audio IO and synthesis library for Rust.

Built on top of cpal.

Documentation

Features

  • Audio input and output
  • Statically sized sample frames
  • Basic waveform generators
  • Modular audio processing nodes with the Source trait
  • Audio automation
  • Low-pass filter
  • ADSR envelope
  • Types for working with musical notes

Differences from Rodio

While this library was inspired by Rodio, it was designed to address some of the pain-points of using Rodio in a real application.

Statically sized sample frames

Like Rodio, Hodaun has a Source trait that abstracts audio streams. The main thing that makes Hodaun different from Rodio is that Hodaun's Sources process audio at the frame level rather than the sample level.

In Rodio, Source processes one sample at a time, even if consecutive samples correspond to different channels of the same sample frame. This makes writing custom Sources that are channel-aware difficult.

By contrast, Hodaun's Source trait groups samples in the same frame into a statically-sized array (or just a number for mono audio) with a value for each channel.

Sample rate

In Rodio, the sample rate is a property of a Source. This can be weird when working with synthesized audio. What is the sample rate of a sine wave? Who cares? It's a mathematical function with inifinite resolution.

In Hodaun, the sample rate is passed to Source, and it is up to the Source to decide what to do with it. In practice, this means that the sample rate of Sources adapts to the sample rate of the audio output device.

Sources that can have various possible sample rates, like audio input devices or audio files, implement UnrolledSource instead of Source. This trait corresponds more closely to Rodio's Source trait. An UnrolledSource can be converted to a Source using UnrolledSource::resample.

External control and automation

Rodio's Source trait lets you do many basic transformations on audio streams, but once the sound is playing, the parameters used for these transformations cannot be changed. For example rodio::Source::amplify takes an f32 that changes the volume of the Source.

But what if you want to change the volume of the Source while it's playing? This is common in basically any application that plays audio.

Hodaun also has Source::amplify, but it takes a value implementing the Automation trait. This can be a static number just like Rodio, but it can also be an externally-controlled value modified elsewhere in your code, or it can even be another Source that changes the value over time.

Dependencies

~0.4–29MB
~428K SLoC