Double All
Write a function double_all(number_list)
, which takes a list of numbers. The function should modify number_list by doubling all values in the list.
Example
Test Code | Output |
---|---|
number_list = [1, 2, 3] double_all(number_list) print(number_list) |
[2, 4, 6] |
number_list = [-1, -2, -3] double_all(number_list) print(number_list) |
[-2, -4, -6] |
Remove All
Write a function remove_all(a_list, an_element)
, which takes a list and some element. Your function should modify a_list by removing all occurrences of an_element from a_list.
Example
Test Code | Output |
---|---|
a_list = [1, 2, 1] remove_all(a_list, 1) print(a_list) |
[2] |
a_list = [1, 2, 1] remove_all(a_list, 2) print(a_list) |
[1, 1] |
a_list = [11, 2, 11] remove_all(a_list, 1) print(a_list) |
[11, 2, 11] |
a_list = [1, 1] remove_all(a_list, 1) print(a_list) |
[] |
Karplus-Strong
Sound is simply a vibration of the air molecules around you. Pleasing sounds have some nice structure to the vibrations. These are continuous structures, but our computers are inherently digital, discrete beings. For a computer to play music, we must create a discrete representation of the continuous wave. We do this by generating a series of samples of the wave. For this assignment you will create a program that can read a textual music notation file and synthesize a digital representation of a simple stringed instrument playing the music. The program will use the Karplus-Strong string synthesis algorithm to generate the samples.
The Karplus-Strong algorithm heuristically generates samples that model a simple stringed instrument, such as a harp, piano, or guitar. The algorithm begins by creating a list of randomly generated floating point numbers between -1 and 1. The length of the list determines the pitch of the string, and you can compute this value by dividing the sample rate (44,100 samples per second) by the frequency of the note to be played.
\[list\_length = \frac{SAMPLE\_RATE}{note\_frequency}\]
Once you have created this list, you have essentially created a list containing samples of the note to be played. To generate all of the samples of a note, you need to repeatedly remove the value contained at the head of the list. This is a sample of this note. In order to maintain the pitch of the note, you must add a new value to the end of the list. According to the Karplus-Strong algorithm, The value you add to the end of the list should be the “decayed” average of the produced sample (the value you just removed from the list) and the value that is now at the front of the list. To do this, compute the normal arithmetic mean of the two values, then multiply it by a floating point value less than one. .996 is a good decay value to start with.
\[ new\_list\_value = \frac{produced\_sample + new\_front\_value}{2} \times DECAY\]
Details
Create a program that uses the Karplus-Strong algorithm to create a sound file that plays one second of a concert A, 440 Hz. A one second sound is a list of samples, 44,100 floating point values between -1 and 1. To create the samples begin with an empty list and repeatedly create new samples using the Karplus-Strong algorithm. To save the samples as a sound file, use the write_sound
function in this sound.py file.
Example
Hint
There are essentially 2 steps to creating the sound:
Create the list of random values that will be used to create the sound samples. The length of the list depends on the sample rate of the sound, 44,100 Hz, and the note to be synthesized, 440 Hz for concert A. To create a list of random values, create an empty list and repeatedly append a random floating point number between -1 and 1.
Create the list of samples. To create a sound that is one second, at a 44,100 Hz sample rate, the list should contain 44,100 numbers between -1 and 1. To create the list, create an empty list and repeatedly append floating point numbers from the beginning of the list of random values. After appending each number compute the new value to add to the end of the random list using the Karplus-Strong algorithm.
Once the list of samples is created, use the write_sound function to write the sound to a file.
Submission
Please show your source code and run your programs. Only programs that have perfect functionality will be accepted as complete.