Top Banner
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
12

Audio Manipulation in Python

Feb 09, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Audio Manipulation in Python

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

Page 2: Audio Manipulation 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?

Page 3: Audio Manipulation in Python

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!

Page 4: Audio Manipulation in Python

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…

Page 5: Audio Manipulation in Python

5

9

Reading a WAV File

JES built-in function:

Example:

10

JES Sound Explorer

explore(sound) -- bring up sound explorer

Page 6: Audio Manipulation in Python

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

Page 7: Audio Manipulation in Python

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)

Page 8: Audio Manipulation in Python

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…

Page 9: Audio Manipulation in Python

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

Page 10: Audio Manipulation in Python

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)

Page 11: Audio Manipulation in 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”)

Page 12: Audio Manipulation in Python

12

23

JES does MIDI music too