Jump to content

OBD:SNDD: Difference between revisions

26,812 bytes removed ,  3 January 2022
m
cleaning up commented-up blocks (my own), for great justice
m (→‎Known data issues: sorta typos)
m (cleaning up commented-up blocks (my own), for great justice)
Line 104: Line 104:
==PC retail==
==PC retail==
Below is the .dat file part used in the PC retail version.
Below is the .dat file part used in the PC retail version.
<!--
{|cellpadding=3 cellspacing=0 style="line-height:13px"
{{HexRow|0x00|
|52|49|46|46|4E|0C|01|00|57|41|56|45|66|6D|74|20|
|FF|FF|FF|FF|FF|FF|FF|FF|00|00|00|00|00|00|00|00|
|00|00|00|00|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|
|00|00|00|00|00|00|00|00|00|00|00|00|FF|FF|FF|FF|
|RIFF P°°WAVEfmt
}}
{{HexRow|0x10|
|32|00|00|00|'''''02'''''|'''''00'''''|'''''02'''''|'''''00'''''
|'''''22'''''|'''''56'''''|'''''00'''''|'''''00'''''|'''''27'''''|'''''57'''''|'''''00'''''|'''''00'''''|
|FF|FF|FF|FF|FF|FF|C8|C8|C8|C8|C8|C8|FF|FF|FF|FF|
|C8|C8|C8|C8|FF|FF|FF|FF|FF|FF|FF|FF|C8|C8|C8|C8|
|C8|C8|C8|C8|C8|C8|C8|C8|FF|FF|FF|FF|FF|FF|FF|FF|
|2°°°°°°°"V°°'W°°
}}
{{HexRow|0x20|
|'''''00'''''|'''''04'''''|'''''04'''''|'''''00'''''|'''''20'''''|'''''00'''''|'''''F4'''''|'''''03'''''
|'''''07'''''|'''''00'''''|'''''00'''''|'''''01'''''|'''''00'''''|'''''00'''''|'''''00'''''|'''''02'''''|
|FF|FF|00|00|C8|C8|B0|B0|E7|E7|FF|FF|FF|FF|FF|FF|
|C8|C8|FF|FF|7C|7C|C3|C3|CE|CE|DD|DD|DD|DD|DD|DD|
|00|00|C8|C8|64|64|D4|D4|A5|A5|DD|DD|DD|DD|DD|DD|
|°°°° °ô°°°°°°°°
}}
{{HexRow|0x30|
|'''''00'''''|'''''FF'''''|'''''00'''''|'''''00'''''|'''''00'''''|'''''00'''''|'''''C0'''''|'''''00'''''
|'''''40'''''|'''''00'''''|'''''F0'''''|'''''00'''''|'''''00'''''|'''''00'''''|'''''CC'''''|'''''01'''''|
|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|
|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|
|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|DD|
|°ÿ°°°°À°@°ð°°°Ì°
}}
{{HexRow|0x40|
|'''''30'''''|'''''FF'''''|'''''88'''''|'''''01'''''|'''''18'''''|'''''FF'''''|66|61|63|74|04|00|00|00|8A|05|
|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|C8|C8|C8|C8|C8|C8|
|DD|DD|DD|DD|DD|DD|00|00|00|00|FF|FF|FF|FF|00|00|
|DD|DD|DD|DD|DD|DD|C8|C8|C8|C8|00|00|00|00|FF|FF|
|0ÿˆ°°ÿfact°°°°Š°
}}
{{HexRow|0x50|
|01|00|64|61|74|61|00|0C|01|00|05|05|10|00|10|00|
|C8|C8|C8|C8|C8|C8|00|00|00|00|FF|FF|FF|FF|FF|FF|
|00|00|C8|C8|C8|C8|FF|FF|FF|FF|00|00|00|00|00|00|
|FF|FF|FF|FF|FF|FF|C8|C8|C8|C8|FF|FF|FF|FF|FF|FF|
|°°data°°°°°°°°°°
}}
{{HexRow|0x60|
|0C|00|AF|FF|2E|00|B4|FF|F0|F1|00|00|0F|30|20|10|
|FF|FF|FF|FF|FF|FF|FF|FF|00|00|00|00|00|00|00|00|
|00|00|00|00|00|00|00|00|C8|C8|C8|C8|C8|C8|C8|C8|
|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|
|°°¯ÿ.°´ÿðñ°°°0°°
}}
|}
:(The contents of the "fmt " header, relevant to SNDD storage, has been highlighted in '''''bold italic'''''.)
{{Table}}
{{OBDth}}
{{OBDtr| 0x00 | char[4]  |FF0000| 52 49 46 46 | RIFF      | identifier for the "IBM/Microsoft RIFF" standard }}
{{OBDtr| 0x04 | uint32  |FFFF00| 4E 0C 01 00 | 68686    | size of the RIFF container from 0x08 to end of file }}
{{OBDtr| 0x08 | char[4]  |00FF00| 57 41 56 45 | WAVE      | identifier for the "WAVE" format }}
{{OBDtr| 0x0C | char[4]  |00FFFF| 66 6D 74 20 | "fmt "    | identifier announcing the following "fmt " (format) section }}
{{OBDtr| 0x10 | uint32  |FFC8C8| 32 00 00 00 | 50        | content size for the "fmt " section, in bytes (typically 50 for MS ADPCM) }}
{{OBDtr| 0x14 | uint16  |FFFFC8| '''''02 00'''''      | 2        | format ID (2 <nowiki>=</nowiki> MS ADPCM format) }}
{{OBDtr| 0x16 | uint16  |C8FFC8| '''''02 00'''''      | 2        | number of channels (2 <nowiki>=</nowiki> stereo) }}
{{OBDtr| 0x18 | uint32  |C8FFFF| '''''22 56 00 00''''' | 22050    | sample rate in Hz (samples per second), a.k.a. "sampling frequency" }}
{{OBDtr| 0x1C | uint32  |FFC8FF| '''''27 57 00 00''''' | 22311    | [[image:sndd_hd1.gif]]
:<small>'''N.B.''' The data rate (in bytes per second) is truncated to the lower integer value.</small> }}
{{OBDtr| 0x20 | uint16  |FFC800| '''''00 04'''''      | 1024      | block alignment a.k.a "block size", in bytes
:<small>'''N.B.''' The block size for MS ADPCM is typically a power of two.</small> }}
{{OBDtr| 0x22 | uint16  |00FFC8| '''''04 00'''''      | 4        | bits per sample (per channel); typically 4 bits for MS ADPCM }}
{{OBDtr| 0x24 | uint16  |C87C64| '''''20 00'''''      | 32        | size of the following extended format specification, in bytes }}
{{OBDtr| 0x26 | uint16  |B0C3D4| '''''F4 03'''''      | 1012      | [[image:sndd_hd2.gif]] }}
{{OBDtr| 0x28 | uint16  |E7CEA5| '''''07 00'''''      | 7        | number of the following coefficient pairs; always 7 in practice }}
|-align=center valign=top
| 0x2A || int16-16 || bgcolor="#FFDDDD" | '''''00 01 00 00''''' || 256, 0 || rowspan=7 align=left | The coefficient pairs themselves (always the same in practice).<br>[[image:sndd_hd3.gif]]
|-align=center valign=top
| 0x2E || int16-16 || bgcolor="#FFDDDD" | '''''00 02 00 FF''''' || 512, -256
|-align=center valign=top
| 0x32 || int16-16 || bgcolor="#FFDDDD" | '''''00 00 00 00''''' || 0, 0
|-align=center valign=top
| 0x36 || int16-16 || bgcolor="#FFDDDD" | '''''C0 00 40 00''''' || 192, 64
|-align=center valign=top
| 0x3A || int16-16 || bgcolor="#FFDDDD" | '''''F0 00 00 00''''' || 240, 0
|-align=center valign=top
| 0x3E || int16-16 || bgcolor="#FFDDDD" | '''''CC 01 30 FF''''' || 460, -208
|-align=center valign=top
| 0x42 || int16-16 || bgcolor="#FFDDDD" | '''''88 01 18 FF''''' || 392, -232
{{OBDtr| 0x46 | char[4]  |FF00C8| 66 61 63 74 | fact      | identifier announcing the following "fact" section }}
{{OBDtr| 0x4A | int32    |C8FF00| 04 00 00 00 | 4        | size of the following "fact" section in bytes }}
{{OBDtr| 0x4E | int32    |C800FF| 8A 05 01 00 | 66954    | actual number of samples (see below for calculation) }}
{{OBDtr| 0x52 | char[4]  |C8C8FF| 64 61 74 61 | data      | identifier announcing the following "data" section }}
{{OBDtr| 0x56 | int32    |00FFC8| 00 0C 01 00 | 68608    | size of the following "data" section in bytes (67 blocks of 1024 bytes) }}
{{OBDtr| 0x5A | block[14]|FF00FF| 05 05 10 00 10 00 0C<br>00 AF FF 2E 00 B4 FF | (5,5) (16,16)<br>(12,-81) (46,-76)| the header of the first 1024-byte block (14 bytes for a stereo block) }}
{{OBDtr| 0x68 | byte[8]...|00C8FF| F0 F1 00 00<br>00 30 20 10 | (-1,0) (-1,1) (0,0) (0,0)<br>(0,0) (3,0) (2,0) (1,0)| the first 8 pairs of nibbles (stereo samples); 1002 more bytes follow }}
|}
-->
[[image:sndd_all.gif]]
[[image:sndd_all.gif]]


Line 458: Line 360:
:It is possible that the "1" and "2" flags used to affect playback in raw PCM mode (something about swapping the .raw data to allow both for Little Endian and Big Endian PCM samples), but currently they do not seem to have any effect.
:It is possible that the "1" and "2" flags used to affect playback in raw PCM mode (something about swapping the .raw data to allow both for Little Endian and Big Endian PCM samples), but currently they do not seem to have any effect.


<!--
The '''duration''' (number of game ticks) is rounded to the ''lower'' value (a.k.a. "floor") in Vanilla Oni data. For example, on PC, '''SNDDmus_ot7.aif''' consists of 152360 samples, which at 22.05 kHz corresponds to 6.90975 seconds, or 414.585 ticks; the duration, however, is indicated as 414 ticks and not 415.
;Flags 0x00000001 and 0x00000002
:Setting these two flags in a PC retail SNDD file has no noticeable effect, both on their own and in combination with the other two. Possibly they are for runtime use.
;Behavior without 0x00000004 or 0x00000008
:If neither the 0x00000004 nor the 0x00000008 flags are set, the stream data from .raw is apparently copied as-is to the output buffer. The expected sample rate is 22.05 kHz, the expected sample type is regular PCM (Little-Endian signed 16-bit linear), and the channel count of the buffer is determined at OSGr level. Note that 22.05 kHz PCM data can also be supplied more explicitly, with the 0x00000008 flag enabled, and PCM parameters listed in the WAVEfmt header: sample rate (ignored), channel count and bit depth.
;Behavior with 0x00000004
:If the 0x00000004 flag is set, it overrides 0x00000008 and forces the interpretation of the stream data as IMA4 ADPCM. In this configuration, only two fields of the 50-byte "format" chunk are used: the channel count (at 0xE), and the
;Behavior with 0x00000008 and without 0x00000004
===raw PCM data (no 0x4 or 0x8 flag)===
===IMA4 ADPCM data (0x4 flag)===
===WAVEfmt header (0x8 flag and no 0x04)===
-->


==Exporting and importing tips==
==Exporting and importing tips==
Line 519: Line 407:
Alternatively, both the flags and the uint at 0x0E can be used to specify a custom sample rate and/or block size (either as fully custom values or as power-of-two multiplicative factors), without the need for a detailed WAVEfmt header. Still, it is probably easiest to either adhere to the standard parameters (without too many extra flags) or go fully custom and read all the parameters from a standard-compliant WAVEfmt header.
Alternatively, both the flags and the uint at 0x0E can be used to specify a custom sample rate and/or block size (either as fully custom values or as power-of-two multiplicative factors), without the need for a detailed WAVEfmt header. Still, it is probably easiest to either adhere to the standard parameters (without too many extra flags) or go fully custom and read all the parameters from a standard-compliant WAVEfmt header.


<!--
correspond to SScSoundDataFlag_PCM_Big and SScSoundDataFlag_PCM_Little. (traces of flag values found in)
;WAVEfmt/Ext header
:If the 0x00000008 flag is OFF (never used in Oni), the WAVEfmt block is completely ignored, and the engine to "format ID" field in the [[OBD:SNDD/wav|wav header]], and the precedence is as follows:
:*the format ID of the wav header is is always 2 (ADPCM), but many mods use 1 (PCM) instead.
;Padding
:The 24 bytes at the end are ''not'' part of the SNDD template. They are not loaded by the Oni engine.
The .raw data contains the actual audio sample blocks without any other headers (other than ADPCM block headers).==
and 2034 more samples in the case of 44.1 kHz mono)
and 44.1 kHz mono
and 2036 for 44.1 kHz mono.
:If the "compressed" flag (0x00000001) is enabled, the .raw audio data is assumed to be compressed with MS ADPCM, at 22.05 kHz and with a block size of 512 bytes per channel (on the Mac, IMA4 compression is used, with 34-byte blocks).
:If the "compressed" flag (0x00000001) is disabled, it would be sensible for the engine to assume standard uncompressed PCM (linear signed 16-bit), sampled at 22.05 kHz and just copy that data to the output buffer. Instead the engine merely skips the initialization of the decompression routine, but still attempts to decompress later on, finding no bytes to decode and immediately interrupting playback as if it had completed. For impulse sounds, the system merely plays no sound, and no further problems occur. However, if the sound participates in a looping permutation (randomized or not), then the engine will be stuck in endless recursion within a single tick, chaining up immediately-interrupted playbacks of the same permutation, which ends in a "Blam". The fix is either that Oni should compress any uncompressed SNDDs at runtime (and then run them through the generic decompression routine), or that the playback of uncompressed sounds should use a separate set of rules.
;IMA4 or MS ADPCM?
:In theory the engine can easily determine between MS ADPCM and IMA4, from several criteria:
:*Coarse criterion: if the raw data size is a multiple of 34 bytes, then the data is very probably in IMA4 format (although coincidence can not be ruled out completely);
:*Fine criterion: if ''every'' 34-byte block starts with a valid predictor-step pair, with the 7-bit step value falling in the 0-88 range, this makes IMA4 practically certain.
:*For sufficiently long sounds (starting at ~21 ticks or 0.35 seconds) the "duration" field can be used to validate IMA4 as well: for N samples, the "duration" being '''floor(N/367.5)''' ticks, the .raw size of a IMA4 sound will be '''34*ceil(N/64)''' bytes per channel, whereas for a standard MS ADPCM sound it will not exceed '''512*ceil(N/1012)''' per channel, i.e., about 5% smaller than for IMA4.
;Key shortcomings of the PC demo and Mac SNDDs as compared to PC retail SNDDs:
*PC demo and Mac SNDDs have a "short" .dat part that specifies only the frame count (animation length in game ticks) and number of channels (mono or stereo). The waveform data is always assumed to be sampled at 22.05 kHz, and compressed into 4-bit ADPCM (either IMA4, on Mac, or MS ADPCM, on PC). Uncompressed PCM playback is broken for PC demo (and probably for Mac as well).
*PC retail SNDDs contain a WAVE "fmt " (format) chunk that can be used to specify arbitrary compression parameters as well as a custom sample rate. In practice, though, Oni always interprets the waveforms as having a 22.05 kHz sample rate. Thus, although the 46 electric spark sounds '''ap_hit_shld''' and '''zap##''' have 44.1 kHz waveforms in PC retail versions of Oni, the actual rendering of these sounds is 2x slower/lower than intended.
;Key shortcomings of Mac SNDDs as compared to PC SNDDs (both retail and demo):
*At 22.05 kHz, the storage size of Mac SNDDs (IMA4) is about 5% larger than for PC equivalents (MS ADPCM), because of a much smaller block size in the .raw part (the smaller .dat part of Mac SNDDs doesn't help).
*Mac SNDDs have encoding/editing artifacts at the ends of looping segments (music and ambient tracks). PC SNDDs (retail or demo) have no such artifacts and allow nearly seamless playback. See [[OBD:SNDD#Looping_issues|BELOW]] for details.
;Key shortcomings of PC SNDDs (both retail and demo) as compared to Mac SNDDs:
*The large block size of PC SNDDs (1012 samples for 22.05 KHz mono or stereo, and 2036 samples for 44.1 kHz mono) is space efficient, but the tradeoff is a coarser sample resolution whenever a block spans a wide range of amplitudes (this is because of how a MS ADPCM block relies on a single "predictor" that accommodates all the waveform samples within that block).
*Custom sample rates are formally supported through the use of the WAVE "fmt " header, but do not actually work, which is frustrating.
----
===PC demo and Mac===
The Mac version and the PC demo version use a simpler format, with no support for different sample rates (all sounds are sampled at 22050 Hz).
The .raw data contains the actual audio sample blocks without any other headers (other than ADPCM block headers).
====.raw part (MS ADPCM, PC demo)====
For PC demo the .raw SNDD data is actually the same as for PC retail, but with the same short .dat header as on Mac. The ADPCM block size is 512 bytes for mono, and 1024 for stereo. The sample rate is 22.05 kHz.
====.raw part (IMA4 ADPCM, Mac)====
For an overview of the IMA ADPCM algorithm and IMA4 header (if interested), see [https://wiki.multimedia.cx/index.php/Apple_QuickTime_IMA_ADPCM HERE]
:The IMA4 ADPCM data has 34-byte blocks (in the case of stereo, there is an even number of such blocks, because Left and Right blocks are interleaved).
:The first two bytes of each block is a header that  set the initial predictor (upper 9 bits) and step (lower 7 bits) for decoding the block's samples. Typically they are only used only for the first block
:The other 32 bytes consist of 64 samples stored as nibbles (half-bytes). In the case of stereo, all the nibbles in a block belong to the same channel (either all Left or all Right).
:Unlike for MS ADPCM, incomplete trailing blocks (if any) are not indicated in any way: the final blocks are stored in their entirety, with no way to tell how much of it is actual data.
:For this reason, identical sounds do not have the same sample count on PC (both retail and demo) and on Mac. As an example, here are the stats for the main menu music:
{{divhide|Main menu music a.k.a. "Oni Trailer"}}
{|
|
{{Table}}
!SNDD name (and frame count)
!PC (retail and demo)
!Mac
!difference
|-
|
:'''SNDDmus_ot6'''
:415 frames = 6.9166667 seconds
:~= '''152512.5''' samples (@ 22.05 kHz)
|
:0x25B0C = 154380 = 150x1024 + 780 bytes
:= 150x1012 + 768 = '''152568''' stereo samples
:= 6.919183673469388 s (@ 22.05 kHz)
|
:0x27940 = 162112 = 4768x34 bytes
:= 2384x64 = '''152576''' stereo samples
:= 6.919546485260771 s (@ 22.05 kHz)
|
:As compared to the PC version of the SNDD,
:the Mac version has 8 extra samples at the end
:(i.e., the last 4 bytes of the last two blocks).
|-
|
:'''SNDDmus_ot7'''
:414 frames = 6.9 seconds
:~= '''152145''' samples (@ 22.05 kHz)
|
:0x25A3C = 154172 = 150x1024 + 572 bytes
:= 150x1012 + 560 = '''152360''' stereo samples
:= 6.909750566893424 s (@ 22.05 kHz)
|
:0x27874 = 161908 = 4762x34 bytes
:= 2381x64 = '''152384''' stereo samples
:= 6.910839002267574 s (@ 22.05 kHz)
|
:As compared to the PC version of the SNDD,
:the Mac version has 24 extra samples at the end
:(i.e., the last 12 bytes of the last two blocks)
|}
|}
{{divhide|end}}
By looking at the end of the Mac SNDDs (or the exported AIFF files), it can be confirmed that the extraneous samples are actually there, at the end of the last two 34-byte blocks (last Left block and last Right block), with no way to interrupt playback upon reaching these trailing samples - because they are no different from regular samples.
Also, from a careful examination of the sound stream that is actually played back by Oni in the main menu, it is clear that all the Oni engines (both Mac and PC) play back all the available data (including the "padding" of the fixed-size IMA4 blocks) before switching to the next segment. The frame count (number of game ticks) does not affect a sound's playback and is used only as an indication for subsequent sounds (e.g., for approximate cueing in [[BSL]]).
This uninterrupted playback of fixed-size IMA4 blocks is one of the aspects that impact seamless playback of sound sequences in Mac Oni (music or ambient tracks). See [[OBD:SNDD#Looping_issues|"Looping issues"]] below.
;NOTE
:Musically, the two segments of the main menu music correspond to the same duration (four bars of a 4:4 beat). However, somewhat suprisingly, the two segments don't have the same sample count - or even the same frame count (in game ticks) -, not even when comparing the two sounds on the same platform. That means that, even on PC where the playback is nearly seamless, we are actually hearing musical loops of unequal length, ending 10 milliseconds early or late, and it still sounds OK.
----
==Exporting and importing tips==
To create a wav/aif file one needs to write a file header like below and then write the contents of the raw data part.
===WAV files (from PC retail/demo SNDDs)===
*Write "RIFF"
*add the size of the part in the raw file + 70 bytes
*write "WAVE"
*write "fmt "
*write 50
*write the wav header '''(for PC demo, the wav header is ''not'' present in the .dat part, and has to be deduced)'''
*'''OPTIONAL/RECOMMENDED: compute the number of samples and add a "fact" section announcing it'''
*write "data"
*add the size of the part in the raw file '''OPTIONAL/RECOMMENDED: increase the size if the last sample block is incomplete'''
*add the raw file data '''OPTIONAL/RECOMMENDED: add padding to the last sample block if it is incomplete'''
*save it as a wav file.
[[image:sndd_wav.gif]]
{{Table}}
{{OBDth}}
{{OBDtrBK|1=Complete ADPCM wav format header (black outline)}}
{{OBDtr| 0x00 | char[4]  |FF0000| 52 49 46 46 | RIFF      | identifier for the "IBM/Microsoft RIFF" standard }}
{{OBDtr| 0x04 | int32    |FFFF00| 9C 28 00 00 | 10396    | size of the file from 0x08 to the end (<nowiki>=</nowiki> size of the .raw part + 70 bytes) }}
{{OBDtr| 0x08 | char[4]  |00FF00| 57 41 56 45 | WAVE      | identifier for the "WAVE" format }}
{{OBDtr| 0x0C | char[4]  |00FFFF| 66 6D 74 20 | "fmt "    | identifier announcing the following wav format header }}
{{OBDtr| 0x10 | int32    |FFC8C8| 32 00 00 00 | 50        | wave format header size }}
{{OBDtr| 0x14 | block[50]|FFC8C8| &nbsp;      | &nbsp;    | [[OBD:SNDD/wav|wav header]] }}
{{OBDtr| 0x46 | char[4]  |FFFFC8| 64 61 74 61 | data      | identifier announcing the following wav data }}
{{OBDtr| 0x4A | int32    |C8FFC8| 56 28 00 00 | 10326    | size of the following wav data in bytes (<nowiki>=</nowiki> size of the .raw part) }}
|}
The above is not 100% consistent with the WAVE storage rules, because it allows for a completely arbitrary "data" size. Microsoft ADPCM data is supposed to be stored as a number of fixed-size blocks (in Oni, each block is either 512 bytes for 22.05 kHz mono, or 1024 bytes for 22.05 kHz stereo and 44.1 kHz mono). Thus, according to the standard, the last block - even if incomplete - must be stored in its entirety, and the "data" size must be a multiple of the block size. In the above example, since the format is 22.05 kHz mono, the "data" size should be increased from 10326 to 10752=21x512, and 426 empty bytes should be added as padding, so that there are 21 complete data blocks.
The standard way to deal with incomplete blocks is to specify not just the data size, but the ''actual number of samples'', by adding a "fact" section to the WAVE header, like this:
{{Table}}
{{OBDth}}
{{OBDtrBK|1=Complete ADPCM wav format header}}
{{OBDtr| 0x00 | char[4]  |FF0000| 52 49 46 46 | RIFF      | identifier for the "IBM/Microsoft RIFF" standard }}
{{OBDtr| 0x04 | int32    |FFFF00| 9C 28 00 00 | 10396    | size of the file from 0x08 to the end (<nowiki>=</nowiki> size of the .raw part + 70 bytes) }}
{{OBDtr| 0x08 | char[4]  |00FF00| 57 41 56 45 | WAVE      | identifier for the "WAVE" format }}
{{OBDtr| 0x0C | char[4]  |00FFFF| 66 6D 74 20 | "fmt "    | identifier announcing the following wav format header section }}
{{OBDtr| 0x10 | int32    |FFC8C8| 32 00 00 00 | 50        | wave format header size }}
{{OBDtr| 0x14 | block[50]|FFC8C8| &nbsp;      | &nbsp;    | [[OBD:SNDD/wav|wav header]] }}
{{OBDtr| 0x46 | char[4]  |FFFFC8| 66 61 63 74 | fact      | identifier announcing the following "fact" section }}
{{OBDtr| 0x4A | int32    |FFFFC8| 04 00 00 00 | 4        | size of the following "fact" section in bytes }}
{{OBDtr| 0x4E | int32    |C8FFC8| B0 4F 00 00 | 20400    | actual number of samples (see below for calculation) }}
{{OBDtr| 0x52 | char[4]  |FFFFC8| 64 61 74 61 | data      | identifier announcing the following wav data }}
{{OBDtr| 0x56 | int32    |C8FFC8| 00 2A 00 00 | 10752    | size of the following wav data in bytes (<nowiki>=</nowiki> size of the .raw part + 426 empty bytes) }}
|}
The actual number of samples is implied from the actual data size (size of the .raw part) and [[OBD:SNDD/wav|wav header]] properties as follows:
* n_whole_blocks = floor(raw_size/block_size);    '''// EXAMPLE: floor(10326/512) = 20'''
* last_block_size = raw_size - whole_blocks*block_size;    '''// EXAMPLE: 10326 - 20x512 = 86'''
* last_block_samples = (last_block_size - 7*n_channels)*(8/bits_per_sample/n_channels) + 2;      '''// EXAMPLE: (86 - 7)*(8/4) + 2 = 160'''
* n_samples = n_whole_blocks*samples_per_block + last_block_samples;        '''// EXAMPLE: 20*1012 + 160 = 20400'''
==Looping issues==
As detailed above, ADPCM data is stored in blocks, but the actual sound data does not necessarily end exactly at the end of a block. This is true both for MS ADPCM (PC retail or demo) and IMA4 ADPCM (Mac), but is especially noticeable for the comparatively large blocks of MS ADPCM, where the padding can be as large as ~1010 samples, i.e., a ~46-millisecond silence in the case of 22.05 kHz (for IMA4, the biggest possible gap is 63 samples, or ~3 milliseconds).
===MS ADPCM===
Although the final block of a MS ADPCM SNDD file (PC retail) is stored in incomplete form (with only the actual samples and no padding), the standard decoding behavior when loading an ADPCM-compressed WAV (e.g., in a non-destructive audio program) is to assume full-sized blocks, with padding up to the end of the last block. Depending on the audio program, this can create a silence or some "bad data" at the end of the imported audio, which can be a problem if one wants to join SNDDs that are supposed to play seamlessly one after another (e.g., a musical or ambient sequence).
As a workaround, one can preprocess .wav files with some tools that can handle incomplete MS ADPCM blocks and convert to a less ambiguous format:
*For [http://sox.sourceforge.net Sox], padding is disabled by default when joining several files.
*For [https://www.ffmpeg.org/download.html FFmpeg], padding can be disabled as an optional setting.
So, you'd either join the .wav files in Sox or FFmpeg, or convert them, e.g., to uncompressed PCM, and then import them into a fancy audio tool.
As an actual solution, the .wav file should be made compliant with RIFF WAVE standards, i.e., the last block should be padded to its full size, and a "fact" section should be used to specify the actual number of samples. This is implemented in OniSplit v 0.9.###
Slight distorsions are sometimes observed near the ends of looping SNDDs (music and ambient tracks). These artifacts were likely caused by Bungie's audio tools, and can not be undone automatically. Barely noticeable, they can be healed by manually editing audio samples near the seams.
===IMA ADPCM===
====Padding====
In the case of IMA ADPCM, the padding is actually present in the stored audio, so it is impossible (both for OniSplit and for a third-party converter) to automatically trim it down to just the relevant audio data. In fact, just by looking at the Mac SNDD itself, there is no way to tell how many of the trailing samples need to be cut for a truly seamless transition (for one thing, the trailing samples are not flat zero).
As a workaround/solution, the correct sample count of a Mac SNDD can be looked up in a PC counterpart (always available, since we're only talking of music/ambients/sirens, which are neither localized nor sampled at 44.1 kHz), and then used to trim the .aif file in FFmpeg, while converting to .wav (either PCM or ADPCM). However, it's easier (and more reliable) to just grab a PC retail copy of Oni and extract the MS ADPCM sounds.
====Initial transient====
The biggest problem with seamless playback of Mac SNDDs (for music and ambient tracks) is that - even if you figure out the correct length of each segment - the waveform of each next segment does not pick up where the previous segment left (or should have left) - instead it builds up from zero over ~7 samples. This introduces about 0.3 milliseconds of silence, and an audible discontinuity in the waveform, even if the two segments are lined up properly.
The values of those initial samples is not recoverable (unlike the padding at the end of SNDDs, which can be trimmed down). Therefore, if working with sound samples extracted from Oni, it is recommended to turn to a PC version's SNDDs.
==PCM export and PC demo detection==
OniSplit v0.9.### implements export to uncompressed PCM (signed 16-bit linear) from both the PC retail and the Mac SNDD format: use '''-extract:pcm''' instead of either '''-extract:wav''' or '''-extract:aif'''. As compared to ADPCM, linear PCM is a much more straightforward format (almost human readable), and makes it easier to analyze artifacts.
Since the only difference between PC demo and Mac is the actual storage format of SNDD files (in the .raw part), and the template checksum is the same, OniSplit has no way of determining which ADPCM algorithm to use, other than by actually scanning and validating the data as IMA4 (or not). Starting with OniSplit v0.9.###, this automatic check is implemented, allowing both '''-extract:wav''' and '''-extract:pcm''' on PC-demo SNDDs. It is very unlikely that any Mac SNDDs will be falsely identified as MS ADPCM (or, rather, invalidated as IMA4). If it ever happens, do as instructed by the following warning: ''"PC-demo MS ADPCM detected; use '''-nodemo''' flag to treat as IMA4."''
Note that transcoding (between IMA4 and MS ADPCM) and encoding is not implemented at this point. So '''-extract:aif''' will not work on PC SNDDs, '''-extract:wav''' will not work on Mac SNDDs, and '''-create''' will only work on sound files that use the correct codec and sample rate supported by the PC retail/demo or Mac Oni engine.
Importing SNDDs for PC demo (with a short .dat part and MS ADPCM in the .raw part) is also not implemented yet. Last but not least, PC retail apparently supports uncompressed PCM sounds, but they need to be tested in the engine first. Possibly ADPCM encoding/transcoding will be implemented at some point, too.
-->


==Notes==
==Notes==