Session 18 The physics of sound and the manipulation of digital sounds Pictures vs. Sounds • Get in groups of 2 or 3 students. • Create a list (to be turned in) of all the modifications that we made to pictures. • Which of these modifications do you think have similar techniques with sound? How does Hearing Work? • The outer ear “catches” sounds • The eardrum vibrates • The inner ear translates the vibrations to nerve impulses for the brain to interpret Acoustics, the physics of sound • Sounds are waves of air pressure – Sound comes in cycles – The frequency of a wave is the number of cycles per second (cps), or Hertz • (Complex sounds have more than one frequency in them.) – The amplitude is the maximum height of the wave Volume and Pitch • We perceive volume as changes in amplitude – If the amplitude doubles, it’s about a 3 decibel (dB) change. – As an absolute measure, it’s in comparison to threshold of audibility • 0 dB can’t be heard. • Normal speech is 60 dB. • A shout is about 80 dB • We perceive pitch as changes in frequency – Higher frequencies are perceived as higher pitches – We can hear between 5 Hz and 20,000 Hz (20 kHz) Logarithmic Scales • Human hearing works with ratios not differences: – The A above middle C is 440 Hz – The A above that has twice the frequency (880 Hz) – The A above that has twice the frequency of that (1760 Hz) Digitizing Sound • We can estimate a curve by creating rectangles • We’ll do the same to estimate the sound curve – Analog-to-digital conversion (ADC) will give us the amplitude at an instant as a number: a sample – How many samples do we need? Nyquist Theorem • We need twice as many samples as the maximum frequency in order to represent (and recreate, later) the original sound. • The number of samples recorded per second is the sampling rate – If we capture 8000 samples per second, the highest frequency we can capture is 4000 Hz (where human voices max out) • That’s how phones work – If we capture more than 44,000 samples per second, we capture everything that we can hear (max 22,000 Hz) • CD quality is 44,100 samples per second Encoding a Sound 1-D Array of samples Each sample = 2 bytes or 16-bits 0 1 2 3 4 getLength(sound)-1 . . . 16-bits allows for 2 16 = 65,536 combinations For signed integers, split roughly in half: - 215 = -32,768 +215 -1 = +32,767 (zero takes away one) Given this information • How much memory is necessary to store 1 minute of CD quality stereo sound? Sound Basics makeSound(fileName) – Will create a new Sound object from the data in the file with the passed file name play(soundObj) – Will start the sound playing. Let’s you repeat immediately blockingPlay(soundObj) – Will play the complete sound before continuing openSoundTool(soundObj) – Will open a sound tool on the object (can do this from the menu too) The Sound Tool • Not all of the sound is shown when you explore a sound – Skips values to fit in the window • You can zoom in – To see all sample values • You can zoom out – To fit the sound in the window again Increase volume def increaseVolume(sound): for sample in getSamples(sound): value = getSampleValue(sample) setSampleValue(sample,value*2) More General Code def changeVolume(sound, factor): """ Changes the sound volume by a given factor with factor < 1 decreasing and factor > 1 increasing the volume """ for sampleNum in range(0, getLength(sound)): sample = getSampleObjectAt(sound, sampleNum) value = getSampleValue(sample) setSampleValue(sample, value * factor) Main Program """ Select a sound and repeatedly adjust its volume. """ def main(): print "Select the Media Folder" setMediaFolder() print "Select the sound (.wav) file to play repeatedly" fileName = pickAFile() sound = makeSound(fileName) volumeAdjustment = requestNumber("Enter factor to increase (>1) “ + \ “or decrease (<1) the volume.") for counter in range(10): blockingPlay(sound) changeVolume(sound, volumeAdjustment) print "Open Sound Tool to 'view' sound" openSoundTool(sound) But… • What happens if we increaseVolume() too many times? – Clipping