Vgm2FamiTrackerTxtSn76489.sdlbas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #! /usr/bin/sdlbrt
  2. finp$="tune.vgm"
  3. '- .vgm to VortexTracker .txt converter - SN-76489 version
  4. '- copyleft Paulo Silva, oct'20
  5. '-------------------------------
  6. '- bugs:
  7. '- - an acute sound might appear because an issue related to volume 0 that VortexTracker doesnt support, R-- note used instead (and the converter should store which note/frequency is for a probable volume changing, and set back the note/frequency value back)
  8. '- - doesnt read yet the author/title information
  9. '- - 'L' will be useful for the loop point location
  10. '- - probably will need a sample counter before converting
  11. '- - missing noise support
  12. '- - frequncy inaccuracy from the germanic notation formula probably fixed, needs more testing
  13. '-------------------------------
  14. ltxtm$="ROW .. : ... .. . ... : ... .. . ... : ... .. . ... : ... .. . ... : ... .. . ...":ltxcr$=ltxtm$
  15. freq0=0:freq1=0:freq2=0:vol0=0:vol1=0:vol2=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$+"_famitracker.txt":fout3$=finp$+"_famitracker_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-(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. hdram=0x040
  51. vgmv=hdrv[8]+hdrv[9]*256
  52. if vgmv>=0x0150 then:hdram=0x080:end if
  53. if vgmv>=0x0170 then:hdram=0x100:end if
  54. ttsam=hdrv[0x18]+hdrv[0x19]*256+hdrv[0x1A]*65536+hdrv[0x1B]*16777216
  55. hsam$="PlayOrder=L0"
  56. for i=1 to (ttsam/(frmi*patsz))-1
  57. hsam$=hsam$+","+str$(i)
  58. next
  59. open finp$ for input as #1
  60. open fout2$ for output as #2
  61. open fout3$ for output as #3
  62. print #2,"# FamiTracker text format":print #2,""
  63. print #2,"# Module information"
  64. print #2,"TITLE \"\""
  65. print #2,"AUTHOR \"\""
  66. print #2,"COPYRIGHT \"\"":print #2,""
  67. print #2,"# Module comment"
  68. print #2,"COMMENT \"\"":print #2,""
  69. print #2,"# Global settings"
  70. print #2,"MACHINE 0"
  71. print #2,"FRAMERATE 0"
  72. print #2,"EXPANSION 0"
  73. print #2,"VIBRATO 1"
  74. print #2,"SPLIT 32":print #2,""
  75. print #2,"# Macros":print #2,""
  76. print #2,"# DPCM samples":print #2,""
  77. print #2,"# Detune settings":print #2,""
  78. print #2,"# Grooves":print #2,""
  79. print #2,"# Tracks using default groove"
  80. print #2,"# Instruments"
  81. print #2,"INST2A03 0 -1 -1 -1 -1 -1 \"instrument01\"":print #2,""
  82. print #2,"# Tracks":print #2,""
  83. print #2,"TRACK "+str$(patsz)+" 1 128 \"test\""
  84. print #2,"COLUMNS : 1 1 1 1 1":print #2,""
  85. print #2,"Pattern 00"
  86. '- the amount of header bytes depends on vgm format version
  87. for eee=0 to hdram-1:q0=readbyte(1):next '- read offset byte first
  88. txou1$=" #("+str$(0)+")"
  89. while veof=0:
  90. q0=readbyte(1)
  91. '- vgm eof command
  92. if q0=0x66 then:
  93. veof=1
  94. print #3,"--vgm-eof--"
  95. print #2,""
  96. print #2,"# End of export"
  97. end if
  98. '- delay 1 byte (1..16 samples)
  99. if bitwiseand(q0,0xF0)=0x70 then
  100. frmc=frmc+bitwiseand(q0,0xF)+1
  101. txou1$=" #("+str$(frmc)+")"
  102. 'print #3,txou1$
  103. end if
  104. '- delay 3 bytes (0..65535 samples)
  105. if q0=0x61 then
  106. q0=readbyte(1)
  107. frmc=frmc+q0
  108. q0=readbyte(1)
  109. frmc=frmc+(q0*256)
  110. txou1$=" #("+str$(frmc)+")"
  111. 'print #3,txou1$
  112. end if
  113. '- 1 ntsc frame delay, 735 samples
  114. if q0=0x62 then
  115. frmc=frmc+735
  116. txou1$=" #("+str$(frmc)+")"
  117. 'print #3,txou1$
  118. end if
  119. '- 1 pal frame delay, 882 samples
  120. if q0=0x63 then
  121. frmc=frmc+882
  122. txou1$=" #("+str$(frmc)+")"
  123. 'print #3,txou1$
  124. end if
  125. '- updates frameout pulses
  126. while frmr<frmc
  127. '- writes 1F in the first line of the first pattern
  128. 'if (patc<1 and patid=0) then
  129. 'ltxcr$=replace$(12,ltxcr$,"1F")
  130. 'ltxcr$=replace$(26,ltxcr$,"1F")
  131. 'ltxcr$=replace$(40,ltxcr$,"1F")
  132. 'end if
  133. '- creates a new pattern
  134. if patc>(patsz-1) then:
  135. print #2," "
  136. patid=patid+1:patc=0
  137. 'txou9$="Pattern "+str$(patid)+"]"
  138. txou9$="Pattern "+right$("00000"+ucase$(hex$(patid)),2)
  139. print #2,txou9$
  140. end if
  141. '- writes a pattern line in each frame
  142. if trg=0 then
  143. print #3,"--frameout-unchanged--"
  144. 'print #2,ltxtm$
  145. ltxcr$=ltxtm$
  146. ltxcr$=replace$(4,ltxcr$, right$("00000"+ucase$(hex$(patc)),2) ) '?????? <-----
  147. print #2,ltxcr$
  148. else
  149. print #3,"--frameout--"
  150. ltxcr$=replace$(4,ltxcr$, right$("00000"+ucase$(hex$(patc)),2) ) '?????? <-----
  151. print #2,ltxcr$
  152. ltxcr$=ltxtm$
  153. trg=0
  154. end if
  155. frmr=frmr+frmi
  156. patc=patc+1
  157. end while
  158. '- token 0x50 gets registers and values to SN76489 output
  159. if q0=0x50 then
  160. q0=readbyte(1)
  161. if bitwiseand (q0,128)=0 then
  162. '- channel 0 - coarse
  163. if freqq=0 then
  164. freq0= bitwiseor ( (bitwiseand(q0,0x3F))*16,(bitwiseand(freq0,0x00F)) )
  165. ltxtm1$=ltxtm$
  166. ltxtm2$=replace$(9,ltxtm1$,nttfrq$(freq0))
  167. ltxcr$=replace$(9,ltxcr$,nttfrq$(freq0))
  168. print #3,ltxtm2$+" #("+str$(frmc)+")"
  169. trg=1
  170. end if
  171. '- channel 1 - coarse
  172. if freqq=1 then
  173. freq1= bitwiseor ( (bitwiseand(q0,0x3F))*16,(bitwiseand(freq1,0x00F)) )
  174. ltxtm1$=ltxtm$
  175. ltxtm2$=replace$(24,ltxtm1$,nttfrq$(freq1))
  176. ltxcr$=replace$(24,ltxcr$,nttfrq$(freq1))
  177. print #3,ltxtm2$+" #("+str$(frmc)+")"
  178. trg=1
  179. end if
  180. '- channel 2 - coarse
  181. if freqq=2 then
  182. freq2= bitwiseor ( (bitwiseand(q0,0x3F))*16,(bitwiseand(freq2,0x00F)) )
  183. ltxtm1$=ltxtm$
  184. ltxtm2$=replace$(39,ltxtm1$,nttfrq$(freq2))
  185. ltxcr$=replace$(39,ltxcr$,nttfrq$(freq2))
  186. print #3,ltxtm2$+" #("+str$(frmc)+")"
  187. trg=1
  188. end if
  189. end if
  190. if bitwiseand (q0,128)=128 then
  191. '- channel 0 - fine
  192. if bitwiseand (q0,0xF0)=0x80 then '- tone ch0
  193. freqq=0
  194. freq0= bitwiseor ( (bitwiseand(freq0,0xFF0)),(bitwiseand(q0,0x00F)) )
  195. ltxtm1$=ltxtm$
  196. ltxtm2$=replace$(9,ltxtm1$,nttfrq$(freq0))
  197. ltxcr$=replace$(9,ltxcr$,nttfrq$(freq0))
  198. print #3,ltxtm2$+" #("+str$(frmc)+")"
  199. trg=1
  200. end if
  201. '- channel 1 - fine
  202. if bitwiseand (q0,0xF0)=0xA0 then '- tone ch1
  203. freqq=1
  204. freq1= bitwiseor ( (bitwiseand(freq1,0xFF0)),(bitwiseand(q0,0x00F)) )
  205. ltxtm1$=ltxtm$
  206. ltxtm2$=replace$(24,ltxtm1$,nttfrq$(freq1))
  207. ltxcr$=replace$(24,ltxcr$,nttfrq$(freq1))
  208. print #3,ltxtm2$+" #("+str$(frmc)+")"
  209. trg=1
  210. end if
  211. '- channel 2 - fine
  212. if bitwiseand (q0,0xF0)=0xC0 then '- tone ch2
  213. freqq=2
  214. freq2= bitwiseor ( (bitwiseand(freq2,0xFF0)),(bitwiseand(q0,0x00F)) )
  215. ltxtm1$=ltxtm$
  216. ltxtm2$=replace$(39,ltxtm1$,nttfrq$(freq2))
  217. ltxcr$=replace$(39,ltxcr$,nttfrq$(freq2))
  218. print #3,ltxtm2$+" #("+str$(frmc)+")"
  219. trg=1
  220. end if
  221. 'if bitwiseand (q0,0xF0)=0xE0 then '- freq noise
  222. ' end if
  223. '- channel 0 - volume
  224. if bitwiseand (q0,0xF0)=0x90 then '- volume ch0
  225. 'freqq=0
  226. vol0=15-bitwiseand(q0,0xF)
  227. ltxtm1$=ltxtm$
  228. ltxtm2$=replace$(16,ltxtm1$,ucase$(right$(hex$(0x10+vol0),1)))
  229. ltxcr$=replace$(16,ltxcr$,ucase$(right$(hex$(0x10+vol0),1)))
  230. print #3,ltxtm2$+" #("+str$(frmc)+")"
  231. trg=1
  232. end if
  233. '- channel 1 - volume
  234. if bitwiseand (q0,0xF0)=0xB0 then '- volume ch1
  235. 'freqq=1
  236. vol1=15-bitwiseand(q0,0xF)
  237. ltxtm1$=ltxtm$
  238. ltxtm2$=replace$(31,ltxtm1$,ucase$(right$(hex$(0x10+vol1),1)))
  239. ltxcr$=replace$(31,ltxcr$,ucase$(right$(hex$(0x10+vol1),1)))
  240. print #3,ltxtm2$+" #("+str$(frmc)+")"
  241. trg=1
  242. end if
  243. '- channel 2 - volume
  244. if bitwiseand (q0,0xF0)=0xD0 then '- volume ch2
  245. 'freqq=2
  246. vol2=15-bitwiseand(q0,0xF)
  247. ltxtm1$=ltxtm$
  248. ltxtm2$=replace$(46,ltxtm1$,ucase$(right$(hex$(0x10+vol2),1)))
  249. ltxcr$=replace$(46,ltxcr$,ucase$(right$(hex$(0x10+vol2),1)))
  250. print #3,ltxtm2$+" #("+str$(frmc)+")"
  251. trg=1
  252. end if
  253. 'if bitwiseand (q0,0xF0)=0xF0 then '- volume ch3
  254. ' end if
  255. end if
  256. end if
  257. if eof(1)<>0 then
  258. veof=1
  259. print #2,""
  260. print #2,"# End of export"
  261. end if
  262. wend
  263. for eeq=0 to patid
  264. txou$="ORDER "+right$(hex$(0x100+eeq),2)+" :"
  265. for eer=0 to 4
  266. txou$=txou$+" "+right$(hex$(0x100+eeq),2)
  267. next
  268. print #2,txou$
  269. next
  270. print #2,""
  271. close #1:close #2:close #3
  272. if debug=0 then:
  273. shell("rm "+fout3$)
  274. end if
  275. 'print #2,"ORDER 00 : 00 00 00 00 00"
  276. 'print #2,"ORDER 01 : 01 01 01 01 01"
  277. 'print #2,"ORDER 02 : 02 02 02 02 02"
  278. 'print #2,"ORDER 03 : 03 03 03 03 03"
  279. 'print #2,"ORDER 04 : 04 04 04 04 04"
  280. 'print #2,"ORDER 05 : 05 05 05 05 05"
  281. 'print #2,"ORDER 06 : 06 06 06 06 06"
  282. 'print #2,"ORDER 07 : 07 07 07 07 07"
  283. 'print #2,"ORDER 08 : 08 08 08 08 08"
  284. 'print #2,"ORDER 09 : 09 09 09 09 09"
  285. 'print #2,"ORDER 0A : 0A 0A 0A 0A 0A"
  286. 'print #2,"ORDER 0B : 0B 0B 0B 0B 0B"
  287. 'print #2,"ORDER 0C : 0C 0C 0C 0C 0C"
  288. 'print #2,"ORDER 0D : 0D 0D 0D 0D 0D"
  289. 'print #2,"ORDER 0E : 0E 0E 0E 0E 0E"
  290. 'print #2,"ORDER 0F : 0F 0F 0F 0F 0F"
  291. 'print #2,""
  292. 'print #2,"[Module]"
  293. 'print #2,"VortexTrackerII=1"
  294. 'print #2,"Version=3.6"
  295. 'print #2,"Title="
  296. 'print #2,"Author="
  297. 'print #2,"NoteTable=2"
  298. 'print #2,"Speed=1"
  299. 'print #2,hsam$
  300. 'print #2," "
  301. 'print #2,"[Ornament1]":print #2,"L0":print #2," "
  302. 'print #2,"[Sample1]":print #2,"Tne +000_ +00_ F_ L":print #2," "