/
driver_fluidsynth.cpp
174 lines (149 loc) · 4.16 KB
/
driver_fluidsynth.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @file driver_fluidsynth.cpp
* FluidSynth music plugin. @ingroup dsfluidsynth
*
* @authors Copyright © 2011-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "driver_fluidsynth.h"
#include "api_audiod.h"
#include <stdio.h>
#include <string.h>
#include <de/c_wrapper.h>
#include "doomsday.h"
static fluid_settings_t* fsConfig;
static fluid_synth_t* fsSynth;
static audiointerface_sfx_t* fsSfx;
static fluid_audio_driver_t* fsDriver;
fluid_synth_t* DMFluid_Synth()
{
DENG_ASSERT(fsSynth != 0);
return fsSynth;
}
fluid_audio_driver_t* DMFluid_Driver()
{
return fsDriver;
}
audiointerface_sfx_generic_t* DMFluid_Sfx()
{
DENG_ASSERT(fsSfx != 0);
return &fsSfx->gen;
}
/**
* Initialize the FluidSynth sound driver.
*/
int DS_Init(void)
{
if(fsSynth)
{
return true; // Already initialized.
}
// Set up a reasonable configuration.
fsConfig = new_fluid_settings();
fluid_settings_setnum(fsConfig, "synth.gain", MAX_SYNTH_GAIN);
// Create the synthesizer.
fsSynth = new_fluid_synth(fsConfig);
if(!fsSynth)
{
Con_Message("Failed to create FluidSynth synthesizer.\n");
return false;
}
#ifndef FLUIDSYNTH_NOT_A_DLL
// Create the output driver that will play the music.
char driverName[50];
if(!UnixInfo_GetConfigValue("defaults", "fluidsynth:driver", driverName, sizeof(driverName) - 1))
{
strcpy(driverName, FLUIDSYNTH_DEFAULT_DRIVER_NAME);
}
fluid_settings_setstr(fsConfig, "audio.driver", driverName);
fsDriver = new_fluid_audio_driver(fsConfig, fsSynth);
if(!fsDriver)
{
Con_Message("Failed to create FluidSynth audio driver '%s'.\n", driverName);
return false;
}
#else
fsDriver = NULL;
#endif
DSFLUIDSYNTH_TRACE("DS_Init: FluidSynth initialized.");
return true;
}
/**
* Shut everything down.
*/
void DS_Shutdown(void)
{
if(!fsSynth) return;
DMFluid_Shutdown();
DSFLUIDSYNTH_TRACE("DS_Shutdown.");
if(fsDriver)
{
delete_fluid_audio_driver(fsDriver);
}
delete_fluid_synth(fsSynth);
delete_fluid_settings(fsConfig);
fsSynth = 0;
fsConfig = 0;
}
/**
* The Event function is called to tell the driver about certain critical
* events like the beginning and end of an update cycle.
*/
void DS_Event(int type)
{
if(!fsSynth) return;
if(type == SFXEV_END)
{
// End of frame, do an update.
DMFluid_Update();
}
}
int DS_Set(int prop, const void* ptr)
{
//if(!fmodSystem) return false;
switch(prop)
{
case AUDIOP_SOUNDFONT_FILENAME: {
const char* path = reinterpret_cast<const char*>(ptr);
DSFLUIDSYNTH_TRACE("DS_Set: Soundfont = " << path);
if(!path || !strlen(path))
{
// Use the default.
path = 0;
}
DMFluid_SetSoundFont(path);
return true; }
case AUDIOP_SFX_INTERFACE:
fsSfx = (audiointerface_sfx_t*) ptr;
DSFLUIDSYNTH_TRACE("DS_Set: iSFX = " << fsSfx);
return true;
default:
DSFLUIDSYNTH_TRACE("DS_Set: Unknown property " << prop);
return false;
}
}
/**
* Declares the type of the plugin so the engine knows how to treat it. Called
* automatically when the plugin is loaded.
*/
DENG_EXTERN_C const char* deng_LibraryType(void)
{
return "deng-plugin/audio";
}
DENG_DECLARE_API(Con);
DENG_API_EXCHANGE(
DENG_GET_API(DE_API_CONSOLE, Con);
)