COSC 1 P 02 Cosc 1 P 02

  • Slides: 13
Download presentation
COSC 1 P 02 Cosc 1 P 02 Week 4 Lecture slides “Programs are

COSC 1 P 02 Cosc 1 P 02 Week 4 Lecture slides “Programs are meant to be read by humans and only incidentally for computers to execute. ” - Donald Knuth Introduction to Computer Science 4. 1

COSC 1 P 02 Sound · Waves of air pressure - increases in pressure

COSC 1 P 02 Sound · Waves of air pressure - increases in pressure (wave) called compressions - decreases in pressure (toughs) called rarefactions · Sound perceived based on shape, frequency and amplitude of wave · Sine wave - simplest sound (regular) - cycle - amplitude - Frequency Introduction to Computer Science 4. 2

COSC 1 P 02 Volume · we hear changes in intensity (amplitude) · Change

COSC 1 P 02 Volume · we hear changes in intensity (amplitude) · Change (ratio) measured in decibels (d. B) · Commonly volume expressed in d. B as ratio to threshold of audibility (0 d. B SPL) - normal speech: 60 d. B SPL - shouting: 80 db SPL - Based on a logarithmic scale ° 10 db is 10 x louder then 0 (silence) ° 20 db is 100 x louder then 0 (silence) ° 30 db is 1000 x louder etc…. Introduction to Computer Science 4. 3

COSC 1 P 02 Pitch/Tone · All sounds are periodic - have cycles ·

COSC 1 P 02 Pitch/Tone · All sounds are periodic - have cycles · Frequency is the number of cycles per second (measures in Hertz (Hz)) - the A above middle C is 440 Hz - we can hear from 2 Hz to 22, 000 Hz (or 22 k. Hz) · we hear pitch as the change in frequency - in standard tuning ratio of notes in neighboring octaves is 2: 1 (e. g. 440 Hz : 880 Hz) - ratio of notes in different octaves remains constant Introduction to Computer Science 4. 4

COSC 1 P 02 Shape · Different sources of sound - sound different even

COSC 1 P 02 Shape · Different sources of sound - sound different even when playing the same note - e. g. flute vs piano · Sounds seldom have a single frequency - overtones are other frequencies in the sound that are at lower amplitude - The central tone is called the fundamental tone · Sounds also vary by attack, fade and other characteristics Introduction to Computer Science 4. 5

COSC 1 P 02 Exploring Sounds · Sound. Inspector tool - Java program built

COSC 1 P 02 Exploring Sounds · Sound. Inspector tool - Java program built using the Basic. IO & Media libraries - draws sound wave and allows zooming in on parts of wave · E. g - c 4. wav – sine wave - middle C - e 4. wav – sine wave – E above C - bassoon-c 4. wav – bassoon playing middle C - rabbit. wav - thisisatest. wav Introduction to Computer Science 4. 6

COSC 1 P 02 Encoding (Digitizing) Sounds · Microphone measures air (sound) pressure -

COSC 1 P 02 Encoding (Digitizing) Sounds · Microphone measures air (sound) pressure - record voltage (analog) as a number (digital) – analog-todigital conversion (ADC) · Sound is change of air pressure over time - record samples of air pressure over time · Number of samples? - depends on highest frequency to capture - Nyquist’s theorem – to capture frequencies up to n Hz must have 2 n samples per second ° to handle 22, 000 Hz need 44, 000 samples per second ° CDs are sampled at 44, 100 samples per second · Sample size - CDs use 2 bytes (16 bits) per sample ° 65, 536 (216) possible values (-32, 768 to 32, 767) · Note: 80 min @ 44, 100 samples per second at 2 bytes per sample x 2 channels (stereo) = 846, 720, 000 bytes or approximately 800 Mb Introduction to Computer Science 4. 7

COSC 1 P 02 Sound & Sound. Player · Sound - class in the

COSC 1 P 02 Sound & Sound. Player · Sound - class in the Media library - provides access to sounds - sounds can be loaded from files (. wav) · Sound. Player - class in the Basic. IO library - provides a window with a button to play a sound - Sound objects placed on Sound. Player · Example - Load and play a sound Introduction to Computer Science 4. 8

COSC 1 P 02 Making a Sound Louder · Increase amplitude of each sample

COSC 1 P 02 Making a Sound Louder · Increase amplitude of each sample - multiply each by some factor (parameter of a method) · Example · Sequence through all samples in sound - Sample class - for each - executes body once for each Sample in the Sound ° index variable (now a Sample) is each Sample in succession · Saving the result · Did it work? Introduction to Computer Science 4. 9

COSC 1 P 02 Generating Clipping · Clipping occurs when the sample value exceeds

COSC 1 P 02 Generating Clipping · Clipping occurs when the sample value exceeds the maximum sample size (e. g. greater than 32, 767 or less than -32, 768 for a 2 byte sample). - sample value when clipped will be the maximum value (32, 767 or -32, 768) · If keep increasing volume, clipping occurs · What happens if all samples clipped? · Example - program to set all samples to maximum value · What does signal look like? · Note: we can still understand it! · if statement - choice between executing two alternatives ° to handle maximum positive and maximum negative Introduction to Computer Science 4. 10

COSC 1 P 02 If Statement · Syntax · If condition is true, the

COSC 1 P 02 If Statement · Syntax · If condition is true, the first set of statements (then part) is executed, otherwise the second set of statements (else part) is executed · Condition - an expression that is either true or false - relational operators - note: also used in for statement (second part) Introduction to Computer Science 4. 11

COSC 1 P 02 Normalizing a Sound · What if we want to make

COSC 1 P 02 Normalizing a Sound · What if we want to make a sound as loud as we can without causing clipping? · Need to know the largest (absolute value) amplitude (sample) · Can scale each sample by the ratio of this largest value to the maximum sample size (32, 767) · Finding maximum - sequence through all samples and if find one that is bigger than the biggest so far, change our guess of what the biggest value is - work with absolute value (i. e. don’t care if positive or negative) - need a starting guess ° since looking at absolute value, no sample can be smaller than 0 so 0 can be starting guess · Example · Note: need two loops - one to find maximum - one to scale samples Introduction to Computer Science 4. 12

COSC 1 P 02 The end Introduction to Computer Science 4. 13

COSC 1 P 02 The end Introduction to Computer Science 4. 13