<!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_arpeggio.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 Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data 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_arpeggio.c</h1> </div> </div> <div class="contents"> <p>Example of an arpeggio generated using the MIDI sequencer API</p> <div class="fragment"><pre class="fragment"><span class="comment">/* FluidSynth Arpeggio - 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_arpeggio -lfluidsynth fluidsynth_arpeggio.c</span> <span class="comment"> *</span> <span class="comment"> * To run:</span> <span class="comment"> * fluidsynth_arpeggio soundfont [steps [duration]]</span> <span class="comment"> *</span> <span class="comment"> * [Pedro Lopez-Cabanillas <plcl@users.sf.net>]</span> <span class="comment"> */</span> <span class="preprocessor">#include <stdlib.h></span> <span class="preprocessor">#include <stdio.h></span> <span class="preprocessor">#include <<a class="code" href="fluidsynth_8h.html" title="FluidSynth is a real-time synthesizer designed for SoundFont(R) files.">fluidsynth.h</a>></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">/* duration of the pattern in ticks. */</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> duration = 1440; <span class="comment">/* notes of the arpeggio */</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> notes[] = { 60, 64, 67, 72, 76, 79, 84, 79, 76, 72, 67, 64 }; <span class="comment">/* number of notes in one pattern */</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pattern_size; <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 note off message */</span> <span class="keywordtype">void</span> schedule_noteoff (<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 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, synth_destination); <a name="a6"></a><a class="code" href="event_8h.html#a0aae74aab2dae9597e4e1c402474629a" title="Set a sequencer event to be a note off event.">fluid_event_noteoff</a> (ev, chan, key); <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 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="a7"></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 arpeggio's notes */</span> <span class="keywordtype">void</span> schedule_pattern () { <span class="keywordtype">int</span> i, note_time, note_duration; note_time = time_marker; note_duration = duration / pattern_size; <span class="keywordflow">for</span> (i = 0; i < pattern_size; ++i) { schedule_noteon (0, notes[i], note_time); note_time += note_duration; schedule_noteoff (0, notes[i], note_time); } time_marker += duration; } <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">"Usage: %s soundfont.sf2 [steps [duration]]\n"</span>, prog_name); printf (<span class="stringliteral">"\t(optional) steps: number of pattern notes, from 2 to %d\n"</span>, pattern_size); printf (<span class="stringliteral">"\t(optional) duration: of the pattern in ticks, default %d\n"</span>, duration); } <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="a8"></a><a class="code" href="settings_8h.html#ad7ab9269a28c2b5852d512ffe3546949" title="Create a new settings object.">new_fluid_settings</a> (); pattern_size = <span class="keyword">sizeof</span>(notes) / <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>); <span class="keywordflow">if</span> (argc < 2) { usage (argv[0]); } <span class="keywordflow">else</span> { <span class="comment">/* create the synth, driver and sequencer instances */</span> synth = <a name="a9"></a><a class="code" href="synth_8h.html#a344f7369c0f57d30f72702d0c88e6178" title="Create new FluidSynth instance.">new_fluid_synth</a> (settings); audiodriver = <a name="a10"></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="a11"></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="a12"></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="a13"></a><a class="code" href="seq_8h.html#a749882ab2b9ce51fa6a265db2cd59915" title="Register a sequencer client.">fluid_sequencer_register_client</a> (sequencer, <span class="stringliteral">"arpeggio"</span>, sequencer_callback, NULL); <span class="comment">/* load a SoundFont */</span> n = <a name="a14"></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 > 2) { n = atoi (argv[2]); <span class="keywordflow">if</span> ((n > 1) && (n <= pattern_size)) pattern_size = n; } <span class="keywordflow">if</span> (argc > 3) { n = atoi (argv[3]); <span class="keywordflow">if</span> (n > 0) duration = n; } <span class="comment">/* get the current time in ticks */</span> time_marker = <a name="a15"></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">"press <Enter> to stop\n"</span>); n = getchar (); } <span class="comment">/* clean and exit */</span> <a name="a16"></a><a class="code" href="seq_8h.html#a57d2277b188240e435b5fc3f39c0bc7e" title="Free a sequencer object.">delete_fluid_sequencer</a> (sequencer); <a name="a17"></a><a class="code" href="audio_8h.html#a05678e633d37da0f93000045393e9442" title="Deletes an audio driver instance.">delete_fluid_audio_driver</a> (audiodriver); <a name="a18"></a><a class="code" href="synth_8h.html#a585a63f3a25b9df17b82ae87f2d38cfc" title="Delete a FluidSynth instance.">delete_fluid_synth</a> (synth); } <a name="a19"></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 <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.1 </small></address> </body> </html>