从C++发送 MIDI 消息

send midi messages from C++

本文关键字:消息 MIDI 发送 C++      更新时间:2023-10-16

我使用的是树莓派,所以它有点像Debian(Raspbian)

有一个正在运行的合成器(Zynaddsubfx),我想从代码向他发送midi消息并让它为我播放音乐。我将为此使用 ALSA。

我设法通过执行以下操作在我的程序中创建了一个"发射端口":

snd_seq_create_simple_port(seq_handle, "My own sequencer",
    SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
    SND_SEQ_PORT_TYPE_APPLICATION)

现在我可以在aconnect -ol中看到 ZynSubAddFX,在aconnect -il中看到我自己的音序器。我能够连接它们:

pi@cacharro:~/projects/tests$ aconnect 129:0 128:0
pi@cacharro:~/projects/tests$ Info, alsa midi port connected

为此,正如CL所建议的那样,我使用了打开的snd_seq_open,存储了序列,然后使用了snd_seq_create_simple_port。但:

如前所述,我只想在用户交互下将命令发送到zynsubaddfx,因此创建队列,添加速度等不是要走的路。

有没有办法通过我打开的端口发送简单的 midi 命令,例如开/关笔记???

在特定时间发送一些事件:

  • 打开音序器;
  • 创建您自己的(源)端口;
  • 构造并发送一些事件。

若要打开排序器,请调用 snd_seq_open 。(您可以使用snd_seq_client_id获取您的客户编号。

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

要创建端口,请使用 分配端口信息对象 snd_seq_port_info_alloca,设置端口参数 snd_seq_port_info_set_ xxx 和 call snd_seq_create_port .或者干脆打电话给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);

要发送事件,请分配事件结构(只需对于更改,您可以使用局部snd_seq_event_t变量),并调用各种 snd_seq_ev_ xxx 函数来设置其属性。然后致电snd_seq_event_output,并在发送完所有内容后snd_seq_drain_output事件。

    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);