123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #! /usr/bin/sdlbrt
- finp$="song.vgm"
- cbstr$="org.tildearrow.furnace - Pattern Data (99)"
- '- .vgm to Furnace Tracker clipboard converter - SN-76489 version
- '- copyleft Paulo Silva, jul'22
- '-------------------------------
- '- bugs:
- '- 4000000/3579540=1.11746202025958643
- chqq=0
- dim frqaa[16],volaa[16]
- dim volka[16],wfca[32,32],frqka[16]
- frqaa[0]=0:frqaa[1]=0:frqaa[2]=0:frqaa[3]=0
- volaa[0]=0:volaa[1]=0:volaa[2]=0:volaa[3]=0
- ltxtm$="...........|...........|...........|...........|...........|...........|...........|...........|":ltxcr$=ltxtm$
- freq0=0:freq1=0:freq2=0:freq3=0:vol0=0:vol1=0:vol2=0:vol3=0
- patsz=64:patc=0:patid=0:freqq=0
- hdram=0x40:vgmv=0:veof=0
- dim hdrv[256]
-
- if argc>2 then:finp$=argv(2):end if
- fout2$=finp$+"_furnacetrackerclipboard.txt":fout3$=finp$+"_furnacetrackerclipboard_debug.txt"
-
- frmc=0:frmr=300:trg=0
-
- frmi=735
- if argc>3 then:
- if argv(3)="1" or ucase$(argv(2))="-PAL" then
- frmi=882:end if:end if
-
- debug=0
- if argc>4 then:
- if argv(4)="1" or ucase$(argv(2))="-DEBUG" then
- debug=1:end if:end if
-
- '- fix: 0x0FE=440hz=A-4
- function nttfrq$(freqb)
- e$= "C-1C#1D-1D#1E-1F-1F#1G-1G#1A-1A#1B-1"
- e$=e$+"C-2C#2D-2D#2E-2F-2F#2G-2G#2A-2A#2B-2"
- e$=e$+"C-3C#3D-3D#3E-3F-3F#3G-3G#3A-3A#3B-3"
- e$=e$+"C-4C#4D-4D#4E-4F-4F#4G-4G#4A-4A#4B-4"
- e$=e$+"C-5C#5D-5D#5E-5F-5F#5G-5G#5A-5A#5B-5"
- e$=e$+"C-6C#6D-6D#6E-6F-6F#6G-6G#6A-6A#6B-6"
- e$=e$+"C-7C#7D-7D#7E-7F-7F#7G-7G#7A-7A#7B-7"
- e$=e$+"C-8C#8D-8D#8E-8F-8F#8G-8G#8A-8A#8B-8..."
- tmq=141-24-(int((log(freqb*.99)/log(2))*12))
- if tmq<1 then:tmq=1:end if
- if tmq>97 then:tmq=97:end if
- return mid$(e$,(tmq*3)-2,3)
- end function
- open finp$ for input as #1
- for i=0 to 255
- hdrv[i]=readbyte(1)
- next
- close #1
- dim chipbt1[8],chipck[64]
- open finp$ for input as #1
- for j=0 to 63
- chipbt1[0]=readbyte(1)
- chipbt1[1]=readbyte(1)
- chipbt1[2]=readbyte(1)
- chipbt1[3]=readbyte(1)
- vlou=0
- vlou*=256:'vlou+=chipbt1[3]
- vlou*=256:vlou+=chipbt1[2]
- vlou*=256:vlou+=chipbt1[1]
- vlou*=256:vlou+=chipbt1[0]
- chipck[j]=vlou
- next
- close #1
- ckfct=chipck[3]/3579540
-
- hdram=0x040
- vgmv=hdrv[8]+hdrv[9]*256
- if vgmv>=0x0150 then:hdram=0x080:end if
- if vgmv>=0x0170 then:hdram=0x100:end if
-
- ttsam=hdrv[0x18]+hdrv[0x19]*256+hdrv[0x1A]*65536+hdrv[0x1B]*16777216
- hsam$="PlayOrder=L0"
- for i=1 to (ttsam/(frmi*patsz))-1
- hsam$=hsam$+","+str$(i)
- next
-
- open finp$ for input as #1
- open fout2$ for output as #2
- open fout3$ for output as #3
- print #2,"---------8<----------------------------------------------"
- print #2,"[Pattern00]"
- print #2," "
- print #2,cbstr$
- print #2,"0"
- '- the amount of header bytes depends on vgm format version
- for eee=0 to hdram-1:q0=readbyte(1):next '- read offset byte first
- txou1$=" #("+str$(0)+")"
-
- while veof=0:
- q0=readbyte(1)
-
- '- vgm eof command
- if q0=0x66 then:
- veof=1
- print #3,"--vgm-eof--"
- print #2,""
- 'print #2,"# End of export"
- end if
-
- '- delay 1 byte (1..16 samples)
- if bitwiseand(q0,0xF0)=0x70 then
- frmc=frmc+bitwiseand(q0,0xF)+1
- txou1$=" #("+str$(frmc)+")"
- 'print #3,txou1$
- end if
-
- '- delay 3 bytes (0..65535 samples)
- if q0=0x61 then
- q0=readbyte(1)
- frmc=frmc+q0
- q0=readbyte(1)
- frmc=frmc+(q0*256)
- txou1$=" #("+str$(frmc)+")"
- 'print #3,txou1$
- end if
-
- '- 1 ntsc frame delay, 735 samples
- if q0=0x62 then
- frmc=frmc+735
- txou1$=" #("+str$(frmc)+")"
- 'print #3,txou1$
- end if
-
- '- 1 pal frame delay, 882 samples
- if q0=0x63 then
- frmc=frmc+882
- txou1$=" #("+str$(frmc)+")"
- 'print #3,txou1$
- end if
-
- '- updates frameout pulses
- while frmr<frmc
-
- '- creates a new pattern
- if patc>(patsz-1) then:
- print #2," "
- patid=patid+1:patc=0
- 'txou9$="Pattern "+str$(patid)+"]"
- txou9$="Pattern "+right$("00000"+ucase$(hex$(patid)),2)
- 'print #2,txou9$
- print #2,"---------8<----------------------------------------------"
- print #2,"["+txou9$+"]"
- print #2," "
- print #2,cbstr$
- print #2,"0"
- end if
-
- '- writes a pattern line in each frame
- if trg=0 then
- print #3,"--frameout-unchanged--"
- ltxcr$=ltxtm$
- print #2,ltxcr$
- else
- print #3,"--frameout--"
- print #2,ltxcr$
- ltxcr$=ltxtm$
- trg=0
- end if
-
- frmr=frmr+frmi
- patc=patc+1
- end while
- '- token 0x50 gets registers and values to SN76489 output '- 0x30 second chip
- if q0=0x50 or q0=0x30 then
- chchp=0
- if q0=0x30 then:chchp=1:end if
- q0=readbyte(1)
- '- channel 0..3 - coarse
- if bitwiseand (q0,0x80)=0x00 then
- frqaa[chqq]= bitwiseor ( (bitwiseand(q0,0x3F))*16,(bitwiseand(frqaa[chqq],0x00F)) )
- ltxtm1$=ltxtm$
- ltxtm2$=replace$((12*chqq)+0,ltxtm1$,nttfrq$(frqaa[chqq]/ckfct)+"00")
- ltxcr$=replace$((12*chqq)+0,ltxcr$,nttfrq$(frqaa[chqq]/ckfct)+"00")
- print #3,ltxtm2$+" #("+str$(frmc)+")"
- trg=1
- end if
- '- channel 0..3 - fine
- if bitwiseand(q0,0x90)=0x80 then
- chqq=(bitwiseand(q0,0x60)/32)+(chchp*4)
- freqq=chqq
- frqaa[chqq]= bitwiseor ( (bitwiseand(frqaa[chqq],0xFF0)),(bitwiseand(q0,0x00F)) )
- ltxtm1$=ltxtm$
- ltxtm2$=replace$((12*chqq)+0,ltxtm1$,nttfrq$(frqaa[chqq]/ckfct)+"00")
- ltxcr$=replace$((12*chqq)+0,ltxcr$,nttfrq$(frqaa[chqq]/ckfct)+"00")
- print #3,ltxtm2$+" #("+str$(frmc)+")"
- trg=1
- end if
- '- channel 0..3 - volume
- if bitwiseand(q0,0x90)=0x90 then
- chqq=(bitwiseand(q0,0x60)/32)+(chchp*4)
- volaa[chqq]=15-bitwiseand(q0,0xF)
- ltxtm1$=ltxtm$
- ltxtm2$=replace$((12*chqq)+5,ltxtm1$,ucase$(right$(hex$(0x100+volaa[chqq]),2)))
- ltxcr$=replace$((12*chqq)+5,ltxcr$,ucase$(right$(hex$(0x100+volaa[chqq]),2)))
- print #3,ltxtm2$+" #("+str$(frmc)+")"
- trg=1
- end if
- end if
-
- if eof(1)<>0 then
- veof=1
- print #2,""
- end if
- wend
- print #2,"---------8<----------------------------------------------"
- print #2,""
- close #1:close #2:close #3
- if debug=0 then:
- shell("rm "+fout3$)
- end if
-
|