CPSC1301 Computer Science 1 - PowerPoint PPT Presentation

About This Presentation
Title:

CPSC1301 Computer Science 1

Description:

CPSC1301 Computer Science 1 Chapter 10 Making Sounds by Combining Pieces part 1 – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 20
Provided by: Barbar368
Category:

less

Transcript and Presenter's Notes

Title: CPSC1301 Computer Science 1


1
CPSC1301Computer Science 1
  • Chapter 10
  • Making Sounds by Combining Piecespart 1

2
Learning Goals
  • Processing ranges of Sound values
  • Blending pictures
  • Changing the sound frequency
  • Computing concepts
  • Method overloading
  • Changing a for loop to increment by a value other
    than 1
  • Modifying a method to be more general

3
Blend Sounds
  • Like blending pictures we can blend two sounds
  • Copy the first 20,000 values of sound1
  • Copy from both by adding .5 sound1 value and .5
    sound2 value
  • Copy the next 20,000 values of sound 2

4
Blend Sounds Method
  • public static void blendSounds(Sound target)
  • Sound sound1
  • new Sound(FileChooser.getMediaPath("aah.wav
    "))
  • Sound sound2
  • new Sound(FileChooser.getMediaPath("bassoon
    -c4.wav"))
  • int value 0
  • // copy the first 20,000 samples from sound1
    into target
  • for (int index0 index lt 20000 index)
  • target.setSampleValueAt(index,
  • sound1.getSampleValueA
    t(index))

5
Blend Sounds - Continued
  • // copy the next 20,000 samples from sound1
    and blend that
  • // with the first 20,000 samples from sound2
  • for (int index 0 index lt 20000 index)
  • value (int) ((sound1.getSampleValueAt(inde
    x 20000)
  • 0.5)
  • (sound2.getSampleValueAt(inde
    x) 0.5))
  • target.setSampleValueAt(index
    20000,value)
  • // copy the next 20,000 samples from sound2
    into the target
  • for (int index20000 index lt 40000 index)
  • target.setSampleValueAt(index 20000,
  • sound2.getSampleValu
    eAt(index))

6
Testing Blend Sounds
  • public class SpliceSounds
  • public static void main (String args)
  • String fileName FileChooser.pickAFile()
    Sound target new Sound(fileName)
  • blendSounds(target)
  • target.play()
  • target.explore()

7
Modify Blend Sounds Exercise
  • Create another blendSounds method
  • That takes the file name of the sounds to blend
  • And a value to start the blend at and another to
    stop the blend at
  • Modify the original blendSounds method to call
    this one

8
Overloading Methods
  • You can have several methods with the same name
  • As long as the parameter list is different
  • In number of parameters
  • And/or types of parameters
  • blendSounds()
  • blendSound(String name1, String name2, int
    startBlend, int endBlend)

9
Changing the Sound Frequency
  • The frequency of a wave is the number of cycles
    per second (cps), or Hertz (Hz)
  • (Complex sounds have more than one frequency in
    them.)
  • Our perception of pitch is related
    (logarithmically) to changes in frequency
  • Higher frequencies are perceived as higher
    pitches
  • We can hear between 5 Hz and 20,000 Hz (20 kHz)
  • A above middle C is 440 Hz

10
Double the Frequency
  • If we take every other sample we double the
    frequency of the sound
  • Completes two cycles instead of one in the same
    time
  • It will sound higher

100 200 300 400 500
100 300 500 0 0
11
Double Frequency Method
  • public static void doubleFreq(Sound target)
  • // make a copy of the original sound
  • Sound s new Sound(target.getFileName())
  • / loop and increment target index
  • by one but source index by 2,
  • and set target value
  • to the copy of the original sound
  • /

12
Double Frequency - Continued
  • for (int sourceIndex0, targetIndex 0
  • sourceIndex lt target.getLength()
  • sourceIndexsourceIndex2,
    targetIndex)
  • target.setSampleValueAt(targetIndex,
  • s.getSampleValueAt(sou
    rceIndex))
  • // clear out the rest of this sound to
    silence (0)
  • for (int i target.getLength() / 2
  • i lt target.getLength()
  • i)
  • target.setSampleValueAt(i,0)

13
Test Double Frequency
  • public class SpliceSounds
  • public static void main (String args)
  • String fileName FileChooser.pickAFile()
  • Sound target new Sound(fileName)
  • doubleFrequency(target)
  • target.play()
  • target.explore()

14
Challenge
  • Create a method that will take every third sample
  • Will this sound higher or lower?
  • Can you make this method more general?
  • By passing in the amount to add to the source
    index each time?

15
Halving the Frequency
  • We can copy each source value twice to half the
    frequency
  • Only get through half a cycle in the same time we
    used to get through a full cycle
  • It will sound lower
  • This is the same algorithm that we used to scale
    up a picture

100 200 300 400 500
100 100 200 200 300
16
Halve Frequency Method
  • public void halveFreq(Sound target)
  • // make a copy of the original sound
  • Sound s new Sound(target.getFileName())
  • / loop through the sound and increment
    target index
  • by one but source index by 0.5 and set
    target value
  • to the copy of the original sound
  • /
  • for (double sourceIndex0, targetIndex 0
  • targetIndex lt target.getLength()
  • sourceIndexsourceIndex0.5,
    targetIndex)
  • target.setSampleValueAt((int) targetIndex,
  • s.getSampleValueAt((int)
    sourceIndex))

17
Testing Halve Frequency
  • public class SpliceSounds
  • public static void main (String args)
  • String fileName FileChooser.pickAFile()
  • Sound target new Sound(fileName)
  • halveFreq (target)
  • target.play()
  • target.explore()

18
Change Frequency Exercise
  • Write a method that will copy each sound value 4
    times to the target
  • Will the new sound be higher or lower?
  • Can you make this more general?
  • By passing in the number of times to copy the
    source value
  • Try it with 3 times and check the index values to
    make sure that you are doing it right

19
Summary
  • You can have more than one method with the same
    name method overloading
  • As long as the parameter list is different
  • Different number of parameters
  • Different types of parameters
  • You can increment or decrement loop variables by
    numbers other than 1
  • You can make methods more general
  • By passing in parameters
  • Some algorithms are the same for pictures and
    sounds
  • Scaling a picture down and doubling the frequency
    of a sound
Write a Comment
User Comments (0)
About PowerShow.com