Guidance
指路人
g.yi.org
Software / Reginald / Examples / rxmidi examples / reset.rex

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
/* This script inserts some controller messages at the beginning of a 
 * General MIDI file to initialize the GM module prior to note playback.
 *
 * Specifically, it turns MOD wheel and sustain pedal off, turns chorus
 * off, sets reverb to 70, and turns all other controllers off.
 *
 * Caveat: This script assumes that a MIDI file does not contain events
 * for multiples MIDI ports. In other words, all of the events upon a
 * given MIDI channel all go out the same port. So that means that there
 * aren't two distinct MIDI channel 1's, for example. For a multiple port
 * MIDI file, this script would have to be modified to search for port #
 * events.
 */

OPTIONS "C_CALL LABELCHECK NOSOURCE"

/* Add the REXX GUI and RxMidi functions. */
LIBRARY rexxgui, rxmidi

/* Check if we have a MIDI file name. We get the filename from
 * the command line (ie, the user typed it after disasm.rex), or
 * if not supplied, we bring up the file dialog.
 */
IF ARG() < 1 THEN DO
	ERROR = guifile('FromFilename', 'EXISTING', 'Choose the MIDI file', 'MIDI files (*.mid, *.rmi) | *.mid;*.rmi | All files (*.*) | *.*')
	IF ERROR \== "" THEN RETURN
END
ELSE fromfilename = ARG(1)

DO
	/* Open/Load the MIDI file */
	MIDIOpenFile(fromfilename)

	/* Start with MIDI channel 1 */
	chan = 1

	/* Another MIDI channel to search? */
	DO UNTIL chan = 17

		   /* Set all tracks as the search tracks, and reset to the start of the file */
		   err = MIDITrack('\')

		   /* Is there data on any track? */
		   IF err == 0 THEN RETURN

		   /* See if there are any note-on events upon this MIDI channel. If there
		    * aren't, then we don't need to insert the controller events upon
		    * this channel. If there are, MIDIGetEvent() will set the first such
		    * note event on this channel as the currently selected event.
		    */
		   result = MIDIGetEvent("On Note", chan)
		   IF result == "" THEN DO

			/* Ok, there is a note-on on this MIDI channel, and it is now the
			 * currently selected event (and its track is the currently selected
			 * track). So we have the insert point before which we will insert
			 * our controller events. We'll insert all events at the same time
			 * as this first note event, but before it. So we won't touch the
			 * MidiEvent.!Measure, MidiEvent.!Beat, nor MidiEvent.!Clock fields.
			 * They are already set to the time of this note-on event. If you want
			 * to get fancy, and try to stagger the times of each controller event
			 * so that they don't all happen at once, you could analyze those
			 * preceding fields of MidiEvent, and try to subtract some from the
			 * Clock field. If it's already zero, then you can try subtracting 1
			 * from Beat (if it's not already 1) and then add one time division
			 * worth of clocks to clock. Then you can start subtracting.
			 *
			 * Also note that MidiEvent.!Channel is set to what we want. So, all
			 * we really need to do is set Type, Data1, and Data2.
			 */
			midievent.!type = 'Controller'

			/* Insert MOD off. Actually, the controllers off message should make this unnecessary */
			midievent.!data1 = 1
			midievent.!data2 = 0
			MIDISetEvent('BEFORE|INS|TIME|CHAN|DATA')

			/* Insert sustain off */
			midievent.!data1 = 64
			midievent.!data2 = 0
			MIDISetEvent('BEFORE|INS|TIME|CHAN|DATA')

			/* Insert chorus off */
			midievent.!data1 = 93
			midievent.!data2 = 0
			MIDISetEvent('BEFORE|INS|TIME|CHAN|DATA')

			/* Set reverb to 70  */
			midievent.!data1 = 91
			midievent.!data2 = 70
			MIDISetEvent('BEFORE|INS|TIME|CHAN|DATA')

			/* Insert reset all controllers. This will go 'BEFORE' all of the
			 * preceding controllers, which is where it needs to be.
			 */
			midievent.!data1 = 121
			midievent.!data2 = 0
			MIDISetEvent('BEFORE|INS|TIME|CHAN|DATA')

		END /* there are notes on this chan */

		/* Let's search the next MIDI channel */
		chan = chan + 1

	END /* UNTIL chan = 17 */

	MIDISaveFile(fromfilename)

	CATCH ERROR
		CONDITION('M')
RETURN
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sat 2024-4-20  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2010-07-16 20:49:22