Vgm2FamiTrackerTxtRp2c33.sdlbasic 10 KB

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