time signature

View: New views
2 Messages — Rating Filter:   Alert me  

time signature

by Rob de Heer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I've been looking for the correct way to add a time signature to a track. Does anyone have some code that works? i tried this and other methods without success. And please pardon my ignorance.

    private void addTimeSignature(Track track,
                                  int location,
                                  int numerator,
                                  int denominator) throws InvalidMidiDataException {
        MetaMessage mm = new MetaMessage();
        byte data[] = new byte[4];

        data[0] = (byte) numerator;

        // denominator must be an exact power of 2 that divides 96
        switch (denominator) {
            case 1:
                data[1] = 0;
                break;
            case 2:
                data[1] = 1;
                break;
            case 4:
                data[1] = 2;
                break;
            case 8:
                data[1] = 3;
                break;
            case 16:
                data[1] = 4;
                break;
            case 32:
                data[1] = 5;
                break;
            default:
                log.info("Time signature denominator " + denominator + " is not supported");
        }

        data[2] = 0x18; // (byte) (96 / denominator);
        data[3] = 0x08; // 8;

        try {
            mm.setMessage(0x58, data, data.length);
            MidiEvent me = new MidiEvent(mm, location);
            track.add(me);
        } catch (InvalidMidiDataException e) {
            // TODO consider better exceptions round here
            log.error("Invalid time signature MIDI data");
        }
    }

Rob

===========================================================================
Java Sound homepage:
        http://java.sun.com/products/java-media/sound/
Mailing list archive:
        http://archives.java.sun.com/archives/javasound-interest.html
===========================================================================
To unsubscribe, send email to listserv@... and include in the body
of the message "signoff JAVASOUND-INTEREST".  For general help, send email to
listserv@... and include in the body of the message "help".

Re: time signature

by Steve Taylor-13 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Is your data array not one byte too short?

As far as I can see a valid time signature message has format FF 58 04 nn  
dd cc bb.
You seem to be missing the 04 following the 58.

Steve.

On Fri, 19 Jun 2009 05:47:27 +0100, Rob de Heer <rdeheer@...>  
wrote:

> Hello,
>
> I've been looking for the correct way to add a time signature to a  
> track. Does anyone have some code that works? i tried this and other  
> methods without success. And please pardon my ignorance.
>
>     private void addTimeSignature(Track track,
>                                   int location,
>                                   int numerator,
>                                   int denominator) throws  
> InvalidMidiDataException {
>         MetaMessage mm = new MetaMessage();
>         byte data[] = new byte[4];
>
>         data[0] = (byte) numerator;
>
>         // denominator must be an exact power of 2 that divides 96
>         switch (denominator) {
>             case 1:
>                 data[1] = 0;
>                 break;
>             case 2:
>                 data[1] = 1;
>                 break;
>             case 4:
>                 data[1] = 2;
>                 break;
>             case 8:
>                 data[1] = 3;
>                 break;
>             case 16:
>                 data[1] = 4;
>                 break;
>             case 32:
>                 data[1] = 5;
>                 break;
>             default:
>                 log.info("Time signature denominator " + denominator + "  
> is not supported");
>         }
>
>         data[2] = 0x18; // (byte) (96 / denominator);
>         data[3] = 0x08; // 8;
>
>         try {
>             mm.setMessage(0x58, data, data.length);
>             MidiEvent me = new MidiEvent(mm, location);
>             track.add(me);
>         } catch (InvalidMidiDataException e) {
>             // TODO consider better exceptions round here
>             log.error("Invalid time signature MIDI data");
>         }
>     }
>
> Rob

===========================================================================
Java Sound homepage:
        http://java.sun.com/products/java-media/sound/
Mailing list archive:
        http://archives.java.sun.com/archives/javasound-interest.html
===========================================================================
To unsubscribe, send email to listserv@... and include in the body
of the message "signoff JAVASOUND-INTEREST".  For general help, send email to
listserv@... and include in the body of the message "help".