Skip to content

Instantly share code, notes, and snippets.

@pedrolcl
Last active July 31, 2021 20:04
Show Gist options
  • Save pedrolcl/5837161d8d5926f219dab3159a48bd8c to your computer and use it in GitHub Desktop.
Save pedrolcl/5837161d8d5926f219dab3159a48bd8c to your computer and use it in GitHub Desktop.
FluidSynth Enum Settings - An example of using fluidsynth in C++
cmake_minimum_required(VERSION 3.5)
project(fluidenums LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# this was the old method before modernization, using pkg-config
# find_package ( PkgConfig REQUIRED )
# pkg_check_modules ( FLUIDSYNTH REQUIRED IMPORTED_TARGET fluidsynth )
#if (FLUIDSYNTH_FOUND)
# add_executable(fluidenums fluidsynth_enumsettings.cpp)
# target_link_libraries ( fluidenums PRIVATE PkgConfig::FLUIDSYNTH )
#endif ()
# after the modernization, a pure cmake method is preferred:
find_package(FluidSynth QUIET REQUIRED)
if (FluidSynth_FOUND)
message(STATUS "Found FluidSynth, version: ${FluidSynth_VERSION}")
add_executable(fluidenums fluidsynth_enumsettings.cpp)
target_link_libraries ( fluidenums PRIVATE FluidSynth::libfluidsynth )
endif ()
/* FluidSynth Enum Settings - An example of using fluidsynth in C++
*
* This code is in the public domain.
*
* To compile:
* g++ -o fluidsynth_enumsettings fluidsynth_enumsettings.cpp -lfluidsynth
*
* To run
* fluidsynth_enumsettings
*
* [Pedro López-Cabanillas]
*
*/
#include <list>
#include <string>
#include <iostream>
#include <iomanip>
#include <fluidsynth.h>
int main(int argc, char**)
{
fluid_settings_t* settings = nullptr;
void* context = nullptr;
if (argc > 1) {
std::cerr << "Usage: fluidsynth_enumsettings" << std::endl;
return 1;
}
/* Create the settings object. This example uses the default values for the settings. */
settings = new_fluid_settings();
if (settings == NULL) {
std::cerr << "Failed to create the settings" << std::endl;
return 2;
}
context = settings;
fluid_settings_foreach(settings, context, [](void *inner_context, const char *name, int type) {
int ok = 0;
double dValue{0.0};
int iValue{0};
char *psValue = nullptr;
fluid_settings_t* inner_settings = (fluid_settings_t*) inner_context;
std::cout << "Setting: " << std::setw(35) << std::left << name;
switch (type) {
case FLUID_NO_TYPE:
std::cout << "type: Undefined";
break;
case FLUID_NUM_TYPE:
ok = fluid_settings_getnum(inner_settings, name, &dValue);
if (ok == FLUID_OK) {
std::cout << "type: Numeric\tvalue: " << dValue;
}
break;
case FLUID_INT_TYPE:
ok = fluid_settings_getint(inner_settings, name, &iValue);
if (ok == FLUID_OK) {
std::cout << "type: Integer\tvalue: " << iValue;
}
break;
case FLUID_STR_TYPE:
ok = fluid_settings_dupstr(inner_settings, name, &psValue);
if (ok == FLUID_OK) {
std::cout << "type: String\tvalue: " << psValue;
fluid_free(psValue);
}
break;
case FLUID_SET_TYPE:
std::cout << "type: Set";
break;
}
std::list<std::string> options;
fluid_settings_foreach_option(inner_settings, name, &options, [](void *context2, const char *, const char *option2){
std::list<std::string> *options_list = (std::list<std::string> *) context2;
if (!options_list->empty()) {
options_list->push_back(", ");
}
options_list->push_back(option2);
});
if (!options.empty()) {
std::cout << "\t\toptions: ";
for(auto it=options.begin(); it != options.end(); ++it) {
std::cout << *it;
}
}
std::cout << std::endl;
});
std::cout << "done" << std::endl;
delete_fluid_settings(settings);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment