Question: Answer in Python : Write a function named mix_sounds. This function will have two parameters, snd1 and snd2, which are two Sound objects. It will
Answer in Python: Write a function named mix_sounds. This function will have two parameters, snd1 and snd2, which are two Sound objects. It will mix them into a new Sound object that is then returned. Neither of the original sounds should be modified in any way.
By mixing, we mean that the original sounds are played at the same time, so that each sound that is mixed is heard at the same time with the other sounds. For example, if we mix the three-note sound (cde.wav) and the sound of water bubbling (water.wav), we get this combination of notes and water (cde-water.wav). And if we mix the door slamming sound (door.wav) and the welcome sound (welcome.wav), we get this ominous door and welcome sound (door-welcome.wav).
The length of the resultant sound should be the length of the longer of the two input sounds, otherwise part of one or more sounds will be cut off! In our notes-and-water example, the three notes were longer than the bubbling water, so the length of the mixed sound was the same as the notes sound.
Hint: Use a conditional statement to determine which sound to assign the name shorter_sound and which one to assign the name longer_sound.
Mixing two (or more!) sounds involves adding corresponding samples together. For example, if one sound has three samples with values: 2, 4, and 6; and another sound has four samples: 10, 11, 12, and 13; mixing them yields a sound of four samples: 12, 15, 18, and 13.
Note that if we try to mix two loud sounds together the resultant, mixed sound will sound distorted. This phenomenon is called clipping, which you experienced during one of the earlier labs, where we increased the volume of the love.wav song and it made it sound static-y. The reason we get clipping is that we use only 16 bits of data for each sample. Once we try to store samples that fall outside the range representable by 16 bits, we cannot properly store them and, hence, get distortion.
3.1 Testing mix_sounds
For this problem, you will need to test using the REPL. The following gives an example of how you could mix together the water.wav and cde.wav WAV files.
import song_generator import sound water = sound.load_sound("water.wav") cde = sound.load_sound("cde.wav") mixed = song_generator.mix_sounds(water, cde) mixed.play() Recall that if you modify your song_generator.py, you should exit your REPL (using exit()) then restart it.
As a minimum, you should test your mix_sounds on these as well as the welcome.wav and door.wav files.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
