Computer Plays Guitar
Computer Plays Guitar
Adventurers will make a function that plays guitar chords in arpeggio style with syncopation.
Allow 90 minutes to complete the module.
Important note: This module should be led by an instructor with intermediate Wolfram Language knowledge. If you would like to learn the language, please try this free online class. If you would like a Wolfram Language expert to help you run your Adventure, please contact us.
Allow 90 minutes to complete the module.
Important note: This module should be led by an instructor with intermediate Wolfram Language knowledge. If you would like to learn the language, please try this free online class. If you would like a Wolfram Language expert to help you run your Adventure, please contact us.
Learning Objective
◼
The Adventurer will be able to create an artificial intelligence to play guitar chord progressions in arpeggio style with variable picking patterns and syncopation
Computational Thinking Principles and Practices
◼
Interpreting a problem or idea in a way that a computer can assist with it
◼
Simulating things that are hard or impossible to do with real-world experiments
Standards Alignment
◼
CSTA Computer Science Standards:
◼
3A-AP-16: Design and iteratively develop computational artifacts for practical intent, personal expression or to address a societal issue by using events to initiate instructions
◼
3A-AP-18: Create artifacts by using procedures within a program, combinations of data and procedures or independent but interrelated programs
Helpful Background
◼
Information on syncopation: https://en.wikipedia.org/wiki/Syncopation
◼
Information on arpeggios:
◼
Information on MIDI: https://en.wikipedia.org/wiki/MIDI
◼
Data resource for guitar chords: https://datarepository.wolframcloud.com/resources/MarkGreenberg%3 AGuitar-Chords-for-Picking
STARTING POINT
STARTING POINT
“Computers can play recordings of music, of course, but can a computer be programmed to produce music that nobody has ever recorded? In today’s Adventure, we will build our way up to making a function that can play a song on a guitar. First we’ll string individual notes into a simple song. Then we’ll improve it by adding chords, arpeggio and syncopation. If you’ve never heard of those words before, don’t worry—we will explain as we go.”
In[7]:=
Sound[SoundNote["G",3,"Guitar"]]
Out[7]=
◼ Try a different instrument.
◼ What does the 3 represent? What happens when you try a different number in that spot?
◼ That sounds great! Would you like to share with the class?
CHECKPOINT
Check that each Adventurer has made a Sound from one note.
“Now that we have made one note, let’s start making songs.”
In[10]:=
SoundNote[#,.5,"Guitar"]&/@{"B","A","G","B","A","G","B","B","B","B","A","A","A","A","B","A","G"}
Out[10]=
{SoundNote[B,0.5,Guitar],SoundNote[A,0.5,Guitar],SoundNote[G,0.5,Guitar],SoundNote[B,0.5,Guitar],SoundNote[A,0.5,Guitar],SoundNote[G,0.5,Guitar],SoundNote[B,0.5,Guitar],SoundNote[B,0.5,Guitar],SoundNote[B,0.5,Guitar],SoundNote[B,0.5,Guitar],SoundNote[A,0.5,Guitar],SoundNote[A,0.5,Guitar],SoundNote[A,0.5,Guitar],SoundNote[A,0.5,Guitar],SoundNote[B,0.5,Guitar],SoundNote[A,0.5,Guitar],SoundNote[G,0.5,Guitar]}
For Adventurers that are unfamiliar with Map or pure functions, showing the output from the code above and taking time to explain those Wolfram Language functions will be necessary.
In[225]:=
Sound[SoundNote[#,.5,"Guitar"]&/@{"B","A","G","B","A","G","B","B","B","B","A","A","A","A","B","A","G"}]
Out[225]=
“Make your own song!”
◼ Can you make a different song?
◼ How do notes differ from chords? What would we have to do to make our function play chords?
◼ That sounds great! Would you like to share with the class?
CHECKPOINT
Check to see that each Adventurer has made a song using notes.
“Songs played on a guitar are played using chords. The Wolfram Data Repository has a data resource that lists all of the guitar chords.”
In[226]:=
ResourceData["Guitar Chords for Picking"]
Out[226]=
Explain to the Adventurers that to use this resource, we will turn it into an Association and set the Association to a variable name so that they may pick out certain chords by their names later on.
In[227]:=
chords=Normal[ResourceData["Guitar Chords for Picking"]];
In[228]:=
chords["Am"]
Out[228]=
{A2,A3,C4,E4}
Sound[SoundNote[chords[#],2,"Guitar"]&/@{"Am","C","D","F","Am","C","E","E","Am","C","D","F","Am","E","Am"}]
Out[7]=
If the Adventurers are curious, the tune comes from the folk song “House of the Rising Sun” made famous by the Animals in the 1900s.
◼ Can you make a different song?
◼ Does this sound better than before? More realistic?
◼ That sounds great! Would you like to share with the class?
CHECKPOINT
Check to see that each Adventurer has made a song using chords.
“Each chord is played with a strum, where all the strings are played at once. Finger-picking is another way to play guitar, and one way to finger-pick, called arpeggio, plucks four strings—four different notes from the chord—one at a time. We will teach our function how to play arpeggio-style.”
“To do this, we will make a function called pick that makes a SoundNote for each note in the chord in order.”
In[229]:=
pick[chord_String]:=SoundNote[#,.2,"Guitar"]&/@chords[chord];
In[230]:=
pick["Am"]
Out[230]=
{SoundNote[A2,0.2,Guitar],SoundNote[A3,0.2,Guitar],SoundNote[C4,0.2,Guitar],SoundNote[E4,0.2,Guitar]}
Sound[pick["Am"]]
Out[10]=
pick/@{"Am","C","D","F","Am","C","E","E","Am","C","D","F","Am","E","Am"}
Point out that now there are lists of lists.
If you know “House of the Rising Sun,” it won’t sound right because the song is in 3/4 beat, and our function is currently playing 4/4.
◼ Does this sound better than before? More realistic?
◼ Does your song sound better in arpeggio?
◼ Can you describe arpeggio? Why does it make your song sound different?
◼ That sounds great! Would you like to share with the class?
CHECKPOINT
Check whether each Adventurer has made a song in arpeggio.
“The function does play a progression of guitar chords in arpeggio, but it sounds mechanical, artificial and lifeless. Our function can do better.”
“We’ll make two changes in this step. We’ll increase the number of picks from 4 to 6, and we’ll change the pick pattern to something that might actually be played on a guitar. To make these changes, we will write a new pick function with more parameters.”
“We’ll make two changes in this step. We’ll increase the number of picks from 4 to 6, and we’ll change the pick pattern to something that might actually be played on a guitar. To make these changes, we will write a new pick function with more parameters.”
Pause here to explain Part to students that are unfamiliar with that function.
◼ What would it take to apply our function to other songs, other styles or other instruments?
◼ What happened here? What is the difference from before?
◼ What happens when you change pick to something other than what I have?
◼ What if you put a number larger than 4 in the list that describes the pick pattern?
◼ Another popular arpeggio pattern is {1,3,2,4,1,3}. Try it. How is it different? Which do you like better?
◼ Arpeggio is far more popular with stringed instruments than with other kinds. Why do you think that is?
CHECKPOINT
Check whether each Adventurer has made a song that changes the pick pattern.
“One more adjustment, called syncopation, will make the music sound more natural. Syncopation can be achieved in various ways. We will accomplish it by delaying and speeding up individual notes in the pattern. We’ll introduce a new list, sync, of multipliers for each of the notes’ durations.”
Pause here to explain MapThread to students that are unfamiliar with that function.
Explain that a multiplier of 1 means the note is unchanged. A multiplier greater than 1 lengthens the note, and less than 1 shortens it. For best results, the list should add up to 6 when there are six numbers in it.
◼ What songs today could we adapt our function to play?
◼ How does adding syncopation change the sound?
CHECKPOINT
Check that each Adventurer successfully added syncopation.
FINAL POINT
FINAL POINT
Ten minutes before the end of the module time.
Summarize
Summarize
Summarize what was done in the module and talk about findings.
Refer
Refer
Refer back to the learning objective and summarize how you have reached it.
Extend
Extend
“If you have time at home, look up on the internet the chord progression for a song you like and teach the function to play it."
Functions Used in This Adventure
Functions Used in This Adventure
Association • Function • List • Map • MapThread • Mod • Part • Rule • Set • SetDelayed • Sound • SoundNote
Possible Additional Relevant Functions
Possible Additional Relevant Functions
Import["*.mid"] • Pick • Play • ResourceData • ResourceObject
Possible Pitfalls
Possible Pitfalls
◼
This function plays sound, so be ready for that. Headphones for each Adventurer would be ideal, but perhaps not practical.
By Mark Greenberg