Sound as a clip of samples: cutting, mixing, measuring and converting it.
import audio
This package is the middle of everything to do with sound in Salam. A Clip holds the sound itself: plain 32 bit floating point samples between -1 and 1, one frame after another, with the channels side by side. Reading and writing files is the job of the format packages, audio.wav today, and audio.codec opens any of them without being told which. Filters, a Fourier transform and careful resampling live in audio.dsp.
What is here is the clip and everything that does not need to know a file format: cutting, joining, mixing, fading, measuring how loud something is, moving between mono and stereo and between sample rates, and the most significant bit first bitstream reader that every audio codec is written on top of.
Things to know
•
Samples are 32 bit floats. Every clip owns that memory; release it with Free.
•
Because a float carries 24 bits of mantissa, 8, 16 and 24 bit sound makes the round trip through a clip exactly. Only a true 32 bit integer source loses its lowest 8 bits.
•
A three minute stereo song at 44100 is about 63 MB as a clip, so decode a long file in pieces rather than all at once.
•
bits remembers the depth a clip was read at, so writing it again keeps that depth. A clip made with New has no depth, and an encoder then picks 16 bit.
•
Anything that fails returns an empty clip rather than stopping the program, so check with IsEmpty.
•
MsbReader reads the most significant bit first. The BitReader in compress reads the other way round, for DEFLATE, and is not interchangeable with it.
A clip of sound
A clip is a rate, a channel count, a frame count and the samples. New gives you silence to fill in; SetSample and Sample reach one sample at a time by frame and channel. A frame is one sample for every channel, so a stereo clip of 100 frames holds 200 samples.
Every clip holds memory of its own, so release each one with Free. Reading or writing outside the clip is not an error: Sample gives 0 and SetSample does nothing, which keeps loops at the edges simple.
How loud is it
Peak is the furthest any single sample gets from zero and RMS follows loudness as an ear hears it. Both have a decibel form, where 0 dB is as loud as a sample can be and everything quieter is negative. GainToDb and DbToGain move between a plain multiplier and decibels.
Silence has no decibel value, so GainToDb(0.0) returns SilenceFloorDb instead of running off to minus infinity. FirstSound and LastSound find where the sound really starts and stops, which is what TrimSilence is built from.
Cutting and joining
Slice and Trim take a piece out, by frame or by time; Concat puts one clip after another and Mix lays them on top of each other. Normalize scales a clip so its peak lands exactly where you ask, and the two fades shape the ends so a cut does not click.
Joining and mixing need both clips to agree on the sample rate and the channel count, and return an empty clip when they do not. Use ToRate and ToChannels first to bring them into line.
Rates, channels and depths
ToMono averages the channels together and ToStereo spreads one channel across two. ToRate changes the sample rate by interpolating between samples, which is quick and good enough for speech; for music use ResampleSinc from audio.dsp. ToBits does not write a file, it rounds the samples to the steps of a depth so you can hear what that depth will cost.
Deinterleave hands back one clip per channel, each of which you must free, and Interleave weaves them back together.
Which format is this
DetectFormat looks at the first few bytes and FormatFromExt at the file name. Both answer with a Format number, which FormatName turns into a short name. Knowing the format does not mean this build can decode it: ask CanDecode in audio.codec for that.
An Info is what a file says about itself, which the format packages fill in by reading a header rather than the whole file. It is the cheap way to show a length or a bit rate in a listing.
Reading a bitstream
Compressed audio is not laid out in bytes, it is laid out in bits, and every format from MP3 to FLAC counts those bits from the most significant end. MsbReader does that, with MsbReadSigned for two's complement fields and MsbUnary for the run of zeros that Rice and Golomb codes are built from. MsbWriter is the same thing in reverse.
A read past the end does not stop the program: it hands back zeros and sets bad, which stays set, so one check after a group of reads is enough. Note again that compress has a bit reader of its own that runs the other way round, for DEFLATE; do not use it for audio.
The formats this family knows by name. DetectFormat and FormatFromExt answer with one of these; whether this build can actually read one is a separate question for CanDecode in audio.codec.
Fills a clip from raw integer PCM at an offset, at any of the four depths, either byte order. 8 bit is unsigned in WAVE and signed in AIFF and au, which is what the last argument picks.
The example for this item is shown with the ones above: audio-Pcm.salam