
ChibiOctive:
	;   E     F     G      A     B     C     D   
	dw &0020,&0030,&0040,&0080,&00C0,&0100,&0140 ;0
	;   41.2  43.6   49    55   61.7  65.4  73.4
	ifndef SmsTranspose
	dw &0180,&01C0,&0200,&0240,&0280,&02C0,&0300 ;1
	endif
	;   82.4  87.3  98    110   123    130   146
	dw &0340,&0380,&03C0,&03AA,&1D5E,&295A,&4161 ;2
	;	164   174    196   220 	246   261   293
	dw &562A,&6024,&717C,&80DB,&8E9D,&9516,&A0A5;3
	;   329   349   392   440   493   523    587
	dw &AB17,&AFC3,&B8A2,&C076,&C746,&CA8F,&D060 ;4
	;    659  698   783   880   987   1046  1174
	dw &D566,&D7DE,&DC17,&E01A,&E3AF,&E520,&E801 ;5
	;   1318  1396  1567  1760  1975  2093  2349
	dw &EAAE,&EBEB,&EE11,&F003,&F1C3,&F2B2,&F3D2;6
	dw &F400,&F600,&F800,&FACC,&FC0,&FE00,&FFFF ;EXTRA
	   


;H=Volume (0-255) 
;L=Channel Num (0-127 unused channels will wrap around) / Top Bit=Noise
;DE=Pitch (0-65535)

ChibiSoundPro_Set:			

	bit 7,l
	jr nz,ChibiSoundPro_Noise
	
	ld bc,ChannelMask
	
	ld a,l
	and %00000011
	add c
	ld c,a
	ld a,(bc)
	ld b,a
	jr ChibiSoundPro_NotNoise
ChibiSoundPro_Noise:
	ifdef SmsSimpleNoise
		ld bc,ChannelNoise
		ld a,l
		and %00000011
		add c
		ld c,a
		ld a,1
		ld (bc),a			;Mark noise as on
		
		ld a,d
		and %11000000
		xor %11000000
		rlca
		rlca
		or a
		jr z,NoiseZero
		dec a
NoiseZero:
		or %11100100		;Whitenoise Speed 0-2 (Chn3)
		out (&7F),a
		
		ld a,h		;Set Vol
		srl a
		srl a
		srl a
		srl a
		xor %11111111	;Set Volume (Chn3)
		
		out (&7F),a
		ret
	else
		ld b,%01000000		;Noise Channel uses tone 2
	endif
	
ChibiSoundPro_NotNoise:	
	ifdef SmsSimpleNoise
		push bc
			ld bc,ChannelNoise
			ld a,l
			and %00000011
			add c
			ld c,a
			ld a,(bc)
			or a
			jr z,NoiseStillOff
			ld a,%11111111	;Noise was on, now off
			out (&7F),a
			xor a
			ld (bc),a
		
NoiseStillOff:
		pop bc			;This was in the wrong place - check other versions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	endif

	ld a,h
	or a
	jr z,silent
	
	ld a,d
	cpl
	ld d,a
	ld a,e
	cpl
	ld e,a
	
	srl d
	rr e
	srl d
	rr e
	
	srl e
	srl e
	srl e
	srl e
	ld a,e
         ;1CCTLLLL	(Latch - Channel Type DataL
	or   %10000000	; Low Tone
	or b	;Channelnum
	out (&7F),a
	ld a,d
		;0-HHHHHH
	;and %00111111	;High Tone
	out (&7F),a

	ld a,h
	srl a
	srl a
	srl a
	srl a
	ld c,a
	ifndef SmsSimpleNoise
		bit 7,l		;We're done if there is no noise
		jr nz,NoiseOn
	endif
	xor %10011111	;Set Volume
	or b	;Channelnum
	out (&7F),a

	ifndef SmsSimpleNoise
		ret
NoiseOn:
		ld a,%11011111	;1CCTVVVV	(Latch - Channel Type Volume)	
		out (&7F),a	;Mute Tone 2

		ld a,%11100111	;1CCT-MRR	(Latch - Channel Type... noise Mode (1=white) Rate (Rate 11= use Tone Channel 2)
		out (&7F),a

		ld a,c
		xor %11111111	;Set Volume of noise
		out (&7F),a
	endif
ChibiSoundPro_Init:
ChibiSoundPro_Update:
	ret

silent:	;Mute both channels
		
		 ;1CCTVVVV
	ld a,%10011111	
	or b	;Channelnum
	out (&7F),a
	ret
	
	align 4
ChannelMask:	db %00000000,%00100000,%01000000,%00000000
ChannelNoise equ ChibiSoundRam+64