Sainte_json a synth working with external json file

Hi everybody,

I “dig” a bit further the idea of parameters saved into a json file to find back easily my synth parameters.
so this is the code

# NLB - 31/03/2020
# Sainte Json
# use a json format external file to load parameters to built a synth
# Avantage : centralize my own synths parameters on github
# the synths are not "fantastiques" it's just to show the idea

require "json"
require 'net/http'

## If you want to test json into spi
##| # call to an external api
##| url = "https://quote-garden.herokuapp.com/quotes/search/war"
##| uri = URI(url)
##| puts response = Net::HTTP.get(uri)
##| sleep 4
##| foo = JSON.parse(response)
##| puts foo

##| # to puts the values
##| foo["results"].each_with_index do |quote, idx|
##|   puts idx.to_s + " -- " + quote["quoteText"] + " by " + quote["quoteAuthor"]
##| end


##| # to puts the values
##| foo["results"].each do |quote|
##|   puts quote["quoteText"] + " by " + quote["quoteAuthor"]
##| end

# to get some indice quote
##| puts foo["results"][25]["quoteText"]
##| puts foo["results"][25]["quoteAuthor"]


# get from the web
url = "https://raw.githubusercontent.com/nlebellier/autour-de-sonic-pi/master/instruments.json"
uri = URI(url)
puts s = Net::HTTP.get(uri)
sleep 2

# get json string from a local file
##| path = "/home/nlb/audio/audio-code/spi/instruments.json"
##| s = File.read(path)


# parse and convert JSON to Ruby
instruments = JSON.parse(s)
puts instruments
puts instruments.size
puts instruments.keys


# sainte_json a hype synth working with json

define :sainte_json do | famille, saveur |
  
  use_synth famille
  # (Laz'rus dig yourself)
  # an extremely good ruby tool : dig
  parameters = instruments.dig(famille, saveur)
  use_synth_defaults attack: parameters["a"],
    decay: parameters["d"],
    sustain: parameters["s"],
    release: parameters["r"],
    cutoff: parameters["cutoff"]
end

use_bpm 160

# the notes "riff"
riff_A = scale(:c, :major_pentatonic)
# the length "l" for each note
l_A = [0.5, 1, 1, 0.5, 0.5]

famille = "tb303"
saveur = "tb303_01"
sainte_json famille, saveur

play_pattern_timed riff_A, l_A

sainte_json "pretty_bell", "curious"
play_pattern_timed riff_A, l_A

sainte_json "piano", "piano_01"
play_pattern_timed riff_A, l_A

The instruments.json

{ "piano":
  {
  "piano_01": {"a":0.01,"d":0.1,"s":0.8, "r":0.25, "cutoff":80},
  "piano_02": {"a":0.01,"d":1,"s":0.1, "r":0.25, "cutoff":60}
  },
 "tb303":
  {
  "tb303_01": 
    {
      "a":0.3,
      "d":0.8,
      "s":0.4, 
      "r":0.5, 
      "cutoff":120
    },
    "tb303_02": {"a":0.1,"d":0.5,"s":0.25, "r":0.5, "cutoff":60},
  "tb303_03": {"a":0.1,"d":0.3,"s":1, "r":1, "cutoff":80}
  },
"pretty_bell":
  {
  "pretty_bell_01": {"a":0.1,"d":0.8,"s":0.4, "r":0.5, "cutoff":40},
  "curious": {"a":0,"d":0.4,"s":0.5, "r":1, "cutoff":80}
  }
}

Cheers

I’ve also found Jason files useful to store information form sonic pi projects. For example in this one.

thanks for the reference.
The next step would be for me to add midi knobs changing values and then store the values into a file
your write_json function will be helpful.