in python
We were unable to transcribe this imageA standard piano has 88 keys that just repeat this pattern of notes over and over again. As you travel to the right the notes get higher, and they get lower as your travel to the left. The notes that are in higher octaves sound higher pitched, and the lower octaves sound lower pitched. You will use a combination of the note’s letter and the octave it is played in to accurately describe an individual key on the piano. Octave 3 Octave 4 Octave 5 1 C# D# F# G# A# C# D# F# G# A# C# D# F# G# A# CDEFGAB CD EGA B C D E FOAB in a song, this would be F#3 this is A4! this would be G5 Notice how the notes themselves are repeated several times. A standard piano has 7 full octaves (1 through 7), but the first key will actually start at AO, and the last key will be C8. Octaves 0 and 8 on the piano are therefore incomplete, they will only have the notes AO, A#O, BO, and C8. By pressing any of the keys on the piano, you are causing a small hammer to hit a string that will vibrate at a particular frequency. A note is octave 5 will have twice the frequency of the same note in octave 4, which is why the octave 5 note sounds higher pitched. You can compute the frequencies for each note by implementing the following formula: En = F x 2n/12 Fo is the frequency of base note, in other words the note that you are using to tune all of the other notes. It is standard to tune your piano so that the note A4 is exactly 440Hz, but the function you write to generate the frequencies of the notes will use any arbitrary starting frequency. n is the number of half steps you are away from the base note (A4): • n >O means you are on a note above the base note O A#4 is one half step above A4, so n=1 o B4 is two half steps above A4, so n=2 o C#5 is four half steps above A4, so n=4 o A5 is twelve half steps above A4, so n=12 n<0 means you are on a note below the base note o G#4 is one half step below A4, so n = -1 o G4 is two half steps below A4, so n = -2 o A3 is twelve half steps below A4, so n = -12 o F3 is sixteen half steps below A4, so n = -16 Note Types There are few different speeds at which a note can be played in a piece of music. We generally think of this in terms of ‘beats’ rather than seconds or minutes. A song will describe its overall tempo in beats per minute (BPM), where a higher BPM means a faster song. When reading music, the notes are organized in “measures.” The composer of the song would tell you how many beats should be in one measure. We will assume that all music used in this project uses 4 beats per measure. This makes computing the speeds easier. We have five different note types in this project. The name of the note type indicates how much of one measure that note should occupy: • “Quarter” notes take up a quarter of the measure, so for us that is exactly 1 beat • “Half” notes take up half of the measure, that would mean they last for 2 beats in our songs “Whole” notes take up the entire measure, so they require 4 beats to play correctly • “Eighth” notes take up an eighth of the measure, so that would be half of one beat • “Sixteenth” notes take up a sixteenth of a measure, so that would be a quarter of one beat Song Files Files that contain songs in this project are denoted by the extension “song”, and are organized like this: Song Tempo InBPM BaseNote Frequency NoteLetterAndoctave, NoteType NoteLetterAndoctave, Note Type The first line of the file is the song’s tempo expressed in beats per minute. The second line of the file is frequency of the base note used to tune the rest of the notes. The rest of the lines contain the notes in the order that they should be played for a particular song. They are comma separated, where the first item is the note’s letter on the piano and octave that is should be played in. You can open up these files in any plain text editor, they use only ASCII characters. Restrictions • There is exactly one import statement allowed in this project: import random O You are not allowed to import anything else. You can only use randint() choice() and seed() from the random library • You are not allowed to use classes, exceptions, the with statement, and list/dictionary comprehensions. . From the built-in functions, you are allowed to use: o range(), len(), sorted(), int(), sum(), list(), float(), str() • From list methods, you are allowed to use: o append(), insert(), copy(), remove(), pop(), index() • From the dictionary methods, you are allowed to us: o get(), values(), keys(), items(), update() • For the file/string methods, you are allowed to use: o open(), strip(), split(), join() o read(), readline(), readlines (), write(), writelines() • You may not make more than one pass of a file when reading it. Do not circumvent this rule by reading in the file as one entity (one string, a list, etc.), and then performing multiple passes on that. • Questions on Piazza like “Can I use [thing not listed above)?” will not be answered by an instructor. random_song(filename, tempo, tuning, num_measures) Description: Uses the random module to write song made of random notes to a file given the name of the song to write to, the desired tempo, and the desired tuning frequency, and the number of measures this song is. We want to make sure that the song you generate would be a valid song in terms of beats in measures, so you are going to pick random notes using the following algorithm: 1. Loop as long as you have not completed the number of measures that is being asked for, then build the next measure by doing the following: a. Pick a random valid index in this exact list: [“Sixteenth”, “Eighth”, ‘Quarter’, ‘Half’, ‘Whole’] b. If this note type at the index that you pick can fit in this measure (the number of beats it requires is less than or equal to the number of beats left in the measure) then: i. Select a random index in this exact list: [“C”, “C#”,”D”, “D#”, “E”, “F”,”F#”,”G”, “G#”,”A”, “A#”, “B”] ii. Select a random octave from 1 to 7 (we’re ignoring the incomplete octaves 0 and 8) iii. join the note type, note, and octave together, and write this as a line in the output file C. If the note duration selected doesn’t fit in the measure, throw away this note and try again in the next iteration of the loop, to pick a new random index to try until a note type that fits is selected. Parameters: filename (string) the name of the output file to write to, tempo (int) the BPM of the song, tuning (float) the base frequency to use for the note A4, and num_measures (int) the number of measure of song to write Return value: None (all output goes to a file) Example: The output is random, so for examples of this it is best to check the provided files. Random module: you should import the random module: import random Review these methods (test them by themselves, perhaps in the interpreter!) as options for selecting values (see the docs here: https://docs.python.org/3/library/random.html ) • random.randint(low_inclusive, high_inclusive), useful for selecting a number in your indicated range with equal probability. Note that the upper and lower values are inclusive. • random.choice(sequence), which selects an item from a sequence with equal probability and returns it. • random.seed (int), which the tester uses to “lock in the sequence that this pseudo-random number generator will generate. Note that the exact number of calls to the random module also affect what values are chosen, so read the directions carefully! It should exactly clarify how often, and in what order, to make calls to the random module. When running your own individual tests you must make sure that your seed value matches the seed value of the test you’re using, otherwise you will never be able to match the nutnut and you’ll be sad Show transcribed image text
A standard piano has 88 keys that just repeat this pattern of notes over and over again. As you travel to the right the notes get higher, and they get lower as your travel to the left. The notes that are in higher octaves sound higher pitched, and the lower octaves sound lower pitched. You will use a combination of the note’s letter and the octave it is played in to accurately describe an individual key on the piano. Octave 3 Octave 4 Octave 5 1 C# D# F# G# A# C# D# F# G# A# C# D# F# G# A# CDEFGAB CD EGA B C D E FOAB in a song, this would be F#3 this is A4! this would be G5 Notice how the notes themselves are repeated several times. A standard piano has 7 full octaves (1 through 7), but the first key will actually start at AO, and the last key will be C8. Octaves 0 and 8 on the piano are therefore incomplete, they will only have the notes AO, A#O, BO, and C8. By pressing any of the keys on the piano, you are causing a small hammer to hit a string that will vibrate at a particular frequency. A note is octave 5 will have twice the frequency of the same note in octave 4, which is why the octave 5 note sounds higher pitched. You can compute the frequencies for each note by implementing the following formula: En = F x 2n/12 Fo is the frequency of base note, in other words the note that you are using to tune all of the other notes. It is standard to tune your piano so that the note A4 is exactly 440Hz, but the function you write to generate the frequencies of the notes will use any arbitrary starting frequency. n is the number of half steps you are away from the base note (A4): • n >O means you are on a note above the base note O A#4 is one half step above A4, so n=1 o B4 is two half steps above A4, so n=2 o C#5 is four half steps above A4, so n=4 o A5 is twelve half steps above A4, so n=12 n
Expert Answer
Answer to A standard piano has 88 keys that just repeat this pattern of notes over and over again. As you travel to the right the …