home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Cdrom / Pavilions / BrainOpera / online / net-music / net-instrument / CMIDI.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  6.7 KB  |  227 lines

  1. import java.applet.AudioClip;
  2. //import midi.*;    // This version ignores fTime arguments. (not needed)
  3. import java.applet.Applet;
  4. import java.net.*;
  5.  
  6. public class CMIDI
  7. {
  8.     static Applet sm_applet = null;
  9.  
  10.     //MidiOutDevice midiOut;
  11.     //int g_arriInstToChan[];
  12.     //int g_arriChanToInst[];
  13.     //int g_iFreeChanNum = 0;
  14.  
  15.     NoteFiles garr_nfInst[];
  16.     int g_iMaxInst = -1;
  17.  
  18. public void MidiOutNoteON(int note, int vel, int chnl, int fTime)
  19. {    garr_nfInst[chnl].Start(note, fTime);
  20.     //try midiOut.Out(MidiDevice.MidiNoteOn | chnl | note<<8 | vel<<16); catch(MidiException e);
  21. }
  22. public void MidiOutNoteOFF(int note, int chnl)
  23. {
  24.     garr_nfInst[chnl].Stop(note);
  25.     //try midiOut.Out(MidiDevice.MidiNoteOff | chnl | note << 8); catch(MidiException e);
  26. }
  27. //=================================================
  28. public int iGetParameter (String str)
  29. { return (new Integer(sm_applet.getParameter(str))).intValue(); }
  30. public void OnInit ()    // Call this to initialize
  31. {    
  32.     garr_nfInst = new NoteFiles[128];
  33.     /*g_arriInstToChan = new int[128];
  34.     int C;
  35.     for (C=0; C<128; C++) g_arriInstToChan[C] = -1;
  36.     g_arriChanToInst = new int[16];
  37.     for (C=0; C<16; C++) g_arriChanToInst[C] = -1;
  38.     MidiOutDevice.GetNumDevices();    // bogus
  39.  
  40.     int iDevice;
  41.     try iDevice = iGetParameter("device");
  42.     catch(NumberFormatException e) iDevice=-1;
  43.     try midiOut = new MidiOutDevice(iDevice); catch(MidiException e);
  44.     */
  45. }
  46. public void OnStart ()    // call this to start
  47. {    //try midiOut.Open(); catch(MidiException e);
  48. }
  49. public void OnStop ()    // call this to stop
  50. {    for (int C=0; C<=g_iMaxInst; C++)
  51.         garr_nfInst[C].AllStop();
  52.     //try midiOut.Close(); catch(MidiException e);
  53. }
  54. public void LoopThis (int fTime)
  55. {    for (int C=0; C<=g_iMaxInst; C++)
  56.         if (garr_nfInst[C] != null)
  57.             garr_nfInst[C].LoopThis(fTime);
  58. }
  59.  
  60. //=================================================
  61. int iGetInstrumentChannel (char pv)
  62. {    int ret = (int)pv;
  63.     if (garr_nfInst[ret] == null) {
  64.         try garr_nfInst[ret] = new NoteFiles(ret, sm_applet);
  65.         catch(Exception e) ret = 0;
  66.         if (ret > g_iMaxInst) g_iMaxInst = ret;
  67.     }
  68.     return ret;
  69.     /*short C=(short)pv;
  70.     if (g_arriInstToChan[C] >= 0 &&
  71.         C == g_arriChanToInst[g_arriInstToChan[C]])
  72.         return g_arriInstToChan[C];
  73.         
  74.     g_arriInstToChan[C] = g_iFreeChanNum;
  75.     g_arriChanToInst[g_iFreeChanNum] = C;
  76.     try midiOut.Out(MidiDevice.MidiProgramChange | g_iFreeChanNum | (C << 8)); catch(MidiException e);
  77.  
  78.     g_iFreeChanNum++;
  79.     if (g_iFreeChanNum == 16) {
  80.         g_iFreeChanNum = 0;
  81.         return 15;
  82.     }
  83.     return g_iFreeChanNum - 1;
  84.     */
  85. }
  86.  
  87. //----------------------------------
  88.  
  89. boolean m_bHaveDefaultInst = false;
  90. final int c_iNumInst = 9;
  91. int marr_iDefaultInst[] = new int[c_iNumInst];
  92.  
  93. //---------------------------------------------
  94. public int iGetParameter (String str, Applet app)
  95. { return (new Integer(app.getParameter(str))).intValue(); }
  96. //---------------------------------------------
  97.  
  98. int iGetDefaultInst (int iIndex, Applet app)    // Hack to get decent MIDI sounds
  99. {    if (!m_bHaveDefaultInst) {
  100.         for (int C=0; C<c_iNumInst; C++) {
  101.             try marr_iDefaultInst[C] = iGetParameter("DefaultInst" + C);
  102.             catch(Exception e) {
  103.                 switch (C) {
  104.                 default: assert(false);
  105.                 // Red
  106.                 case 0: marr_iDefaultInst[C] = 0; break;
  107.                 case 1: marr_iDefaultInst[C] = 0; break;
  108.                 case 2: marr_iDefaultInst[C] = 1; break;
  109.                 // Green
  110.                 case 3: marr_iDefaultInst[C] = 1; break;
  111.                 case 4: marr_iDefaultInst[C] = 1; break;
  112.                 // Blue
  113.                 case 5: marr_iDefaultInst[C] = 1; break;
  114.                 // Violet
  115.                 case 6: marr_iDefaultInst[C] = 0; break;
  116.                 case 7: marr_iDefaultInst[C] = 0; break;
  117.                 case 8: marr_iDefaultInst[C] = 0; break;
  118.                 }
  119.             }
  120.         }
  121.         m_bHaveDefaultInst = true;
  122.     }
  123.     return marr_iDefaultInst[iIndex];
  124. }
  125.  
  126. void assert (boolean b) { if (!b) (new int[1])[-1] = 0; }
  127.  
  128. }
  129.  
  130. //==============================
  131.  
  132. class NoteFiles
  133. {
  134.     int m_iNumFiles;    // How many files are there?
  135.     int m_iLowestNote;    // Which MIDI note number is file #0?
  136.     int m_iMaxSoundLen;    // What's the max length of a note-on?
  137.     
  138.     // Assume file names are of the form PrefixNumber.Suffix
  139.     // Where Number increases from 0.
  140.     // String m_strFilePrefix;
  141.     static String sm_strFileSuffix = ".au";
  142.  
  143.     AudioClip marr_aucSounds[];
  144.     int marr_fEndTime[];
  145.  
  146. //----------------------------------
  147. public NoteFiles (String strPrefix, int iNumFiles,
  148.     int iLowNote, int iMaxLen, Applet app)
  149. {    m_iNumFiles = iNumFiles;
  150.     m_iLowestNote = iLowNote;
  151.     m_iMaxSoundLen = iMaxLen;
  152.     marr_aucSounds = new AudioClip[iNumFiles];
  153.     marr_fEndTime = new int[iNumFiles];
  154.     for (int C=0; C<iNumFiles; C++) {
  155.         marr_fEndTime[C] = 0;
  156.         String str = strPrefix + C + sm_strFileSuffix;
  157.         try {    
  158.             marr_aucSounds[C] = app.getAudioClip(
  159.                 //new URL(app.getDocumentBase(), str));
  160.                 new URL(str));
  161.             System.out.println("Loaded: " + str);
  162.         } catch(MalformedURLException e)
  163.             System.out.println("Not found: " + str);
  164.     }
  165. }
  166.  
  167. public int iGetParameter (Applet app, String str)
  168. { return (new Integer(app.getParameter(str))).intValue(); }
  169.  
  170. public NoteFiles (int iInstNum, Applet app)
  171. {    String str = "Inst" + iInstNum;
  172.     String strPrefix = app.getParameter(str + "File");
  173.     int iNumFiles = iGetParameter(app, str + "NumFiles");
  174.     int iLowNote = iGetParameter(app, str + "LowNote");
  175.     int iMaxLen = iGetParameter(app, str + "MaxLen");
  176.  
  177.     m_iNumFiles = iNumFiles;
  178.     m_iLowestNote = iLowNote;
  179.     m_iMaxSoundLen = iMaxLen;
  180.     marr_aucSounds = new AudioClip[iNumFiles];
  181.     marr_fEndTime = new int[iNumFiles];
  182.     for (int C=0; C<iNumFiles; C++) {
  183.         marr_fEndTime[C] = 0;
  184.         str = strPrefix + C + sm_strFileSuffix;
  185.         try marr_aucSounds[C] = app.getAudioClip(
  186.             new URL(app.getDocumentBase(), str));
  187.         catch(Exception e);
  188.         System.out.println("Loaded: " + str);
  189.     }
  190. }
  191. //-----------------------------------------
  192. public void LoopThis (int fTime)
  193. {    for (int C=0; C<m_iNumFiles; C++)
  194.     {    if (marr_fEndTime[C]!=0 && marr_fEndTime[C] <= fTime) {
  195.             marr_fEndTime[C] = 0;
  196.             marr_aucSounds[C].stop();
  197.         }
  198.     }
  199. }
  200. //-----------------------------------------
  201. public void Start (int note, int fTime)
  202. {    int C = iNoteToIndex(note);
  203.     marr_aucSounds[C].loop();
  204.     marr_fEndTime[C] = fTime + m_iMaxSoundLen;
  205. }
  206. //-----------------------------------------
  207. public void Stop (int note)
  208. {    int C = iNoteToIndex(note);
  209.     marr_aucSounds[C].stop();
  210.     marr_fEndTime[C] = 0;
  211. }
  212. //-----------------------------------------
  213. public void AllStop ()
  214. {    for (int C=0; C<m_iNumFiles; C++) {
  215.         marr_aucSounds[C].stop();
  216.         marr_fEndTime[C] = 0;
  217.     }
  218. }
  219. //-----------------------------------------
  220. public int iNoteToIndex (int iMIDINote)
  221. {    while (iMIDINote < m_iLowestNote) iMIDINote += 12;
  222.     while (iMIDINote >= m_iLowestNote + m_iNumFiles) iMIDINote -= 12;
  223.     return iMIDINote - m_iLowestNote;
  224. }
  225.  
  226. }    // end class NoteFiles
  227.