Vgm2FamiTrackerTxtC03051.sdlbasic 9.2 KB

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