Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1P02 Introduction to Computer Science 4.1 Cosc 1P02 Week 4 Lecture slides “Programs are meant to be read by humans and only incidentally for computers.

Similar presentations


Presentation on theme: "COSC 1P02 Introduction to Computer Science 4.1 Cosc 1P02 Week 4 Lecture slides “Programs are meant to be read by humans and only incidentally for computers."— Presentation transcript:

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

2 COSC 1P02 Introduction to Computer Science 4.2 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

3 COSC 1P02 Introduction to Computer Science 4.3 Volume  we hear changes in intensity (amplitude)  Change (ratio) measured in decibels (dB)  Commonly volume expressed in dB as ratio to threshold of audibility (0 dB SPL)  normal speech: 60 dB SPL  shouting: 80 db SPL  Based on a logarithmic scale  10 db is 10x louder then 0 (silence)  20 db is 100x louder then 0 (silence)  30 db is 1000x louder etc….

4 COSC 1P02 Introduction to Computer Science 4.4 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 440Hz  we can hear from 2Hz to 22,000 Hz (or 22 kHz)  we hear pitch as the change in frequency  in standard tuning ratio of notes in neighboring octaves is 2:1 (e.g. 440Hz : 880Hz)  ratio of notes in different octaves remains constant

5 COSC 1P02 Introduction to Computer Science 4.5 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

6 COSC 1P02 Introduction to Computer Science 4.6 Exploring Sounds  SoundInspector tool  Java program built using the BasicIO & Media libraries  draws sound wave and allows zooming in on parts of wave  E.g  c4.wav – sine wave - middle C  e4.wav – sine wave – E above C  bassoon-c4.wav – bassoon playing middle C  rabbit.wav  thisisatest.wav

7 COSC 1P02 Introduction to Computer Science 4.7 Encoding (Digitizing) Sounds  Microphone measures air (sound) pressure  record voltage (analog) as a number (digital) – analog-to- digital 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 2n 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 (2 16 ) 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

8 COSC 1P02 Introduction to Computer Science 4.8 Sound & SoundPlayer  Sound  class in the Media library  provides access to sounds  sounds can be loaded from files (.wav )  SoundPlayer  class in the BasicIO library  provides a window with a button to play a sound  Sound objects placed on SoundPlayer  Example  Load and play a sound

9 COSC 1P02 Introduction to Computer Science 4.9 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?

10 COSC 1P02 Introduction to Computer Science 4.10 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

11 COSC 1P02 Introduction to Computer Science 4.11 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)

12 COSC 1P02 Introduction to Computer Science 4.12 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

13 COSC 1P02 Introduction to Computer Science 4.13 The end


Download ppt "COSC 1P02 Introduction to Computer Science 4.1 Cosc 1P02 Week 4 Lecture slides “Programs are meant to be read by humans and only incidentally for computers."

Similar presentations


Ads by Google