Vgm2FurnaceClipboardTxt_Sn76489x2_09.sdlbas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #! /usr/bin/sdlbrt
  2. finp$="song.vgm"
  3. cbstr$="org.tildearrow.furnace - Pattern Data (99)"
  4. '- .vgm to Furnace Tracker clipboard converter - SN-76489 version
  5. '- copyleft Paulo Silva, jul'22
  6. '-------------------------------
  7. '- bugs:
  8. '- 4000000/3579540=1.11746202025958643
  9. chqq=0
  10. dim frqaa[16],volaa[16]
  11. dim volka[16],wfca[32,32],frqka[16]
  12. frqaa[0]=0:frqaa[1]=0:frqaa[2]=0:frqaa[3]=0
  13. volaa[0]=0:volaa[1]=0:volaa[2]=0:volaa[3]=0
  14. ltxtm$="...........|...........|...........|...........|...........|...........|...........|...........|":ltxcr$=ltxtm$
  15. freq0=0:freq1=0:freq2=0:freq3=0:vol0=0:vol1=0:vol2=0:vol3=0
  16. patsz=64:patc=0:patid=0:freqq=0
  17. hdram=0x40:vgmv=0:veof=0
  18. dim hdrv[256]
  19. if argc>2 then:finp$=argv(2):end if
  20. fout2$=finp$+"_furnacetrackerclipboard.txt":fout3$=finp$+"_furnacetrackerclipboard_debug.txt"
  21. frmc=0:frmr=300:trg=0
  22. frmi=735
  23. if argc>3 then:
  24. if argv(3)="1" or ucase$(argv(2))="-PAL" then
  25. frmi=882:end if:end if
  26. debug=0
  27. if argc>4 then:
  28. if argv(4)="1" or ucase$(argv(2))="-DEBUG" then
  29. debug=1:end if:end if
  30. '- fix: 0x0FE=440hz=A-4
  31. function nttfrq$(freqb)
  32. e$= "C-1C#1D-1D#1E-1F-1F#1G-1G#1A-1A#1B-1"
  33. e$=e$+"C-2C#2D-2D#2E-2F-2F#2G-2G#2A-2A#2B-2"
  34. e$=e$+"C-3C#3D-3D#3E-3F-3F#3G-3G#3A-3A#3B-3"
  35. e$=e$+"C-4C#4D-4D#4E-4F-4F#4G-4G#4A-4A#4B-4"
  36. e$=e$+"C-5C#5D-5D#5E-5F-5F#5G-5G#5A-5A#5B-5"
  37. e$=e$+"C-6C#6D-6D#6E-6F-6F#6G-6G#6A-6A#6B-6"
  38. e$=e$+"C-7C#7D-7D#7E-7F-7F#7G-7G#7A-7A#7B-7"
  39. e$=e$+"C-8C#8D-8D#8E-8F-8F#8G-8G#8A-8A#8B-8..."
  40. tmq=141-24-(int((log(freqb*.99)/log(2))*12))
  41. if tmq<1 then:tmq=1:end if
  42. if tmq>97 then:tmq=97:end if
  43. return mid$(e$,(tmq*3)-2,3)
  44. end function
  45. open finp$ for input as #1
  46. for i=0 to 255
  47. hdrv[i]=readbyte(1)
  48. next
  49. close #1
  50. dim chipbt1[8],chipck[64]
  51. open finp$ for input as #1
  52. for j=0 to 63
  53. chipbt1[0]=readbyte(1)
  54. chipbt1[1]=readbyte(1)
  55. chipbt1[2]=readbyte(1)
  56. chipbt1[3]=readbyte(1)
  57. vlou=0
  58. vlou*=256:'vlou+=chipbt1[3]
  59. vlou*=256:vlou+=chipbt1[2]
  60. vlou*=256:vlou+=chipbt1[1]
  61. vlou*=256:vlou+=chipbt1[0]
  62. chipck[j]=vlou
  63. next
  64. close #1
  65. ckfct=chipck[3]/3579540
  66. hdram=0x040
  67. vgmv=hdrv[8]+hdrv[9]*256
  68. if vgmv>=0x0150 then:hdram=0x080:end if
  69. if vgmv>=0x0170 then:hdram=0x100:end if
  70. ttsam=hdrv[0x18]+hdrv[0x19]*256+hdrv[0x1A]*65536+hdrv[0x1B]*16777216
  71. hsam$="PlayOrder=L0"
  72. for i=1 to (ttsam/(frmi*patsz))-1
  73. hsam$=hsam$+","+str$(i)
  74. next
  75. open finp$ for input as #1
  76. open fout2$ for output as #2
  77. open fout3$ for output as #3
  78. print #2,"---------8<----------------------------------------------"
  79. print #2,"[Pattern00]"
  80. print #2," "
  81. print #2,cbstr$
  82. print #2,"0"
  83. '- the amount of header bytes depends on vgm format version
  84. for eee=0 to hdram-1:q0=readbyte(1):next '- read offset byte first
  85. txou1$=" #("+str$(0)+")"
  86. while veof=0:
  87. q0=readbyte(1)
  88. '- vgm eof command
  89. if q0=0x66 then:
  90. veof=1
  91. print #3,"--vgm-eof--"
  92. print #2,""
  93. 'print #2,"# End of export"
  94. end if
  95. '- delay 1 byte (1..16 samples)
  96. if bitwiseand(q0,0xF0)=0x70 then
  97. frmc=frmc+bitwiseand(q0,0xF)+1
  98. txou1$=" #("+str$(frmc)+")"
  99. 'print #3,txou1$
  100. end if
  101. '- delay 3 bytes (0..65535 samples)
  102. if q0=0x61 then
  103. q0=readbyte(1)
  104. frmc=frmc+q0
  105. q0=readbyte(1)
  106. frmc=frmc+(q0*256)
  107. txou1$=" #("+str$(frmc)+")"
  108. 'print #3,txou1$
  109. end if
  110. '- 1 ntsc frame delay, 735 samples
  111. if q0=0x62 then
  112. frmc=frmc+735
  113. txou1$=" #("+str$(frmc)+")"
  114. 'print #3,txou1$
  115. end if
  116. '- 1 pal frame delay, 882 samples
  117. if q0=0x63 then
  118. frmc=frmc+882
  119. txou1$=" #("+str$(frmc)+")"
  120. 'print #3,txou1$
  121. end if
  122. '- updates frameout pulses
  123. while frmr<frmc
  124. '- creates a new pattern
  125. if patc>(patsz-1) then:
  126. print #2," "
  127. patid=patid+1:patc=0
  128. 'txou9$="Pattern "+str$(patid)+"]"
  129. txou9$="Pattern "+right$("00000"+ucase$(hex$(patid)),2)
  130. 'print #2,txou9$
  131. print #2,"---------8<----------------------------------------------"
  132. print #2,"["+txou9$+"]"
  133. print #2," "
  134. print #2,cbstr$
  135. print #2,"0"
  136. end if
  137. '- writes a pattern line in each frame
  138. if trg=0 then
  139. print #3,"--frameout-unchanged--"
  140. ltxcr$=ltxtm$
  141. print #2,ltxcr$
  142. else
  143. print #3,"--frameout--"
  144. print #2,ltxcr$
  145. ltxcr$=ltxtm$
  146. trg=0
  147. end if
  148. frmr=frmr+frmi
  149. patc=patc+1
  150. end while
  151. '- token 0x50 gets registers and values to SN76489 output '- 0x30 second chip
  152. if q0=0x50 or q0=0x30 then
  153. chchp=0
  154. if q0=0x30 then:chchp=1:end if
  155. q0=readbyte(1)
  156. '- channel 0..3 - coarse
  157. if bitwiseand (q0,0x80)=0x00 then
  158. frqaa[chqq]= bitwiseor ( (bitwiseand(q0,0x3F))*16,(bitwiseand(frqaa[chqq],0x00F)) )
  159. ltxtm1$=ltxtm$
  160. ltxtm2$=replace$((12*chqq)+0,ltxtm1$,nttfrq$(frqaa[chqq]/ckfct)+"00")
  161. ltxcr$=replace$((12*chqq)+0,ltxcr$,nttfrq$(frqaa[chqq]/ckfct)+"00")
  162. print #3,ltxtm2$+" #("+str$(frmc)+")"
  163. trg=1
  164. end if
  165. '- channel 0..3 - fine
  166. if bitwiseand(q0,0x90)=0x80 then
  167. chqq=(bitwiseand(q0,0x60)/32)+(chchp*4)
  168. freqq=chqq
  169. frqaa[chqq]= bitwiseor ( (bitwiseand(frqaa[chqq],0xFF0)),(bitwiseand(q0,0x00F)) )
  170. ltxtm1$=ltxtm$
  171. ltxtm2$=replace$((12*chqq)+0,ltxtm1$,nttfrq$(frqaa[chqq]/ckfct)+"00")
  172. ltxcr$=replace$((12*chqq)+0,ltxcr$,nttfrq$(frqaa[chqq]/ckfct)+"00")
  173. print #3,ltxtm2$+" #("+str$(frmc)+")"
  174. trg=1
  175. end if
  176. '- channel 0..3 - volume
  177. if bitwiseand(q0,0x90)=0x90 then
  178. chqq=(bitwiseand(q0,0x60)/32)+(chchp*4)
  179. volaa[chqq]=15-bitwiseand(q0,0xF)
  180. ltxtm1$=ltxtm$
  181. ltxtm2$=replace$((12*chqq)+5,ltxtm1$,ucase$(right$(hex$(0x100+volaa[chqq]),2)))
  182. ltxcr$=replace$((12*chqq)+5,ltxcr$,ucase$(right$(hex$(0x100+volaa[chqq]),2)))
  183. print #3,ltxtm2$+" #("+str$(frmc)+")"
  184. trg=1
  185. end if
  186. end if
  187. if eof(1)<>0 then
  188. veof=1
  189. print #2,""
  190. end if
  191. wend
  192. print #2,"---------8<----------------------------------------------"
  193. print #2,""
  194. close #1:close #2:close #3
  195. if debug=0 then:
  196. shell("rm "+fout3$)
  197. end if