<!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: example.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>example.c</h1> </div> </div> <div class="contents"> <p>Example producing short random music with FluidSynth</p> <div class="fragment"><pre class="fragment"><span class="comment">/*</span> <span class="comment"></span> <span class="comment"> An example of how to use FluidSynth.</span> <span class="comment"></span> <span class="comment"> To compile it on Linux:</span> <span class="comment"> $ gcc -o example example.c `pkg-config fluidsynth --libs`</span> <span class="comment"></span> <span class="comment"> To compile it on Windows:</span> <span class="comment"> ... </span> <span class="comment"></span> <span class="comment"></span> <span class="comment"> Author: Peter Hanappe.</span> <span class="comment"> This code is in the public domain. Use it as you like.</span> <span class="comment"></span> <span class="comment">*/</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> <span class="preprocessor">#if defined(WIN32)</span> <span class="preprocessor"></span><span class="preprocessor">#include <windows.h></span> <span class="preprocessor">#define sleep(_t) Sleep(_t * 1000)</span> <span class="preprocessor"></span><span class="preprocessor">#else</span> <span class="preprocessor"></span><span class="preprocessor">#include <stdlib.h></span> <span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv) { <a class="code" href="types_8h.html#aa363402d3c77333b0f070ba531d034ba" title="Configuration settings instance.">fluid_settings_t</a>* settings; <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>* adriver; <span class="keywordtype">int</span> sfont_id; <span class="keywordtype">int</span> i, key; <span class="comment">/* Create the settings. */</span> settings = <a name="a0"></a><a class="code" href="settings_8h.html#ad7ab9269a28c2b5852d512ffe3546949" title="Create a new settings object.">new_fluid_settings</a>(); <span class="comment">/* Change the settings if necessary*/</span> <span class="comment">/* Create the synthesizer. */</span> synth = <a name="a1"></a><a class="code" href="synth_8h.html#a344f7369c0f57d30f72702d0c88e6178" title="Create new FluidSynth instance.">new_fluid_synth</a>(settings); <span class="comment">/* Create the audio driver. The synthesizer starts playing as soon</span> <span class="comment"> as the driver is created. */</span> adriver = <a name="a2"></a><a class="code" href="audio_8h.html#a4ad51317b10b89bfe94ade5db345864b" title="Create a new audio driver.">new_fluid_audio_driver</a>(settings, synth); <span class="comment">/* Load a SoundFont and reset presets (so that new instruments</span> <span class="comment"> * get used from the SoundFont) */</span> sfont_id = <a name="a3"></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, <span class="stringliteral">"example.sf2"</span>, 1); <span class="comment">/* Initialize the random number generator */</span> srand(getpid()); <span class="keywordflow">for</span> (i = 0; i < 12; i++) { <span class="comment">/* Generate a random key */</span> key = 60 + (int) (12.0f * rand() / (float) RAND_MAX); <span class="comment">/* Play a note */</span> <a name="a4"></a><a class="code" href="synth_8h.html#a4a98222fe1c36bfd598dc4cd89f4b75c" title="Send a note-on event to a FluidSynth object.">fluid_synth_noteon</a>(synth, 0, key, 80); <span class="comment">/* Sleep for 1 second */</span> sleep(1); <span class="comment">/* Stop the note */</span> <a name="a5"></a><a class="code" href="synth_8h.html#a5e8f96cacbc6460f7677a6191cbd4472" title="Send a note-off event to a FluidSynth object.">fluid_synth_noteoff</a>(synth, 0, key); } <span class="comment">/* Clean up */</span> <a name="a6"></a><a class="code" href="audio_8h.html#a05678e633d37da0f93000045393e9442" title="Deletes an audio driver instance.">delete_fluid_audio_driver</a>(adriver); <a name="a7"></a><a class="code" href="synth_8h.html#a585a63f3a25b9df17b82ae87f2d38cfc" title="Delete a FluidSynth instance.">delete_fluid_synth</a>(synth); <a name="a8"></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>