Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# For use with this Chord Generator:
# https://in-thread.sonic-pi.net/t/guitar-backing-track-generator/6836
# https://github.com/adebiasi/SonicPi/blob/main/GuitarBackingTrackGenerator.rb
#
## Credit to these folks!
# https://api.uberchord.com/#strings-pattern
#
require "net/http"
require "json"
require "cgi"
roots = ["Ab", "A", "A#", "Bb", "B", "C", "C#", "Db", "D", "D#", "Eb", "E", "F", "F#", "Gb", "G", "G#"]
types = ["", "m", "7", "maj7", "m7", "dim"]
def make_uri(chord_name)
URI("https://api.uberchord.com/v1/chords/#{chord_name}")
end
def chord_name_for_api(root, type)
CGI.escape "#{root}_#{type}"
end
def chord_name_for_sonic_pi(root, type)
"#{root}#{type}"
end
def dict_string(chord_name, notes)
formatted_notes = notes.downcase.split(" ").join("")
"\"#{chord_name}\" => \"#{formatted_notes}\","
end
def fetch_strings!(root, type)
body = Net::HTTP.get(make_uri(chord_name_for_api(root,type)))
parsed = JSON.parse(body)
notes = parsed[0]["strings"]
dict_string(chord_name_for_sonic_pi(root, type), notes)
rescue => e
puts e
end
roots.each do |root|
types.each do |type|
puts fetch_strings!(root, type)
end
end
@strickinato
Copy link
Author

strickinato commented Jun 14, 2022

Note: the API returned weird 12 string values for Abdim, Gdim and Gbdim - so I changed them manually -- but here's the result:

                   "Ab" => "466544",
                   "Abm" => "466444",
                   "Ab7" => "464574",
                   "Abmaj7" => "4x554x",
                   "Abm7" => "464444",
                   "Abdim" => "xx6434",
                   "A" => "x02220",
                   "Am" => "x02210",
                   "A7" => "x02020",
                   "Amaj7" => "x02224",
                   "Am7" => "x02010",
                   "Adim" => "x0121x",
                   "A#" => "x02220",
                   "A#m" => "x02220",
                   "A#7" => "x02220",
                   "A#maj7" => "x02220",
                   "A#m7" => "x02220",
                   "A#dim" => "x02220",
                   "Bb" => "x13331",
                   "Bbm" => "x13321",
                   "Bb7" => "x13131",
                   "Bbmaj7" => "x13231",
                   "Bbm7" => "x13121",
                   "Bbdim" => "x12320",
                   "B" => "x24442",
                   "Bm" => "x24432",
                   "B7" => "x24242",
                   "Bmaj7" => "x24342",
                   "Bm7" => "x20202",
                   "Bdim" => "x2343x",
                   "C" => "x32010",
                   "Cm" => "x35543",
                   "C7" => "x32310",
                   "Cmaj7" => "x35453",
                   "Cm7" => "x35343",
                   "Cdim" => "x3454x",
                   "C#" => "x32010",
                   "C#m" => "x32010",
                   "C#7" => "x32010",
                   "C#maj7" => "x32010",
                   "C#m7" => "x32010",
                   "C#dim" => "x32010",
                   "Db" => "x43121",
                   "Dbm" => "x46654",
                   "Db7" => "x46464",
                   "Dbmaj7" => "x46564",
                   "Dbm7" => "x42100",
                   "Dbdim" => "x42020",
                   "D" => "xx0232",
                   "Dm" => "xx0231",
                   "D7" => "xx0212",
                   "Dmaj7" => "xx0222",
                   "Dm7" => "xx0211",
                   "Ddim" => "xx0131",
                   "D#" => "xx0232",
                   "D#m" => "xx0232",
                   "D#7" => "xx0232",
                   "D#maj7" => "xx0232",
                   "D#m7" => "xx0232",
                   "D#dim" => "xx0232",
                   "Eb" => "x65343",
                   "Ebm" => "xx1342",
                   "Eb7" => "xx1323",
                   "Ebmaj7" => "xx1333",
                   "Ebm7" => "xx1322",
                   "Ebdim" => "x6787x",
                   "E" => "022100",
                   "Em" => "022000",
                   "E7" => "020100",
                   "Emaj7" => "0x2444",
                   "Em7" => "020000",
                   "Edim" => "x7x986",
                   "F" => "133211",
                   "Fm" => "133111",
                   "F7" => "131241",
                   "Fmaj7" => "1x2210",
                   "Fm7" => "131111",
                   "Fdim" => "1x310x",
                   "F#" => "133211",
                   "F#m" => "133211",
                   "F#7" => "133211",
                   "F#maj7" => "133211",
                   "F#m7" => "133211",
                   "F#dim" => "133211",
                   "Gb" => "244322",
                   "Gbm" => "244222",
                   "Gb7" => "242352",
                   "Gbmaj7" => "2x332x",
                   "Gbm7" => "242222",
                   "Gbdim" => "xx4212",
                   "G" => "320033",
                   "Gm" => "355333",
                   "G7" => "320001",
                   "Gmaj7" => "3x443x",
                   "Gm7" => "353333",
                   "Gdim" => "xx5686",
                   "G#" => "320033",
                   "G#m" => "320033",
                   "G#7" => "320033",
                   "G#maj7" => "320033",
                   "G#m7" => "320033",
                   "G#dim" => "320033",

@adebiasi
Copy link

adebiasi commented Jun 14, 2022

"A#" => "x02220",
"A#m" => "x02220",
"A#7" => "x02220",
"A#maj7" => "x02220",
"A#m7" => "x02220",
"A#dim" => "x02220",

Thanks for this!

I noticed that all the chords with '#' char are wrong.

@strickinato
Copy link
Author

strickinato commented Jun 15, 2022

Ah yes - you're totally right! (I had a feeling there would be some issue 😄 ), and there were two:

1 -- I forgot to url encode the chordnames, which meant that it was interpreting A#7 as just A - since the # is interpreted in urls as the page anchor.

2 -- the "strangeness" I had pointed out w/r/t Abdim, Gdim and Gbdim was because the fingerings it was returning were in frets higher than 9. ie. x 11 12 13 12 x - which your original code isn't set up to handle.

So once again, I manually added the fingerings lower down on the fret board.

Here is the output
"Ab" => "466544",
"Abm" => "466444",
"Ab7" => "464574",
"Abmaj7" => "4x554x",
"Abm7" => "464444",
"Abdim" => "xx6434",
"A" => "x02220",
"Am" => "x02210",
"A7" => "x02020",
"Amaj7" => "x02224",
"Am7" => "x02010",
"Adim" => "x0121x",
"A#" => "x13331",
"A#m" => "x13321",
"A#7" => "x13131",
"A#maj7" => "x13231",
"A#m7" => "x13121",
"A#dim" => "x12320",
"Bb" => "x13331",
"Bbm" => "x13321",
"Bb7" => "x13131",
"Bbmaj7" => "x13231",
"Bbm7" => "x13121",
"Bbdim" => "x12320",
"B" => "x24442",
"Bm" => "x24432",
"B7" => "x24242",
"Bmaj7" => "x24342",
"Bm7" => "x20202",
"Bdim" => "x2343x",
"C" => "x32010",
"Cm" => "x35543",
"C7" => "x32310",
"Cmaj7" => "x35453",
"Cm7" => "x35343",
"Cdim" => "x3454x",
"C#" => "x43121",
"C#m" => "x46654",
"C#7" => "x46464",
"C#maj7" => "x46564",
"C#m7" => "x42100",
"C#dim" => "x42020",
"Db" => "x43121",
"Dbm" => "x46654",
"Db7" => "x46464",
"Dbmaj7" => "x46564",
"Dbm7" => "x42100",
"Dbdim" => "x42020",
"D" => "xx0232",
"Dm" => "xx0231",
"D7" => "xx0212",
"Dmaj7" => "xx0222",
"Dm7" => "xx0211",
"Ddim" => "xx0131",
"D#" => "x65343",
"D#m" => "xx1342",
"D#7" => "xx1323",
"D#maj7" => "xx1333",
"D#m7" => "xx1322",
"D#dim" => "x6787x",
"Eb" => "x65343",
"Ebm" => "xx1342",
"Eb7" => "xx1323",
"Ebmaj7" => "xx1333",
"Ebm7" => "xx1322",
"Ebdim" => "x6787x",
"E" => "022100",
"Em" => "022000",
"E7" => "020100",
"Emaj7" => "0x2444",
"Em7" => "020000",
"Edim" => "x7x986",
"F" => "133211",
"Fm" => "133111",
"F7" => "131241",
"Fmaj7" => "1x2210",
"Fm7" => "131111",
"Fdim" => "1x310x",
"F#" => "244322",
"F#m" => "244222",
"F#7" => "242352",
"F#maj7" => "2x332x",
"F#m7" => "242222",
"F#dim" => "133211",
"Gb" => "244322",
"Gbm" => "244222",
"Gb7" => "242352",
"Gbmaj7" => "2x332x",
"Gbm7" => "242222",
"Gbdim" => "xx4212",
"G" => "320033",
"Gm" => "355333",
"G7" => "320001",
"Gmaj7" => "3x443x",
"Gm7" => "353333",
"Gdim" => "xx5686",
"G#" => "466544",
"G#m" => "466444",
"G#7" => "464574",
"G#maj7" => "4x554x",
"G#m7" => "464444",
"G#dim" => "320033",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment