/
FluidSynthSynthesis.cpp
143 lines (102 loc) · 3.05 KB
/
FluidSynthSynthesis.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "FluidSynthSynthesis.h"
#include <string>
#include <math.h>
FluidSynthSynthesis::FluidSynthSynthesis(const std::string& soundfont_file)
{
settings = NULL;
synth = NULL;
adriver = NULL;
soundfont = FLUID_FAILED;
{
std::unique_lock<std::mutex> lock(synth_mutex);
settings = new_fluid_settings();
// use pulseaudio audio driver
fluid_settings_setstr(settings, "audio.driver", "pulseaudio");
fluid_settings_setint(settings, "synth.midi-channels", 60);
fluid_settings_setnum(settings, "synth.gain", 4.0);
synth = new_fluid_synth(settings);
adriver = new_fluid_audio_driver(settings, synth);
if(settings == NULL || synth == NULL || adriver == NULL){
if(adriver){ delete_fluid_audio_driver(adriver); adriver = NULL; }
if(synth){ delete_fluid_synth(synth); synth = NULL; }
if(settings){ delete_fluid_settings(settings); settings = NULL; }
}
if(synth){
soundfont = fluid_synth_sfload(synth, soundfont_file.c_str(), 1);
if(soundfont == FLUID_FAILED){
printf("WARN: loading soundfont failed\n");
}
else{
// selects separate soundfont for each channel
for(unsigned int i=0;i<60;i++){
fluid_synth_program_select(synth, i, soundfont, 0, i); // BANK 0 PRESET i
}
}
}
lastParameters.resize(3);
prev_channel = 0;
prev_key = 0;
}
}
FluidSynthSynthesis::~FluidSynthSynthesis()
{
{
std::unique_lock<std::mutex> lock(synth_mutex);
if(adriver){ delete_fluid_audio_driver(adriver); adriver = NULL; }
if(soundfont != FLUID_FAILED)
fluid_synth_sfunload(synth, soundfont, 1);
if(synth){ delete_fluid_synth(synth); synth = NULL; }
if(settings){ delete_fluid_settings(settings); settings = NULL; }
}
}
bool FluidSynthSynthesis::play()
{
return true;
}
bool FluidSynthSynthesis::pause()
{
return true;
}
std::string FluidSynthSynthesis::getSynthesizerName()
{
return "fluidsynth";
}
bool FluidSynthSynthesis::reset()
{
return true;
}
bool FluidSynthSynthesis::getParameters(std::vector<float>& p)
{
p = lastParameters;
return true;
}
bool FluidSynthSynthesis::setParameters(const std::vector<float>& p)
{
if(p.size() == 3)
{
std::unique_lock<std::mutex> lock(synth_mutex);
int channel = 0;
int key = 60;
int velocity = 100;
float ch = ((p[0] < 0.0f) ? 0.0f : ((p[0] > 1.0) ? 1.0f : p[0]));
float k = ((p[1] < 0.0f) ? 0.0f : ((p[1] > 1.0) ? 1.0f : p[1]));
float v = ((p[2] < 0.0f) ? 0.0f : ((p[2] > 1.0) ? 1.0f : p[2]));
channel = floor(ch*60.9999); // [0,60]
key = 48 + floor(k*35.99999); // C-4 - B-6 (3 octaves)
velocity = floor(v*127.9999); // [0,127]
channel = 16;
if(synth){
fluid_synth_noteoff(synth, prev_channel, prev_key);
fluid_synth_noteon(synth, channel, key, velocity);
printf("NOTE CH %d KEY %d VEL %d\n", channel, key, velocity);
prev_channel = channel;
prev_key = key;
}
lastParameters = p;
}
return true;
}
int FluidSynthSynthesis::getNumberOfParameters()
{
return 3;
}