home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 709.dms / 709.adf / Sonix.AMOS / Sonix.amosSourceCode
Encoding:
AMOS Source Code  |  1978-10-10  |  10.0 KB  |  412 lines

  1. '--------------------------------------------------------------------------- 
  2. '               Sonix (IFF .SMUS) Music file converter   
  3. '
  4. '                        By Francois Lionet  
  5. '                    (c) Mandarin / Jawx 1990  
  6. '--------------------------------------------------------------------------- 
  7. ' This program converts IFF SMUS files to AMOS music bank format.  
  8. '
  9. ' Because the AMOS music system is closer to the SoundTracker format, this 
  10. ' converter can only use IFF samples with just ONE sample for all octaves. 
  11. '
  12. ' If your music contains multiple octave samples or the converter fails to 
  13. ' locate the required sample from the disc, a file selector will open and  
  14. ' you will be requested to allocate a sample. It is thus important to create 
  15. ' some IFF samples before you use this program.
  16. '
  17. ' You can also load a fake instrument (something small like a TOM for example) 
  18. ' into each instrument used by the song. This will create a fake bank with the 
  19. ' melody converted. You will then be able to edit it with the AMOS Music 
  20. ' editor (to be released soon!)... 
  21. '                  *** NTSC users see below!!! *** 
  22. '---------------------------------------------------------------------------   
  23. '
  24. Set Buffer 16
  25. '
  26. Dim CHK$(8),INS$(32),V$(4),APAT(4),NTE(128+12),SAM_RATE(128),DELAY(16),DELTA#(4),FLAG(4)
  27. Global CHK$(),PCHUNK,LCHUNK
  28. Global INS$(),V$(),MS_TEMPO,MS_VOL,NTRACK,ADPOKE,VCE,MXINS,NAME$,SAM_RATE()
  29. '
  30. ' I use the ram disk as temporary storage... 
  31. F_INST$="RAM:SMus_Conv.Temp"
  32. '
  33. ' Change the next line if you have a NTSC system 
  34. ' PAL clock
  35. CLOCK#=1.0/3546895
  36. '
  37. ' NTSC clock 
  38. ' clock#=1.0/3579545 
  39. '
  40. Restore TNOTE
  41. For N=12 To 108
  42.    Read NTE(N)
  43. Next 
  44. Restore TDELAY
  45. For N=0 To 15
  46.    Read DELAY(N)
  47. Next 
  48. Restore TDELTA
  49. For N=0 To 3
  50.    Read DELTA#(N)
  51. Next 
  52. '
  53. I_END=$8000
  54. I_SLUP=$8100
  55. I_SLDOWN=$8200
  56. I_SVOL=$8300
  57. I_REP=$8500
  58. I_LEDM=$8600
  59. I_LEDA=$8700
  60. I_TEMPO=$8800
  61. I_INST=$8900
  62. SONGDATA=4
  63. '
  64. AGAIN:
  65. On Error Goto D_ERROR
  66. Screen Open 0,640,32,2,Hires : Curs Off 
  67. Palette 0,$FFF
  68. '
  69. '---> Load and store SMUS file   
  70. F$=Fsel$("*.SMUS","","Please choose SONG to convert")
  71. If F$="" : ABORT : End If 
  72. VCE=0 : MXINS=0
  73. CHK$(0)="SMUS" : CHK$(1)="SHDR" : CHK$(2)="INS1" : CHK$(3)="TRAK"
  74. RD_IFF[F$,0]
  75. If Param=1 : Bell : Print "This file is not an IFF music file!" : Print "Press a key to restart..." : Wait Key : Goto AGAIN : End If 
  76. If Param : Error 32 : End If 
  77. '
  78. '---> Compute instrument list
  79. Open Out 2,F_INST$
  80. Print #2,Chr$(0)+Chr$(0)+Chr$(0)+Chr$(0);
  81. '
  82. Reserve As Work 10,32*MXINS+34
  83. Doke Start(10),MXINS
  84. '
  85. For NI=0 To MXINS
  86.    If INS$(NI)<>""
  87.       CHK$(0)="8SVX" : CHK$(1)="VHDR" : CHK$(2)="BODY"
  88.       Print 
  89.       Print "* Processing "+INS$(NI)
  90.       NAME$=INS$(NI)
  91.       I$=PATH_INS$+INS$(NI)
  92.       FLAG=-1
  93.       If Exist(I$)
  94.          RD_IFF[I$,NI]
  95.          FLAG=Param
  96.       Else 
  97.          If Exist(I$+".Instr")
  98.             RD_IFF[I$+".Instr",NI]
  99.             FLAG=Param
  100.          Else 
  101.             If Exist(I$+"(IFF).Instr")
  102.                RD_IFF[I$+"(IFF).Instr",NI]
  103.                FLAG=Param
  104.             End If 
  105.          End If 
  106.       End If 
  107.       Repeat 
  108.          If FLAG<0 : Print "  I can't find "+I$+" !" : End If 
  109.          If FLAG=1 : Print "  This instrument is not an IFF SINGLE sample sound!" : End If 
  110.          If FLAG>1 : Bell : Print : Print "  Disk error!" : End If 
  111.          If FLAG
  112.             Print "  Please click and use the file selector to choose another instrument..."
  113.             WT_CLIC
  114.             I$=Fsel$("**","","Choose a new instrument for",INS$(NI))
  115.             If I$="" : ABORT : End If 
  116.             EXTRACT_NAME[I$]
  117.             RD_IFF[I$,NI] : FLAG=Param
  118.          End If 
  119.       Until FLAG=0
  120.    End If 
  121. Next 
  122. Close 2
  123. '  
  124. '---> Open output
  125. F$=Fsel$("*.Abk","","Please enter output bank name")
  126. If F$="" : ABORT : End If 
  127. On Error Goto D_ERROR
  128. Open Out 2,F$
  129. Print #2,"AmBk........Music   ";
  130. Print #2,"................";
  131. '
  132. ' Copy instruments 
  133. Print "* Saving instruments";
  134. DTA_COPY[Start(10),Length(10)]
  135. Open In 1,F_INST$
  136. FILE_COPY[Lof(1)]
  137. Print 
  138. LINST=Lof(1)+Length(10)
  139. Close 1
  140. Erase 10 : Kill F_INST$
  141. '
  142. ' Compute pattern  
  143. L=0
  144. For V=0 To 3
  145.    L=L+Len(V$(V))*3
  146. Next 
  147. Reserve As Work 10,L
  148. NPAT=4 : AOFF=Start(10) : APAT=AOFF+2+NPAT*2
  149. Doke AOFF,NPAT
  150. For V=0 To 3
  151.    Print "* Processing track";V
  152.    Doke AOFF+2+V*2,APAT-AOFF
  153.    FLAG(V)=0 : CU_I=0 : KY=0
  154.    If V$(V)<>""
  155.       Doke APAT,I_INST : Add APAT,2
  156.       For P=0 To Len(V$(V))-2 Step 2
  157.          P1=Peek(Varptr(V$(V))+P) : P2=Peek(Varptr(V$(V))+P+1)
  158.          If P1<=128
  159.             Inc FLAG(V)
  160.             F=NTE(P1+KY) : PER=0
  161.             If F
  162.                PER#=1.0/(SAM_RATE(CU_I)*F*CLOCK#)
  163.                PER=PER#+0.5
  164.                If PER<124 : PER=0 : End If 
  165.             End If 
  166.             D=(P2 and %110000)/%10000
  167.             D#=DELAY(P2 and %1111)*DELTA#(D)
  168.             DEL=D# : Add DELTA#,DEL-D#
  169.             If DELTA#<>0.0
  170.                If Int(DELTA#)=DELTA#
  171.                   Add DEL,DELTA#
  172.                   DELTA#=0.0
  173.                End If 
  174.             End If 
  175.             Poke APAT,P1-1 : Poke APAT+1,DEL : Doke APAT+2,PER
  176.             Add APAT,4
  177.          Else 
  178.             If P1=$83
  179.                KY=P2
  180.             End If 
  181.             If P1=$81
  182.                CU_I=P2 : Doke APAT,I_INST+P2 : Add APAT,2
  183.             End If 
  184.             If P1=$84
  185.                Doke APAT,I_SVOL+(P2*64)/256 : Add APAT,2
  186.             End If 
  187.             If P1=$88
  188.                Doke APAT,I_STEMPO+(P2*100)/188 : Add APAT,2
  189.             End If 
  190.          End If 
  191.       Next 
  192.    End If 
  193.    Doke APAT,I_END : Add APAT,2
  194.    V$(V)=""
  195. Next 
  196. LPATTERNS=APAT-AOFF
  197. '
  198. ' Copy songs 
  199. Reserve As Work 11,256
  200. AMU=Start(11)
  201. Doke AMU,1 : Add AMU,2
  202. Loke AMU,6 : Add AMU,4
  203. LLIST=1 : LPAT=(LLIST+1)*2 : APAT=8+SONGDATA+16
  204. For N=0 To 3
  205.    APAT(N)=APAT+AMU
  206.    Doke AMU+N*2,APAT
  207.    Add APAT,LPAT
  208. Next 
  209. Add AMU,8 : Doke AMU,MS_TEMPO : Add AMU,SONGDATA
  210. For A=0 To 15 : Poke AMU+A,Peek(Varptr(F$)+A) : Next : Add AMU,16
  211. P=0
  212. For V=0 To 3
  213.    If FLAG(V)
  214.       Doke APAT(V),0 : Doke APAT(V)+2,-2
  215.    Else 
  216.       Doke APAT(V),-1
  217.    End If 
  218.    AMU=APAT(V)+4
  219. Next 
  220. LSONG=AMU-Start(11)
  221. '
  222. ' Output SONG and PATTERNS 
  223. Print : Print "* Saving song";
  224. DTA_COPY[Start(11),LSONG]
  225. Print : Print "* Saving patterns";
  226. DTA_COPY[Start(10),LPATTERNS]
  227. '
  228. ' Music bank header
  229. Pof(2)=4
  230. OUT_NB[2,3]
  231. OUT_NB[2,0]
  232. TL=8+16+LINST+LSONG+LPATTERNS : OUT_NB[4,$80000000+TL]
  233. Pof(2)=20
  234. OUT_NB[4,16]
  235. OUT_NB[4,16+LINST]
  236. OUT_NB[4,16+LINST+LSONG]
  237. OUT_NB[4,0]
  238. Close 
  239. '
  240. ' Finished 
  241. Clw 
  242. Print "* Conversion finished!"
  243. Print "  Press any key to restart..."
  244. Erase 10 : Erase 11
  245. Load F$ : Music 1 : Tempo 40
  246. Wait Key 
  247. Erase 3
  248. Goto AGAIN
  249. '  
  250. ' Disk error 
  251. D_ERROR:
  252. Close 
  253. Clw : Boom : Print "Disk error!"
  254. Print : Print "Press a key to restart..."
  255. Wait Key 
  256. Resume AGAIN
  257. Procedure ABORT
  258.    Close 
  259.    Screen Close 0
  260.    Edit 
  261. End Proc
  262. Procedure RD_IFF[N$,PAR]
  263.    On Error Goto RD_ERR2
  264.    Open In 1,N$
  265.    On Error Goto RD_ERR1
  266.    LFILE=Lof(1)
  267.    I$=Input$(1,4) : If I$<>"FORM" : Error 21 : End If 
  268.    Pof(1)=Pof(1)+4
  269.    I$=Input$(1,4) : If I$<>CHK$(0) : Error 21 : End If 
  270.    LCHUNK=0
  271.    PCHUNK=Pof(1)
  272.    Do 
  273.       On Error Goto RD_ERR1
  274.       Exit If PCHUNK>=LFILE
  275.       Pof(1)=PCHUNK
  276.       H$=Input$(1,4)
  277.       A$=Input$(1,4) : LCHUNK=(Leek(Varptr(A$))+1) and $FFFFFFFE
  278.       For NCHK=1 To 8
  279.          Exit If CHK$(NCHK)=""
  280.          If H$=CHK$(NCHK)
  281.             On Error Goto RD_ERR0
  282.             Gosub "LD_"+CHK$(NCHK)
  283.          End If 
  284.       Next 
  285.       Add PCHUNK,LCHUNK+8
  286.    Loop 
  287.    For N=0 To 8
  288.       CHK$(N)=""
  289.    Next 
  290.    Error 20
  291.    ' IFF - SMUS 
  292.    LD_SHDR:
  293.    I$=Input$(1,4)
  294.    MS_TEMPO=40
  295.    Return 
  296.    '
  297.    LD_INS1:
  298.    I$=Input$(1,LCHUNK)
  299.    N=Peek(Varptr(I$))
  300.    X=0
  301.    Do 
  302.       P=Peek(Varptr(I$)+X+4)
  303.       Exit If P=0
  304.       INS$(N)=INS$(N)+Chr$(P)
  305.       Inc X
  306.    Loop 
  307.    MXINS=Max(N,MXINS)
  308.    Return 
  309.    '
  310.    LD_TRAK:
  311.    V$(VCE)=Input$(1,LCHUNK)
  312.    Inc VCE
  313.    Return 
  314.    ' IFF-8VSX 
  315.    LD_VHDR:
  316.    Print "  Loading IFF instrument";
  317.    I$=Input$(1,LCHUNK)
  318.    If Peek(Varptr(I$)+14)<>1 : Error 21 : End If 
  319.    If Peek(Varptr(I$)+15) : Error 21 : End If 
  320.    AD=Start(10)+PAR*32+2
  321.    Loke AD,Length(10)+Pof(2)
  322.    Doke AD+8,Leek(Varptr(I$))/2
  323.    Loke AD+4,Length(10) : Doke AD+10,2
  324.    L2=Leek(Varptr(I$)+4)
  325.    If L2 : Loke AD+4,Length(10)+Pof(2)+L1 : Doke AD+10,L2/2 : End If 
  326.    Doke AD+12,Leek(Varptr(I$)+16)/$400
  327.    SAM_RATE(PAR)=Leek(Varptr(I$)+8) : Doke AD+14,SAM_RATE(PAR)
  328.    Doke AD+16+14,Deek(Varptr(I$)+12)
  329.    Add AD,16
  330.    For N=1 To 14
  331.       If N<=Len(NAME$) : Poke AD+N-1,Asc(Mid$(NAME$,N,1)) : End If 
  332.    Next 
  333.    Return 
  334.    '  
  335.    LD_BODY:
  336.    FILE_COPY[LCHUNK] : FILE_EVEN
  337.    Print 
  338.    Return 
  339.    '
  340.    RD_ERR0: Pop 
  341.    RD_ERR1: Close 1
  342.    RD_ERR2: E=Errn : Resume RD_END
  343.    RD_END:
  344. End Proc[E-22]
  345. Procedure WT_CLIC
  346.    Repeat : Until Mouse Key
  347.    While Mouse Key : Wend 
  348. End Proc
  349. Procedure EXTRACT_NAME[N$]
  350.    For N=Len(N$) To 1 Step -1
  351.       A$=Mid$(N$,N,1)
  352.       Exit If(A$=":") or(A$="/")
  353.    Next 
  354.    NAME$=Mid$(N$,N+1)
  355. End Proc
  356. Procedure FILE_COPY[LONG]
  357.    LS=512
  358.    Repeat 
  359.       NL=LONG/LS
  360.       If NL
  361.          For N=1 To NL
  362.             A$=Input$(1,LS)
  363.             Print #2,A$;
  364.             Print ".";
  365.          Next 
  366.          Add LONG,-LS*NL
  367.       End If 
  368.       LS=LS/2
  369.    Until LONG=0
  370. End Proc
  371. Procedure FILE_EVEN
  372.    L=Lof(2)
  373.    If Btst(0,L)
  374.       Print #2,Chr$(0);
  375.    End If 
  376. End Proc
  377. Procedure DTA_COPY[AD,LONG]
  378.    LS=512
  379.    Repeat 
  380.       NL=LONG/LS
  381.       If NL
  382.          A$=Space$(LS)
  383.          For N=1 To NL
  384.             Copy AD,AD+LS To Varptr(A$)
  385.             Add AD,LS
  386.             Print #2,A$;
  387.             Print ".";
  388.          Next 
  389.          Add LONG,-LS*NL
  390.       End If 
  391.       LS=LS/2
  392.    Until LONG=0
  393. End Proc
  394. Procedure OUT_NB[BITS,NB]
  395.    For N=4-BITS To 3
  396.       A$=Chr$(Peek(Varptr(NB)+N)) : Print #2,A$;
  397.    Next 
  398. End Proc
  399. TNOTE:
  400. Data 33,35,37,39,41,44,46,49,52
  401. Data 55,58,62,65,69,73,78,82,87,92,98,104
  402. Data 110,117,123,131,139,147,156,165,175,185,196,208
  403. Data 220,233,247,262,277,294,311,330,349,370,392,415
  404. Data 440,466,494,523,554,587,622,659,698,740,784,830
  405. Data 880,932,988,1046,1109,1175,1245,1319,1397,1480,1568,1661
  406. Data 1760,1865,1986,2093,2217,2349,2489,2637,2794,2960,3136,3322
  407. Data 3520,3729,3952,4186,4435,4699,4978,5274,5588,5920,6272,6645
  408. Data 7040,7459,7902,8372
  409. TDELAY:
  410. Data 32,16,8,4,2,1,1,1,48,24,12,6,3,1,1,1
  411. TDELTA:
  412. Data 1.0,2.0/3.0,4.0/5.0,6.0/7.0