Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C API not working for MIDI input #272

Open
jg-854 opened this issue Nov 19, 2021 · 5 comments
Open

C API not working for MIDI input #272

jg-854 opened this issue Nov 19, 2021 · 5 comments

Comments

@jg-854
Copy link

jg-854 commented Nov 19, 2021

Hi,

I am trying to use the C API and I have built a rudimentary program that listens to the MIDI messages from a keyboard. I know my keyboard works (tested in Garageband, as well as using the testing with qmidiin.cpp). Even though the length of the message after calling the function is 3, the message array doesn't seem to be updated. Why is this?

I tried the C API test function provided in the repo, and that works. Moreover, I am able to retrieve the name of of the device that I am connected to. It is just reading the message that seems to not work.

`

#include <stdio.h>
#include<stdlib.h>
#include "rtmidi_c.h"

struct RtMidiWrapper *midiin;
                                
int main() {
    
    midiin = rtmidi_in_create_default();
    unsigned int ports = rtmidi_get_port_count(midiin);
    rtmidi_in_ignore_types(midiin,false,false,false);
    printf("MIDI input ports found: %u \n", ports);

    if (ports != 0)
    {
        printf("Connecting to device \n ");
        rtmidi_open_port(midiin,0,"Midi Keyboard");
    }   
    else{
        printf("Could not find a device!");
        goto cleanup;
    }


    unsigned char message[1024] ;
    size_t length;
    double td;

    while (true)
    {
        td = rtmidi_in_get_message(midiin,message,&length);
        if (length > 0)
        {
            printf("\n Length of message received: %zu ",length);
            for (int i =0 ; i< length ; i++)
            {
                printf("Byte %d: %u  ", i,message[i]);
            }
        }
    }

    cleanup:

        rtmidi_close_port(midiin);
        rtmidi_in_free(midiin);
        return 0;
}

`

I am running on a M1 MBP using Monterey
Thanks!

@garyscavone
Copy link
Contributor

Did you try the programs in the tests directory?

@jg-854
Copy link
Author

jg-854 commented Nov 19, 2021

Thanks for the reply -no I did not. I ran the programs in a separate directory. This directory contains rtmidi_c.h and the dynamic libraries as well as my source code.

@jg-854
Copy link
Author

jg-854 commented Nov 19, 2021

Did you try the programs in the tests directory?

Sorry, I misread your question. I did run the programs in test directory. They all work. However, the C API test doesn't cover the 'get message' function. that is what I am interested in.

@koppi
Copy link

koppi commented May 21, 2022

@jg-854 replace size_t length; with size_t length = 1024; in the above code and it should work as expected.

@garyscavone
Copy link
Contributor

Great! Can you submit a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants