<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><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.75.2" /><link rel="home" href="index.html" title="Writing an ALSA Driver" /><link rel="up" href="ch09.html" title="Chapter 9. RawMIDI Interface" /><link rel="prev" href="ch09.html" title="Chapter 9. RawMIDI Interface" /><link rel="next" href="ch09s03.html" title="Callbacks" /></head><body><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="ch09.html">Prev</a> </td><th width="60%" align="center">Chapter 9. RawMIDI Interface</th><td width="20%" align="right"> <a accesskey="n" href="ch09s03.html">Next</a></td></tr></table><hr /></div><div class="section" title="Constructor"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="rawmidi-interface-constructor"></a>Constructor</h2></div></div></div><p> To create a rawmidi device, call the <code class="function">snd_rawmidi_new</code> function: </p><div class="informalexample"><pre class="programlisting"> struct snd_rawmidi *rmidi; err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi); if (err < 0) return err; rmidi->private_data = chip; strcpy(rmidi->name, "My MIDI"); rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX; </pre></div><p> </p><p> The first argument is the card pointer, the second argument is the ID string. </p><p> The third argument is the index of this component. You can create up to 8 rawmidi devices. </p><p> The fourth and fifth arguments are the number of output and input substreams, respectively, of this device (a substream is the equivalent of a MIDI port). </p><p> Set the <em class="structfield"><code>info_flags</code></em> field to specify the capabilities of the device. Set <code class="constant">SNDRV_RAWMIDI_INFO_OUTPUT</code> if there is at least one output port, <code class="constant">SNDRV_RAWMIDI_INFO_INPUT</code> if there is at least one input port, and <code class="constant">SNDRV_RAWMIDI_INFO_DUPLEX</code> if the device can handle output and input at the same time. </p><p> After the rawmidi device is created, you need to set the operators (callbacks) for each substream. There are helper functions to set the operators for all the substreams of a device: </p><div class="informalexample"><pre class="programlisting"> snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops); </pre></div><p> </p><p> The operators are usually defined like this: </p><div class="informalexample"><pre class="programlisting"> static struct snd_rawmidi_ops snd_mymidi_output_ops = { .open = snd_mymidi_output_open, .close = snd_mymidi_output_close, .trigger = snd_mymidi_output_trigger, }; </pre></div><p> These callbacks are explained in the <a class="link" href="ch09s03.html" title="Callbacks"><em class="citetitle">Callbacks</em></a> section. </p><p> If there are more than one substream, you should give a unique name to each of them: </p><div class="informalexample"><pre class="programlisting"> struct snd_rawmidi_substream *substream; list_for_each_entry(substream, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams, list { sprintf(substream->name, "My MIDI Port %d", substream->number + 1); } /* same for SNDRV_RAWMIDI_STREAM_INPUT */ </pre></div><p> </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch09.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch09.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch09s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 9. RawMIDI Interface </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Callbacks</td></tr></table></div></body></html>