-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfluidsynth_fx_8c-example.html
More file actions
75 lines (73 loc) · 13.9 KB
/
fluidsynth_fx_8c-example.html
File metadata and controls
75 lines (73 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!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"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>libfluidsynth: fluidsynth_fx.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">libfluidsynth
 <span id="projectnumber">1.1.11</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">fluidsynth_fx.c</div> </div>
</div><!--header-->
<div class="contents">
<p>Example of using effects with fluidsynth</p>
<div class="fragment"><div class="line"><span class="comment">/* FluidSynth FX - An example of using effects with fluidsynth</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * This code is in the public domain.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * To compile:</span></div><div class="line"><span class="comment"> * gcc -g -O -o fluidsynth_fx fluidsynth_fx.c -lfluidsynth</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * To run</span></div><div class="line"><span class="comment"> * fluidsynth_fx soundfont gain</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * [Peter Hanappe]</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#include <stdio.h></span></div><div class="line"><span class="preprocessor">#include <stdlib.h></span></div><div class="line"><span class="preprocessor">#include <fluidsynth.h></span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">/* The structure with the effects data. This example simply applies a</span></div><div class="line"><span class="comment"> * linear gain the to synthesizer output. */</span></div><div class="line"><span class="keyword">struct </span>fx_data_t {</div><div class="line"> <a class="code" href="types_8h.html#ae265f10ae174a13afe010de50d87e1a4">fluid_synth_t</a>* synth;</div><div class="line"> <span class="keywordtype">float</span> gain;</div><div class="line">} fx_data_t;</div><div class="line"></div><div class="line"><span class="comment">/* This function implements the callback function of the audio driver</span></div><div class="line"><span class="comment"> * (see new_fluid_audio_driver2 below). The data argument is a pointer</span></div><div class="line"><span class="comment"> * to your private data structure. 'len' is the number of samples in</span></div><div class="line"><span class="comment"> * the buffers. 'nin' and 'nout' are the number of input and output</span></div><div class="line"><span class="comment"> * audio buffers. 'in' and 'out' are an array of float buffers with</span></div><div class="line"><span class="comment"> * the samples. The audio driver fills the 'in' buffers the incoming</span></div><div class="line"><span class="comment"> * audio. The 'out' buffers should be filled by your function. The</span></div><div class="line"><span class="comment"> * 'out' buffers will be sent to the audio output of the sound card.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * IMPORTANT NOTE: The API was designed to be generic but none of the</span></div><div class="line"><span class="comment"> * audio drivers currently handles audio input. Either 'nin' will be</span></div><div class="line"><span class="comment"> * zero, or the buffers will be filled with zero samples.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="keywordtype">int</span> fx_function(<span class="keywordtype">void</span>* data, <span class="keywordtype">int</span> len, </div><div class="line"> <span class="keywordtype">int</span> nin, <span class="keywordtype">float</span>** in, </div><div class="line"> <span class="keywordtype">int</span> nout, <span class="keywordtype">float</span>** out)</div><div class="line">{</div><div class="line"> <span class="keyword">struct </span>fx_data_t* fx_data = (<span class="keyword">struct </span>fx_data_t*) data;</div><div class="line"> <span class="keywordtype">int</span> i, k;</div><div class="line"> <span class="keywordtype">float</span>* out_i;</div><div class="line"></div><div class="line"> <span class="comment">/* Call the synthesizer to fill the output buffers with its</span></div><div class="line"><span class="comment"> * audio output. */</span></div><div class="line"> <span class="keywordflow">if</span> (<a name="a0"></a><a class="code" href="synth_8h.html#a1ac90e2732aa652679305f78cbd66670">fluid_synth_process</a>(fx_data->synth, len, nin, in, nout, out) != 0) {</div><div class="line"> <span class="comment">/* Some error occured. Very unlikely to happen, though. */</span></div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/* Apply your effects here. In this example, the gain is</span></div><div class="line"><span class="comment"> * applied to all the output buffers. */</span></div><div class="line"> <span class="keywordflow">for</span> (i = 0; i < nout; i++) {</div><div class="line"> out_i = out[i];</div><div class="line"> <span class="keywordflow">for</span> (k = 0; k < len; k++) {</div><div class="line"> out_i[k] *= fx_data->gain;</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv) </div><div class="line">{</div><div class="line"> <a class="code" href="types_8h.html#aa363402d3c77333b0f070ba531d034ba">fluid_settings_t</a>* settings;</div><div class="line"> <a class="code" href="types_8h.html#ae265f10ae174a13afe010de50d87e1a4">fluid_synth_t</a>* synth = NULL;</div><div class="line"> <a class="code" href="types_8h.html#ac3706330ce49cac5b7dd079e90d376d8">fluid_audio_driver_t</a>* adriver = NULL;</div><div class="line"> <span class="keywordtype">int</span> err = 0;</div><div class="line"> <span class="keyword">struct </span>fx_data_t fx_data;</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (argc != 3) {</div><div class="line"> fprintf(stderr, <span class="stringliteral">"Usage: fluidsynth_simple [soundfont] [gain]\n"</span>);</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/* Create the settings object. This example uses the default</span></div><div class="line"><span class="comment"> * values for the settings. */</span></div><div class="line"> settings = <a name="a1"></a><a class="code" href="settings_8h.html#ad7ab9269a28c2b5852d512ffe3546949">new_fluid_settings</a>();</div><div class="line"> <span class="keywordflow">if</span> (settings == NULL) {</div><div class="line"> fprintf(stderr, <span class="stringliteral">"Failed to create the settings\n"</span>);</div><div class="line"> err = 2;</div><div class="line"> <span class="keywordflow">goto</span> cleanup;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="comment">/* Create the synthesizer */</span></div><div class="line"> synth = <a name="a2"></a><a class="code" href="synth_8h.html#a344f7369c0f57d30f72702d0c88e6178">new_fluid_synth</a>(settings);</div><div class="line"> <span class="keywordflow">if</span> (synth == NULL) {</div><div class="line"> fprintf(stderr, <span class="stringliteral">"Failed to create the synthesizer\n"</span>);</div><div class="line"> err = 3;</div><div class="line"> <span class="keywordflow">goto</span> cleanup;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/* Load the soundfont */</span></div><div class="line"> <span class="keywordflow">if</span> (<a name="a3"></a><a class="code" href="synth_8h.html#aaf9376cf7189f9c64da5ffdeed85c9c4">fluid_synth_sfload</a>(synth, argv[1], 1) == -1) {</div><div class="line"> fprintf(stderr, <span class="stringliteral">"Failed to load the SoundFont\n"</span>);</div><div class="line"> err = 4;</div><div class="line"> <span class="keywordflow">goto</span> cleanup;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/* Fill in the data of the effects unit */</span></div><div class="line"> fx_data.synth = synth;</div><div class="line"> fx_data.gain = atof(argv[2]);</div><div class="line"></div><div class="line"> <span class="comment">/* Create the audio driver. As soon as the audio driver is</span></div><div class="line"><span class="comment"> * created, the synthesizer can be played. */</span></div><div class="line"> adriver = <a name="a4"></a><a class="code" href="audio_8h.html#a3959d8add1dea97e507a5ea2c802c0bc">new_fluid_audio_driver2</a>(settings, fx_function, (<span class="keywordtype">void</span>*) &fx_data);</div><div class="line"> <span class="keywordflow">if</span> (adriver == NULL) {</div><div class="line"> fprintf(stderr, <span class="stringliteral">"Failed to create the audio driver\n"</span>);</div><div class="line"> err = 5;</div><div class="line"> <span class="keywordflow">goto</span> cleanup;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/* Play a note */</span></div><div class="line"> <a name="a5"></a><a class="code" href="synth_8h.html#a4a98222fe1c36bfd598dc4cd89f4b75c">fluid_synth_noteon</a>(synth, 0, 60, 100);</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"Press \"Enter\" to stop: "</span>);</div><div class="line"> fgetc(stdin);</div><div class="line"> printf(<span class="stringliteral">"done\n"</span>);</div><div class="line"></div><div class="line"> </div><div class="line"> cleanup:</div><div class="line"> </div><div class="line"> <span class="keywordflow">if</span> (adriver) {</div><div class="line"> <a name="a6"></a><a class="code" href="audio_8h.html#a05678e633d37da0f93000045393e9442">delete_fluid_audio_driver</a>(adriver);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (synth) {</div><div class="line"> <a name="a7"></a><a class="code" href="synth_8h.html#a585a63f3a25b9df17b82ae87f2d38cfc">delete_fluid_synth</a>(synth);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (settings) {</div><div class="line"> <a name="a8"></a><a class="code" href="settings_8h.html#a550270f09e4f3c645cbe9e6d3c514ca4">delete_fluid_settings</a>(settings);</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keywordflow">return</span> err;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>