Overview/Questions - Computer Science –Review: ... Create an empty sound file Saving a WAV file. 7...

12
1 1 Aaron Stevens 11, 13 April 2011 CS101 Lecture 28: Python: Computing with Audio Some material copyright Pearson (Guzdial and Ericson) 2 Overview/Questions – Review: audio samples, sampling rate – How can we create/play WAV audio files? – Programming with sound in Python

Transcript of Overview/Questions - Computer Science –Review: ... Create an empty sound file Saving a WAV file. 7...

1

1

Aaron Stevens11, 13 April 2011

CS101 Lecture 28:Python: Computing with Audio

Some material copyright Pearson (Guzdial and Ericson)

2

Overview/Questions

– Review: audio samples, sampling rate– How can we create/play WAV audio files?– Programming with sound in Python

2

33

Review: Sound Wave Properties

Wavelength:distance between waves (affectspitch -- high or low sounds)

Amplitude:strength of power of waves (volume)

Frequency:the number of times a wave occursin a second.

4

Thinking in waveforms

– What kind of waveform would a high pitched/lowpitched sound look like?

– What kind of waveform would silence look like?

– How would we combine sounds, e.g. similar to animage overlay?

3

55

Sampling: 3-bit depth

For each sample, we need to select a discrete value for the amplitude.These values are recorded in 3 bits (right hand side).

66

From Sample to Sound Wave

Using the recorded information, the computer must re-recreate thesound wave. Some of the original information was lost by thesampling process!

4

7

Waveform Audio File Format(.WAV Files)These files store a bitstream of the audio

samples:• compatible with Window, MAC, Linux• typically uncompressed

– What are the benefits of an uncompressedformat?

– What are the drawbacks?

8

Recording a .WAV file.Example: using Audacity to record a .WAVfile.

Recall: a speaker has an electromagnet,just like a microphone…

5

9

Reading a WAV File

JES built-in function:

Example:

10

JES Sound Explorer

explore(sound) -- bring up sound explorer

6

11

How Sampling Works:

The sampling rate describes howmany samples per unit of time.For example, 1000 samples persecond.• Each sample has a timestamp and a

value• The value describes the amplitude of the

sound wave at one point in time

12

Programming with Audio

Reading and playing a WAV FileGet/set individual samplesCreate an empty sound fileSaving a WAV file

7

13

Get Individual Samples

JES built-in function:

Example:value = getSample(sound, 1)

14

Setting Sample ValuesJES built-in function:

Example:sound = makeEmptySound(3000, 1000)setSampleValueAt(sound, 0, 0)setSampleValueAt(sound, 1, 2)setSampleValueAt(sound, 2, -3)setSampleValueAt(sound, 3, 3)setSampleValueAt(sound, 4, -2)setSampleValueAt(sound, 5, 0)

8

15

Make it louder!We can increase volume by increasing the amplitude of eachsample.(use example voice.wav)

16

The range() functionWhat if we want to modify only some of the samples in a sound,

but not all of them?range() gives us a list of numbers, which we can use as

indexes into a list of samples.

range(<start>,<stop>,<increment>)Show some examples of range…

9

17

SILENCE!How can we create some silence in this sound?(use example back-in-black-mono.wav)

18

Combining Sounds

We combinesounds by “adding”the amplitudes ofthe correspondingsamples.

Example:Red + Yellow = Orange

10

19

What You Learned Today

– Choosing the sampling rate– Reading & writing WAV files– Sound objects– Getting & setting sample values in JES

20

Announcements and To Do

– Readings:• The CS101 Guide to Python/JES

– http://www.cs.bu.edu/courses/cs101b1/jes/• Optional (free) book about Python:

How to Think Like A Computer Scientist: Learning with PythonAvailable online at: http://openbookproject.net//thinkCSpy/

– HW11 is due Wednesday 4/13– Quiz 5 on FRI 4/15

• Covers lectures 25-26-27-28 (python)

11

21

Creating an Empty Sound

JES built-in function:

Example:sound = makeEmptySound(3000, 1000)

This creates an empty sound with a length of 3000samples, and a sampling rate of 1000 samples persecond. The sound is 3 second long.

22

Writing a WAV File

JES built-in function:

Example:writeSoundTo(mySound, “Z:/mySound.wav”)

12

23

JES does MIDI music too