22 #include <QStandardPaths>
23 #include <QCoreApplication>
25 #include "synthengine.h"
27 const QString QSTR_PREFERENCES(
"FluidSynth");
28 const QString QSTR_INSTRUMENTSDEFINITION(
"InstrumentsDefinition");
29 const QString QSTR_DATADIR(
"soundfonts");
30 const QString QSTR_SOUNDFONT(
"default.sf2");
32 const QString QSTR_AUDIODRIVER(
"AudioDriver");
33 const QString QSTR_PERIODSIZE(
"PeriodSize");
34 const QString QSTR_PERIODS(
"Periods");
35 const QString QSTR_SAMPLERATE(
"SampleRate");
36 const QString QSTR_CHORUS(
"Chorus");
37 const QString QSTR_REVERB(
"Reverb");
38 const QString QSTR_GAIN(
"Gain");
39 const QString QSTR_POLYPHONY(
"Polyphony");
41 const QString QSTR_DEFAULT_AUDIODRIVER =
42 #if defined(Q_OS_LINUX)
43 QLatin1Literal(
"alsa");
44 #elif defined(Q_OS_WIN)
45 QLatin1Literal(
"dsound");
46 #elif defined(Q_OS_OSX)
47 QLatin1Literal(
"coreaudio");
49 QLatin1Literal(
"oss");
51 const int DEFAULT_PERIODS =
57 const int DEFAULT_PERIODSIZE = 512;
58 const double DEFAULT_SAMPLERATE = 48000.0;
59 const int DEFAULT_CHORUS = 0;
60 const int DEFAULT_REVERB = 0;
61 const double DEFAULT_GAIN = .4;
62 const int DEFAULT_POLYPHONY = 32;
64 SynthEngine::SynthEngine(
QObject *parent)
71 SynthEngine::~SynthEngine()
76 void SynthEngine::uninitialize()
79 ::delete_fluid_audio_driver(m_driver);
83 ::delete_fluid_synth(m_synth);
86 if (m_settings != 0) {
87 ::delete_fluid_settings(m_settings);
92 void SynthEngine::initializeSynth(QSettings* settings)
94 QString fs_audiodriver = QSTR_DEFAULT_AUDIODRIVER;
95 int fs_periodSize = DEFAULT_PERIODSIZE;
96 int fs_periods = DEFAULT_PERIODS;
97 double fs_sampleRate = DEFAULT_SAMPLERATE;
98 int fs_chorus = DEFAULT_CHORUS;
99 int fs_reverb = DEFAULT_REVERB;
100 double fs_gain = DEFAULT_GAIN;
101 int fs_polyphony = DEFAULT_POLYPHONY;
103 settings->beginGroup(QSTR_PREFERENCES);
104 fs_audiodriver = settings->value(QSTR_AUDIODRIVER, QSTR_DEFAULT_AUDIODRIVER).toString();
105 fs_periodSize = settings->value(QSTR_PERIODSIZE, DEFAULT_PERIODSIZE).toInt();
106 fs_periods = settings->value(QSTR_PERIODS, DEFAULT_PERIODS).toInt();
107 fs_sampleRate = settings->value(QSTR_SAMPLERATE, DEFAULT_SAMPLERATE).toDouble();
108 fs_chorus = settings->value(QSTR_CHORUS, DEFAULT_CHORUS).toInt();
109 fs_reverb = settings->value(QSTR_REVERB, DEFAULT_REVERB).toInt();
110 fs_gain = settings->value(QSTR_GAIN, DEFAULT_GAIN).toDouble();
111 fs_polyphony = settings->value(QSTR_POLYPHONY, DEFAULT_POLYPHONY).toInt();
112 settings->endGroup();
115 m_settings = ::new_fluid_settings();
116 ::fluid_settings_setstr(m_settings,
"audio.driver", qPrintable(fs_audiodriver));
117 ::fluid_settings_setint(m_settings,
"audio.period-size", fs_periodSize);
118 ::fluid_settings_setint(m_settings,
"audio.periods", fs_periods);
119 ::fluid_settings_setnum(m_settings,
"synth.sample-rate", fs_sampleRate);
120 ::fluid_settings_setint(m_settings,
"synth.chorus.active", fs_chorus);
121 ::fluid_settings_setint(m_settings,
"synth.reverb.active", fs_reverb);
122 ::fluid_settings_setnum(m_settings,
"synth.gain", fs_gain);
123 ::fluid_settings_setint(m_settings,
"synth.polyphony", fs_polyphony);
124 m_synth = ::new_fluid_synth(m_settings);
125 m_driver = ::new_fluid_audio_driver(m_settings, m_synth);
128 void SynthEngine::setInstrument(
int channel,
int pgm)
130 ::fluid_synth_program_change(m_synth, channel, pgm);
133 void SynthEngine::noteOn(
int channel,
int midiNote,
int velocity)
135 ::fluid_synth_noteon(m_synth, channel, midiNote, velocity);
138 void SynthEngine::noteOff(
int channel,
int midiNote,
int )
140 ::fluid_synth_noteoff(m_synth, channel, midiNote);
143 void SynthEngine::loadSoundFont()
146 ::fluid_synth_sfunload(m_synth,
unsigned(m_sfid), 1);
148 m_sfid = ::fluid_synth_sfload(m_synth, qPrintable(m_soundFont), 1);
151 void SynthEngine::initialize(QSettings *settings)
153 initializeSynth(settings);
157 m_soundFont = m_defSoundFont;
162 void SynthEngine::panic()
164 ::fluid_synth_system_reset(m_synth);
167 void SynthEngine::controlChange(
const int channel,
const int midiCtl,
const int value)
169 ::fluid_synth_cc(m_synth, channel, midiCtl, value);
172 void SynthEngine::bender(
const int channel,
const int value)
174 ::fluid_synth_pitch_bend(m_synth, channel, value + 8192);
177 void SynthEngine::setSoundFont(
const QString &value)
179 if (value != m_soundFont) {
185 void SynthEngine::scanSoundFonts(
const QDir &initialDir)
187 QDir dir(initialDir);
188 dir.setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
189 dir.setSorting(QDir::Name);
191 filters <<
"*.sf2" <<
"*.SF2";
192 QFileInfoList entries= dir.entryInfoList(filters);
193 foreach(
const QFileInfo &info, entries) {
194 QString name = info.absoluteFilePath();
196 m_soundFontsList << name;
197 }
else if (info.isDir()){
198 scanSoundFonts(name);
203 void SynthEngine::scanSoundFonts()
205 m_soundFontsList.clear();
206 QStringList paths = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
207 #if defined(Q_OS_OSX)
208 paths << (QCoreApplication::applicationDirPath() + QLatin1Literal(
"../Resources"));
210 foreach(
const QString& p, paths) {
211 QDir d(p + QDir::separator() + QSTR_DATADIR);
218 void SynthEngine::readSettings(QSettings *settings)
221 #if defined(Q_OS_OSX)
222 dir = QDir(QCoreApplication::applicationDirPath() + QLatin1Literal(
"/../Resources"));
223 #elif defined(Q_OS_UNIX)
224 dir = QDir(QCoreApplication::applicationDirPath() + QLatin1Literal(
"/../share/soundfonts/"));
226 dir = QDir(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QSTR_DATADIR, QStandardPaths::LocateDirectory));
228 QFileInfo sf2(dir, QSTR_SOUNDFONT);
230 m_defSoundFont = sf2.absoluteFilePath();
233 qDebug() <<
"defSoundFont:" << m_defSoundFont;
234 settings->beginGroup(QSTR_PREFERENCES);
235 m_soundFont = settings->value(QSTR_INSTRUMENTSDEFINITION, m_defSoundFont).toString();
236 settings->endGroup();
239 void SynthEngine::close()
241 m_currentConnection.clear();
245 void SynthEngine::open()
247 m_currentConnection = QSTR_FLUIDSYNTH;
The QObject class is the base class of all Qt objects.