Vgm2FurnaceClipboardTxt_ym2203_02.sdlbas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #! /usr/bin/sdlbrt
  2. finp$="song.vgm"
  3. cbstr$="org.tildearrow.furnace - Pattern Data (103)"
  4. '- .vgm to furnace tracker clipboard converter - YM2203
  5. '- copyleft nitrofurano, aug'22
  6. '-------------------------------
  7. '- bugs:
  8. '- -
  9. '-------------------------------
  10. ltxtmp$="...........|"
  11. ltxtm$="":for i=1 to 6:ltxtm$+=ltxtmp$:next
  12. 'ltxtm$="...........|...........|...........|...........|...........|...........|"
  13. ltxcr$=ltxtm$
  14. instch$="0102030404040404"
  15. dim frqaa[16],frqka[16],volaa[16],volka[16],wfca[32,32]
  16. dim hdrv[256],wfc0[32],wfc1[32],wfc2[32],wfc3[32],wfc4[32]
  17. dim fnuml[16],fnumh[16],fblk[16],fnumw[16],ffqv[16]
  18. frqaa[0]=0:frqaa[1]=0:frqaa[2]=0:volaa[0]=0:volaa[1]=0:volaa[2]=0
  19. patsz=64:patc=0:patid=0:freqq=0:hdram=0x40:vgmv=0:veof=0
  20. frqka[0]=0:frqka[1]=0:frqka[2]=0:frqka[3]=0:frqka[4]=0
  21. volka[0]=0:volka[1]=0:volka[2]=0:volka[3]=0:volka[4]=0
  22. if argc>2 then:finp$=argv(2):end if
  23. fout2$=finp$+"_furnaceclipboard.txt":fout3$=finp$+"_furnaceclipboard_debug.txt"
  24. frmc=0:frmr=300:trg=0
  25. frmi=735
  26. if argc>3 then:
  27. if argv(3)="1" or ucase$(argv(2))="-PAL" then
  28. frmi=882:end if:end if
  29. debug=0
  30. if argc>4 then:
  31. if argv(4)="1" or ucase$(argv(2))="-DEBUG" then
  32. debug=1:end if:end if
  33. 'debug=1 '- remove this linecode later... xD
  34. '- fix: 0x0FE=440hz=A-4
  35. function nttfrqo$(freqb,opc)
  36. e$= "C-1C#1D-1D#1E-1F-1F#1G-1G#1A-1A#1B-1"
  37. e$=e$+"C-2C#2D-2D#2E-2F-2F#2G-2G#2A-2A#2B-2"
  38. e$=e$+"C-3C#3D-3D#3E-3F-3F#3G-3G#3A-3A#3B-3"
  39. e$=e$+"C-4C#4D-4D#4E-4F-4F#4G-4G#4A-4A#4B-4"
  40. e$=e$+"C-5C#5D-5D#5E-5F-5F#5G-5G#5A-5A#5B-5"
  41. e$=e$+"C-6C#6D-6D#6E-6F-6F#6G-6G#6A-6A#6B-6"
  42. e$=e$+"C-7C#7D-7D#7E-7F-7F#7G-7G#7A-7A#7B-7"
  43. e$=e$+"B-7B-7B-7B-7B-7B-7B-7B-7B-7B-7B-7B-7"
  44. e$=e$+"B-7B-7B-7B-7B-7B-7B-7B-7B-7B-7B-7B-7..."
  45. if opc=0 then
  46. tmq=141-(int((log(freqb*.99)/log(2))*12))
  47. else
  48. tmq=(int((log(freqb)/log(2))*12))-112-12
  49. end if
  50. if tmq<1 then:tmq=1:end if
  51. if tmq>97 then:tmq=97:end if
  52. return mid$(e$,(tmq*3)-2,3)
  53. end function
  54. open finp$ for input as #1
  55. for i=0 to 255
  56. hdrv[i]=readbyte(1)
  57. next
  58. close #1
  59. hdram=0x040
  60. vgmv=hdrv[8]+hdrv[9]*256
  61. if vgmv>=0x0150 then:hdram=0x080:end if
  62. if vgmv>=0x0160 then:hdram=0x0C0:end if
  63. if vgmv>=0x0170 then:hdram=0x100:end if
  64. '- wrong information???
  65. ttsam=hdrv[0x18]+hdrv[0x19]*256+hdrv[0x1A]*65536+hdrv[0x1B]*16777216
  66. hsam$="PlayOrder=L0"
  67. for i=1 to (ttsam/(frmi*patsz))-1
  68. hsam$=hsam$+","+str$(i)
  69. next
  70. open finp$ for input as #1
  71. open fout2$ for output as #2
  72. open fout3$ for output as #3
  73. print #2,"---------8<----------------------------------------------"
  74. print #2,"[Pattern00]"
  75. print #2," "
  76. print #2,cbstr$
  77. print #2,"0"
  78. '- the amount of header bytes depends on vgm format version
  79. for eee=0 to hdram-1:q0=readbyte(1):next '- read offset byte first
  80. txou1$=" #("+str$(0)+")"
  81. while veof=0:
  82. q0=readbyte(1)
  83. '- vgm eof command
  84. if q0=0x66 then:
  85. veof=1
  86. print #3,"--vgm-eof--"
  87. print #2," "
  88. end if
  89. '- delay 1 byte (1..16 samples)
  90. if bitwiseand(q0,0x70)=0x70 then
  91. frmc=frmc+bitwiseand(q0,0xF)+1
  92. txou1$=" #("+str$(frmc)+")"
  93. 'print #3,txou1$
  94. end if
  95. '- delay 3 bytes (0..65535 samples)
  96. if q0=0x61 then
  97. q0=readbyte(1)
  98. frmc=frmc+q0
  99. q0=readbyte(1)
  100. frmc=frmc+(q0*256)
  101. txou1$=" #("+str$(frmc)+")"
  102. 'print #3,txou1$
  103. end if
  104. '- 1 ntsc frame delay, 735 samples
  105. if q0=0x62 then
  106. frmc=frmc+735
  107. txou1$=" #("+str$(frmc)+")"
  108. 'print #3,txou1$
  109. end if
  110. '- 1 pal frame delay, 882 samples
  111. if q0=0x63 then
  112. frmc=frmc+882
  113. txou1$=" #("+str$(frmc)+")"
  114. 'print #3,txou1$
  115. end if
  116. '- updates frameout pulses
  117. while frmr<frmc
  118. '- creates a new pattern
  119. if patc>(patsz-1) then:
  120. print #2," "
  121. patid=patid+1:patc=0
  122. txou9$="[Pattern"+ucase$(right$(hex$(0x100+patid),2))+"]"
  123. print #2,"---------8<----------------------------------------------"
  124. print #2,txou9$
  125. print #2,""
  126. print #2,cbstr$
  127. print #2,"0"
  128. end if
  129. '- writes a pattern line in each frame
  130. if trg=0 then
  131. print #3,"--frameout-unchanged--"
  132. print #2,ltxtm$
  133. else
  134. print #3,"--frameout--"
  135. print #2,ltxcr$
  136. ltxcr$=ltxtm$
  137. trg=0
  138. end if
  139. frmr=frmr+frmi
  140. patc=patc+1
  141. end while
  142. '-------------------------------------------------------------------------------
  143. '-------------------------------------------------------------------------------
  144. '- ym2203
  145. '- token 0x55 gets registers and values to ym2203 output
  146. if q0=0x55 then
  147. q0=readbyte(1)
  148. '-------------------------------------------------------------------------------
  149. '- frequencies/notes on psg - channel 3 to 5
  150. if (q0>=0 and q0<=5) then
  151. q0qq=q0
  152. qqch=int(q0/2)+3
  153. q0=readbyte(1)
  154. if q0qq mod 2=0 then
  155. frqaa[qqch]=bitwiseor((bitwiseand(frqaa[qqch],0xF00)),(bitwiseand(q0,0x0FF))) '- fine
  156. else
  157. frqaa[qqch]=bitwiseor((bitwiseand(q0*256,0xF00)),(bitwiseand(frqaa[qqch],0x0FF))) '- coarse
  158. end if
  159. ltxtm1$=ltxtm$
  160. ltxtm2$=replace$((12*qqch),ltxtm1$,nttfrqo$(frqaa[qqch],0)+"00")
  161. ltxcr$=replace$((12*qqch),ltxcr$,nttfrqo$(frqaa[qqch],0)+"00")
  162. print #3,ltxtm2$
  163. trg=1
  164. end if
  165. '-------------------------------------------------------------------------------
  166. '- volume on psg, channels 3 to 5
  167. if (q0>=8 and q0<=10) then
  168. chqq=(q0-8)+3
  169. q0=readbyte(1)
  170. volaa[chqq]=bitwiseand(q0,0x0F)
  171. ltxtm1$=ltxtm$
  172. ltxtm2$=replace$((12*chqq)+5,ltxtm1$,""+ucase$(right$(hex$(0x100+volaa[chqq]*1),2)))
  173. ltxcr$=replace$((12*chqq)+5,ltxcr$,""+ucase$(right$(hex$(0x100+volaa[chqq]*1),2)))
  174. print #3,ltxtm2$
  175. trg=1
  176. end if
  177. '-------------------------------------------------------------------------------
  178. '-fnum1
  179. if bitwiseand(q0,0xFC)=0xA0 then
  180. q3=bitwiseand(q0,0x03)
  181. q0=readbyte(1)
  182. fnuml[q3]=q0
  183. fnumw[q3]=fnuml[q3]+(bitwiseand(fnumh[q3],0x07)*256)
  184. fblk[q3]=(bitwiseand(fnumh[q3],0x38)/8)
  185. ffqv[q3]=fnumw[q3]*(2^fblk[q3])
  186. ltxtm1$=ltxtm$
  187. ltxtm2$=replace$((12*q3),ltxtm1$,nttfrqo$(ffqv[q3],1)+"00")
  188. ltxcr$=replace$((12*q3),ltxcr$,nttfrqo$(ffqv[q3],1)+"00")
  189. print #3,ltxtm2$
  190. trg=1
  191. end if
  192. '-fnum2,block
  193. if bitwiseand(q0,0xFC)=0xA4 then
  194. q3=bitwiseand(q0,0x03)
  195. q0=readbyte(1)
  196. fnumh[q3]=q0
  197. fnumw[q3]=fnuml[q3]+(bitwiseand(fnumh[q3],0x07)*256)
  198. fblk[q3]=(bitwiseand(fnumh[q3],0x38)/8)
  199. ffqv[q3]=fnumw[q3]*(2^fblk[q3])
  200. ltxtm1$=ltxtm$
  201. ltxtm2$=replace$((12*q3),ltxtm1$,nttfrqo$(ffqv[q3],1)+"00")
  202. ltxcr$=replace$((12*q3),ltxcr$,nttfrqo$(ffqv[q3],1)+"00")
  203. print #3,ltxtm2$
  204. trg=1
  205. end if
  206. '-------------------------------------------------------------------------------
  207. end if
  208. '-------------------------------------------------------------------------------
  209. '-------------------------------------------------------------------------------
  210. if eof(1)<>0 then:veof=1:end if
  211. wend
  212. '--------------------------------
  213. close #1:close #2:close #3
  214. if debug=0 then:
  215. shell("rm "+fout3$)
  216. end if