当前位置: 首页>>代码示例>>Java>>正文


Java MidiEvent类代码示例

本文整理汇总了Java中javax.sound.midi.MidiEvent的典型用法代码示例。如果您正苦于以下问题:Java MidiEvent类的具体用法?Java MidiEvent怎么用?Java MidiEvent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MidiEvent类属于javax.sound.midi包,在下文中一共展示了MidiEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: writeTempo

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Write the given tempo out to {@link #sequence} at the given tick.
 * 
 * @param tempo The tempo to write.
 * @param tick The tick at which to write it.
 * 
 * @throws InvalidMidiDataException If the tempo contained invalid Midi data.
 */
private void writeTempo(Tempo tempo, long tick) throws InvalidMidiDataException {
	MetaMessage mm = new MetaMessage();
	
	int mspq = tempo.getMicroSecondsPerQuarter();
	
	byte[] data = {
			(byte) ((mspq & 0xff000000) >> 24),
			(byte) ((mspq & 0x00ff0000) >> 16),
			(byte) ((mspq & 0x0000ff00) >> 8),
			(byte) (mspq & 0x000000ff)};
	
	// Clear leading 0's
	int i;
	for (i = 0; i < data.length - 1 && data[i] == 0; i++);
	if (i != 0) {
		data = Arrays.copyOfRange(data, i, data.length);
	}
	
	mm.setMessage(EventParser.TEMPO, data, data.length);
	
	sequence.getTracks()[0].add(new MidiEvent(mm, tick));
}
 
开发者ID:apmcleod,项目名称:voice-splitting,代码行数:31,代码来源:MidiWriter.java

示例2: setUp

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
protected void setUp() throws Exception {
       super.setUp();	
       seq = new Sequence(Sequence.PPQ, resolution,1);
	

	
	int mpq = (int)(60000000 / tempo);
	try
	{
		MetaMessage tempoMsg = new MetaMessage();
		tempoMsg.setMessage(0x51,new byte[] {
			(byte)(mpq>>16 & 0xff),
			(byte)(mpq>>8 & 0xff),
			(byte)(mpq & 0xff)
		},3);
		MidiEvent tempoEvent = new MidiEvent(tempoMsg,0);
           seq.getTracks()[0].add(tempoEvent);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:22,代码来源:TempoAfterImportSaveLoadTest.java

示例3: computeTrackLength

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Compute the length of a track as it will be written to the
 * output stream.
 *
 * @param track the track to measure
 * @param dos a MidiDataOutputStream used for helper method
 * @return the length of the track
 */
private int computeTrackLength(Track track, MidiDataOutputStream dos)
{
  int count = 0, length = 0, i = 0, eventCount = track.size();
  long ptick = 0;
  while (i < eventCount)
    {
      MidiEvent me = track.get(i);
      long tick = me.getTick();
      length += dos.variableLengthIntLength((int) (tick - ptick));
      ptick = tick;
      length += me.getMessage().getLength();
      i++;
    }
  return length;
}
 
开发者ID:vilie,项目名称:javify,代码行数:24,代码来源:MidiFileWriter.java

示例4: computeTrackLength

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
  * Compute the length of a track as it will be written to the
  * output stream.
  *
  * @param track the track to measure
  * @param dos a MidiDataOutputStream used for helper method
  * @return the length of the track
  */
 private int computeTrackLength(Track track, MidiDataOutputStream dos)
 {
   int count = 0, length = 0, i = 0, eventCount = track.size();
   long ptick = 0;
   while (i < eventCount)
     {
MidiEvent me = track.get(i);
long tick = me.getTick();
length += dos.variableLengthIntLength((int) (tick - ptick));
ptick = tick;
length += me.getMessage().getLength();
i++;
     } 
   return length;
 }
 
开发者ID:nmldiegues,项目名称:jvm-stm,代码行数:24,代码来源:MidiFileWriter.java

示例5: writeTimeSignature

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Write the given time signature out to {@link #sequence} at the given tick.
 * 
 * @param timeSignature The time signature to write.
 * @param tick The tick at which to write it.
 * @throws InvalidMidiDataException If the time signature contained invalid Midi data.
 */
private void writeTimeSignature(TimeSignature timeSignature, long tick) throws InvalidMidiDataException {
	MetaMessage mm = new MetaMessage();
	
	int denominator = timeSignature.getDenominator();
	
	// Base 2 log calculator for whole numbers
	int i = 0;
	while (denominator != 1) {
		denominator /= 2;
		i++;
	}
	
	byte[] data = {
			(byte) timeSignature.getNumerator(),
			(byte) i,
			(byte) timeSignature.getMetronomeTicksPerBeat(),
			(byte) timeSignature.getNotes32PerQuarter()};
	
	mm.setMessage(EventParser.TIME_SIGNATURE, data, data.length);
	
	sequence.getTracks()[0].add(new MidiEvent(mm, tick));
}
 
开发者ID:apmcleod,项目名称:voice-splitting,代码行数:30,代码来源:MidiWriter.java

示例6: addMidiNote

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Add the given MidiNote into the {@link #sequence}.
 *  
 * @param note The note to add.
 * 
 * @throws InvalidMidiDataException If the MidiNote contains invalid Midi data. 
 */
public void addMidiNote(MidiNote note) throws InvalidMidiDataException {
	int correctVoice = note.getCorrectVoice();
	
	// Pad with enough tracks
	while (sequence.getTracks().length <= correctVoice) {
		sequence.createTrack();
	}
	
	// Get the correct track
	Track track = sequence.getTracks()[correctVoice];
	
	ShortMessage noteOn = new ShortMessage();
	noteOn.setMessage(ShortMessage.NOTE_ON | correctVoice, note.getPitch(), note.getVelocity());
	MidiEvent noteOnEvent = new MidiEvent(noteOn, note.getOnsetTick());
	
	ShortMessage noteOff = new ShortMessage();
	noteOff.setMessage(ShortMessage.NOTE_OFF | correctVoice, note.getPitch(), 0);
	MidiEvent noteOffEvent = new MidiEvent(noteOff, note.getOffsetTick());
	
	track.add(noteOnEvent);
	track.add(noteOffEvent);
}
 
开发者ID:apmcleod,项目名称:voice-splitting,代码行数:30,代码来源:MidiWriter.java

示例7: getMicrosecondsPerQuarterNote

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Gets the number of microseconds per quarter note for a sequence, used to
 * determine its BPM.
 * 
 * @param sequence
 * @return
 */
private static int getMicrosecondsPerQuarterNote(Sequence sequence) {

	// Check all MIDI tracks for MIDI_SET_TEMPO message
	for (Track track : sequence.getTracks()) {
		for (int i = 0; i < track.size(); i++) {
			MidiEvent event = track.get(i);
			MidiMessage message = event.getMessage();
			if (message instanceof MetaMessage) {
				MetaMessage m = (MetaMessage) message;
				byte[] data = m.getData();
				int type = m.getType();
				if (type == MIDI_SET_TEMPO) {
					return ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
				}
			}
		}
	}
	return 0;
}
 
开发者ID:TaylorPeer,项目名称:AI-909,代码行数:27,代码来源:MidiUtilities.java

示例8: findFirstTempo

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Find the first tempo meta message and return the tempo value
 * @return
 */
public static float findFirstTempo(Sequence sequence) throws Exception
{
		for(Track track : sequence.getTracks())
		{
			for(int n=0;n<track.size();n++)
			{
				MidiEvent event = track.get(n);

				if(event.getMessage() instanceof MetaMessage
						&& ((MetaMessage)event.getMessage()).getType()==0x51)
				{
					float tempo = new TempoMessage((MetaMessage)event.getMessage()).getBpm();
					System.out.println("Found tempomessage "+tempo+" bpm");
					return tempo;
				}
			}
		}
		throw new Exception("No tempo message found");
}
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:24,代码来源:MidiSequenceConverter.java

示例9: quantizeOnTheFly

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
private static void quantizeOnTheFly(Vector<MidiEvent> eventsToBeQuantized,
		Quantization q, QuantizeBuffer quantizeBuffer) {
	for (MidiEvent event : eventsToBeQuantized) {
		event = q.quantize(event);
		long tick = event.getTick();
		long d = tick - quantizeBuffer.startTick;
		if (d < 0) {
			d = 0; // cannot do better
		} else {
			d %= quantizeBuffer.data.length; // if the data is cached at
			// the beginning of the
			// buffer (thus reaches over
			// the end of the current
			// buffer), this part of the
			// buffer that has already
			// been played and will be
			// reused later, "rotating"
		}
		Vector<MidiEvent> v = quantizeBuffer.data[(int) d];
		if (v == null) {
			v = new Vector<MidiEvent>();
			quantizeBuffer.data[(int) d] = v;
		}
		v.add(event);
	}
}
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:27,代码来源:FrinikaSequencerPlayer.java

示例10: commitAddImpl

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
@SuppressWarnings("deprecation")
@Override
public void commitAddImpl() { // Jens, renamed to be able to handle notification of CommitListeners in MultiEvent, see MultiEvent.commitXxx()

	// System. out.println(" COMMIT ADD PROG ");
	try {

		metaEvent = new MidiEvent(getMessage(), startTick);
		getTrack().add(metaEvent);

	} catch (InvalidMidiDataException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:17,代码来源:MetaEvent.java

示例11: commitAddImpl

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
@SuppressWarnings("deprecation")
   @Override
public
   void commitAddImpl() { // Jens, renamed to be able to handle notification of CommitListeners in MultiEvent, see MultiEvent.commitXxx()
       try
       {
           ShortMessage shm = new ShortMessage();
           shm.setMessage(ShortMessage.PITCH_BEND,channel,value & 0x3f,(value >> 7));
           midiEvent = new MidiEvent(shm,startTick);
           getTrack().add(midiEvent);
        
       } catch(InvalidMidiDataException e)
       {
           e.printStackTrace();
    
       }
       zombie=false;
   }
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:19,代码来源:PitchBendEvent.java

示例12: commitAddImpl

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
@SuppressWarnings("deprecation")
   @Override
public
   void commitAddImpl() { // Jens, renamed to be able to handle notification of CommitListeners in MultiEvent, see MultiEvent.commitXxx()
       try
       {
           ShortMessage shm = new ShortMessage();
           shm.setMessage(ShortMessage.CONTROL_CHANGE,channel,controlNumber,value);
           midiEvent = new MidiEvent(shm,startTick);
           getTrack().add(midiEvent);
        
       } catch(InvalidMidiDataException e)
       {
           e.printStackTrace();
    
       }
       zombie=false;
   }
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:19,代码来源:ControllerEvent.java

示例13: parseMacro

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
protected MidiEvent[] parseMacro(String macro) throws InvalidMidiDataException {
	if ((macro == null) || (macro.trim().length() == 0)) {
		return new MidiEvent[0];
	}
	SysexMacro m = AbstractSysexMacro.findMacro(macro);
	if (m == null) {
		throw new InvalidMidiDataException("Cannot find macro '"+Toolbox.firstWord(macro)+"'.");
	}
	MidiMessage[] mm = null;
	mm = m.parseMessages(macro);
	if (mm == null) {
		throw new InvalidMidiDataException("Parsing of '"+macro+"' failed.");
	}
	MidiEvent[] me = new MidiEvent[mm.length];
	for (int i = 0; i < mm.length; i++) {
		me[i] = new MidiEvent(mm[i], getStartTick());
	}
	return me;
}
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:20,代码来源:SysexEvent.java

示例14: export

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Returns a clone of this sequence suitable for Midi file export. What it does is to map the FTW channel setting to all the midi events
 * for the corresponding tracks
 * @return
 * @throws InvalidMidiDataException 
 */
public Sequence export() throws InvalidMidiDataException
{
    Sequence newSeq = new Sequence(getDivisionType(),getResolution());
    for(FrinikaTrackWrapper ftw : frinikaTrackWrappers)
    {
        Track track = newSeq.createTrack();
        for(int n=0;n<ftw.size();n++)
        {

                    MidiEvent sourceMidiEvent = ftw.get(n);
            MidiMessage msg = sourceMidiEvent.getMessage();
            if(msg instanceof ShortMessage)
            {
                ShortMessage shm = (ShortMessage)msg;
                ShortMessage nshm = new ShortMessage();
                nshm.setMessage(shm.getCommand(),ftw.getMidiChannel(),shm.getData1(),shm.getData2());
                msg = nshm;
            }
            MidiEvent newEvent = new MidiEvent(msg,sourceMidiEvent.getTick());
            track.add(newEvent);
        }
    }
    return newSeq;
}
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:31,代码来源:FrinikaSequence.java

示例15: renderBar

import javax.sound.midi.MidiEvent; //导入依赖的package包/类
/**
 * Render a bar of notes as MIDI to the specified Track from the
 * specified start tick with the specified ticks per bar.
 * @param notes the notes to render
 * @param track the MIDI Track to render to
 * @param startTick the tick at the start of the bar
 * @param ticksPerBar the number of ticks per bar
 */
public void renderBar(int[] notes, Track track, 
		long startTick, int ppqn)
	throws InvalidMidiDataException	{
	final int channel = getInstrument().getChannel();
	MidiMessage msg;
	for ( int i = 0; i < notes.length; i++) {
		int note = notes[i];
		float timeOn = swing(Note.getTime(note));
		int pitch = Note.getPitch(note);
		int level = Note.getLevel(note);
		long onTick = ticks2MidiTicks(timeOn, ppqn);
		msg = ChannelMsg.createChannel(
				ChannelMsg.NOTE_ON, channel, pitch, level);
		track.add(new MidiEvent(msg, startTick + onTick));
		// note off
		msg = ChannelMsg.createChannel(
				ChannelMsg.NOTE_OFF, channel, pitch, 0);
		float timeOff = swing(Note.getTime(note)+Note.getDuration(note));
		long offTick = ticks2MidiTicks(timeOff, ppqn);
		track.add(new MidiEvent(msg, startTick + offTick));
	}			
}
 
开发者ID:petersalomonsen,项目名称:frinika,代码行数:31,代码来源:Performer.java


注:本文中的javax.sound.midi.MidiEvent类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。