Setting synth/sample defaults from list provided by current_synth_defaults / current_sample_defaults

Okay, I’ve dived down another rabbit hole.
I’m looking to save any previous synth/sample defaults, manipulate them in a function, and then restore the previous settings.
When I run current_synth_defaults (after setting some defaults), I get this:
(map amp: 1.5,
release: 0.25)
What is this? Is it a hash? An array? How do I programmatically work with it?
And when I have the default/value pair, how do I restore them to their previous value (short of building a command string and using eval)? How do I feed these param/value pairs back to use_synth_defaults or use_merged_synth_defaults?
For a little background, I came up with a method to arpeggiate a chord, then I thought it would be nice to have more control over each note, so I want to pass in an array of param values to be applied note by note, then restore any previous defaults.
Any ideas how to approach this?
Thanks!

I’ve done it like this. Hopefully this will get you started:

sd = get(:new_synth_defaults) || {}
  sd.each do | k, v |
    puts "Setting key #{k} to #{v}"
    use_merged_synth_defaults k => v
  end

My use case was to change the synth_defaults in another live_loop; this approach applies those new values within this live_loop.

It looks like current_synth_defaults returns an object of type SPMap. This can be converted into a hash by calling .to_h on it, and then that can be passed to the *_synth_defaults functions.

Also, there’s no need to explicitly set synth defaults back to their previous values, as there is the with_synth_defaults function to do that for you:

define :updated_synth_defaults do
  # Calculate some new synth defaults based on the current values
  defaults = current_synth_defaults.to_h
  
  defaults[:amp] ||= 1    # Set amp to 1 if it was not already set
  defaults[:amp] *= 2     # Double amp
  defaults[:cutoff] = 74  # Set cutoff value
  
  defaults
end

# Setting up initial defaults
use_synth_defaults amp: 2

# Look at the initial synth defaults
puts "Before:", current_synth_defaults

with_synth_defaults updated_synth_defaults do
  # Here we have the modified defaults
  puts "During:", current_synth_defaults
end

# Now they should be back to what they were initially
puts "After:", current_synth_defaults

Thanks so much for the help, folks. This clarifies things quite a bit.
I was also delighted to see you illustrate the ||= operator. I hadn’t come across it before, and it’s super cool!
What a great community! So smart and so helpful!

Oh, one more question.
Is there a way to wipe out all synth defaults previously set? An unset method? I couldn’t find it.
Thanks!

There are a few more operators like ||=. In general for each operator (e.g. **) there is another version followed by = (e.g. **=) where e.g. variable **= 2 is equivalent to variable = variable ** 2. This can be useful to reduce typing, especially when you have long variable names.

Just calling use_synth_defaults on its own with no arguments seems to work to wipe out all existing defaults.

Yeah, I know about *=, +=, etc., for arithmetic operations. I just didn’t realize that logical operators also worked. Cool!
Also cool to just call use_synth_defaults with no args. Very useful to know.
Thanks!

1 Like