Java Code Examples for javax.sound.midi.ShortMessage#getCommand()

The following examples show how to use javax.sound.midi.ShortMessage#getCommand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MidiWaveformSynthesizer.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static final void addNotesToTrack(final Track track, final Track trk) throws InvalidMidiDataException {
    for (int ii = 0; ii < track.size(); ii++) {
        final MidiEvent me = track.get(ii);
        final MidiMessage mm = me.getMessage();
        if (mm instanceof ShortMessage) {
            final ShortMessage sm = (ShortMessage) mm;
            final int command = sm.getCommand();
            int com = -1;
            if (command == ShortMessage.NOTE_ON) {
                com = LOCAL_NOTE_ON;
            } else if (command == ShortMessage.NOTE_OFF) {
                com = LOCAL_NOTE_OFF;
            }
            if (com > 0) {
                final byte[] b = sm.getMessage();
                final int l = (b == null ? 0 : b.length);
                final MetaMessage metaMessage = new MetaMessage(com, b, l);
                final MidiEvent me2 = new MidiEvent(metaMessage, me.getTick());
                trk.add(me2);
            }
        }
    }
}
 
Example 2
Source File: MiPort.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void send(MidiMessage inMessage, long inTimeStamp)
{
if(inMessage instanceof ShortMessage)
	{
	ShortMessage	mm = (ShortMessage)inMessage;
	
	switch(mm.getCommand())
		{
		case ShortMessage.NOTE_ON:
		case ShortMessage.NOTE_OFF:
			MiProvider.instance().noteReceived(mm, inTimeStamp);
			break;

		case ShortMessage.CONTROL_CHANGE:
			//System.err.println("Control change");
			break;
		}
	}
}
 
Example 3
Source File: MidiWaveformSynthesizer.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
public void decode(final float[] data, final int frameSize, final int updatePeriod, final int samplingRate,
        final int nBits) {
    if (frameSize <= 0) {
        throw new IllegalArgumentException("Frame size must be greater than zero");
    }
    final Track track = mergeShortMessageEvent(sequence.getTracks());
    final float length = 1e-6f * sequence.getMicrosecondLength();
    final float ts = 1.0f / samplingRate;
    final long trackTicks = track.ticks();
    final float tickLength = trackTicks <= 0 ? 0 : length / trackTicks;
    final int frameCount = data.length / frameSize;
    final float scale = 2 << Math.max(1, nBits + 1);

    final int fftSize = 2 * frameSize;
    final FloatFFT_1D fft = new FloatFFT_1D(fftSize);
    final float[] apodization = new float[fftSize];
    for (int i = 0; i < fftSize; i++) {
        apodization[i] = (float) Apodization.Hann.getIndex(i, apodization.length);
    }

    int frameCounter = 0;
    int tickIndex = 0;
    final float[] waveForm = new float[2 * frameSize];
    final int nUpdateDistance = (int) (updatePeriod / 1000.0 * samplingRate);
    for (int i = 0; frameCounter < frameCount; i++) {
        final float t = i * ts;
        final MidiEvent tickEvt = track.get(tickIndex);
        final float tickTimeStamp = tickEvt.getTick() * tickLength;

        // update waveform by one sample
        update(samplingRate, nBits);

        if ((t > tickTimeStamp) && (tickIndex < track.size() - 1)) {
            if ((tickEvt.getMessage() instanceof ShortMessage)) {
                final ShortMessage sm = (ShortMessage) tickEvt.getMessage();
                final int note = sm.getData1() & 0xFF;
                final int velocity = sm.getData2() & 0xFF;

                final int command = sm.getCommand();
                if ((command == ShortMessage.NOTE_ON) || (command == LOCAL_NOTE_ON)) {
                    noteAmplitude[note] = velocity;
                } else if ((command == ShortMessage.NOTE_OFF) || (command == LOCAL_NOTE_OFF)) {
                    noteAmplitude[note] = 0.0f;
                }
            }
            tickIndex++;
        }

        if (i > 0 && (i % nUpdateDistance == 0)) {
            for (int j = 0; j < waveForm.length; j++) {
                final float noise = (1e-3f * System.nanoTime() % 2); // adds some noise
                waveForm[j] = apodization[j] * getSample(j) + noise / scale;
            }
            decodeFrame(fft, waveForm, data, (frameCounter * frameSize) % data.length);
            frameCounter++;
        }
    }

    // return synthesizer to its original state
    for (int note = 0; note < N_NOTES; note++) {
        noteAmplitude[note] = 0.0f;
        synthesizerChannel.noteOff(note, 0);
    }
}
 
Example 4
Source File: MidiToAudioWriter.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static List<Patch> getPatchs(List<MidiEvent> events){
	Patch[] channels = new Patch[16];
	
	Iterator<MidiEvent> it = events.iterator();
	while(it.hasNext()){
		MidiEvent event = (MidiEvent)it.next();
		MidiMessage msg = event.getMessage();
		if( msg instanceof ShortMessage ){
			ShortMessage shortMessage = (ShortMessage)msg;
			
			int channel = shortMessage.getChannel();
			if( channel >= 0 && channel < channels.length ){
				int command = shortMessage.getCommand();
				int data1 = shortMessage.getData1();
				int data2 = shortMessage.getData2();
				int bank = (command == ShortMessage.CONTROL_CHANGE && data1 == MidiControllers.BANK_SELECT ? data2 : -1);
				int program = (command == ShortMessage.PROGRAM_CHANGE ? data1 : -1);
				if( bank >= 0 || program >= 0 ){
					if( bank < 0 ){
						bank = (channels[channel] != null ? channels[channel].getBank() : 0);
					}
					if( program < 0 ){
						program = (channels[channel] != null ? channels[channel].getProgram() : 0);
					}
					channels[channel] = new Patch(bank, program);
				}
			}
		}
	}
	List<Patch> patchs = new ArrayList<Patch>();
	for( int i = 0 ; i < channels.length ; i ++ ){
		if( channels[i] != null ){
			boolean patchExists = false;
			Iterator<Patch> patchIt = patchs.iterator();
			while( patchIt.hasNext() ){
				Patch patch = (Patch) patchIt.next();
				if( patch.getBank() == channels[i].getBank() && patch.getProgram() == channels[i].getProgram() ){
					patchExists = true;
				}
			}
			if(!patchExists ){
				patchs.add(channels[i]);
			}
		}
	}
	patchs.add(new Patch(128, 0));
	
	return patchs;
}
 
Example 5
Source File: MiProvider.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void	noteReceived(ShortMessage inMessage, long inTimeStamp)
{
byte	pitch		= (byte)inMessage.getData1(),
		velocity	= (byte)inMessage.getData2(),
		stringIndex = (byte)getString(inMessage.getChannel());

if(stringIndex != -1)
	{
	byte	fretIndex = (byte)getFret(pitch, stringIndex);

	if(fretIndex != -1)
		{
		switch(inMessage.getCommand())
			{
			case ShortMessage.NOTE_ON:
				{
				switch(f_Mode)
					{
					case MiConfig.MODE_FRETBOARD_ECHO:
						if(velocity == 0 || velocity > f_MinVelocity)	// questo VA MODIFICATO!!!
							echo(stringIndex, fretIndex, velocity > 0);
						break;

					case MiConfig.MODE_CHORDS_RECORDING:
						if(velocity == 0 || velocity > f_MinVelocity)	// questo VA MODIFICATO!!!
							echo(stringIndex, fretIndex, velocity > 0);

						chord_AddNote(stringIndex, fretIndex, pitch, velocity, inTimeStamp);
						break;

					case MiConfig.MODE_SCALES_RECOGNITION:
						if(velocity == 0 || velocity > f_MinVelocity)	// questo VA MODIFICATO!!!
							echo(stringIndex, fretIndex, velocity > 0);

						scale_AddNote(stringIndex, fretIndex, pitch, velocity, inTimeStamp);
						break;

					case MiConfig.MODE_SONG_RECORDING:
						if(velocity == 0 || velocity > f_MinVelocity)	// questo VA MODIFICATO!!!
							echo(stringIndex, fretIndex, velocity > 0);

						MiRecorder.instance().addNote(stringIndex, fretIndex, pitch, velocity, inTimeStamp);
						break;
					}
				}
				break;

			case ShortMessage.NOTE_OFF:
				switch(f_Mode)
					{
					case MiConfig.MODE_FRETBOARD_ECHO:
						echo(stringIndex, fretIndex, false);
						break;

					case MiConfig.MODE_CHORDS_RECORDING:
						echo(stringIndex, fretIndex, false);
						chord_AddNote(stringIndex, fretIndex, pitch, (byte)0, inTimeStamp);
						break;

					case MiConfig.MODE_SCALES_RECOGNITION:
						echo(stringIndex, fretIndex, false);
						scale_AddNote(stringIndex, fretIndex, pitch, (byte)0, inTimeStamp);
						break;

					case MiConfig.MODE_SONG_RECORDING:
						echo(stringIndex, fretIndex, false);
						MiRecorder.instance().addNote(stringIndex, fretIndex, pitch, (byte)0, inTimeStamp);
						break;
					}
				break;
			}
		}
	}
}