Audio is held in the sampleblocks table of the
database, one row per block
of one channel. A waveblock
in the project document refers to a row by blockid. Blocks hold no time
information; their position comes from the document.
Columns
| Column | Meaning |
|---|---|
blockid | The block’s number. New blocks are numbered by SQLite’s AUTOINCREMENT. |
sampleformat | The format of samples; see below |
summin | Smallest sample in the whole block |
summax | Largest sample in the whole block |
sumrms | RMS level of the whole block: sqrt(totalSquares / sampleCount) |
summary256 | A summary of the samples in groups of 256 |
summary64k | A summary in groups of 65,536 |
samples | The raw samples |
samples holds the block’s samples, in the host’s byte order, so
little-endian on the platforms Audacity ships for. Its length divided by the sample size
gives the number of samples in the block.
Sample formats
sampleformat stores Audacity’s sampleFormat value. The value’s
top 16 bits are the sample size in bytes (SAMPLE_SIZE).
| Value (hex) | Value (decimal) | Name | Sample size | Meaning |
|---|---|---|---|---|
0x00020001 | 131,073 | int16Sample | 2 | 16-bit integers |
0x00040001 | 262,145 | int24Sample | 4 | 24-bit integers, each in a 4-byte signed integer holding −8,388,608 to 8,388,607 (the value is not shifted up) |
0x0004000F | 262,159 | floatSample | 4 | 32-bit floating point |
0 | 0 | undefinedSample | 0 | Undefined |
From stored values to audio
Audacity’s sample range is −1.0 to 1.0 as a float (it clips at ±1). A stored integer is
divided by 32,768 (16-bit) or by 8,388,608 (24-bit) to get that value, and a float is
used as it is, so −32,768 is −1.0 and 32,767 is just under 1.0. A format other than
the three above is invalid (Audacity refuses a sequence that names it),
and a block whose byte length is not a multiple of its sample size has a partial last
sample that is dropped. The sequence’s own sampleformat and
effectivesampleformat describe the sequence (the stored format and the format the
audio really has); the block’s sampleformat column is the one that tells how its
bytes are read.
samples blob (int16Sample, 3 samples): 00 00 00 40 00 80
= 0, 16384, −32768 → 0.0, 0.5, −1.0
summary256: 256 frames × 12 bytes = 3,072 bytes
frame 0: min −1.0 (00 00 80 BF) max 0.5 (00 00 00 3F) rms sqrt((0 + 0.25 + 1) / 3) = 0.645497 (4E 3F 25 3F)
frames 1 to 255: min FLT_MAX (FF FF 7F 7F) max −FLT_MAX (FF FF 7F FF) rms 0
summary64k: 1 frame: min −1.0 max 0.5 rms 5.96285 (A6 CF BE 40), see the note below
summin −1.0, summax 0.5, sumrms 0.645497
Summaries
Each summary is a run of frames. A frame is three 32-bit floats, 12 bytes: minimum, maximum, then RMS, in the host’s byte order.
frames64k = (sampleCount + 65535) / 65536, rounded down.frames256 = frames64k × 256.summary256isframes256 × 12bytes, andsummary64kisframes64k × 12bytes.
summary256 covers the samples 256 at a time, and summary64k
combines each 256 entries of it. A block is shorter than the summary’s size, so the
last frames of summary256 are padding: minimum FLT_MAX,
maximum -FLT_MAX and RMS 0, values that do not affect the results. The last
frame in use can also cover fewer than 256 samples. The summaries are computed from the
samples as floats, even when the samples are stored as integers.
Exactly, from CalcSummary, all in 32-bit float arithmetic except where
noted:
- For each group of 256 samples (the last may be shorter, n samples): the
minimum, the maximum, and
sqrt(sumOfSquares / n); the sum of squares is added up as floats. The block’ssumrmsissqrt(Σ sumOfSquares / sampleCount)computed in double precision over all groups. - Each remaining frame of
summary256up toframes256is padding as above; the count of frames in use is 256 less the number of padding frames (used). - For each group of 256 frames of
summary256: the lowest minimum, the highest maximum, andsqrt(Σ rms² / denominator). The denominator is 256 for every group except the last, where it is used minus fraction, with fraction = 1 − n ÷ 256 for the short last group of 256 samples (0 when the last group is full). summinandsummaxare the lowest and highest of thesummary64kframes.
The last summary64k RMS is therefore not the true RMS of its samples. The
RMS of the last, short group of 256 is already an average, but it is counted in the
denominator as only n ÷ 256 of a group, so the figure is too large when the
last group is short (5.96 in the example above, for samples whose RMS is 0.65). A reader that compares summaries with the samples
should reproduce this formula, not the true RMS.
Block sizes
By default a block may hold at most 1,048,576 bytes of samples. With that limit:
- The minimum block size, in samples, is 1,048,576 ÷ sample size ÷ 2.
- The maximum block size is twice the minimum.
Editing can leave a block shorter than the minimum, which is allowed. A block longer than the maximum fails the consistency check.
Silent blocks
Silence takes no room. A block’s blockid of zero or less means a silent
block of that many samples: the value is the negative of the length, and no row of
sampleblocks exists. Silent blocks read as zero samples and zero summaries.