CSC111 Lab 3 Solution

From dftwiki3
Revision as of 21:32, 11 February 2010 by Thiebaut (talk | contribs)
Jump to: navigation, search

Here are some program that can be used for different sound operations:

# ------------------------------------------------------
def part4():
  # reverses sound1 into sound2
  file = pickAFile()
  sound1 = makeSound( file )
  sound2 = makeSound( file )
  
  N = getLength( sound1 )
  for i in range( 1, N ):
    s = getSampleValueAt( sound1, i )
    j = N-i
    setSampleValueAt( sound2, j, s )
     
  blockingPlay( sound1 )
  blockingPlay( sound2 )
  
# ------------------------------------------------------
def part5():
  # reverses sound1 into sound2
  file = pickAFile()
  sound1 = makeSound( file )
  blockingPlay( sound1 )
  
  N = getLength( sound1 )
  for i in range( 1, N/2 ):
    s1 = getSampleValueAt( sound1, i )
    j = N-i
    s2 = getSampleValueAt( sound1, j )
    setSampleValueAt( sound1, i, s2 )
    setSampleValueAt( sound1, j, s1 )
     
  blockingPlay( sound1 )
  
# ------------------------------------------------------
def part6():
  # copy a sound to a different place (offset)
  # into an empty sound object (twice as long)
  file = pickAFile()
  sound1 = makeSound( file )
  N = getLength( sound1 )
  blockingPlay( sound1 )

  sound2 = makeEmptySound( N * 2 )
  offset = 15000
  for i in range( 1, N ):
    s = getSampleValueAt( sound1, i )
    j = i+offset
    setSampleValueAt( sound2, j, s )
     
  blockingPlay( sound2 )

# ------------------------------------------------------
def part7():
  # make tow copies of sound1 into sound2
  # one at offset 0, one at offset 15000
  file = pickAFile()
  sound1 = makeSound( file )
  N = getLength( sound1 )
  blockingPlay( sound1 )

  sound2 = makeEmptySound( N * 2 )
  
  offset = 0
  for i in range( 1, N ):
    s = getSampleValueAt( sound1, i )
    j = i+offset
    setSampleValueAt( sound2, j, s )
    
  offset = 15000
  for i in range( 1, N ):
    s1 = getSampleValueAt( sound1, i )
    j = i+offset
    s2 = getSampleValueAt( sound2, j )
    setSampleValueAt( sound2, j, s1+s2 )
     
  blockingPlay( sound2 )

# ------------------------------------------------------
def part8():
  # make tow copies of sound1 into sound2
  # one at offset 0, one at offset 15000
  file = pickAFile()
  sound1 = makeSound( file )
  N = getLength( sound1 )
  blockingPlay( sound1 )

  sound2 = makeEmptySound( N * 2 )
  
  offset = 0
  for i in range( 1, N ):
    s = getSampleValueAt( sound1, i )
    j = i+offset
    setSampleValueAt( sound2, j, s )
    
  offset = 7000  
  for i in range( 1, N ):
    s1 = getSampleValueAt( sound1, i )
    j = i+offset
    s2 = getSampleValueAt( sound2, j )
    setSampleValueAt( sound2, j, s1/2 + s2 )
     
  blockingPlay( sound2 )
  
# ------------------------------------------------------
def part9():
  # make tow copies of sound1 into sound2
  # one at offset 0, one at offset 14000
  file = pickAFile()
  sound1 = makeSound( file )
  N = getLength( sound1 )
  blockingPlay( sound1 )

  sound2 = makeEmptySound( N * 2 )
  
  offset = 0
  for i in range( 1, N ):
    s = getSampleValueAt( sound1, i )
    j = i+offset
    setSampleValueAt( sound2, j, s )
    
  offset = 7000  
  for i in range( 1, N ):
    s1 = getSampleValueAt( sound1, i )
    j = i+offset
    s2 = getSampleValueAt( sound2, j )
    setSampleValueAt( sound2, j, s1/2 + s2 )

  offset = 14000  
  for i in range( 1, N ):
    s1 = getSampleValueAt( sound1, i )
    j = i+offset
    s2 = getSampleValueAt( sound2, j )
    setSampleValueAt( sound2, j, s1/4 + s2 )
     
  blockingPlay( sound2 )
  
# ------------------------------------------------------
def part10():
  # A more general version of Part9()
  file = pickAFile()
  sound1 = makeSound( file )
  N = getLength( sound1 )
  blockingPlay( sound1 )

  sound2 = makeEmptySound( N * 2 )
  
  divider = 1
  for offset in [0, 7000, 14000 ]:
    for i in range( 1, N ):
      s1 = getSampleValueAt( sound1, i )
      j = i+offset
      s2 = getSampleValueAt( sound2, j )
      setSampleValueAt( sound2, j, s1/divider + s2 )
    divider = divider * 2
     
  blockingPlay( sound2 )