CSCA08H3 Midterm: CSCA08H3 Midterm 2011 Fall Solutions

84 views6 pages
16 Oct 2018
School
Course
Professor
CSC A08 F Midterm Test ā€” Solutions Fall 2011
The next two questions involve the sound module that we used in the ļ¬rst assignment. At the end of the
test, youā€™ll ļ¬nd a list of the sound functions we learned about. Note that you may not need to use all of
them.
You may assume that the sound module has already been imported use the line import sound.
Question 1. [6 marks]
For each part of this question, you need to write a few lines of code. For all three parts, assume that the
variable snd has already been deļ¬ned and refers to a Sound object.
Part (a) [2 marks] Calling functions
Write code to create a silent Sound of the same length as snd and assign it to the variable silent snd.
silent_snd = sound.create_sound(len(snd))
Part (b) [2 marks] Looping over a Sound
Write code that prints the value of the left channel of each Sample in snd, each value printed on its own
line.
for samp in snd:
print sound.get_left(samp)
or
for samp in snd:
left = sound.get_left(samp)
print left
Part (c) [2 marks] Conditionals in Loops
Write code that prints the index of each Sample in snd for which the left and right channels are equal,
each value printed on its own line.
for samp in snd:
if sound.get_left(samp) == sound.get_right(samp):
print sound.get_index(samp)
or
for samp in snd:
left = sound.get_left(samp)
right = sound.get_right(samp)
if left == right:
index = sound.get_index(samp)
print index
Page 1 of 6 contā€™d. . .
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
CSC A08 F Midterm Test ā€” Solutions Fall 2011
Question 2. [5 marks]
Take a look at the following function deļ¬nition for triple. The triple function will run without error,
but it does not do what the docstring says.
def triple(snd)
ā€™ā€™ā€™Return a copy of Sound snd with the left and right channels tripled.
The original snd is unchanged.ā€™ā€™ā€™
new snd = sound.copy(snd) # new line
for samp in snd n
ĀÆew snd: # edited line
left = sound.get_left(samp)
right = sound.get_right(samp)
sound.set_left(samp, left * 3)
sound.set_right(samp, right * 3)
return snd new snd: # edited line
Part (a) [1 mark] Explain, in English, what is wrong with the triple function as written.
The original version of the function modiļ¬es the parameter snd instead of making
a copy and returning a new sound.
Part (b) [2 marks] In the space above, edit triple so that it performs as the docstring describes. You
may need to add additional lines or code and/or edit existing ones.
Part (c) [2 marks] Write the lines of code that will open a ļ¬le called ā€™song.wavā€™, call triple (assuming
the errors have been ļ¬xed), and then play the new song (which will be three times as loud as the original).
sound.play(triple(sound.load_sound(ā€™song.wavā€™)))
or
snd = sound.load_sound(ā€™song.wavā€™)
new_snd = triple(snd)
sound.play(new_snd)
Page 2 of 6 contā€™d. . .
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

Document Summary

The next two questions involve the sound module that we used in the rst assignment. At the end of the test, you"ll nd a list of the sound functions we learned about. Note that you may not need to use all of them. You may assume that the sound module has already been imported use the line import sound. For each part of this question, you need to write a few lines of code. For all three parts, assume that the variable snd has already been de ned and refers to a sound object. Write code to create a silent sound of the same length as snd and assign it to the variable silent snd. Write code that prints the value of the left channel of each sample in snd, each value printed on its own line. for samp in snd: print sound. get_left(samp) or for samp in snd: left = sound. get_left(samp) print left.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers

Related Documents

Related Questions