R/settings.R

Defines functions validate_fluidsynth_settings libfluidsynth_version fluidsynth_setting_default fluidsynth_setting_options fluidsynth_setting_list

Documented in fluidsynth_setting_default fluidsynth_setting_list fluidsynth_setting_options libfluidsynth_version

#' Fluidsynth settings
#'
#' Get available settings and their types.
#' See [fluidsynth docs](https://www.fluidsynth.org/api/fluidsettings.html)
#' for more information on the available options.
#'
#' @export
#' @name fluidsynth_settings
#' @rdname fluidsynth_settings
#' @family fluidsynth
#' @returns a list with available options
#' @useDynLib fluidsynth C_fluidsynth_list_settings
#' @references [FluidSynth Settings Reference](https://www.fluidsynth.org/api/fluidsettings.html)
#' @examples
#' # List available settings:
#' fluidsynth_setting_list()
#' fluidsynth_setting_options('audio.driver')
#' fluidsynth_setting_default('synth.sample-rate')
fluidsynth_setting_list <- function(){
  out <- .Call(C_fluidsynth_list_settings)
  names(out) <- c('name', 'type')
  data.frame(out)
}

#' @export
#' @rdname fluidsynth_settings
#' @useDynLib fluidsynth C_fluidsynth_list_options
#' @param setting string with one of the options listed in [fluidsynth_setting_list()], see examples.
fluidsynth_setting_options <- function(setting){
  setting <- as.character(setting)
  .Call(C_fluidsynth_list_options, setting)
}

#' @export
#' @rdname fluidsynth_settings
#' @useDynLib fluidsynth C_fluidsynth_get_default
fluidsynth_setting_default <- function(setting){
  setting <- as.character(setting)
  .Call(C_fluidsynth_get_default, setting)
}

#' @export
#' @rdname fluidsynth_settings
#' @useDynLib fluidsynth C_fluidsynth_version
libfluidsynth_version <- function(){
  .Call(C_fluidsynth_version)
}

validate_fluidsynth_settings <- function(opts){
  if(length(opts)){
    settings <- fluidsynth_setting_list()
    for(o in names(opts)){
      if(!(o %in% settings$name))
        stop("Unsupported fluidsynth option: ", o)
      val <- opts[[o]]
      if(length(val) != 1)
        stop("Option is not of length 1: ", o)
      type <- settings[settings$name == o, "type"]
      if(type == 'string' && !is.character(val))
        stop("Option should be a string: ", o)
      if(type != 'string'){
        if(!is.numeric(val))
          stop("Option should be a number: ", o)
        opts[[o]] <- as.numeric(val)
      }
    }
    return(opts)
  }
}

Try the fluidsynth package in your browser

Any scripts or data that you put into this service are public.

fluidsynth documentation built on Oct. 4, 2024, 5:11 p.m.