Skip to content

Instantly share code, notes, and snippets.

@32bitkid
Last active August 30, 2021 22:35
Show Gist options
  • Save 32bitkid/b95306cf6500593c451bedc1cbe6e346 to your computer and use it in GitHub Desktop.
Save 32bitkid/b95306cf6500593c451bedc1cbe6e346 to your computer and use it in GitHub Desktop.
Play a C-Chord on the OPL2 from QuickBasic.
DECLARE FUNCTION GetFNumFn% (block%, hz#)
DECLARE FUNCTION GetBlockNumFn% (hz#)
DECLARE SUB InitChannel (channel%)
DECLARE SUB EndNote (channel%)
DECLARE SUB PlayNote (channel%, hz#)
DECLARE SUB SetReg (Reg%, Value%)
CONST BaseAddr% = &H380
CLS : DEFINT A-Z
FOR i = 0 TO 224: SetReg i, 0: NEXT
' Set Up Channels
FOR i% = 0 TO 2: InitChannel i%: NEXT
' Play C-Chord
PlayNote 0, 261.63
PlayNote 1, 329.63
PlayNote 2, 392!
SLEEP: FOR i% = 0 TO 2: EndNote i%: NEXT
SUB EndNote (channel%)
SetReg &HB0 + channel%, 0
END SUB
FUNCTION GetBlockNumFn% (hz#)
FOR i = 0 TO 7
max& = 1024 / (2 ^ (20 - i) / 49176)
IF max& > hz# THEN GOTO done
NEXT
done:
GetBlockNumFn% = i
END FUNCTION
FUNCTION GetFNumFn% (block%, hz#)
GetFNumFn% = hz# * 2 ^ (20 - block%) / 49716
END FUNCTION
SUB InitChannel (channel%)
SetReg &H20 + channel%, &H1
SetReg &H23 + channel%, &H1
SetReg &H40 + channel%, &H1F
SetReg &H43 + channel%, &H0
SetReg &H60 + channel%, &HE4
SetReg &H63 + channel%, &HE4
SetReg &H80 + channel%, &H9D
SetReg &H83 + channel%, &H9D
END SUB
SUB PlayNote (channel%, hz#)
b% = GetBlockNumFn(hz#)
n% = GetFNumFn(b%, hz#)
lo% = n% AND 255
hi% = 32 OR INT(b% * 4) OR INT(n% / 256)
SetReg &HA0 + channel%, lo%
SetReg &HB0 + channel%, hi%
END SUB
DEFSNG A-Z
SUB SetReg (Reg%, Value%)
OUT BaseAddr% + 8, Reg%
OUT BaseAddr% + 9, Value%
END SUB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment