matchPars: Match soundgen pars (experimental)

View source: R/matchPars.R

matchParsR Documentation

Match soundgen pars (experimental)


Attempts to find settings for soundgen that will reproduce an existing sound. The principle is to mutate control parameters, trying to improve fit to target. The currently implemented optimization algorithm is simple hill climbing. Disclaimer: this function is experimental and may or may not work for particular tasks. It is intended as a supplement to - not replacement of - manual optimization. See vignette('sound_generation', package = 'soundgen') for more information.


  samplingRate = NULL,
  pars = NULL,
  init = NULL,
  probMutation = 0.25,
  stepVariance = 0.1,
  maxIter = 50,
  minExpectedDelta = 0.001,
  compareSoundsPars = list(),
  verbose = TRUE



the sound we want to reproduce using soundgen: path to a .wav file or numeric vector


sampling rate of target (only needed if target is a numeric vector, rather than a .wav file)


arguments to soundgen that we are attempting to optimize


a list of initial values for the optimized parameters pars and the values of other arguments to soundgen that are fixed at non-default values (if any)


the probability of a parameter mutating per iteration


scale factor for calculating the size of mutations


maximum number of mutated sounds produced without improving the fit to target


minimum improvement in fit to target required to accept the new sound candidate


a list of control parameters passed to compareSounds


if TRUE, plays back the accepted candidate at each iteration and reports the outcome


Returns a list of length 2: $history contains the tried parameter values together with their fit to target ($history$sim), and $pars contains a list of the final - hopefully the best - parameter settings.


## Not run: 
target = soundgen(sylLen = 600, pitch = c(300, 200),
                  rolloff = -15, play = TRUE, plot = TRUE)
# we hope to reproduce this sound

# Match pars based on acoustic analysis alone, without any optimization.
# This *MAY* match temporal structure, pitch, and stationary formants
m1 = matchPars(target = target,
               samplingRate = 16000,
               maxIter = 0,  # no optimization, only acoustic analysis
               verbose = TRUE)
cand1 =, c(m1$pars, list(
  temperature = 0.001, play = TRUE, plot = TRUE)))

# Try to improve the match by optimizing rolloff
# (this may take a few minutes to run, and the results may vary)
m2 = matchPars(target = target,
               samplingRate = 16000,
               pars = 'rolloff',
               maxIter = 100,
               verbose = TRUE)
# rolloff should be moving from default (-9) to target (-15):
sapply(m2$history, function(x) x$pars$rolloff)
cand2 =, c(m2$pars, list(play = TRUE, plot = TRUE)))

## End(Not run)

soundgen documentation built on Aug. 14, 2022, 5:05 p.m.