Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CPSC 231 - Fall XXXXXXXXXXUniversity of Calgary cpsc 231 assignment #6 Guitar Heroine The goal of your final assignment in cpsc 231 is to explore compu- tational algorithms for digital synthesis of...

1 answer below »
CPSC 231 - Fall XXXXXXXXXXUniversity of Calgary
cpsc 231 assignment #6
Guitar Heroine
The goal of your final assignment in cpsc 231 is to explore compu-
tational algorithms for digital synthesis of sounds, instruments, and
music. In the process, you’ll practice using and creating data types in
Python, and even spend some time writing a little recursive function.
Audio is encoded digitally on computers as a discrete signal, or a
sequence of discrete samples, that represents the sound pressure level
over time. In other words, it’s no more than a big, one-dimensional
a
ay of numbers. Periodic repetitions and patterns in the numbers
give the sound its characteristic tone and tim
e. For example, the
sound of a strum on an acoustic guitar looks like this when plotted
as a graph:
-1
-0.5
0
0.5
1
If we can come up with an algorithm that generates these numbers
so that the result has a meaningful tone and tim
e, we can synthe-
size the sound of musical instruments! In 1983, Kevin Karplus and
Alex Strong described a very simple algorithm that can generate the
sound of plucking a string on a guitar.1 Despite its simplicity, it pro- 1 Kevin Karplus and Alex Strong.
Digital synthesis of plucked-string and
drum tim
es. Computer Music Journal, 7
(2):43–55, 1983
duces an incredibly rich and realistic sound. You’ll end your work
in cpsc 231 this semester by implementing the Karplus-Strong algo-
ithm to turn your computer into a live acoustic guitar synthesize
that you can play music on.
Due Dates
Individual component Friday, November 30, 11:59 pm
Paired component Friday, December 7, 11:59 pm
2 cpsc 231 - fall XXXXXXXXXXuniversity of calgary
Individual Component
As with all your previous assignments, we’ll use the individual com-
ponent to “warm up” a little bit before going about the live synthesis
of the sound of an acoustic guitar. Here, we’ll first use a recursive al-
gorithm to generate a one-dimensional fractal noise signal. This kind
of noise can create patterns that are more similar to natural phenom-
ena than noise consisting of uniformly-distributed random numbers
(i.e. white noise). We will stream this noise to the audio output of
your computer to synthesize the sound of ocean waves crashing on
the shore.
Before you begin, it may be worthwhile to review the sections in
the course text that pertain to digital audio and music (pp. 171–175
and 231–235). You can use the exact same programming environment
you set up for previous assignments to complete this assignment.
Create and submit a separate Python program for each problem.
Problem 1: Brownian Bridge
A Brownian
idge is a stochastic process (think of this as a random
function over time) that models a random walk connecting two
points. In our case, the
idge is a function of time, and if we fix
its value at an initial and a final time point, b(t0) and b(t1), we can
compute the in-between values with the following recursive process:
1. Compute the midpoint of the interval, ym = 12 (b(t0) + b(t1)).
2. Add to the midpoint value a random value δ, drawn from a nor-
mal distribution2 with zero mean and given variance, and assign 2 You can use random.gauss() o
andom.normalvariate() to generate
such a random value, providing the
mean and standard deviation (square
oot of variance) as arguments.
the sum to the
idge function: b(tm)← ym + δ.
3. Recur on the subintervals (t0, tm) and (tm, t1), dividing the vari-
ance for the next level by a given scale factor.
The shape of the function is controlled by two parameters: the
volatility and the Hurst exponent. Volatility is the initial value of the
variance, and controls how far the random walk strays from the
straight line connecting the fixed endpoints. The Hurst exponent, H,
controls the smoothness of the result. The variance of the distribution
from which the random value δ is drawn is divided by 22H at each
ecursive level. When H = 0.5, the variance is halved after each recur-
sion, and the result is a true Brownian
idge that models Brownian
motion between the endpoints.
Note that Program 2.3.5 in the course
text uses a recursive function to plot a
Brownian
idge on the screen. Rathe
than drawing a graph, this problem
equires you to fill an a
ay with values
of the function. Studying that example
would certainly be a good idea, and
you may complete this problem by
starting from the textbook example and
modifying it if you like.
Write a recursive Python function to fill an a
ay with numerical
values that form a Brownian
idge between two specified endpoints.
The function should take a reference to an a
ay, a first and last in-
dex, a variance, and a scale factor as parameters. These would allow
the function to fill the a
ay according to the recu
ence described.
For example, your function may have a definition that looks like this:
assignment #6: guitar heroine 3
def fill_
ownian(a, i0, i1, variance, scale):
"""
Recursive function that fills an a
ay with values forming a
Brownian
idge between indices i0 and i1 (exclusive).
Assumes that a[i0] and a[i1] are values of the fixed endpoints.
:param a: A
ay to fill with Brownian
idge.
:param i0: Index of first endpoint.
:param i1: Index of second endpoint
:param variance: Variance of the normal distribution from
which to sample a displacement.
:param scale: Scale factor to reduce variance for next level.
"""
When you’ve completed the function, write a program to test it
out. Call your recursive function to fill an a
ay with 129 elements,
the first and last of which are anchored at a value of 0.0. Use a
volatility of 0.05 and allow the Hurst exponent to be specified as a
command line argument. Then visualize the contents of your a
ay
y drawing a graphical line plot of the values to the screen. You may
either write your own code, use some of the code we wrote in class
or that’s found in the course text, or use the stdstats module from
the booksite li
ary to draw the plot.
Figure 1: Line plots of 129 values of a
Brownian
idge with Hurst exponents
of 1.0, 0.5, and 0.2, respectively from
top to bottom.
Inputs: The Hurst exponent for your Brownian
idge function, spec-
ified as a command line argument. For example, you might run
your program with the following invocation:
$ python3 my-p1.py 0.5
Outputs: A line plot of your randomly-generated Brownian
idge
etween the two zero endpoints. It should resemble those shown
on the right when run with the co
esponding Hurst exponent.
Problem 2: Ocean Waves
If you stream the Brownian
idge function values to your com-
puter’s audio output,3 the result sounds like a pleasant, rumbling 3 You can use the booksite li
ary
function stdaudio.playSamples(a) to
stream a sequence of samples in the
a
ay a to your computer’s default
audio output device. The values in a
should be floats between -1.0 and +1.0.
kind of noise. In fact, if you generate the values with a Hurst expo-
nent of 0.5, the result is what audio engineers call “
own noise” o
“red noise”. This is distinct from “pink noise” and the higher-pitched
and more commonly found “white noise”, which is simply a stream
of uniformly distributed random numbers.
Both kinds of noise are useful, and we can actually synthesize a
half-decent sound of ocean waves crashing on the shore by oscillating
etween
own noise and white noise at a very low frequency. If
nb(t) represents the sequence of
own noise values over time, and
nw(t) represents white noise, we can achieve the ocean wave effect by
4 cpsc 231 - fall XXXXXXXXXXuniversity of calgary
lending them with a time-varying blend factor, s(t), as follows:
y(t) = (1− s(t)) nb(t) + s(t) nw(t),
with s(t) = sin6(π f t).
In this equation, t represents the time (in seconds), and f is the fre-
quency at which the ocean waves are crashing on the shore.
Write a program that generates 20 seconds of crashing ocean
waves using the method described above, then plays the sound
through the computer’s audio output. If you use the stdaudio mod-
ule, the output sample rate is fixed to XXXXXXXXXXHz (samples per sec-
ond), which means you will need to generate a total of 20 × 44 100 =
XXXXXXXXXXdata values. Use your Brownian
idge function from Prob-
lem 1 to generate values for nb(t).4 You can adjust any parameters 4 You will want to re-anchor you
idge to a value of 0.0 several times
a second so that the values don’t
drift too far from the neutral zero-
point. In other words, rather than
generating all XXXXXXXXXXsamples with
a single function call, you should
generate a few thousand samples at a
time, setting the two endpoints to 0.0
each time.
within these equations that you’d like in order to synthesize a more
ealistic or more pleasant ocean sound, but perhaps we can suggest
that you start with the following:
• Use a Hurst exponent of H = 0.5. Increase it slightly if you want
lower-frequency noise, and decrease it for higher-frequency noise.
• A frequency of f = 0.25 will give you a wave crash every fou
seconds. Adjust as you like.
• The exponent of 6 on the blend factor, s(t), controls the balance of
the two noise types during each cycle. Increase the exponent fo
quicker wave crashes, but use even exponents to keep the blend
factor non-negative.
• An amplitude of about 0.25 works well for the white noise compo-
nent. In other words, generate random values between -0.25 and
+0.25 for nw(t). Adjust as you like as well.
You may also want to introduce one more slowly-oscillating function
to modulate the amplitude (volume) of your output sound to achieve
the ultimate ocean wave simulator, but that’s purely optional.
Inputs: None.
Outputs: The sound of synthetic ocean waves crashing to sooth and
elax you for 20 seconds when you run your program.
Paired Component
For once, the paired component of the assignment might actually be
easier than the individual component! We know it’s the last week of
the term, and you’re both busy and exhausted with all your othe
end-of-semester work, so this is our gift to you for making it all the
way to the finish line. The end result is quite fantastic, especially
for the amount of time required to complete it. And if you’ve really
assignment #6: guitar heroine 5
got time and energy to spare at this point, we’ve got many bonus
opportunities worth taking up.
You may still work with a partner of your own choosing to com-
plete the remainder of this assignment, though remember that it may
not be the same
Answered Same Day Nov 29, 2020

Solution

Snehil answered on Dec 05 2020
138 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here