MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / Start

Method Start

src/music/fluidsynth.cpp:75–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73}
74
75std::optional<std::string_view> MusicDriver_FluidSynth::Start(const StringList &param)
76{
77 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
78
79 auto sfont_name = GetDriverParam(param, "soundfont");
80 int sfont_id;
81
82 Debug(driver, 1, "Fluidsynth: sf {}", sfont_name.has_value() ? *sfont_name : "(null)");
83
84 /* Create the settings. */
85 _midi.settings = new_fluid_settings();
86 if (_midi.settings == nullptr) return "Could not create midi settings";
87
88 /* Read config file(s) */
89 fluid_cmd_handler_t *cmd_handler = new_fluid_cmd_handler2(_midi.settings, nullptr, nullptr, nullptr);
90 if (cmd_handler == NULL) return "Failed to create the early command handler";
91
92 char buf[MAX_PATH] = {};
93 const char *config_file = fluid_get_sysconf(buf, sizeof(buf)); // system-wide config file
94 load_and_execute_config_file(cmd_handler, config_file);
95 config_file = fluid_get_userconf(buf, sizeof(buf)); // user config file (potentially overrides system)
96 load_and_execute_config_file(cmd_handler, config_file);
97
98 delete_fluid_cmd_handler(cmd_handler);
99
100 /* Don't try to lock sample data in memory, OTTD usually does not run with privileges allowing that */
101 fluid_settings_setint(_midi.settings, "synth.lock-memory", 0);
102
103 /* Install the music render routine and set up the samplerate */
104 uint32_t samplerate = MxSetMusicSource(RenderMusicStream);
105 fluid_settings_setnum(_midi.settings, "synth.sample-rate", samplerate);
106 Debug(driver, 1, "Fluidsynth: samplerate {:.0f}", (float)samplerate);
107
108 /* Create the synthesizer. */
109 _midi.synth = new_fluid_synth(_midi.settings);
110 if (_midi.synth == nullptr) return "Could not open synth";
111
112 /* Load a SoundFont and reset presets (so that new instruments
113 * get used from the SoundFont) */
114 if (!sfont_name.has_value()) {
115 sfont_id = FLUID_FAILED;
116
117 /* Try loading the default soundfont registered with FluidSynth. */
118 char *default_soundfont = nullptr;
119 fluid_settings_dupstr(_midi.settings, "synth.default-soundfont", &default_soundfont);
120 if (default_soundfont != nullptr && std::filesystem::exists(default_soundfont) && fluid_is_soundfont(default_soundfont)) {
121 sfont_id = fluid_synth_sfload(_midi.synth, default_soundfont, 1);
122 }
123 fluid_free(default_soundfont);
124
125 /* If no default soundfont found, try our own list. */
126 if (sfont_id == FLUID_FAILED) {
127 for (const char *soundfont : _default_soundfonts) {
128 if (!std::filesystem::exists(soundfont) || !fluid_is_soundfont(soundfont)) continue;
129 sfont_id = fluid_synth_sfload(_midi.synth, soundfont, 1);
130 if (sfont_id != FLUID_FAILED) break;
131 }
132 }

Callers

nothing calls this directly

Calls 5

GetDriverParamFunction · 0.85
MxSetMusicSourceFunction · 0.85
valueMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected