Issue
How to generate sounds in Perl on Debian 9?
Own research done, not much found. Eg. Audio::NoiseGen, which has problems with dependencies (namely Audio::PortAudio, which in turn is old and doesn't accept portaudio-2.0
on my Debian 9).
I mean sound synthesis, not playing files. The closest to what I'm looking for is using the system
command to call the sox
program. But that's reaching outside of Perl, so not a good integration. And I need a good one like a Perl module.
Solution
audio is just a curve, so code which makes a point wobble up and down and is sampled at some interval of time will synthesize audio (raw audio in PCM format) ... here perl generates such a curve
perl -e 'for ($c=0; $c<4*44100; $c++) {
$k=1*sin((1500+$c/16e1)*$c*22e-6); print pack "f", $k;
} ' |
sox -V -r 44100 -c 1 -b 32 -e floating-point -t raw - \
-c 2 -b 16 -t wav - trim 0 3 gain -1 dither |
tee test.wav |
aplay
output from perl is piped into sox which effectively just adds a wav format header to the PCM audio ... then the wav file is piped into tee which outputs a wav file while piping to aplay which renders the audio ... for the benefit of others this installs needed debian (ubuntu) packages
sudo apt-get install sox alsa-utils
now just save above code into a file and source it ... vi somefile-with-above-code
source somefile-with-above-code
its handy to separate the process of audio synthesis from its sonic rendering so above division of labor is often justified
Alternatively you can use perl to synthesize the raw audio and dump it directly to a file yourself ... just save following perl snippet into a file vi my-audio-perl-file
perl -e 'for ($c=0; $c<4*44100; $c++) {
$k=1*sin((1500+$c/16e1)*$c*22e-6); print pack "f", $k;
} '
then execute it redirecting output to a file using
source ./my-audio-perl-file > synth_audio.pcm
then use Audacity to visualize and render the PCM file ... File -> Import Raw Data
-> Encoding -> 32 bit float
-> Byte order -> Little-endian
-> Channels -> 1 Channel (Mono)
and hit Import
Answered By - Scott Stensland