Vgm2FamiTrackerTxtAy38910.sdlbasic 10 KB

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