How to write Python code for Sound:
Fade-out
def fade_out (snd, fade_length):
This function again takes a sound object and an integer indicating the length of the fade. However, this time, the fade is a fade-out (from loud to quiet), and the fade-out begins fade_length samples from the end of the sound rather than from the beginning. For example, if fade_length is 88200 and the length of the sound is samp samples, the fade-out should only affect samples numbered samp-88200 up to samp-1.
Let's use a raining sound to demonstrate. As with the water bubbling above, The volume is stable throughout. Now, with the call fade_out (rain, 88200) (where rain is a sound object loaded with the rain sound), we get rain with a short fade-out. The first few seconds of the rain are as before. Then, two seconds before the end, the fade-out starts, with the sound progressing toward zero volume. The final sample of the sound has value 0.
The multiplicative factors for fade_out are the same as for fade_in, but are applied in the reverse order. For example, if fade_length were 4, the channels of the fourth-last sample would be multiplied by 0.75, the channels of the third-last sample would be multiplied by 0.5, the channels of the second-last sample would be multiplied by 0.25, and the channels of the final sample in the sound would be multiplied by 0.0.