# Setting synth/sample defaults from list provided by current\_synth\_defaults / current\_sample\_defaults

**URL:** <https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181>\
**Category:** Support, Help & Resources\
**Created:** [September 2, 2024, 2:36am UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181 "2024-09-02T02:36:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [September 2, 2024, 2:36am UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181/1 "2024-09-02T02:36:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nairobiny](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@nairobiny](https://in-thread.sonic-pi.net/u/nairobiny)\
**Post date:** [September 2, 2024, 6:30am UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181/2 "2024-09-02T06:30:33Z")

</div>

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

```ruby
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`.

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [September 2, 2024, 10:27am UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181/3 "2024-09-02T10:27:25Z")

</div>

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:

```ruby
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

```

---

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [September 2, 2024, 2:41pm UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181/4 "2024-09-02T14:41:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [September 2, 2024, 3:03pm UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181/5 "2024-09-02T15:03:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [September 3, 2024, 9:06am UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181/6 "2024-09-03T09:06:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [September 3, 2024, 12:53pm UTC](https://in-thread.sonic-pi.net/t/setting-synth-sample-defaults-from-list-provided-by-current-synth-defaults-current-sample-defaults/9181/7 "2024-09-03T12:53:12Z")

</div>

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!
