1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Constructor</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Writing an ALSA Driver"><link rel="up" href="rawmidi-interface.html" title="Chapter&#160;9.&#160;RawMIDI Interface"><link rel="prev" href="rawmidi-interface.html" title="Chapter&#160;9.&#160;RawMIDI Interface"><link rel="next" href="rawmidi-interface-callbacks.html" title="Callbacks"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Constructor</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="rawmidi-interface.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;9.&#160;RawMIDI Interface</th><td width="20%" align="right">&#160;<a accesskey="n" href="rawmidi-interface-callbacks.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="rawmidi-interface-constructor"></a>Constructor</h2></div></div></div><p>
2      To create a rawmidi device, call the
3      <code class="function">snd_rawmidi_new</code> function:
4        </p><div class="informalexample"><pre class="programlisting">
5
6  struct snd_rawmidi *rmidi;
7  err = snd_rawmidi_new(chip-&gt;card, "MyMIDI", 0, outs, ins, &amp;rmidi);
8  if (err &lt; 0)
9          return err;
10  rmidi-&gt;private_data = chip;
11  strcpy(rmidi-&gt;name, "My MIDI");
12  rmidi-&gt;info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
13                      SNDRV_RAWMIDI_INFO_INPUT |
14                      SNDRV_RAWMIDI_INFO_DUPLEX;
15
16          </pre></div><p>
17      </p><p>
18      The first argument is the card pointer, the second argument is
19      the ID string.
20      </p><p>
21      The third argument is the index of this component.  You can
22      create up to 8 rawmidi devices.
23      </p><p>
24      The fourth and fifth arguments are the number of output and
25      input substreams, respectively, of this device (a substream is
26      the equivalent of a MIDI port).
27      </p><p>
28      Set the <em class="structfield"><code>info_flags</code></em> field to specify
29      the capabilities of the device.
30      Set <code class="constant">SNDRV_RAWMIDI_INFO_OUTPUT</code> if there is
31      at least one output port,
32      <code class="constant">SNDRV_RAWMIDI_INFO_INPUT</code> if there is at
33      least one input port,
34      and <code class="constant">SNDRV_RAWMIDI_INFO_DUPLEX</code> if the device
35      can handle output and input at the same time.
36      </p><p>
37      After the rawmidi device is created, you need to set the
38      operators (callbacks) for each substream.  There are helper
39      functions to set the operators for all the substreams of a device:
40        </p><div class="informalexample"><pre class="programlisting">
41
42  snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &amp;snd_mymidi_output_ops);
43  snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &amp;snd_mymidi_input_ops);
44
45          </pre></div><p>
46      </p><p>
47      The operators are usually defined like this:
48        </p><div class="informalexample"><pre class="programlisting">
49
50  static struct snd_rawmidi_ops snd_mymidi_output_ops = {
51          .open =    snd_mymidi_output_open,
52          .close =   snd_mymidi_output_close,
53          .trigger = snd_mymidi_output_trigger,
54  };
55
56          </pre></div><p>
57      These callbacks are explained in the <a class="link" href="rawmidi-interface-callbacks.html" title="Callbacks"><em class="citetitle">Callbacks</em></a>
58      section.
59      </p><p>
60      If there are more than one substream, you should give a
61      unique name to each of them:
62        </p><div class="informalexample"><pre class="programlisting">
63
64  struct snd_rawmidi_substream *substream;
65  list_for_each_entry(substream,
66                      &amp;rmidi-&gt;streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
67                      list {
68          sprintf(substream-&gt;name, "My MIDI Port %d", substream-&gt;number + 1);
69  }
70  /* same for SNDRV_RAWMIDI_STREAM_INPUT */
71
72          </pre></div><p>
73      </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="rawmidi-interface.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="rawmidi-interface.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="rawmidi-interface-callbacks.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&#160;9.&#160;RawMIDI Interface&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Callbacks</td></tr></table></div></body></html>
74