Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > 0a2d1da5078620d6abbc0a5e920f2a92 > files > 45

fluidsynth-devel-1.1.3-1.fc14.x86_64.rpm

<!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/xhtml;charset=UTF-8"/>
<title>libfluidsynth: fluidsynth_metronome.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.1 -->
<script type="text/javascript">
function hasClass(ele,cls) {
  return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
  if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
  if (hasClass(ele,cls)) {
    var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
    ele.className=ele.className.replace(reg,' ');
  }
}

function toggleVisibility(linkObj) {
 var base = linkObj.getAttribute('id');
 var summary = document.getElementById(base + '-summary');
 var content = document.getElementById(base + '-content');
 var trigger = document.getElementById(base + '-trigger');
 if ( hasClass(linkObj,'closed') ) {
   summary.style.display = 'none';
   content.style.display = 'block';
   trigger.src = 'open.png';
   removeClass(linkObj,'closed');
   addClass(linkObj,'opened');
 } else if ( hasClass(linkObj,'opened') ) {
   summary.style.display = 'block';
   content.style.display = 'none';
   trigger.src = 'closed.png';
   removeClass(linkObj,'opened');
   addClass(linkObj,'closed');
 }
 return false;
}
</script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<h1>fluidsynth_metronome.c</h1>  </div>
</div>
<div class="contents">
<p>Example of a simple metronome using the MIDI sequencer API</p>
<div class="fragment"><pre class="fragment"><span class="comment">/* FluidSynth Metronome - Sequencer API example</span>
<span class="comment"> *</span>
<span class="comment"> * This code is in the public domain.</span>
<span class="comment"> *</span>
<span class="comment"> * To compile:</span>
<span class="comment"> *   gcc -o fluidsynth_metronome -lfluidsynth fluidsynth_metronome.c</span>
<span class="comment"> *</span>
<span class="comment"> * To run:</span>
<span class="comment"> *   fluidsynth_metronome soundfont [beats [tempo]]</span>
<span class="comment"> *</span>
<span class="comment"> * [Pedro Lopez-Cabanillas &lt;plcl@users.sf.net&gt;]</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<span class="preprocessor">#include &lt;stdio.h&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="fluidsynth_8h.html" title="FluidSynth is a real-time synthesizer designed for SoundFont(R) files.">fluidsynth.h</a>&gt;</span>

<a class="code" href="types_8h.html#ae265f10ae174a13afe010de50d87e1a4" title="Synthesizer instance.">fluid_synth_t</a> *synth;
<a class="code" href="types_8h.html#ac3706330ce49cac5b7dd079e90d376d8" title="Audio driver instance.">fluid_audio_driver_t</a> *audiodriver;
<a class="code" href="types_8h.html#a7c7acad4ee620fc954a7ad4d7e87e1c3" title="Sequencer instance.">fluid_sequencer_t</a> *sequencer;
<span class="keywordtype">short</span> synth_destination, client_destination;
<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> time_marker;
<span class="comment">/* default tempo, beats per minute */</span>
<span class="preprocessor">#define TEMPO 120</span>
<span class="preprocessor"></span><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> note_duration = 60000 / TEMPO;
<span class="comment">/* metronome click/bell */</span>
<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> weak_note = 33;
<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> strong_note = 34;
<span class="comment">/* number of notes in one pattern */</span>
<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pattern_size = 4;
<span class="comment">/* prototype */</span>
<span class="keywordtype">void</span>
sequencer_callback (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> time, <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba" title="Sequencer event.">fluid_event_t</a> *event,
                    <a class="code" href="types_8h.html#a7c7acad4ee620fc954a7ad4d7e87e1c3" title="Sequencer instance.">fluid_sequencer_t</a> *seq, <span class="keywordtype">void</span> *data);

<span class="comment">/* schedule a note on message */</span>
<span class="keywordtype">void</span>
schedule_noteon (<span class="keywordtype">int</span> chan, <span class="keywordtype">short</span> key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> ticks)
{
    <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba" title="Sequencer event.">fluid_event_t</a> *ev = <a name="a0"></a><a class="code" href="event_8h.html#a1d535480f26630b755102415a6d246cb" title="Create a new sequencer event structure.">new_fluid_event</a> ();
    <a name="a1"></a><a class="code" href="event_8h.html#a22e7fc6d5ce4ca9eab6dd6d60c85d78b" title="Set source of a sequencer event (DOCME).">fluid_event_set_source</a> (ev, -1);
    <a name="a2"></a><a class="code" href="event_8h.html#ae0857e30ffdcfe4e277eb3c55df7ba32" title="Set destination of a sequencer event (DOCME).">fluid_event_set_dest</a> (ev, synth_destination);
    <a name="a3"></a><a class="code" href="event_8h.html#a1337c75403ebf9c43607a3f3139c6bba" title="Set a sequencer event to be a note on event.">fluid_event_noteon</a> (ev, chan, key, 127);
    <a name="a4"></a><a class="code" href="seq_8h.html#afb0c62f3a2cff3decbf628535f4511ef" title="Schedule an event for sending at a later time.">fluid_sequencer_send_at</a> (sequencer, ev, ticks, 1);
    <a name="a5"></a><a class="code" href="event_8h.html#a473c54f0c43bdea789b211c23c7daeee" title="Delete a sequencer event structure.">delete_fluid_event</a> (ev);
}

<span class="comment">/* schedule a timer event (shall trigger the callback) */</span>
<span class="keywordtype">void</span>
schedule_timer_event ()
{
    <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba" title="Sequencer event.">fluid_event_t</a> *ev = <a class="code" href="event_8h.html#a1d535480f26630b755102415a6d246cb" title="Create a new sequencer event structure.">new_fluid_event</a> ();
    <a class="code" href="event_8h.html#a22e7fc6d5ce4ca9eab6dd6d60c85d78b" title="Set source of a sequencer event (DOCME).">fluid_event_set_source</a> (ev, -1);
    <a class="code" href="event_8h.html#ae0857e30ffdcfe4e277eb3c55df7ba32" title="Set destination of a sequencer event (DOCME).">fluid_event_set_dest</a> (ev, client_destination);
    <a name="a6"></a><a class="code" href="event_8h.html#aa99a8c9110e819b53fc42baa0bfd8fbf" title="Set a sequencer event to be a timer event.">fluid_event_timer</a> (ev, NULL);
    <a class="code" href="seq_8h.html#afb0c62f3a2cff3decbf628535f4511ef" title="Schedule an event for sending at a later time.">fluid_sequencer_send_at</a> (sequencer, ev, time_marker, 1);
    <a class="code" href="event_8h.html#a473c54f0c43bdea789b211c23c7daeee" title="Delete a sequencer event structure.">delete_fluid_event</a> (ev);
}

<span class="comment">/* schedule the metronome pattern */</span>
<span class="keywordtype">void</span>
schedule_pattern ()
{
    <span class="keywordtype">int</span> i, note_time;
    note_time = time_marker;
    <span class="keywordflow">for</span> (i = 0; i &lt; pattern_size; ++i) {
        schedule_noteon (9, i ? weak_note : strong_note, note_time);
        note_time += note_duration;
    }
    time_marker = note_time;
}

<span class="keywordtype">void</span>
sequencer_callback (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> time, <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba" title="Sequencer event.">fluid_event_t</a> *event,
                    <a class="code" href="types_8h.html#a7c7acad4ee620fc954a7ad4d7e87e1c3" title="Sequencer instance.">fluid_sequencer_t</a> *seq, <span class="keywordtype">void</span> *data)
{
    schedule_timer_event ();
    schedule_pattern ();
}

<span class="keywordtype">void</span>
usage (<span class="keywordtype">char</span>* prog_name)
{
    printf (<span class="stringliteral">&quot;Usage: %s soundfont.sf2 [beats [tempo]]\n&quot;</span>, prog_name);
    printf (<span class="stringliteral">&quot;\t(optional) beats: number of pattern beats, default %d\n&quot;</span>,
            pattern_size);
    printf (<span class="stringliteral">&quot;\t(optional) tempo: BPM (Beats Per Minute), default %d\n&quot;</span>, TEMPO);
}

<span class="keywordtype">int</span>
main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
{
    <span class="keywordtype">int</span> n;
    <a class="code" href="types_8h.html#aa363402d3c77333b0f070ba531d034ba" title="Configuration settings instance.">fluid_settings_t</a> *settings;
    settings = <a name="a7"></a><a class="code" href="settings_8h.html#ad7ab9269a28c2b5852d512ffe3546949" title="Create a new settings object.">new_fluid_settings</a> ();
    <span class="keywordflow">if</span> (argc &lt; 2) {
        usage (argv[0]);
    } <span class="keywordflow">else</span> {
        <span class="comment">/* create the synth, driver and sequencer instances */</span>
        synth = <a name="a8"></a><a class="code" href="synth_8h.html#a344f7369c0f57d30f72702d0c88e6178" title="Create new FluidSynth instance.">new_fluid_synth</a> (settings);
        audiodriver = <a name="a9"></a><a class="code" href="audio_8h.html#a4ad51317b10b89bfe94ade5db345864b" title="Create a new audio driver.">new_fluid_audio_driver</a> (settings, synth);
        sequencer = <a name="a10"></a><a class="code" href="seq_8h.html#a9ab3db0b0977a97bc9f6bc17b1c1534d" title="Create a new sequencer object which uses the system timer.">new_fluid_sequencer</a> ();
        <span class="comment">/* register the synth with the sequencer */</span>
        synth_destination = <a name="a11"></a><a class="code" href="seqbind_8h.html#a3680e34efa6745fcbe851fcd802cba39" title="Registers a synthesizer as a destination client of the given sequencer.">fluid_sequencer_register_fluidsynth</a> (sequencer,
                synth);
        <span class="comment">/* register the client name and callback */</span>
        client_destination = <a name="a12"></a><a class="code" href="seq_8h.html#a749882ab2b9ce51fa6a265db2cd59915" title="Register a sequencer client.">fluid_sequencer_register_client</a> (sequencer,
                <span class="stringliteral">&quot;fluidsynth_metronome&quot;</span>, sequencer_callback, NULL);
        <span class="comment">/* load a SoundFont */</span>
        n = <a name="a13"></a><a class="code" href="synth_8h.html#aaf9376cf7189f9c64da5ffdeed85c9c4" title="Load a SoundFont file (filename is interpreted by SoundFont loaders).">fluid_synth_sfload</a> (synth, argv[1], 1);
        <span class="keywordflow">if</span> (n != -1) {
            <span class="keywordflow">if</span> (argc &gt; 2) {
                n = atoi (argv[2]);
                <span class="keywordflow">if</span> (n &gt; 0) pattern_size = n;
            }
            <span class="keywordflow">if</span> (argc &gt; 3) {
                n = atoi (argv[3]);
                <span class="keywordflow">if</span> (n &gt; 0) note_duration = 60000 / n;
            }
            <span class="comment">/* get the current time in ticks */</span>
            time_marker = <a name="a14"></a><a class="code" href="seq_8h.html#a2a51cf34c359020eb6c9b7ab549ab239" title="Get the current tick of a sequencer.">fluid_sequencer_get_tick</a> (sequencer);
            <span class="comment">/* schedule patterns */</span>
            schedule_pattern ();
            schedule_timer_event ();
            schedule_pattern ();
            <span class="comment">/* wait for user input */</span>
            printf (<span class="stringliteral">&quot;press &lt;Enter&gt; to stop\n&quot;</span>);
            n = getchar ();
        }
        <span class="comment">/* clean and exit */</span>
        <a name="a15"></a><a class="code" href="seq_8h.html#a57d2277b188240e435b5fc3f39c0bc7e" title="Free a sequencer object.">delete_fluid_sequencer</a> (sequencer);
        <a name="a16"></a><a class="code" href="audio_8h.html#a05678e633d37da0f93000045393e9442" title="Deletes an audio driver instance.">delete_fluid_audio_driver</a> (audiodriver);
        <a name="a17"></a><a class="code" href="synth_8h.html#a585a63f3a25b9df17b82ae87f2d38cfc" title="Delete a FluidSynth instance.">delete_fluid_synth</a> (synth);
    }
    <a name="a18"></a><a class="code" href="settings_8h.html#a550270f09e4f3c645cbe9e6d3c514ca4" title="Delete the provided settings object.">delete_fluid_settings</a> (settings);
    <span class="keywordflow">return</span> 0;
}
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Tue Oct 12 2010 for libfluidsynth by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.1 </small></address>
</body>
</html>