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. In a ZWS movie these tags are in the decompressed data; the sound data inside them is in its own coding. The explorer shows the sound data field by field and plays every format: uncompressed, ADPCM, MP3, Nellymoser and Speex.
Sound formats
Every sound tag starts with a 4-bit format, a 2-bit rate, a size bit and a channel bit.
| 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 |
| 4 | Nellymoser 16 kHz | 10 |
| 5 | Nellymoser 8 kHz | 10 |
| 6 | Nellymoser | 6 |
| 11 | Speex | 10 |
| 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; the specification says the field is ignored for Nellymoser and Speex |
| 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; ignored for Nellymoser and Speex, which are mono |
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.
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 |
StartSound2 (89, SWF 9)
| Type | Name | Meaning |
|---|---|---|
| STRING | SoundClassName | The ActionScript 3 class of the sound |
| SOUNDINFO | SoundInfo | How to play it |
SOUNDINFO
| Type | Name | Meaning |
|---|---|---|
| UB[2] | Reserved | 0 |
| UB[1] | SyncStop | Stop the sound instead of starting it (0x20) |
| UB[1] | SyncNoMultiple | Do not start it if it is already playing (0x10) |
| UB[1] | HasEnvelope | An envelope follows (0x08) |
| UB[1] | HasLoops | A loop count follows (0x04) |
| UB[1] | HasOutPoint | An out point follows (0x02) |
| UB[1] | HasInPoint | An in point follows (0x01) |
| 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, as SoundRate |
| 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 |
| 4, 5, 6 | Nellymoser blocks of 64 bytes | |
| 11 | Speex frames | |
MP3 in SWF
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.
Nellymoser
Nellymoser Asao is a mono speech codec from Nellymoser, Inc. The SWF specification describes it only in outline (frames of 256 samples turned into the frequency domain, with the strongest bands coded) and refers to the company for details, which were never published. A SoundStreamBlock of format 6 holds what it calls a NELLYMOSERDATA record, without defining it. What follows is from the reverse-engineered decoders, nelly2pcm and FFmpeg’s, which the explorer follows; FFmpeg’s output and the explorer’s agree.
Format 4 is 16,000 Hz and format 5 8,000 Hz. The specification says SoundRate is ignored for Nellymoser, without saying what rate format 6 has; Ruffle plays format 6 at SoundRate’s rate, as FFmpeg does in FLV, and so does the explorer. The data is a run of 64-byte blocks, each 256 samples. A block is read as bits, least significant bit of each byte first:
| Bits | Part | Meaning |
|---|---|---|
| 6 | First band level | The power of band 0 |
| 22 × 5 | Band deltas | The power of each of bands 1 to 22, as a step from the band before (116 bits in all) |
| 198 | First coefficients | 124 coefficients for the first 128 samples |
| 198 | Second coefficients | 124 coefficients for the next 128 samples, with the same band powers |
The band powers decide how many bits, 0 to 6, each coefficient gets, so that each set fills its 198 bits. A coefficient’s bits pick a value from a fixed dequantisation table, scaled by its band’s power; a coefficient with no bits gets the band’s power with a random sign. The last 4 of the 128 coefficients are zero. An inverse MDCT and a sine window, overlapping each half with the one before, give 128 samples from each set.
Speex
Speex is an open speech codec from Xiph.Org, documented in its manual and in RFC 5574, and stored as format 11 from SWF 10. In SWF it is always mono, encoded at 16 kHz and decoded to 16-bit samples: SoundRate, SoundSize and SoundType are disregarded. Flash Player 10 (10.0.12) has Speex 1.2 beta 3 built in. The data is wideband Speex frames of 20 ms (320 samples) each, a wideband frame being a narrowband frame followed by the high-band bits; the frames are described on the Speex in Ogg page. Each SoundStreamBlock is read as one Speex packet, padded to a whole byte at its end. A DefineSound's data has no packet boundaries, so the explorer checks whether every frame is padded to a byte (a 0 bit, then 1 bits) and reads it frame by frame if so, or as one run of frames if not. The explorer shows Speex data as one field and plays it with its own Speex decoder, which follows the reference decoder, libspeex, including its perceptual enhancer.
SWF ADPCM
Flash’s own ADPCM, close to IMA ADPCM, with 2 to 5 bits a sample. 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 (worth 2bits − 2) down: if it is set, diff += step; then step = step >> 1. After the last, diff += step. This is (magnitude + ½) × step ÷ 2bits − 2, in integers.
- If the sign bit (worth 2bits − 1) is set, p −= diff, otherwise p += diff. Hold p between −32,768 and 32,767.
- i += INDEX[bits][magnitude], held between 0 and 88.
- Output p.
| Code size | INDEX by magnitude (sign bit removed) |
|---|---|
| 2 bits | −1, 2 |
| 3 bits | −1, −1, 2, 4 |
| 4 bits | −1, −1, −1, −1, 2, 4, 6, 8 |
| 5 bits | −1, −1, −1, −1, −1, −1, −1, −1, 1, 2, 4, 6, 8, 10, 13, 16 |
STEPS is the standard IMA ADPCM 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.