Rice_Crisp
Rice_Crisp

Reputation: 1261

Writing continuous controller data to a midi file

I'm trying to write a java program that reads data from an image and turns it into midi data. I've gotten as far as creating multiple tracks with the proper midi note on and note off messages. However, I also want to write continuous controller data to each track (an unused slider or knob, not one of the predetermined channels such as modwheel or pitchbend). I'm assuming I need to be doing some with the CONTROL_CHANGE ShortMessage, but I could be wrong. (http://docs.oracle.com/javase/7/docs/api/javax/sound/midi/ShortMessage.html#CONTROL_CHANGE). I don't really have the java programming knowledge to figure this out. Here's what I have thus far for creating the note on/off midi data. I assume the continuous controller data would be of a similiar design. Any help would be appreciated.

    private static MidiEvent createNoteOnEvent(int nKey, long lTick)
    {
        return createNoteEvent(ShortMessage.NOTE_ON,
                               nKey,
                               VELOCITY,
                               lTick);
    }

    private static MidiEvent createNoteOffEvent(int nKey, long lTick)
    {
        return createNoteEvent(ShortMessage.NOTE_OFF,
                               nKey,
                               0,
                               lTick);
    }

    private static MidiEvent createNoteEvent(int nCommand,
                                             int nKey,
                                             int nVelocity,
                                             long lTick)
    {
        ShortMessage message = new ShortMessage();
        try {
            message.setMessage(nCommand,
                               0,   
                               nKey,
                               nVelocity);
        }
        catch (InvalidMidiDataException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
        MidiEvent event = new MidiEvent(message,
                                          lTick);
        return event;
    }

    private static void out(String strMessage)
    {
        System.out.println(strMessage);
    }

Upvotes: 1

Views: 410

Answers (2)

Rice_Crisp
Rice_Crisp

Reputation: 1261

Figured it out, in case someone else runs across the same problem. The hex 0xB0 is just the standard continuous controller data channel. 17 could just be typed, hex isn't required. Other channels can be found by Googling midi format or something like that:

private static MidiEvent createCCData(int cc, int val, long lTick) {
    ShortMessage mm = new ShortMessage();
    mm = new ShortMessage();
    try {
        mm.setMessage(0xB0,cc,val);
    } 
    catch (InvalidMidiDataException e) {
        e.printStackTrace();
        System.exit(1);
    }
    MidiEvent me = new MidiEvent(mm,lTick);     
    return me;
}

Upvotes: 1

Nik Reiman
Nik Reiman

Reputation: 40430

The pre-defined MIDI CC's such as volume, pitchbend, etc, are just recommended assignments, but you can technically use any CC for a spare knob or slider that you wish.

Upvotes: 0

Related Questions