Download presentation
Presentation is loading. Please wait.
Published byVivian Floyd Modified over 9 years ago
1
COSC 1P02 Introduction to Computer Science 5.1 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."
2
COSC 1P02 Introduction to Computer Science 5.2 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
3
COSC 1P02 Introduction to Computer Science 5.3 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)
6
COSC 1P02 Introduction to Computer Science 5.6 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
8
COSC 1P02 Introduction to Computer Science 5.8 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
9
COSC 1P02 Introduction to Computer Science 5.9 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
10
COSC 1P02 Introduction to Computer Science 5.10 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() )
11
COSC 1P02 Introduction to Computer Science 5.11 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)
12
COSC 1P02 Introduction to Computer Science 5.12 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, …
13
COSC 1P02 Introduction to Computer Science 5.13
14
COSC 1P02 Introduction to Computer Science 5.14 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
15
COSC 1P02 Introduction to Computer Science 5.15 SoundPlayer Methods
16
COSC 1P02 Introduction to Computer Science 5.16 Sound Methods
17
COSC 1P02 Introduction to Computer Science 5.17 Sample Methods
18
COSC 1P02 Introduction to Computer Science 5.18 The end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.