send midi messages from C++

◇◆丶佛笑我妖孽 提交于 2019-12-07 16:10:28

To send some events at a specific time:

  • open the sequencer;
  • create your own (source) port;
  • construct and send some events.

To open the sequencer, call snd_seq_open. (You can get your client number with snd_seq_client_id.)

    snd_seq_t seq;
    snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);

To create a port, allocate a port info object with snd_seq_port_info_alloca, set port parameters with snd_seq_port_info_set_xxx, and call snd_seq_create_port. Or simply call snd_seq_create_simple_port.

    int port;
    port = snd_seq_create_simple_port(seq, "my port",
            SND_SEQ_PORT_CAP_READ | SND_SEQ_POR_CAP_WRITE,
            SND_SEQ_PORT_TYPE_APPLICATION);

To send an event, allocate an event structure (just for a change, you can use a local snd_seq_event_t variable), and call various snd_seq_ev_xxx functions to set its properties. Then call snd_seq_event_output, and snd_seq_drain_output after you've sent all events.

    snd_seq_event_t ev;
    snd_seq_ev_clear(&ev);
    snd_seq_ev_set_direct(&ev);

    /* either */
    snd_seq_ev_set_dest(&ev, 64, 0); /* send to 64:0 */
    /* or */
    snd_seq_ev_set_subs(&ev);        /* send to subscribers of source port */

    snd_seq_ev_set_noteon(&ev, 0, 60, 127);
    snd_seq_event_output(seq, &ev);

    snd_seq_ev_set_noteon(&ev, 0, 67, 127);
    snd_seq_event_output(seq, &ev);

    snd_seq_drain_output(seq);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!