in reply to Sound from Scratch
Followup: Fun with PDL
The Perl code in the repository uses only two non-core modules. The first is Feature::Compat::Class, which in turn pulls in a few others. But this is non-core in some temporary way: With Perl 5.38, the OO system is in core, so at some point the module will be replaced by use feature 'class'. The second one is PDL, the Perl Data Language.
I meant to learn a bit about PDL for a long time, and eventually the sound stuff gave me enough motivation to do it. We are not doing research, but acoustics is a branch of physics, so let's give it a try.
The first nice match between PDL and sound is pulse-code modulation (PCM), a simple method to encode sound digitally. Sound comes as a wave where pressure changes over time, and if you measure the pressure at a not completely arbitrary rate of 44100, you get 44100 data points per second. PCM simply takes these data points as a representation of the sound, and the WAV format is a way to store these in a file. Players on all platforms can play .wav files back as sound.
I use PDL to create the samples by calculation, not by measuring them. Creating one second of a 440Hz sine wave is a one-liner:
use PDL; my $samples = sin(sequence(44100) * 2 * atan2(0,-1) * 440 / 44100);
Of course, this is just the start. A sine wave sounds bland. You can use arbitrary functions to create the samples. Adding sine functions with a multiple of the frequency (overtones) give a richer tone. Changing the amplitude over time ("envelope") can give a sound like plucked strings. Or even like something which is not possible with physical instruments.
PDL also makes it easy to display a waveform (screenshot). If the waveform looks to messy, you can apply Fast Fourier Transform (FFT) to a batch of samples to get a spectrum (screenshot). This spectrum shows sharp peaks because the waveform is a sum of sine functions with frequencies of n*220Hz. (The graphics stuff is not yet in the repository)
While playing with spectra and wave forms, I was accidentally listening to Kaki King's Playing with Pink Noise. This song isn't actually pink noise, but it made me wonder whether it is possible to synthesize noise with different spectral properties using Perl. And of course it is: Fourier transforms are reversible. You can create a spectrum from a batch of samples, and as well you can create a batch of samples from a spectrum. So you create a one-dimensional spectrum to your liking, apply FFT, and the result is noise. There is a bit more math involved (complex numbers), but anyway: The repository now has another program bin/noise to create noise, and also to restrict it to a frequency range.
Noise plus envelopes... these are the first steps to percussions. Let's see where this leads us.
We recently had a thread about learning PDL: For me, playing with sound does the trick quite nicely.
|
|---|