file-format music-imf-560
{
	opl3-rate: 560
	compressed [file.remaining-bytes] "imf-opl3"
	{
		sound
		{
			channels: 1
			samples-per-second: 44100
			bits-per-sample: 16
		}
	}
}

file-format music-imf-560-2
{
	unsigned16 file-size
	opl3-rate: 560
	compressed [file-size] "imf-opl3"
	{
		sound
		{
			channels: 1
			samples-per-second: 44100
			bits-per-sample: 16
		}
	}
}

file-format music-imf-280
{
	opl3-rate: 280
	compressed [file.remaining-bytes] "imf-opl3"
	{
		sound
		{
			channels: 1
			samples-per-second: 44100
			bits-per-sample: 16
		}
	}
}

file-format adlib-sound
{
	opl3-rate: 140
	compressed [file.remaining-bytes] "adl-opl3"
	{
		sound
		{
			channels: 1
			samples-per-second: 44100
			bits-per-sample: 16
		}
	}
}

function adl-opl3
{
	previous-value: -1
	read unsigned32 length
	read unsigned16 priority

	loop (4): i
	{
		read unsigned8 a
		write opl3-reg (0x20 * (i + 1)) a

		read unsigned8 b
		write opl3-reg (0x20 * (i + 1) + 3) b
	}

	read unsigned8 mWave
	write opl3-reg 0xE0 mWave

	read unsigned8 cWave
	write opl3-reg 0xE3 cWave

	read unsigned8 nConn
	write opl3-reg 0xC0 nConn

	read unsigned8 voice
	read unsigned8 mode
	read data(3) padding
	
	read unsigned8 block

	block = (block & 7) << 2
	note: false

	loop(length)
	{
  		read unsigned8 sound-dat
  		if (sound-dat == 0)
		{
    			write opl3-reg 0xB0 block
    			note = false
  		}
		else
		{
			if (previous-value != sound-dat)
			{
				previous-value = sound-dat
    				write opl3-reg 0xA0 sound-dat
			}
    			if (note == false)
			{
      				write opl3-reg 0xB0 (block | 0x20)
      				note = true
			}
    		}

		write opl3-wait 1
  	}
}

function imf-opl3
{
	loop(file.remaining-bytes / 4)
	{
		read unsigned16 reg
		read unsigned16 wait

		write opl3-reg (reg & 0xFF) (reg >> 8) 
		write opl3-wait wait
	}
}