impl Rust: WAV noise generator

In this source, Jon Gjengset demonstrates how to build a WAV audio noise generator from scratch using Rust. The project serves as a deep dive into audio protocols, bit patterns, and digital signal processing.

Core Concepts and Implementation

Project Goals and Audio Basics: The objective is to write a Rust program that generates WAV files containing different "colors" of noise. The project explores the simplest audio representation: Pulse Code Modulation (PCM) in an uncompressed WAV format.

1:15

The Colors of Noise: Noise is categorized into "colors" based on its frequency spectrum. White noise has a flat spectrum, while other colors like pink, brown, and blue vary the intensity across different frequency ranges.

1:39

Sampling Theory: The stream covers the Nyquist-Shannon sampling theorem, which explains that a sampling rate must be at least twice the highest frequency to be captured. This is why 44.1 kHz is standard for human hearing (up to ~20 kHz).

11:30

WAV File Structure (RIFF): WAV files use the Resource Interchange File Format (RIFF). The implementation involves manually constructing the file header, including the fmt (format) chunk and the data chunk, following the 1991 specification.

13:22

Efficient Byte Handling in Rust: To write data structures directly to files without manual serialization, the zero-copy crate is used. This allows for safe casting of Rust structs into byte slices while respecting endianness.

48:08

Signal Processing and Noise Generation

Frequency to Time Domain (FFT): Instead of generating waves directly in the time domain, the project uses Inverse Fast Fourier Transforms (IFFT) to convert frequency-based definitions of noise into playable audio samples.

1:28:44

Phase Randomization: A critical step in generating natural-sounding noise is randomizing the phase of each frequency bin. Without this, all waves align at the start of a sample, creating a sharp, unwanted "click" or impulse.

2:09:17

Implementing Specific Noise Profiles:

Pink Noise: Modeled with power density decreasing by 3 decibels per octave (proportional to 1/f).

2:49:41

Brownian Noise: Also known as red noise, it decreases more sharply at 6 decibels per octave (1/f²), resulting in a much deeper, bass-heavy sound.

3:30:46

Human Perception and A-Weighting: The final part of the implementation touches on psychoacoustics, applying an "A-weighting" curve to account for how the human ear perceives loudness differently across the frequency spectrum.

4:56:45