com.leff.midi.MidiFile Java Examples

The following examples show how to use com.leff.midi.MidiFile. 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: MidiUtils.java    From brailleback with Apache License 2.0 6 votes vote down vote up
private static File writeMidiTrackToTempFile(Context context, MidiTrack noteTrack) {
    // Always add the default tempo track first.
    final ArrayList<MidiTrack> tracks = new ArrayList<MidiTrack>();
    tracks.add(DEFAULT_TEMPO_TRACK);
    tracks.add(noteTrack);

    // Attempt to write the track to a file and return it.
    try {
        final File midiDir = new File(context.getCacheDir(), MIDI_TEMP_DIR_NAME);
        if (!midiDir.exists() && !midiDir.mkdirs()) {
           return null;
        }

        final MidiFile midi = new MidiFile(MidiFile.DEFAULT_RESOLUTION, tracks);
        final File output = File.createTempFile("talkback", ".mid", midiDir);
        midi.writeToFile(output);
        return output;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #2
Source File: MidiFileFromScratch.java    From android-midi-lib with MIT License 4 votes vote down vote up
public static void main(String[] args)
{
    // 1. Create some MidiTracks
    MidiTrack tempoTrack = new MidiTrack();
    MidiTrack noteTrack = new MidiTrack();

    // 2. Add events to the tracks
    // 2a. Track 0 is typically the tempo map
    TimeSignature ts = new TimeSignature();
    ts.setTimeSignature(4, 4, TimeSignature.DEFAULT_METER, TimeSignature.DEFAULT_DIVISION);

    Tempo t = new Tempo();
    t.setBpm(228);

    tempoTrack.insertEvent(ts);
    tempoTrack.insertEvent(t);

    // 2b. Track 1 will have some notes in it
    for(int i = 0; i < 80; i++)
    {
        int channel = 0, pitch = 1 + i, velocity = 100;
        NoteOn on = new NoteOn(i * 480, channel, pitch, velocity);
        NoteOff off = new NoteOff(i * 480 + 120, channel, pitch, 0);

        noteTrack.insertEvent(on);
        noteTrack.insertEvent(off);

        // There is also a utility function for notes that you should use
        // instead of the above.
        noteTrack.insertNote(channel, pitch + 2, velocity, i * 480, 120);
    }

    // It's best not to manually insert EndOfTrack events; MidiTrack will
    // call closeTrack() on itself before writing itself to a file

    // 3. Create a MidiFile with the tracks we created
    ArrayList<MidiTrack> tracks = new ArrayList<MidiTrack>();
    tracks.add(tempoTrack);
    tracks.add(noteTrack);

    MidiFile midi = new MidiFile(MidiFile.DEFAULT_RESOLUTION, tracks);

    // 4. Write the MIDI data to a file
    File output = new File("exampleout.mid");
    try
    {
        midi.writeToFile(output);
    }
    catch(IOException e)
    {
        System.err.println(e);
    }
}
 
Example #3
Source File: MidiProcessor.java    From android-midi-lib with MIT License 3 votes vote down vote up
public MidiProcessor(MidiFile input)
{

    mMidiFile = input;

    mMPQN = Tempo.DEFAULT_MPQN;
    mPPQ = mMidiFile.getResolution();

    mEventsToListeners = new HashMap<Class<? extends MidiEvent>, List<MidiEventListener>>();
    mListenersToEvents = new HashMap<MidiEventListener, List<Class<? extends MidiEvent>>>();

    mMetronome = new MetronomeTick(new TimeSignature(), mPPQ);

    this.reset();
}