A movie has two kinds of sound. An event sound is defined once with DefineSound and started by StartSound, a button or a script. A streaming sound belongs to a timeline: SoundStreamHead gives its format, and each frame carries its share in a SoundStreamBlock, so it plays in step with the frames. The explorer shows the sound data field by field and plays uncompressed, ADPCM and MP3 sound.
Sound formats
Every sound tag starts with a 4-bit format, a 2-bit rate, a size bit and a channel bit. This format has four sound formats:
| SoundFormat | Coding | From SWF |
|---|---|---|
| 0 | Uncompressed, in the byte order of the machine that wrote it | 1 |
| 1 | ADPCM | 1 |
| 2 | MP3 | 4 |
| 3 | Uncompressed, little-endian | 4 |
| Field | Values |
|---|---|
| SoundRate, UB[2] | 0 5.5 kHz, 1 11 kHz, 2 22 kHz, 3 44 kHz: 5,512, 11,025, 22,050 and 44,100 Hz. 5.5 kHz is not allowed for MP3 |
| SoundSize, UB[1] | 0 8-bit, 1 16-bit; it matters only for uncompressed formats, as compressed ones always decode to 16 bits |
| SoundType, UB[1] | 0 mono, 1 stereo |
8-bit samples are unsigned and 16-bit samples signed. Format 0’s byte order is that of the machine that made the file, so 16-bit data in it is ambiguous; the specification says to use format 3 instead. Nothing in the file records the order, so a reader has to choose one: the explorer reads format 0 as little-endian, as Ruffle does. Later versions added Nellymoser and Speex.
DefineSound (14)
| Type | Name | Meaning |
|---|---|---|
| UI16 | SoundId | The character ID |
| UB[4] | SoundFormat | See sound formats |
| UB[2] | SoundRate | The sample rate |
| UB[1] | SoundSize | 8 or 16-bit |
| UB[1] | SoundType | Mono or stereo |
| UI32 | SoundSampleCount | Samples, per channel |
| bytes | SoundData | The sound, to the end of the tag, laid out by its format: see sound data |
StartSound (15)
| Type | Name | Meaning |
|---|---|---|
| UI16 | SoundId | The sound to start or stop |
| SOUNDINFO | SoundInfo | How to play it |
SOUNDINFO
| Type | Name | Meaning |
|---|---|---|
| UB[2] | Reserved | 0 |
| UB[1] | SyncStop | Stop the sound |
| UB[1] | SyncNoMultiple | Do not start it if it is already playing |
| UB[1] | HasEnvelope | An envelope follows |
| UB[1] | HasLoops | A loop count follows |
| UB[1] | HasOutPoint | An out point follows |
| UB[1] | HasInPoint | An in point follows |
| UI32 | InPoint | If HasInPoint: samples to skip at the start |
| UI32 | OutPoint | If HasOutPoint: the position of the last sample to play |
| UI16 | LoopCount | If HasLoops: how many times to play it |
| UI8 | EnvPoints | If HasEnvelope: the number of envelope points |
| SOUNDENVELOPE × EnvPoints | EnvelopeRecords | Each UI32 Pos44 (samples at 44 kHz), UI16 LeftLevel, UI16 RightLevel (0 to 32,768) |
The specification gives InPoint and OutPoint only “in samples”; Ruffle, which matches Flash Player, counts them at 44 kHz like Pos44. A mono sound should have the same left and right levels; if they differ they are averaged.
SoundStreamHead (18)
| Type | Name | Meaning |
|---|---|---|
| UB[4] | Reserved | 0 |
| UB[2] | PlaybackSoundRate | The rate to play at |
| UB[1] | PlaybackSoundSize | Always 1, 16-bit |
| UB[1] | PlaybackSoundType | Mono or stereo |
| UB[4] | StreamSoundCompression | The format of the blocks: 1 ADPCM, or from SWF 4 2 MP3 |
| UB[2] | StreamSoundRate | The rate of the data |
| UB[1] | StreamSoundSize | Always 1, 16-bit |
| UB[1] | StreamSoundType | Mono or stereo |
| UI16 | StreamSoundSampleCount | The average number of samples in each SoundStreamBlock, per channel |
| SI16 | LatencySeek | Only when StreamSoundCompression is 2 (MP3): should equal the SeekSamples of the first block |
A timeline with streaming sound has one SoundStreamHead or SoundStreamHead2 before its first SoundStreamBlock. The main timeline plays one streaming sound at a time, but each sprite can have its own. The three Playback fields are advisory, and Flash Player may ignore them.
SoundStreamHead2 (45, SWF 3)
The same fields as SoundStreamHead, but StreamSoundCompression may be any sound format, and the two Size fields may be 0 (8-bit) or 1 (16-bit). StreamSoundSize may be 8-bit only for uncompressed formats, and StreamSoundRate may not be 5.5 kHz for MP3.
SoundStreamBlock (19)
One frame’s share of the stream, the whole body (the specification asks for a long record header), laid out by the format that the stream’s head gives: see sound data. There is at most one in each frame. For ADPCM it is, as far as is known, a complete ADPCMSOUNDDATA with its own code size.
For smooth playback, the specification asks for a block in every frame, each with about the sample rate ÷ the frame rate samples. MP3 blocks hold whole MP3 frames only, so their sample counts are multiples of 576 or 1,152 and vary from block to block; a frame that should get less than one MP3 frame gets a block with a SampleCount and SeekSamples of 0 and no MP3 data. Each block’s SeekSamples is how far the ideal sample count at the end of the previous frame runs ahead of the samples in the blocks before this one, which is where playback starts after a jump to that frame. The first block’s SeekSamples is instead the encoder’s latency, which Flash Player adds to the SeekSamples of any later block it seeks to.
Sound data
What DefineSound’s SoundData and a SoundStreamBlock’s body hold, by SoundFormat:
| SoundFormat | DefineSound | SoundStreamBlock |
|---|---|---|
| 0, 3 | Samples, left and right interleaved for stereo; 8-bit samples unsigned, 16-bit signed | |
| 1 | ADPCMSOUNDDATA | |
| 2 | MP3SOUNDDATA | MP3STREAMSOUNDDATA |
MP3 (SWF 4)
MP3 data is ordinary MPEG audio Layer III frames (ISO/IEC 11172-3 and 13818-3), MPEG-1, 2 or 2.5, with a constant or a variable bit rate. The frames carry their own sample rate and channel mode, which a player follows. An MPEG-1 frame holds 1,152 samples, an MPEG-2 or 2.5 frame 576. The explorer lists every frame’s header fields and decodes the frames to play the sound.
MP3SOUNDDATA and MP3STREAMSOUNDDATA
| Type | Name | Meaning |
|---|---|---|
| SI16 | SeekSamples | Samples to skip at the start of the frames: in a stream, see SoundStreamBlock; in an event sound, only the encoder’s initial latency |
| MP3FRAME[] | Mp3Frames | Zero or more frames, to the end of the data |
A SoundStreamBlock holds MP3STREAMSOUNDDATA, which puts one field before that:
| Type | Name | Meaning |
|---|---|---|
| UI16 | SampleCount | The samples in this block, per channel |
| MP3SOUNDDATA | Mp3SoundData | SeekSamples and the frames, as above |
MP3FRAME
Bits, most significant first; the header is 4 bytes, big-endian.
| Type | Name | Meaning |
|---|---|---|
| UB[11] | Syncword | All 11 bits set |
| UB[2] | MpegVersion | 0 MPEG-2.5, 1 reserved, 2 MPEG-2, 3 MPEG-1 |
| UB[2] | Layer | 0 reserved, 1 Layer III, 2 Layer II, 3 Layer I; always 1 in SWF |
| UB[1] | ProtectionBit | 0 a CRC follows the header, 1 none |
| UB[4] | Bitrate | An index into the bit-rate table |
| UB[2] | SamplingRate | An index into the sample-rate table |
| UB[1] | PaddingBit | 1 the frame holds one extra byte |
| UB[1] | Reserved | The MPEG private bit, unused |
| UB[2] | ChannelMode | 0 stereo, 1 joint stereo, 2 dual channel, 3 mono |
| UB[2] | ModeExtension | For joint stereo in Layer III: bit 1 (value 2) M/S stereo, bit 0 (value 1) intensity stereo |
| UB[1] | Copyright | 1 copyrighted |
| UB[1] | Original | 1 original, 0 a copy |
| UB[2] | Emphasis | 0 none, 1 50/15 µs, 2 reserved, 3 CCITT J.17 |
| UB[16] | CRC | If ProtectionBit is 0: a CRC-16 of the header’s last 2 bytes and the side information |
| bytes | SampleData | The side information and the Huffman-coded samples, to the end of the frame |
A frame is (MPEG-1 ? 144 : 72) × bit rate in bit/s ÷ sample rate bytes, rounded down, plus PaddingBit. SampleData is that less the 4 header bytes and, with a CRC, 2 more. Bitrate 0 is the free format, whose length the header does not give.
| Bitrate | MPEG-1, kbit/s | MPEG-2 and 2.5, kbit/s |
|---|---|---|
| 0 | Free format | Free format |
| 1 | 32 | 8 |
| 2 | 40 | 16 |
| 3 | 48 | 24 |
| 4 | 56 | 32 |
| 5 | 64 | 40 |
| 6 | 80 | 48 |
| 7 | 96 | 56 |
| 8 | 112 | 64 |
| 9 | 128 | 80 |
| 10 | 160 | 96 |
| 11 | 192 | 112 |
| 12 | 224 | 128 |
| 13 | 256 | 144 |
| 14 | 320 | 160 |
| 15 | Not allowed | Not allowed |
| SamplingRate | MPEG-1, Hz | MPEG-2, Hz | MPEG-2.5, Hz |
|---|---|---|---|
| 0 | 44,100 | 22,050 | 11,025 |
| 1 | 48,000 | 24,000 | 12,000 |
| 2 | 32,000 | 16,000 | 8,000 |
| 3 | Reserved | Reserved | Reserved |
The bit rates are those of Layer III. SampleData is decoded as ISO/IEC 11172-3 sets out for Layer III: side information, scale factors, Huffman-coded spectral values, requantisation, stereo processing, anti-aliasing, the inverse MDCT and the polyphase filter bank. See MP3 for the frame body.
The specification’s own table has no CRC row: it gives SampleData’s size as the frame length less 4, so a CRC, when there is one, is counted in SampleData. Its table also prints 2 twice under ChannelMode, where mono is 3, and gives emphasis 1 as 50/15 ms for MPEG’s 50/15 µs.
SWF ADPCM
Flash’s own ADPCM, close to IMA ADPCM, with 2 to 5 bits a sample, and the only compressed sound format before MP3 arrived in SWF 4. ADPCMSOUNDDATA is read as bits, most significant first, with no byte alignment between packets:
- UB[2]
AdpcmCodeSize: bits a code minus 2 (0 is 2 bits, 3 is 5). - Packets of 4,096 samples a channel, the last perhaps shorter: ADPCMMONOPACKET for mono, ADPCMSTEREOPACKET for stereo.
| Type | Name | Meaning |
|---|---|---|
| SI16 | InitialSample | Mono: the packet’s first output sample, the same as the first sample uncompressed |
| UB[6] | InitialIndex | Mono: the starting place in the step table |
| SI16, UB[6] | InitialSampleLeft, InitialIndexLeft | Stereo, instead of the two above |
| SI16, UB[6] | InitialSampleRight, InitialIndexRight | Stereo |
| UB[4,095 × (AdpcmCodeSize + 2)] a channel | AdpcmCodeData | 4,095 codes a channel, 8,190 for stereo, interleaved left, right, left …; fewer in the last packet |
The specification types InitialSample as SI16, but like everything in ADPCMSOUNDDATA it is 16 bits of the bit stream, not byte-aligned. It does not say how the last packet ends; the explorer, like FFmpeg, stops when the data or the sample count runs out.
Each code of bits bits is decoded with the channel’s predictor p and step index i:
- step = STEPS[i].
- diff = 0. For each magnitude bit, from the highest (value 2bits − 2) to the lowest: if it is set, diff += step; then step = step >> 1. After the loop, diff += step.
- If the sign bit (2bits − 1) is set, p −= diff, otherwise p += diff. Clamp p to −32,768 … 32,767.
- i += INDEX[bits][magnitude]; clamp i to 0 … 88.
- Output p.
| Bits | INDEX by magnitude (sign bit removed) |
|---|---|
| 2 | −1, 2 |
| 3 | −1, −1, 2, 4 |
| 4 | −1, −1, −1, −1, 2, 4, 6, 8 |
| 5 | −1, −1, −1, −1, −1, −1, −1, −1, 1, 2, 4, 6, 8, 10, 13, 16 |
STEPS is the standard IMA step table of 89 entries:
7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230,
253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442,
11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
32767
The INDEX tables are the specification’s, which extends Jack Jansen’s public-domain IMA ADPCM code to 2, 3 and 5-bit codes; the step table and the update are Jansen’s, which the specification refers to rather than repeating. FFmpeg’s SWF ADPCM decoder works the same way.
Sound tags from later versions
| Tag | SWF | Fields |
|---|---|---|
| StartSound2 (89) | 9 | STRING SoundClassName, then SOUNDINFO: starts a sound by its ActionScript 3 class. It has no place in a SWF 1 to 5 movie |