Use Python to program to play guitar strings (#3) and make guitar heroine (#4). Use stddraw, stdaudio, math, random, etc. (Do not use pyaudio, it does not work in my computer) I use python 3.7n (latest version) so it should work there. Additional information is given in the files that I attached.
:
OOOOOO
Problem 3: Guitar String
Start by implementing the Karplus-Strong algorithm to synthesize
the sound generated from plucking a single guitar string. The al-
gorithm maintains a wavetable, or an array of sample values, that is
played back in a loop. Thus, if p is the size (number of samples) of
the wavetable, then the nth sample played in the stream is the same
as sample n-p:
yn = yn-p
If the wavetable is unmodified as it is played in a loop, the re-
sulting sound is purely periodic (repeating at a frequency of 44 100
/ p) and usually sounds very artificial. The key innovation of the
Karplus-Strong algorithm is to apply a very simple modification to a
wavetable after each sample is played, which simulates the propaga-
tion of vibration waves along a plucked string anchored at both ends,
resulting in a realistic guitar sound. Rather than simply repeating the
wavetable value p samples previous, it simply two successive values
in the wavetable:
yn =
– (yn-p+yn-y-1)
We can make use of a circular buffer to implement this algorithm in
software. This data structure allows us to examine and remove ele-
ments from its left, and to append elements to its right. One iteration
of the Karplus-Strong algorithm is realized by examining the first
two samples in the buffer, calculating their average (and optionally
multiplying by a decay factor), appending the result to right, then
removing the first value on the left to keep the length of the buffer.
timet
.2
.4
.5
.3
.4
.3
.0
-.1 -3
.996 x (.2+.4)
Figure 2: One iteration of the Karplus-
Strong wavetable update, with a decay
factor of 0.996, implemented on a
circular buffer. (Image borrowed from
the Princeton Cos 126 assignment
specification of the same name.)
time t+1
.4
.5
.3
.2
.4
.3
.0
-.1
-.3 .2988
Simulating a guitar string using the Karplus-Strong algorithm
involves maintaining a dynamic wavetable of length p, with
44 100
p=
Test client for Problem 3:
where f is the desired frequency (pitch) of the guitar string, and the
quotient is rounded up to obtain an integer value for p. We must also
provide the following two operations:
import stddraw
import stdaudio
from picture import Picture
from guitarstring import GuitarString
a_string = GuitarString (440.00)
c_string = GuitarString(523.25)
# show a nice background picture
p = Picture(‘cpsc231-guitar.png’)
stddraw.picture(p)
stddraw.show(0.0)
Plucking the string. Simulating a pluck of the guitar string essentially
amounts to exciting it at a multitude of frequencies. This can be
accomplished by filling the wavetable with white noise.
Updating the wavetable and playing a sample. Apply the Karplus-Strong
algorithm to compute a new sample for the wavetable, then stream
that sample to the audio output.
This combination of data (the wavetable), and specific operations
on the data, is a sign that a Python class would be a great way to
create an abstraction of a guitar string. Create a class that implements
a GuitarString data type with the Api shown in Table 1. Start with a
decay factor of 0.996 for your wavetable update, as shown in Figure 2
above, but feel free to adjust this value to get the sound you like.
When you’ve completed your implementation of the GuitarString
data type, test it with the client program listed on the right.
escape = False
while not escape:
# check for and process events
stddraw..checkForEvents()
while stddraw.hasNextKeyTyped():
key = stddraw.nextKeyTyped()
key == chr(27):
escape = True
elif key == ‘a’:
a_string.pluck()
elif key == ‘c’:
c_string.pluck()
GuitarString(f)
# simulate and play strings
y = a_string.tick()
y += c_string.tick()
stdaudio.playSample(y)
g.pluck()
Create a guitar string that vibrates at the given
fundamental frequency (pitch), f.
Pluck the guitar string by replacing all values
in the wavetable with white noise. Use random
values between -0.5 and +0.5.
Advance the Karplus-Strong simulation by
one step, returning the sample value that was
computed and added to the wavetable.
g.tick)
Table 1: The GuitarString application
programming interface that you need to
implement for Problem 3.
deque)
deque(iterable)
d.append(x)
d. popleft()
Create an empty deque.
Create a deque containing the elements of an
iterable, such as a list or str object.
Add x to the right side of the deque.
Remove and return an element from the left
side of the deque.
Remove all elements from the deque.
A reference to the element at the given index
(left is index o).
Number of elements in the deque.
d.clear()
d[index]
len(d)
Table 2: The application programming
interface of the deque data type from
the Python collections module.
Python’s collections module includes a double-ended queue
data type, called a deque, that you can use to store the wavetable. It
supports the operations we need, and with the degree of efficiency
we need, to maintain a circular buffer of samples for the guitar string.
The relevant portions of the deque API are shown in Table 2 above.
Inputs: Key presses (‘a’ or ‘c’) to play your two guitar strings while
your program is running.
Outputs: Synthesized sounds of an acoustic guitar playing a concert
A or a concert C coming out live from your computer.
Problem 4: Guitar Heroine
Now that you have an abstracted and tested guitar string, it’s time to
turn your computer into a guitar! Write a client similar to that from
Problem 3, but allows you to play three full octaves of notes. Map
keys on your keyboard to guitar strings corresponding to music notes
as shown in Figure 3 below. When a key is pressed, pluck the guitar
string corresponding to its note in your program.
2
4 5
7 8 9
df 9
j k
A
B
С
DEFGA
B
C DEFGA
B
CDEFG
А
q
We
r
t y ui op [ 2 x
с
V
bnm,
Figure 3: Mapping of keys on the
computer keyboard to notes on the
piano keyboard. (Image borrowed
from the Princeton Cos 126 assignment
specification of the same name.)
The lowest note on this keyboard (A2) corresponds to a fundamen-
tal frequency of 110 Hz. Each octave corresponds to a doubling of the
fundamental frequency (e.g. Az = 220 Hz), and contains 12 semitones.
We will use an equal-tempered chromatic scale, which means the
frequencies of the semitones are equally spaced apart in a geometric
series. Thus, the frequency of each semitone is 21/12 times that of
the previous. For example, the fundamental frequency of the first Bb
(which corresponds to the black piano key labelled ‘2’) is
fb) = 212 fa = 116.54 Hz.
After completing this part, you should have a real-time synthe-
sized interactive “guitar” that allows you to play notes (including
chords) and songs by typing keys on your keyboard. When playing
multiple strings, the resulting sound wave is the superposition of the
sound waves generated by each individual string. Thus, the output
sample at each time step is simply the sum of the individual samples
from each guitar string.
Play a few songs to test out your brand new acoustic guitar. When
you’re satisfied with it, submit all the files needed to run your pro-
gram, and you’re done for the semester!
Inputs: Key presses playing some serious guitar rips while your
program is running
Outputs: Beautifully synthesized acoustic guitar music coming out
live from your computer.
apparently calcu-
updating the
You may have heard or even experi-
enced that Python programs are not
known to be very quick. Up until now,
computational efficiency has not been
a concern for us, but
lating sample
values and
wavetable for 37
guitar strings 44100
times a second, can be a little too much
for the Python interpreter to handle.
If you implemented your guitar string
carefully, you should be able to run
the full three octaves interactively on a
modern desktop or laptop computer.
However, if you’re working on an
older laptop computer, your guitar
synthesizer may stutter when your
program can’t feed it samples fast
enough. In such a case, we suggest that
you develop and test your program
using just one octave of
switch to the full three octaves before
submitting it. Alternatively, you can
complete this assignment using the
CPsc lab machines, but please don’t
forget to bring headphones!
strings, then