-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfluidsynth_arpeggio_8c-example.html
More file actions
75 lines (73 loc) · 16.6 KB
/
fluidsynth_arpeggio_8c-example.html
File metadata and controls
75 lines (73 loc) · 16.6 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_arpeggio.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_arpeggio.c</div> </div>
</div><!--header-->
<div class="contents">
<p>Example of an arpeggio generated using the MIDI sequencer API</p>
<div class="fragment"><div class="line"><span class="comment">/* FluidSynth Arpeggio - Sequencer API example</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 -o fluidsynth_arpeggio -lfluidsynth fluidsynth_arpeggio.c</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_arpeggio soundfont [steps [duration]]</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * [Pedro Lopez-Cabanillas <plcl@users.sf.net>]</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <stdlib.h></span></div><div class="line"><span class="preprocessor">#include <stdio.h></span></div><div class="line"><span class="preprocessor">#include <fluidsynth.h></span></div><div class="line"></div><div class="line"><a class="code" href="types_8h.html#ae265f10ae174a13afe010de50d87e1a4">fluid_synth_t</a> *synth;</div><div class="line"><a class="code" href="types_8h.html#ac3706330ce49cac5b7dd079e90d376d8">fluid_audio_driver_t</a> *audiodriver;</div><div class="line"><a class="code" href="types_8h.html#a7c7acad4ee620fc954a7ad4d7e87e1c3">fluid_sequencer_t</a> *sequencer;</div><div class="line"><span class="keywordtype">short</span> synth_destination, client_destination;</div><div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> time_marker;</div><div class="line"><span class="comment">/* duration of the pattern in ticks. */</span></div><div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> duration = 1440;</div><div class="line"><span class="comment">/* notes of the arpeggio */</span></div><div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> notes[] = { 60, 64, 67, 72, 76, 79, 84, 79, 76, 72, 67, 64 };</div><div class="line"><span class="comment">/* number of notes in one pattern */</span></div><div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pattern_size;</div><div class="line"><span class="comment">/* prototype */</span></div><div class="line"><span class="keywordtype">void</span></div><div class="line">sequencer_callback (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> time, <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba">fluid_event_t</a> *event,</div><div class="line"> <a class="code" href="types_8h.html#a7c7acad4ee620fc954a7ad4d7e87e1c3">fluid_sequencer_t</a> *seq, <span class="keywordtype">void</span> *data);</div><div class="line"></div><div class="line"><span class="comment">/* schedule a note on message */</span></div><div class="line"><span class="keywordtype">void</span></div><div class="line">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)</div><div class="line">{</div><div class="line"> <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba">fluid_event_t</a> *ev = <a name="a0"></a><a class="code" href="event_8h.html#a1d535480f26630b755102415a6d246cb">new_fluid_event</a> ();</div><div class="line"> <a name="a1"></a><a class="code" href="event_8h.html#abe9c6ec481cfd9791d5290a4aa4101df">fluid_event_set_source</a> (ev, -1);</div><div class="line"> <a name="a2"></a><a class="code" href="event_8h.html#adb65323ce4cc44441be32fb5f2dc958a">fluid_event_set_dest</a> (ev, synth_destination);</div><div class="line"> <a name="a3"></a><a class="code" href="event_8h.html#a1337c75403ebf9c43607a3f3139c6bba">fluid_event_noteon</a> (ev, chan, key, 127);</div><div class="line"> <a name="a4"></a><a class="code" href="seq_8h.html#afb0c62f3a2cff3decbf628535f4511ef">fluid_sequencer_send_at</a> (sequencer, ev, ticks, 1);</div><div class="line"> <a name="a5"></a><a class="code" href="event_8h.html#a473c54f0c43bdea789b211c23c7daeee">delete_fluid_event</a> (ev);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/* schedule a note off message */</span></div><div class="line"><span class="keywordtype">void</span></div><div class="line">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)</div><div class="line">{</div><div class="line"> <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba">fluid_event_t</a> *ev = <a class="code" href="event_8h.html#a1d535480f26630b755102415a6d246cb">new_fluid_event</a> ();</div><div class="line"> <a class="code" href="event_8h.html#abe9c6ec481cfd9791d5290a4aa4101df">fluid_event_set_source</a> (ev, -1);</div><div class="line"> <a class="code" href="event_8h.html#adb65323ce4cc44441be32fb5f2dc958a">fluid_event_set_dest</a> (ev, synth_destination);</div><div class="line"> <a name="a6"></a><a class="code" href="event_8h.html#a0aae74aab2dae9597e4e1c402474629a">fluid_event_noteoff</a> (ev, chan, key);</div><div class="line"> <a class="code" href="seq_8h.html#afb0c62f3a2cff3decbf628535f4511ef">fluid_sequencer_send_at</a> (sequencer, ev, ticks, 1);</div><div class="line"> <a class="code" href="event_8h.html#a473c54f0c43bdea789b211c23c7daeee">delete_fluid_event</a> (ev);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/* schedule a timer event (shall trigger the callback) */</span></div><div class="line"><span class="keywordtype">void</span></div><div class="line">schedule_timer_event ()</div><div class="line">{</div><div class="line"> <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba">fluid_event_t</a> *ev = <a class="code" href="event_8h.html#a1d535480f26630b755102415a6d246cb">new_fluid_event</a> ();</div><div class="line"> <a class="code" href="event_8h.html#abe9c6ec481cfd9791d5290a4aa4101df">fluid_event_set_source</a> (ev, -1);</div><div class="line"> <a class="code" href="event_8h.html#adb65323ce4cc44441be32fb5f2dc958a">fluid_event_set_dest</a> (ev, client_destination);</div><div class="line"> <a name="a7"></a><a class="code" href="event_8h.html#aa99a8c9110e819b53fc42baa0bfd8fbf">fluid_event_timer</a> (ev, NULL);</div><div class="line"> <a class="code" href="seq_8h.html#afb0c62f3a2cff3decbf628535f4511ef">fluid_sequencer_send_at</a> (sequencer, ev, time_marker, 1);</div><div class="line"> <a class="code" href="event_8h.html#a473c54f0c43bdea789b211c23c7daeee">delete_fluid_event</a> (ev);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/* schedule the arpeggio's notes */</span></div><div class="line"><span class="keywordtype">void</span></div><div class="line">schedule_pattern ()</div><div class="line">{</div><div class="line"> <span class="keywordtype">int</span> i, note_time, note_duration;</div><div class="line"> note_time = time_marker;</div><div class="line"> note_duration = duration / pattern_size;</div><div class="line"> <span class="keywordflow">for</span> (i = 0; i < pattern_size; ++i) {</div><div class="line"> schedule_noteon (0, notes[i], note_time);</div><div class="line"> note_time += note_duration;</div><div class="line"> schedule_noteoff (0, notes[i], note_time);</div><div class="line"> }</div><div class="line"> time_marker += duration;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span></div><div class="line">sequencer_callback (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> time, <a class="code" href="types_8h.html#aca09348be1b6e6ee7fce49dd4734f1ba">fluid_event_t</a> *event,</div><div class="line"> <a class="code" href="types_8h.html#a7c7acad4ee620fc954a7ad4d7e87e1c3">fluid_sequencer_t</a> *seq, <span class="keywordtype">void</span> *data)</div><div class="line">{</div><div class="line"> schedule_timer_event ();</div><div class="line"> schedule_pattern ();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span></div><div class="line">usage (<span class="keywordtype">char</span>* prog_name)</div><div class="line">{</div><div class="line"> printf (<span class="stringliteral">"Usage: %s soundfont.sf2 [steps [duration]]\n"</span>, prog_name);</div><div class="line"> printf (<span class="stringliteral">"\t(optional) steps: number of pattern notes, from 2 to %d\n"</span>,</div><div class="line"> pattern_size);</div><div class="line"> printf (<span class="stringliteral">"\t(optional) duration: of the pattern in ticks, default %d\n"</span>,</div><div class="line"> duration);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span></div><div class="line">main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line"> <span class="keywordtype">int</span> n;</div><div class="line"> <a class="code" href="types_8h.html#aa363402d3c77333b0f070ba531d034ba">fluid_settings_t</a> *settings;</div><div class="line"> settings = <a name="a8"></a><a class="code" href="settings_8h.html#ad7ab9269a28c2b5852d512ffe3546949">new_fluid_settings</a> ();</div><div class="line"> pattern_size = <span class="keyword">sizeof</span>(notes) / <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>);</div><div class="line"> <span class="keywordflow">if</span> (argc < 2) {</div><div class="line"> usage (argv[0]);</div><div class="line"> } <span class="keywordflow">else</span> {</div><div class="line"> <span class="comment">/* create the synth, driver and sequencer instances */</span></div><div class="line"> synth = <a name="a9"></a><a class="code" href="synth_8h.html#a344f7369c0f57d30f72702d0c88e6178">new_fluid_synth</a> (settings);</div><div class="line"> audiodriver = <a name="a10"></a><a class="code" href="audio_8h.html#a4ad51317b10b89bfe94ade5db345864b">new_fluid_audio_driver</a> (settings, synth);</div><div class="line"> sequencer = <a name="a11"></a><a class="code" href="seq_8h.html#a9ab3db0b0977a97bc9f6bc17b1c1534d">new_fluid_sequencer</a> ();</div><div class="line"> <span class="comment">/* register the synth with the sequencer */</span></div><div class="line"> synth_destination = <a name="a12"></a><a class="code" href="seqbind_8h.html#ae18ee23fd409270c9da4f0cd5c557c3d">fluid_sequencer_register_fluidsynth</a> (sequencer,</div><div class="line"> synth);</div><div class="line"> <span class="comment">/* register the client name and callback */</span></div><div class="line"> client_destination = <a name="a13"></a><a class="code" href="seq_8h.html#af48b466b7b2e97a438b166ae194b248c">fluid_sequencer_register_client</a> (sequencer,</div><div class="line"> <span class="stringliteral">"arpeggio"</span>, sequencer_callback, NULL);</div><div class="line"> <span class="comment">/* load a SoundFont */</span></div><div class="line"> n = <a name="a14"></a><a class="code" href="synth_8h.html#aaf9376cf7189f9c64da5ffdeed85c9c4">fluid_synth_sfload</a> (synth, argv[1], 1);</div><div class="line"> <span class="keywordflow">if</span> (n != -1) {</div><div class="line"> <span class="keywordflow">if</span> (argc > 2) {</div><div class="line"> n = atoi (argv[2]);</div><div class="line"> <span class="keywordflow">if</span> ((n > 1) && (n <= pattern_size)) pattern_size = n;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (argc > 3) {</div><div class="line"> n = atoi (argv[3]);</div><div class="line"> <span class="keywordflow">if</span> (n > 0) duration = n;</div><div class="line"> }</div><div class="line"> <span class="comment">/* get the current time in ticks */</span></div><div class="line"> time_marker = <a name="a15"></a><a class="code" href="seq_8h.html#a2a51cf34c359020eb6c9b7ab549ab239">fluid_sequencer_get_tick</a> (sequencer);</div><div class="line"> <span class="comment">/* schedule patterns */</span></div><div class="line"> schedule_pattern ();</div><div class="line"> schedule_timer_event ();</div><div class="line"> schedule_pattern ();</div><div class="line"> <span class="comment">/* wait for user input */</span></div><div class="line"> printf (<span class="stringliteral">"press <Enter> to stop\n"</span>);</div><div class="line"> n = getchar ();</div><div class="line"> }</div><div class="line"> <span class="comment">/* clean and exit */</span></div><div class="line"> <a name="a16"></a><a class="code" href="seq_8h.html#a57d2277b188240e435b5fc3f39c0bc7e">delete_fluid_sequencer</a> (sequencer);</div><div class="line"> <a name="a17"></a><a class="code" href="audio_8h.html#a05678e633d37da0f93000045393e9442">delete_fluid_audio_driver</a> (audiodriver);</div><div class="line"> <a name="a18"></a><a class="code" href="synth_8h.html#a585a63f3a25b9df17b82ae87f2d38cfc">delete_fluid_synth</a> (synth);</div><div class="line"> }</div><div class="line"> <a name="a19"></a><a class="code" href="settings_8h.html#a550270f09e4f3c645cbe9e6d3c514ca4">delete_fluid_settings</a> (settings);</div><div class="line"> <span class="keywordflow">return</span> 0;</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>