home *** CD-ROM | disk | FTP | other *** search
Java Source | 2017-09-21 | 6.7 KB | 227 lines |
- import java.applet.AudioClip;
- //import midi.*; // This version ignores fTime arguments. (not needed)
- import java.applet.Applet;
- import java.net.*;
-
- public class CMIDI
- {
- static Applet sm_applet = null;
-
- //MidiOutDevice midiOut;
- //int g_arriInstToChan[];
- //int g_arriChanToInst[];
- //int g_iFreeChanNum = 0;
-
- NoteFiles garr_nfInst[];
- int g_iMaxInst = -1;
-
- public void MidiOutNoteON(int note, int vel, int chnl, int fTime)
- { garr_nfInst[chnl].Start(note, fTime);
- //try midiOut.Out(MidiDevice.MidiNoteOn | chnl | note<<8 | vel<<16); catch(MidiException e);
- }
- public void MidiOutNoteOFF(int note, int chnl)
- {
- garr_nfInst[chnl].Stop(note);
- //try midiOut.Out(MidiDevice.MidiNoteOff | chnl | note << 8); catch(MidiException e);
- }
- //=================================================
- public int iGetParameter (String str)
- { return (new Integer(sm_applet.getParameter(str))).intValue(); }
- public void OnInit () // Call this to initialize
- {
- garr_nfInst = new NoteFiles[128];
- /*g_arriInstToChan = new int[128];
- int C;
- for (C=0; C<128; C++) g_arriInstToChan[C] = -1;
- g_arriChanToInst = new int[16];
- for (C=0; C<16; C++) g_arriChanToInst[C] = -1;
- MidiOutDevice.GetNumDevices(); // bogus
-
- int iDevice;
- try iDevice = iGetParameter("device");
- catch(NumberFormatException e) iDevice=-1;
- try midiOut = new MidiOutDevice(iDevice); catch(MidiException e);
- */
- }
- public void OnStart () // call this to start
- { //try midiOut.Open(); catch(MidiException e);
- }
- public void OnStop () // call this to stop
- { for (int C=0; C<=g_iMaxInst; C++)
- garr_nfInst[C].AllStop();
- //try midiOut.Close(); catch(MidiException e);
- }
- public void LoopThis (int fTime)
- { for (int C=0; C<=g_iMaxInst; C++)
- if (garr_nfInst[C] != null)
- garr_nfInst[C].LoopThis(fTime);
- }
-
- //=================================================
- int iGetInstrumentChannel (char pv)
- { int ret = (int)pv;
- if (garr_nfInst[ret] == null) {
- try garr_nfInst[ret] = new NoteFiles(ret, sm_applet);
- catch(Exception e) ret = 0;
- if (ret > g_iMaxInst) g_iMaxInst = ret;
- }
- return ret;
- /*short C=(short)pv;
- if (g_arriInstToChan[C] >= 0 &&
- C == g_arriChanToInst[g_arriInstToChan[C]])
- return g_arriInstToChan[C];
-
- g_arriInstToChan[C] = g_iFreeChanNum;
- g_arriChanToInst[g_iFreeChanNum] = C;
- try midiOut.Out(MidiDevice.MidiProgramChange | g_iFreeChanNum | (C << 8)); catch(MidiException e);
-
- g_iFreeChanNum++;
- if (g_iFreeChanNum == 16) {
- g_iFreeChanNum = 0;
- return 15;
- }
- return g_iFreeChanNum - 1;
- */
- }
-
- //----------------------------------
-
- boolean m_bHaveDefaultInst = false;
- final int c_iNumInst = 9;
- int marr_iDefaultInst[] = new int[c_iNumInst];
-
- //---------------------------------------------
- public int iGetParameter (String str, Applet app)
- { return (new Integer(app.getParameter(str))).intValue(); }
- //---------------------------------------------
-
- int iGetDefaultInst (int iIndex, Applet app) // Hack to get decent MIDI sounds
- { if (!m_bHaveDefaultInst) {
- for (int C=0; C<c_iNumInst; C++) {
- try marr_iDefaultInst[C] = iGetParameter("DefaultInst" + C);
- catch(Exception e) {
- switch (C) {
- default: assert(false);
- // Red
- case 0: marr_iDefaultInst[C] = 0; break;
- case 1: marr_iDefaultInst[C] = 0; break;
- case 2: marr_iDefaultInst[C] = 1; break;
- // Green
- case 3: marr_iDefaultInst[C] = 1; break;
- case 4: marr_iDefaultInst[C] = 1; break;
- // Blue
- case 5: marr_iDefaultInst[C] = 1; break;
- // Violet
- case 6: marr_iDefaultInst[C] = 0; break;
- case 7: marr_iDefaultInst[C] = 0; break;
- case 8: marr_iDefaultInst[C] = 0; break;
- }
- }
- }
- m_bHaveDefaultInst = true;
- }
- return marr_iDefaultInst[iIndex];
- }
-
- void assert (boolean b) { if (!b) (new int[1])[-1] = 0; }
-
- }
-
- //==============================
-
- class NoteFiles
- {
- int m_iNumFiles; // How many files are there?
- int m_iLowestNote; // Which MIDI note number is file #0?
- int m_iMaxSoundLen; // What's the max length of a note-on?
-
- // Assume file names are of the form PrefixNumber.Suffix
- // Where Number increases from 0.
- // String m_strFilePrefix;
- static String sm_strFileSuffix = ".au";
-
- AudioClip marr_aucSounds[];
- int marr_fEndTime[];
-
- //----------------------------------
- public NoteFiles (String strPrefix, int iNumFiles,
- int iLowNote, int iMaxLen, Applet app)
- { m_iNumFiles = iNumFiles;
- m_iLowestNote = iLowNote;
- m_iMaxSoundLen = iMaxLen;
- marr_aucSounds = new AudioClip[iNumFiles];
- marr_fEndTime = new int[iNumFiles];
- for (int C=0; C<iNumFiles; C++) {
- marr_fEndTime[C] = 0;
- String str = strPrefix + C + sm_strFileSuffix;
- try {
- marr_aucSounds[C] = app.getAudioClip(
- //new URL(app.getDocumentBase(), str));
- new URL(str));
- System.out.println("Loaded: " + str);
- } catch(MalformedURLException e)
- System.out.println("Not found: " + str);
- }
- }
-
- public int iGetParameter (Applet app, String str)
- { return (new Integer(app.getParameter(str))).intValue(); }
-
- public NoteFiles (int iInstNum, Applet app)
- { String str = "Inst" + iInstNum;
- String strPrefix = app.getParameter(str + "File");
- int iNumFiles = iGetParameter(app, str + "NumFiles");
- int iLowNote = iGetParameter(app, str + "LowNote");
- int iMaxLen = iGetParameter(app, str + "MaxLen");
-
- m_iNumFiles = iNumFiles;
- m_iLowestNote = iLowNote;
- m_iMaxSoundLen = iMaxLen;
- marr_aucSounds = new AudioClip[iNumFiles];
- marr_fEndTime = new int[iNumFiles];
- for (int C=0; C<iNumFiles; C++) {
- marr_fEndTime[C] = 0;
- str = strPrefix + C + sm_strFileSuffix;
- try marr_aucSounds[C] = app.getAudioClip(
- new URL(app.getDocumentBase(), str));
- catch(Exception e);
- System.out.println("Loaded: " + str);
- }
- }
- //-----------------------------------------
- public void LoopThis (int fTime)
- { for (int C=0; C<m_iNumFiles; C++)
- { if (marr_fEndTime[C]!=0 && marr_fEndTime[C] <= fTime) {
- marr_fEndTime[C] = 0;
- marr_aucSounds[C].stop();
- }
- }
- }
- //-----------------------------------------
- public void Start (int note, int fTime)
- { int C = iNoteToIndex(note);
- marr_aucSounds[C].loop();
- marr_fEndTime[C] = fTime + m_iMaxSoundLen;
- }
- //-----------------------------------------
- public void Stop (int note)
- { int C = iNoteToIndex(note);
- marr_aucSounds[C].stop();
- marr_fEndTime[C] = 0;
- }
- //-----------------------------------------
- public void AllStop ()
- { for (int C=0; C<m_iNumFiles; C++) {
- marr_aucSounds[C].stop();
- marr_fEndTime[C] = 0;
- }
- }
- //-----------------------------------------
- public int iNoteToIndex (int iMIDINote)
- { while (iMIDINote < m_iLowestNote) iMIDINote += 12;
- while (iMIDINote >= m_iLowestNote + m_iNumFiles) iMIDINote -= 12;
- return iMIDINote - m_iLowestNote;
- }
-
- } // end class NoteFiles
-