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

#152 in Audio

Download history 113/week @ 2023-12-14 80/week @ 2023-12-21 21/week @ 2023-12-28 21/week @ 2024-01-04 32/week @ 2024-01-11 25/week @ 2024-01-18 36/week @ 2024-01-25 71/week @ 2024-02-01 163/week @ 2024-02-08 136/week @ 2024-02-15 208/week @ 2024-02-22 224/week @ 2024-02-29 158/week @ 2024-03-07 79/week @ 2024-03-14 82/week @ 2024-03-21 72/week @ 2024-03-28

406 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–36MB
~473K SLoC