Sophie

Sophie

distrib > Mageia > 5 > x86_64 > media > core-release > by-pkgid > b707d9a4ee443103660a75ccb6e51334 > files > 2562

csound-doc-5.19.01-10.mga5.noarch.rpm

<?xml version="1.0" encoding="UTF-8" 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=UTF-8" />
    <title>serialBegin</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.76.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="sensekey.html" title="sensekey" />
    <link rel="next" href="serialEnd.html" title="serialEnd" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">serialBegin</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="sensekey.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="serialEnd.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry" title="serialBegin">
      <a id="serialBegin"></a>
      <div class="titlepage"></div>
      <a id="IndexSerialBegin" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">serialBegin</span>
        </h2>
        <p>serialBegin — 
      Open a serial port.
    </p>
      </div>
      <div class="refsect1" title="Description">
        <a id="idp41858416"></a>
        <h2>Description</h2>
        <p>
      Open a serial port for arduino.
    </p>
      </div>
      <div class="refsect1" title="Syntax">
        <a id="idp41942016"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">iPort <span class="command"><strong>serialBegin</strong></span> SPortName [, ibaudRate]</pre>
      </div>
      <div class="refsect1" title="Initialization">
        <a id="idp41943120"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>SPortName</em></span> -- port name number
    </p>
        <p>
      <span class="emphasis"><em>ibaudrate</em></span> -- serial speed, defaulting to
      9600 bps.
    </p>
      </div>
      <div class="refsect1" title="Examples">
        <a id="idp41944424"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the serialBegin opcode. It uses the file <a class="ulink" href="examples/serialBegin.csd" target="_top"><em class="citetitle">serialBegin.csd</em></a>.
      </p>
        <div class="example">
          <a id="idp41945416"></a>
          <p class="title">
            <strong>Example 754. Example of the serialBegin opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
-n  <span class="comment">;;;no output</span>
<span class="comment">;-iadc    ;;;uncomment -iadc if realtime audio input is needed too</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o serialBegin.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span>  <span class="op">=</span> 44100
<span class="ohdr">ksmps</span> <span class="op">=</span> 500 <span class="comment">; the default krate can be too fast for the arduino to handle</span>
<span class="ohdr">nchnls</span> <span class="op">=</span> 2
<span class="ohdr">0dbfs</span>  <span class="op">=</span> 1

<span class="oblock">instr</span> 1

iPort <span class="opc">serialBegin</span> "<span class="op">/</span>dev<span class="op">/</span>cu.usbmodemfa131", 9600			<span class="comment">;connect to the arduino with baudrate = 9600</span>

kGain <span class="opc">init</span> 16							<span class="comment">;read our knob value</span>
kVal <span class="opc">serialRead</span> iPort
<span class="octrl">if</span> (kVal <span class="op">!=</span> <span class="op">-</span>1) then
    kGain <span class="op">=</span> kVal<span class="op">/</span>128
<span class="octrl">endif</span>

aSig <span class="opc">in</span>								<span class="comment">;get our audio input and get its rms</span>
kRms <span class="opc">rms</span> aSig<span class="op">*</span>kGain

kRms <span class="op">=</span> kRms<span class="op">*</span>kRms<span class="op">*</span>255						<span class="comment">;scale the rms to a good value for the LED and send it out</span>
<span class="opc">serialWrite</span> iPort, (kRms <span class="op">&lt;</span> 255 <span class="op">?</span> kRms <span class="op">:</span> 255)			<span class="comment">;must be in range: 0-255</span>

<span class="oblock">endin</span>
<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="stamnt">f</span> 1 0 1024 10 1 1 1 1 1 1

<span class="stamnt">i</span> 1 0 200
<span class="stamnt">e</span>
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
        This is the matching Arduino code :
      </p>
        <pre class="screen">
void setup() {
  // enable serial communication
  Serial.begin(9600);

  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
}

void loop() {
  // only do something if we received something (this should be at csound's k-rate)
  if (Serial.available()) {
    // set the brightness of LED (connected to pin 9) to our input value
    int brightness = Serial.read();
    analogWrite(9, brightness);

    // while we are here, get our knob value and send it to csound
    int sensorValue = analogRead(A0);
    Serial.write(sensorValue/4); // scale to 1-byte range (0-255)
  }     
}
.....</pre>
        <p>
    </p>
      </div>
      <div class="refsect1" title="See Also">
        <a id="idp41948664"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="serialEnd.html" title="serialEnd"><em class="citetitle"></em>serialEnd</a>,
      <a class="link" href="serialWrite_i.html" title="serialWrite_i"><em class="citetitle"></em>serialWrite_i</a>,
      <a class="link" href="serialWrite.html" title="serialWrite"><em class="citetitle"></em>serialWrite</a>,
      <a class="link" href="serialRead.html" title="serialRead"><em class="citetitle"></em>serialRead</a>,
      <a class="link" href="serialPrint.html" title="serialPrint"><em class="citetitle"></em>serialPrint</a>,
      <a class="link" href="serialFlush.html" title="serialFlush"><em class="citetitle"></em>serialFlush</a>.
    </p>
      </div>
      <div class="refsect1" title="Credits">
        <a id="idp41952512"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Matt Ingalls</td>
          </tr>
          <tr>
            <td>2011</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New in version 5.14</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="sensekey.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="serialEnd.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">sensekey </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> serialEnd</td>
        </tr>
      </table>
    </div>
  </body>
</html>