liveBook · Manning

Audio

As explained in Learn Haskell by Example

Audio data is digital information representing sound through sampled signal values and associated recording parameters.

Audio in a program is sampled sound: a sequence of numeric values measured over time. Mono audio uses one signal; stereo uses two. Sampling approximates a continuous analog signal, so sample rate sets samples per second and bit resolution affects representation accuracy and signal-to-noise ratio. section 10.1

Figure 10.1 Sampling of an analog signal
figure

Sampling turns a continuously changing waveform into discrete values at fixed time intervals. section 10.1

A practical mono representation uses `Sample = Double` and `Signal = [Sample]`, with a 44.1 kHz sample rate. Frequencies (`Hz`) and durations (`Seconds`) are also `Double`; converting a duration or period into a sample count requires rounding because partial samples cannot exist. section 10.1.1

Generate basic synthesized audio by treating a waveform as a function over one normalized period:

  • Define `Wave` as a function from a position between 0 and 1 to a sample value. section 10.1.2
  • Use sine, square, sawtooth, or triangle functions to produce different sound characteristics. section 10.1.2
  • Repeat period positions for the requested duration; faster traversal yields a higher frequency. section 10.1.2
  • Keep initial sample values between -1 and 1, then clamp signals before export to avoid excessive output values. section 10.1.2

Export four waveforms

Generate five-second 220 Hz tones from sine, triangle, sawtooth, and square waves, concatenate them, and write the resulting mono signal to a WAV file. WAV output converts samples to `Int16`, records the sample rate and channel count, and exports the audio data. section 10.1.2

Audio signals can become the output of synthesizer components. An `Oscillator` wraps `Event -> Signal`: silence events create zero-valued samples, while tone events combine a selected waveform, frequency, duration, and ADSR envelope. Multiple oscillators can therefore give the same event distinct timbres. section 10.3.2 section 10.3

How Learn Haskell by Example covers it

How deep it goes
Works from sampling through waveform generation, signal limiting, WAV export, and event-driven oscillators; defers richer filters and modulation.
Examples
Demonstrates four 220 Hz waveforms exported as WAV audio and several waveform-and-ADSR oscillator presets.
What it assumes
Assumes procedural programming, basic algorithms, data structures, operating systems, and file systems; requires no prior functional programming.
What you can do after
Build it from scratch as sampled mono signals, periodic tones, WAV files, and event-driven oscillators.
How it connects
Connects sampling, sample rate, waveforms, WAV files, oscillators, events, and ADSR envelopes.

Also explained in

Compare how 3 books explain audio →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.