Stretching and Compressing Sound - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Stretching and Compressing Sound

Description:

We will take Exam #2 next Friday (April 13th) It will cover material through chapter 10. ... get through half a cycle in the same time we used to get ... – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 14
Provided by: BenSc
Category:

less

Transcript and Presenter's Notes

Title: Stretching and Compressing Sound


1
Session 31
  • Stretching and Compressing Sound
  • (Changing Pitch)

2
Quick Announcements
  • This is all we are going to do with Sound. There
    are more things we could look at, but we need to
    move on.
  • We will take Exam 2 next Friday (April 13th)
  • It will cover material through chapter 10.

3
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

4
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
5
Double Frequency Method
  • public void doubleFreq()
  • // make a copy of the original sound
  • Sound s new Sound(this.getFileName())
  • / loop and increment target index
  • by one but source index by 2,
  • and set target value
  • to the copy of the original sound
  • /

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

7
Test Double Frequency
  • Sound s new Sound(FileChooser.getMediaPath(
  • "c4.wav"))
  • s.explore()
  • s.doubleFreq()
  • s.explore()

8
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
9
Halve Frequency Method
  • public void halveFreq()
  • // make a copy of the original sound
  • Sound s new Sound(this.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
  • sourceIndexsourceIndex0.5,
    targetIndex)
  • this.setSampleValueAt((int) targetIndex,
  • s.getSampleValueAt((int)
    sourceIndex))

10
Testing Halve Frequency
  • Sound s new Sound(FileChooser.getMediaPath(
  • "c4.wav"))
  • s.explore()
  • s.halveFreq()
  • s.explore()

11
Generalizing this
  • //Program 86
  • public void changeFreq2(double factor)
  • Sound s new Sound(this.getFileName())
  • for (double sourceIndex0, targetIndex 0
  • targetIndex
  • sourceIndexsourceIndexfactor,
    targetIndex)
  • if (sourceIndex s.getLength() )
  • sourceIndex 0
  • this.setsampleValueAt((int) targetIndex,
  • s.getSampleValueAt((int) sourceIndex))

12
What the heck does that do?
13
My changeFrequency method
  • /Note, this is VERY different from that shown
    in your book
  • _at_param init The samplingRate of the
    original file
  • _at_param mod The samplingRate of the modified
    file
  • /
  • public Sound changeFrequency(int init, int mod)
  • Sound output new Sound( (int)
    (this.getLength() mod/init) )
  • for (int i0i
  • int replacethis.getSampleValueAt( (int) (
    i init/mod ) )
  • output.setSampleValueAt( i , replace)
  • return output
Write a Comment
User Comments (0)
About PowerShow.com