<!-- Converted to DocBook/SGML by Kevin Conder -->
<refentry id="midinoteonkey">
  <refmeta>
    <refentrytitle>midinoteonkey</refentrytitle>
  </refmeta>

  <refnamediv>
    <refname>midinoteonkey</refname>
    <refpurpose>
      Gets a MIDI note number value.
      <indexterm id="IndexMidinoteonkey"><primary>midinoteonkey</primary></indexterm>
    </refpurpose>
  </refnamediv>
  
  <refsect1>
    <title>Description</title>
    <para>
      <emphasis>midinoteonkey</emphasis> is designed to simplify writing instruments that can be used interchangeably for either score or MIDI input, and to make it easier to adapt instruments originally written for score input to work with MIDI input.
    </para>

    <para>
      In general, it should be possible to write instrument definitions that work identically with both scores
and MIDI, including both MIDI files and real-time MIDI input, without using any conditional statements, and that take full advantage of MIDI voice messages.
    </para>

    <para>
      Note that correlating Csound instruments with MIDI channel numbers is done using the <link linkend="massign"><citetitle>massign</citetitle></link> opcode for real-time performance,.  For file-driven performance, instrument numbers default to MIDI channel number + 1, but the defaults are overridden by any MIDI program change messages in the file.
    </para>
  </refsect1>

  <refsect1>
    <title>Syntax</title>
    <synopsis><command>midinoteonkey</command> xkey, xvelocity</synopsis>
  </refsect1>

  <refsect1>
    <title>Performance</title>
    <para>
      <emphasis>xkey</emphasis> -- returns MIDI key during MIDI activation, remains unchanged otherwise.
    </para>

    <para>
      <emphasis>xvelocity</emphasis> -- returns MIDI velocity during MIDI activation, remains unchanged otherwise.
    </para>

    <para>
      If the instrument was activated by MIDI input, the opcode overwrites the values of <emphasis>xkey</emphasis> and <emphasis>xvelocity</emphasis> with the corresponding values from MIDI input. If the instrument was <emphasis>NOT</emphasis> activated by MIDI input, the values of <emphasis>xkey</emphasis> and <emphasis>xvelocity</emphasis> remain unchanged.
    </para>

    <para>
      This enables score p-fields to receive MIDI input data during MIDI activation, and score values otherwise.
    </para>

    <note>
      <title>Adapting a score-activated Csound instrument.</title>
      <para>
        To adapt an ordinary Csound instrument designed for score activation for score/MIDI interoperability:

        <itemizedlist>
           <listitem><para>Change all <link linkend="linen"><citetitle>linen</citetitle></link>, <link linkend="linseg"><citetitle>linseg</citetitle></link>, and <link linkend="expseg"><citetitle>expseg</citetitle></link> opcodes to <link linkend="linenr"><citetitle>linenr</citetitle></link>, <link linkend="linsegr"><citetitle>linsegr</citetitle></link>, and <link linkend="expsegr"><citetitle>expsegr</citetitle></link>, respectively, except for a de-clicking or damping envelope. This will not materially change score-driven performance.</para></listitem>
           <listitem><para>Add the following lines at the beginning of the instrument definition:

              <informalexample>
                <programlisting>
; Ensures that a MIDI-activated instrument
; will have a positive p3 field.
mididefault 60, p3 
; Puts MIDI key translated to cycles per
; second into p4, and MIDI velocity into p5
midinoteoncps p4, p5 
                </programlisting>
              </informalexample>

             Obviously, <emphasis>midinoteoncps</emphasis> could be changed to <emphasis>midinoteonoct</emphasis> or any of the other options, and the choice of p-fields is arbitrary.</para></listitem>
        </itemizedlist>
      </para>
    </note>
  </refsect1>

  <refsect1>
    <title>Examples</title>
    <para>
      Here is an example of the midinoteonkey opcode. It uses the files <ulink url="examples/midinoteonkey.orc"><citetitle>midinoteonkey.orc</citetitle></ulink> and <ulink url="examples/midinoteonkey.sco"><citetitle>midinoteonkey.sco</citetitle></ulink>.

      <example>
        <title>Example of the midinoteonkey opcode.</title>

        <programlisting>
<inlinemediaobject>
            <imageobject>
              <imagedata fileref="examples/midinoteonkey.orc" format="linespecific">
            </imageobject>
          </inlinemediaobject>
        </programlisting>

        <programlisting>
<inlinemediaobject>
            <imageobject>
              <imagedata fileref="examples/midinoteonkey.sco" format="linespecific">
            </imageobject>
          </inlinemediaobject>
        </programlisting>
      </example>

      Its output should include lines like:
      <screen>
 i1    60.00000
 i1    69.00000
      </screen>
    </para>
  </refsect1>

  <refsect1>
    <title>See Also</title>
    <para>
      <link linkend="midichannelaftertouch"><citetitle>midichannelaftertouch</citetitle></link>, 
      <link linkend="midicontrolchange"><citetitle>midicontrolchange</citetitle></link>, 
      <link linkend="mididefault"><citetitle>mididefault</citetitle></link>, 
      <link linkend="midinoteoff"><citetitle>midinoteoff</citetitle></link>, 
      <link linkend="midinoteoncps"><citetitle>midinoteoncps</citetitle></link>, 
      <link linkend="midinoteonoct"><citetitle>midinoteonoct</citetitle></link>, 
      <link linkend="midinoteonpch"><citetitle>midinoteonpch</citetitle></link>, 
      <link linkend="midipitchbend"><citetitle>midipitchbend</citetitle></link>, 
      <link linkend="midipolyaftertouch"><citetitle>midipolyaftertouch</citetitle></link>, 
      <link linkend="midiprogramchange"><citetitle>midiprogramchange</citetitle></link>
    </para>
  </refsect1>

  <refsect1>
    <title>Credits</title>
    <para>Author: Michael Gogins</para>
    <para>Example written by &namekevin;.</para>
    <para>New in version 4.20</para>
  </refsect1>
</refentry>
