<!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_fx.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_fx.c</h1> </div> </div> <div class="contents"> <p>Example of using effects with fluidsynth</p> <div class="fragment"><pre class="fragment"><span class="comment">/* FluidSynth FX - An example of using effects with fluidsynth</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 -g -O -o fluidsynth_fx fluidsynth_fx.c -lfluidsynth</span> <span class="comment"> *</span> <span class="comment"> * To run</span> <span class="comment"> * fluidsynth_fx soundfont gain</span> <span class="comment"> *</span> <span class="comment"> * [Peter Hanappe]</span> <span class="comment"> */</span> <span class="preprocessor">#include <stdio.h></span> <span class="preprocessor">#include <stdlib.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> <span class="comment">/* The structure with the effects data. This example simply applies a</span> <span class="comment"> * linear gain the to synthesizer output. */</span> <span class="keyword">struct </span>fx_data_t { <a class="code" href="types_8h.html#ae265f10ae174a13afe010de50d87e1a4" title="Synthesizer instance.">fluid_synth_t</a>* synth; <span class="keywordtype">float</span> gain; } fx_data_t; <span class="comment">/* This function implements the callback function of the audio driver</span> <span class="comment"> * (see new_fluid_audio_driver2 below). The data argument is a pointer</span> <span class="comment"> * to your private data structure. 'len' is the number of samples in</span> <span class="comment"> * the buffers. 'nin' and 'nout' are the number of input and output</span> <span class="comment"> * audio buffers. 'in' and 'out' are an array of float buffers with</span> <span class="comment"> * the samples. The audio driver fills the 'in' buffers the incoming</span> <span class="comment"> * audio. The 'out' buffers should be filled by your function. The</span> <span class="comment"> * 'out' buffers will be sent to the audio output of the sound card.</span> <span class="comment"> *</span> <span class="comment"> * IMPORTANT NOTE: The API was designed to be generic but none of the</span> <span class="comment"> * audio drivers currently handles audio input. Either 'nin' will be</span> <span class="comment"> * zero, or the buffers will be filled with zero samples.</span> <span class="comment"> */</span> <span class="keywordtype">int</span> fx_function(<span class="keywordtype">void</span>* data, <span class="keywordtype">int</span> len, <span class="keywordtype">int</span> nin, <span class="keywordtype">float</span>** in, <span class="keywordtype">int</span> nout, <span class="keywordtype">float</span>** out) { <span class="keyword">struct </span>fx_data_t* fx_data = (<span class="keyword">struct </span>fx_data_t*) data; <span class="keywordtype">int</span> i, k; <span class="keywordtype">float</span>* out_i; <span class="comment">/* Call the synthesizer to fill the output buffers with its</span> <span class="comment"> * audio output. */</span> <span class="keywordflow">if</span> (<a name="a0"></a><a class="code" href="synth_8h.html#a1ac90e2732aa652679305f78cbd66670" title="Synthesize floating point audio to audio buffers.">fluid_synth_process</a>(fx_data->synth, len, nin, in, nout, out) != 0) { <span class="comment">/* Some error occured. Very unlikely to happen, though. */</span> <span class="keywordflow">return</span> -1; } <span class="comment">/* Apply your effects here. In this example, the gain is</span> <span class="comment"> * applied to all the output buffers. */</span> <span class="keywordflow">for</span> (i = 0; i < nout; i++) { out_i = out[i]; <span class="keywordflow">for</span> (k = 0; k < len; k++) { out_i[k] *= fx_data->gain; } } <span class="keywordflow">return</span> 0; } <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 = NULL; <a class="code" href="types_8h.html#ac3706330ce49cac5b7dd079e90d376d8" title="Audio driver instance.">fluid_audio_driver_t</a>* adriver = NULL; <span class="keywordtype">int</span> err = 0; <span class="keyword">struct </span>fx_data_t fx_data; <span class="keywordflow">if</span> (argc != 3) { fprintf(stderr, <span class="stringliteral">"Usage: fluidsynth_simple [soundfont] [gain]\n"</span>); <span class="keywordflow">return</span> 1; } <span class="comment">/* Create the settings object. This example uses the default</span> <span class="comment"> * values for the settings. */</span> settings = <a name="a1"></a><a class="code" href="settings_8h.html#ad7ab9269a28c2b5852d512ffe3546949" title="Create a new settings object.">new_fluid_settings</a>(); <span class="keywordflow">if</span> (settings == NULL) { fprintf(stderr, <span class="stringliteral">"Failed to create the settings\n"</span>); err = 2; <span class="keywordflow">goto</span> cleanup; } <span class="comment">/* Create the synthesizer */</span> synth = <a name="a2"></a><a class="code" href="synth_8h.html#a344f7369c0f57d30f72702d0c88e6178" title="Create new FluidSynth instance.">new_fluid_synth</a>(settings); <span class="keywordflow">if</span> (synth == NULL) { fprintf(stderr, <span class="stringliteral">"Failed to create the synthesizer\n"</span>); err = 3; <span class="keywordflow">goto</span> cleanup; } <span class="comment">/* Load the soundfont */</span> <span class="keywordflow">if</span> (<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, argv[1], 1) == -1) { fprintf(stderr, <span class="stringliteral">"Failed to load the SoundFont\n"</span>); err = 4; <span class="keywordflow">goto</span> cleanup; } <span class="comment">/* Fill in the data of the effects unit */</span> fx_data.synth = synth; fx_data.gain = atof(argv[2]); <span class="comment">/* Create the audio driver. As soon as the audio driver is</span> <span class="comment"> * created, the synthesizer can be played. */</span> adriver = <a name="a4"></a><a class="code" href="audio_8h.html#a3959d8add1dea97e507a5ea2c802c0bc" title="Create a new audio driver.">new_fluid_audio_driver2</a>(settings, fx_function, (<span class="keywordtype">void</span>*) &fx_data); <span class="keywordflow">if</span> (adriver == NULL) { fprintf(stderr, <span class="stringliteral">"Failed to create the audio driver\n"</span>); err = 5; <span class="keywordflow">goto</span> cleanup; } <span class="comment">/* Play a note */</span> <a name="a5"></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, 60, 100); printf(<span class="stringliteral">"Press \"Enter\" to stop: "</span>); fgetc(stdin); printf(<span class="stringliteral">"done\n"</span>); cleanup: <span class="keywordflow">if</span> (adriver) { <a name="a6"></a><a class="code" href="audio_8h.html#a05678e633d37da0f93000045393e9442" title="Deletes an audio driver instance.">delete_fluid_audio_driver</a>(adriver); } <span class="keywordflow">if</span> (synth) { <a name="a7"></a><a class="code" href="synth_8h.html#a585a63f3a25b9df17b82ae87f2d38cfc" title="Delete a FluidSynth instance.">delete_fluid_synth</a>(synth); } <span class="keywordflow">if</span> (settings) { <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> err; } </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>