Download Week5 - Brock Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Sound recording and reproduction wikipedia , lookup

Sound reinforcement system wikipedia , lookup

Music technology (electronic and digital) wikipedia , lookup

Public address system wikipedia , lookup

Transcript
COSC 1P02
Cosc 1P02
Week 5 Lecture slides
Psychiatrist to patient "You have nothing to worry
about - anyone who can pay my bill is certainly not
a failure."
Introduction to Computer Science
5.1
COSC 1P02
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
5.2
COSC 1P02
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
5.3
COSC 1P02
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
5.6
COSC 1P02
Mirroring A Sound
 Mirror around mid-point
 odd vs even number of samples
 Copying sample values
 only half copied
 can’t use for-each
 Indexing a collection
 aSound.getSample(i)
 returns Sample at position (index) i in sound
 positions start at 0 (last position numSamples-1)
 Example
 mid point
 uses counting for
 from position & to position
 for each vs for & indexing
Introduction to Computer Science
5.8
COSC 1P02
Reversing a Sound
 Produce a new sound which is the old sound in reverse order
 like mirror except all samples reproduced in reverse
order
 create a new Sound object
 Example
 Sound objects now declared in method
 more than one sound involved
 must be passed as parameter (scope)
 Creating a new sound with same attributes
 Sound constructor
 Cascading method calls
 aSound.getSample(i) returns a Sample object
 this object can perform the getValue() method
 saves declaring a variable that isn’t going to be used except to
make next call
Introduction to Computer Science
5.9
COSC 1P02
Function Methods
 Methods can produce a result
 value (e.g. sin())
 object (e.g. getSample())
 Method declaration
 void – procedure method
 no result
 executed for its effect
 type – function method
 result is of specified type
 executed for result
 Method call
 procedure method
 called as a statement (e.g. close())
 function method
 called as an expression (e.g. sin())
Introduction to Computer Science
5.10
COSC 1P02
 Method execution
 value of argument computed
 calling code suspended
 value of argument copied to parameter
 body of method executed
 functions: return “value” computed
 calling code continues (functions: with return “value”)
 Return statement
 syntax
 execution
 expression computed
 method terminates (returns)
Introduction to Computer Science
5.11
COSC 1P02
Changing the Frequency of a Sound
 Change pitch by changing frequency
 e.g. A above middle C is 440Hz, next A is 880Hz
 To double frequency must halve cycle length
 if sampling rate is constant, there will be half as many
samples in the higher frequency version of the same sound
 Select every other sample to create a new sound
 same rough shape but compressed (higher frequency)
 new sound will be half as long (half as many samples)
 Example
 new sound has half the samples
 need a value for each sample in result (for-each)
 every other sample of old sound
 pos has values: 0, 2, 4, …
Introduction to Computer Science
5.12
COSC 1P02
Combining Sounds
 When two sounds occur together, their sound waves merge
 e.g. instruments in an orchestra
 merging is just addition of the samples at the same point in
time
 What if one sound is longer than the other
 merging occurs over duration of shorter sound
 result has tail end of longer sound
 Example
 determine longer & shorter sound
 result has length of longer sound
 merge as many samples as in shorter sound
 to maintain volume, use half the amplitude of each sample
 copy tail of longer sound
 Note: for loop index doesn’t have to start at 0 or 1
Introduction to Computer Science
5.14
COSC 1P02
SoundPlayer Methods
method
close()
placeSound(sound)
waitForUser()
meaning
wait until user presses Close button and
close window
make sound playable when pressing the Play
button
Wait until user presses OK before continuing
Introduction to Computer Science
5.15
COSC 1P02
Sound Methods
method
o = new Sound()
o = new Sound(nSamp)
o = new Sound(nSamp,sound)
i = getNumSamples()
s = getSample(pos)
i = getSampleRate()
save()
meaning
constructor: creates a sound object loading samples from a
file selected via a file open dialog
constructor: creates a sound object with nSamp samples
all 0 (silence).
constructor: creates a sound object with nSamp samples
all 0 (silence) and attributes of sound.
returns number of samples in the sound
returns the Sample at position pos in the sound
returns to sampling rate of the sound (samples/second)
present file save dialog to allow user to save sound as
modified
Introduction to Computer Science
5.16
COSC 1P02
Sample Methods
method
i = getValue()
setValue(amp)
meaning
returns the value (amplitude) of the sample
changes the value of the sample to amp
Introduction to Computer Science
5.17
COSC 1P02
The end
Introduction to Computer Science
5.18