Sound from Scratch

May 2025

Sound is Pressure Change

Perl's Part in the Game

Let's create the data with Perl

Our Data Format: LPCM

Main Ingredients

Low Level Interface

?

The play Method

(some details omitted)

?

Inspect the Wave

?

Analyze Sound

?

The spectrum method uses PDL::FFT (contained in the PDL distribution) to do Fourier transformations

... where the fun happens

Let's try other periodic functions

More fun …

A function generating all overtones

Noise...

...via Aoede

For Music: Keep it Simple

Add overtones

Polytonality

You can only add notes of equal length!

Improving...

Add a volume envelope

Envelope Parameters

More Features...

Effects

Creating Longer Sound

Tuning the API

Using a Data File

Sort of like MIDI, but plain text

Even Less Code!

Many Distractions...

Interactive Sound

Real-time Frequency Analysis

Also, sort of a music player...

Perlcussion

  • Noise, envelopes and optionally mix in sine waves

MIDI

  • ...does not exactly make sound from scratch
  • Many CPAN modules write MIDI files: MIDI::* Music::Drummer and many more
  • The MIDI sound is defined in SoundFont files
    • I have implemented some SF2 parameters (no modulators, reverb, chorus...)
    • Here's an example from MIDI::Chord::Guitar :

Lessons Learned

  • Perl's core OO is sufficient for all my needs
    • Debugging support is missing. I cheat by falling back to Object::Pad
  • The maintainers of PDL and Prima are very helpful
    • Both have added stuff I could immediately use
  • ChatGPT knows about PDL::PP
    • It takes a careful review to fix its suggestions, though.

References