Documentation and notes collected for SMPB research.  2015/02/16 12:17 - MarkeyJester.

All values are assumed hexadecimal, and are displayed in big endian (from little endian) in this document.

--------------------------------

The SMPB files contain sample data of varying formats.
I've only known 16-bit signed PCM and 4-bit ADPCM to exist, but others may exists.
There also appears to be looping information here.

The SMDB format appears to be the same format, despite having a different name.

Offset		(Size)	= Description

00000000	(Long)	= Standard ASCII identifier "SMPB".
00000004	(Long)	= Unknown, it's usually 00000002 or 00000101 (Version?).
00000008	(Long)	= Size of the SMPB file (This may not include padding 00's on the very end)
0000000C	(Long)	= Always seems to be 00000000.
00000010	(Long)	= Offset of sample pointers. [See SAMPLES below]
00000014	(Long)	= Number of samples.
00000018	(Long)	= Offset of unknown simple wave data (80 bytes each). [See UNKNOWNWAVE below]
0000001C	(Long)	= Number of unknown waves.
00000020	(Long)	= Offset of unknown data (possibly mute data for ADPCM). [See UNKNOWNADPCM below]
00000024	(Long)	= Either; Number of above, or size of above. (Ususally 00000001)
00000028	(Long)	= Offste of unknown data (possibly mute data for PCM). [See UNKNOWNPCM below]
0000002C	(Long)	= Either; Number of above, or size of above. (Ususally 00000001)

--------------------------------

SAMPLES:

This is a table of entires, each entry consists of 58 bytes.
Each entry can locate a sample with a size, loop back size, and specific PCM/ADPCM format.
Some entries can be (and a lot of the time are) made as blank slots.

This is at the offset of SAMPLES+:

+0000		(Long)	= Pointer to next part of table. [See TABLE01 below]
+0004 - +0017		= This is always usually 00 (Blank), I have yet to see it filled with anything.

TABLE01:
+0018		(Long)	= Number of samples within TABLE02 (SMPB tends to be 00000001, while SMDB tends to have more than one).
+001C		(Long)	= Pointer to next part of table. [See TABLE02 below]
+0020		(Long)	= Always seems to be 00000000.
+0024		(Long)	= Always seems to be 00000202.  (Might be byte/word specific).

TABLE02:	This is just one entry, each entry is 30 bytes
+0028		(Byte)	= Offset of sample, byte 02 only (00XX----).  This is sometimes +80, the bit setting reason is unknown, possibly an address related reason for the SH-4, omit this to get the address.
+0029		(Byte)	= Audio format
+002A		(Word)	= Offset of sample, bytes 00 and 01 only (00--XXXX) (REMEMBER: little endian).
+002C		(Word)	= Loop back size (Number of PCM/ADPCM elements (One word of a 16-bit sample is an element)).
+002E		(Word)	= Size of sample (Number of PCM/ADPCM elements (One word of a 16-bit sample is an element)).
+0030		(Word)	= Envelope: Attack rate (0000 Slow - 001F Fast)
+0032		(Word)	= Envelope: Not sure, possibly Release rate (0000 - 001F)
+0034		(Long)	= Unknown.
+0038		(Word)	= Unknown.
+003A		(Byte)	= Unknown (usually 20).
+003B		(Byte)	= Volume (00 Mute - 0F Loud)
+003C		(Word)	= Unknown (usually 0000 or 0004).
+003E - +0047		= This is a recurring pattern of 1FF8 (Top volume for 16-bit?).
+0048 - +004B		= This is a recurring pattern of values at around 10 - 19 (Top volume for 8/4-bit?).
+004C		(Word)	= Unknown (usually 7F00).
+004E		(Word)	= Default Frequency (Pitch).
+0050		(Word)	= Unknown (size? usually FFFF).
+0052		(Word)	= Unknown (usually 0100).
+0054		(Byte)	= Unknown (usually 7F).
+0055		(Byte)	= Unknown (usually 00 or FF).
+0056		(Word)	= Unknown (usually 0000).

The offset of the sample is contained at bytes +0018 (AA), +001C (CC) and +001D (BB) to form the offset 00AABBCC respectively.

The "Size of sample" (+002E) can sometimes be FFFF.  This is a NULL size, in which case the "Loop back size" (+002C) should be used.

The "Audio format" (+0029) signifies the sample's format:

	-> 00 = PCM 16-bit signed
	-> 01 = ADPCM 4-bit
	-> 02 = PCM 16-bit signed (Looping type)
	-> 03 = ADPCM 4-bit (Looping type)

It probably wouldn't be much of a stretch to say this could be binary controlled where bit 0 signifies PCM/ADPCM, bit 1 signifies looping.
When looping is enabled, the "Loop back size" is used to loop back endlessly when the end of the sample is reached.
ADPCM loop back can be tricky however since it's based on the principles of using the "previous" sample element to determin the next.


The ADPCM 4-bit appears to be derived from a variation of YMZ280B (if not equal to, then certainly close).

The frequency range should therefore be between 0.172hz and 44.100hz (00 to FF).
So it's possible that one of the unknown values above, signifies the value 00 to FF representing the default frequency.
One thing to note is that most (if not all) "Sonic Adventure 2" samples appear to be at the middle frequency of 22.136hz (between 0.172 and 44.100hz).
Though samples in "Chu-Chu Rocket!" for example, appear to vary between samples.

--------------------------------

UNKNOWNWAVE:

This is a series of 80 byte 8-bit data which increments in triangle/sine like form, purpose is unknown.
The long-word at offset 0000001C signifies how many of these exists one after another.

This is an example of two entries from "Sonic Adventure 2":

	00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
	10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
	20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
	30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
	40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F
	50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F
	60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F
	70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F

	00 01 01 01 0D 14 1A 1F 24 27 2B 2E 31 34 36 38
	3A 3C 3E 40 42 43 45 46 48 49 4A 4C 4D 4E 4F 50
	51 52 53 54 55 56 57 58 59 59 5A 5B 5C 5C 5D 5E
	5F 5F 60 61 61 62 62 63 64 64 65 65 66 66 67 67
	68 68 69 69 6A 6A 6B 6B 6C 6C 6D 6D 6E 6E 6E 6F
	6F 70 70 71 71 71 72 72 72 73 73 74 74 74 75 75
	75 76 76 76 77 77 77 78 78 78 79 79 79 79 7A 7A
	7A 7B 7B 7B 7C 7C 7C 7C 7D 7D 7D 7D 7E 7E 7E 7F

I'd rule these out of simple looping tone waves due to "Chu-Chu Rocket!" containing them in the SAMPLES section.
While I don't know too much about the audio hardware, my only theory is this being chip specific data to control the echo/effects, but don't quote me on that.

--------------------------------

UNKNOWNADPCM:

This is a very short piece of data that usually consists of:

	00 00 80 00 80 00 80 00 80 00 00 00

It's possible this is either:

	-> Mute data for ADPCM (though normally the ADPCM would use 80 80 80 80 with no interlacing 00's to keep the wave still).
	-> Mute data for PCM 16-bit signed/unsigned (unlikely).

--------------------------------

UNKNOWNPCM:

This too is very short and usually consists of:

	00 00 00 00

It's possible this is mute data for PCM 16-bit signed data (i.e. for resting a channel).

