Java Code Examples for javax.sound.midi.ShortMessage#getData2()
The following examples show how to use
javax.sound.midi.ShortMessage#getData2() .
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 | 4 votes |
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 2
Source File: FastShortMessage.java From Bytecoder with Apache License 2.0 | 4 votes |
/** Creates a FastShortMessage from this ShortMessage */ FastShortMessage(ShortMessage msg) { this.packedMsg = msg.getStatus() | (msg.getData1() << 8) | (msg.getData2() << 16); }
Example 3
Source File: RealTimeSequencer.java From Bytecoder with Apache License 2.0 | 4 votes |
/** * chase all events from beginning of Track * and send note off for those events that are active * in noteOnCache array. * It is possible, of course, to catch notes from other tracks, * but better than more complicated logic to detect * which notes are really from this track */ private void sendNoteOffIfOn(Track track, long endTick) { int size = track.size(); int done = 0; try { for (int i = 0; i < size; i++) { MidiEvent event = track.get(i); if (event.getTick() > endTick) break; MidiMessage msg = event.getMessage(); int status = msg.getStatus(); int len = msg.getLength(); if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) { int note = -1; if (msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage) msg; if (smsg.getData2() > 0) { // only consider Note On with velocity > 0 note = smsg.getData1(); } } else { byte[] data = msg.getMessage(); if ((data[2] & 0x7F) > 0) { // only consider Note On with velocity > 0 note = data[1] & 0x7F; } } if (note >= 0) { int bit = 1<<(status & 0x0F); if ((noteOnCache[note] & bit) != 0) { // the bit is set. Send Note Off getTransmitterList().sendMessage(status | (note<<8), -1); // clear the bit noteOnCache[note] &= (0xFFFF ^ bit); done++; } } } } } catch (ArrayIndexOutOfBoundsException aioobe) { // this happens when messages are removed // from the track while this method executes } }
Example 4
Source File: FastShortMessage.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** Creates a FastShortMessage from this ShortMessage */ FastShortMessage(ShortMessage msg) { this.packedMsg = msg.getStatus() | (msg.getData1() << 8) | (msg.getData2() << 16); }
Example 5
Source File: RealTimeSequencer.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * chase all events from beginning of Track * and send note off for those events that are active * in noteOnCache array. * It is possible, of course, to catch notes from other tracks, * but better than more complicated logic to detect * which notes are really from this track */ private void sendNoteOffIfOn(Track track, long endTick) { int size = track.size(); int done = 0; try { for (int i = 0; i < size; i++) { MidiEvent event = track.get(i); if (event.getTick() > endTick) break; MidiMessage msg = event.getMessage(); int status = msg.getStatus(); int len = msg.getLength(); if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) { int note = -1; if (msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage) msg; if (smsg.getData2() > 0) { // only consider Note On with velocity > 0 note = smsg.getData1(); } } else { byte[] data = msg.getMessage(); if ((data[2] & 0x7F) > 0) { // only consider Note On with velocity > 0 note = data[1] & 0x7F; } } if (note >= 0) { int bit = 1<<(status & 0x0F); if ((noteOnCache[note] & bit) != 0) { // the bit is set. Send Note Off getTransmitterList().sendMessage(status | (note<<8), -1); // clear the bit noteOnCache[note] &= (0xFFFF ^ bit); done++; } } } } } catch (ArrayIndexOutOfBoundsException aioobe) { // this happens when messages are removed // from the track while this method executes } if (DEBUG_PUMP) Printer.println(" sendNoteOffIfOn: sent "+done+" messages."); }
Example 6
Source File: MidiToAudioWriter.java From tuxguitar with GNU Lesser General Public License v2.1 | 4 votes |
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 7
Source File: MiProvider.java From tuxguitar with GNU Lesser General Public License v2.1 | 4 votes |
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; } } } }