CSC111 Lab 5 Solutions 2014

From dftwiki3
Revision as of 23:05, 27 February 2014 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 21:37, 25 February 2014 (EST)


Solution Programs for Lab #5


# lab5 solution programs (loops)
# D. Thiebaut

# single For-Loops
"""
for i in range( 3, 8 ):
    print( i, end=" ")
print()

for i in [3, 4, 5, 6, 7]:
    print( i, end=" " )
print()


for i in range( 4, 20+1, 2 ):
    print( i, end=" " )
print()


for i in range( -3, 19+1, 2 ):
    print( i, end=" " )    
print()

start = int( input( "start where? " ) )
end   = int( input( "end where? " ) )
if start <= end:
    for i in range( start, end+1 ):
        print( i, end=" " )
else:
    for i in range( start, end-1, -1 ):
        print( i, end=" " )
print()

for i in range( 11 ):
    print( "%s%s----%s%s" % ( i*'*', i*'..', i*'-', i*'??' ) )

"""
 
# JES Program
# JES program to take a sound file and play with it
# D. Thiebaut
#

file = pickAFile()
sound = makeSound( file )
blockingPlay( sound )

# =======================================================
# clear waveform after "Remember"
# get start index for clearing
startIndex = 13393 # index just before "the force..."

# get the number of samples in the sound file
noSamples = getLength( sound )

# clear all the samples from startIndex to end:
for i in range( startIndex, noSamples ):
  setSampleValueAt( sound, i, 0 )
  
# play sound again
#blockingPlay( sound )

# =======================================================
# Copy "remember" to the end of itself in the waveform
rememberLength = startIndex  # that's the length of the word "remember"
for i in range( 0, startIndex ):
  value = getSampleValueAt( sound, i )
  setSampleValueAt( sound, i+rememberLength, value )
  
# play sound: "remember, remember"
blockingPlay( sound )

# get the original sound again 
sound = makeSound( file )
blockingPlay( sound )

# =======================================================
# copy Always right after Remember
startAlways = 30444   # index just before "Always"
endRemember = 8968

# set j to the end of Remember
j = endRemember
for i in range( startAlways, noSamples ):
  # copy from Always
  value = getSampleValueAt( sound, i )
  # into waveform after Remember
  setSampleValueAt( sound, j, value )
  # advance j by 1
  j = j+1

# now j points to the end of the moved "Always"
endRememberAlways = j

# clear from the end of RememberAlways to the end of the waveform
for i in range( endRememberAlways, noSamples ):
  setSampleValueAt( sound, i, 0 )
  
blockingPlay( sound )

"""
# =======================================================
# create an echo

startIndex = 13393    # index just before "the force..."
noSamples = getLength( sound )

# clear all the samples from startIndex to end:
for i in range( startIndex, noSamples ):
  setSampleValueAt( sound, i, 0 )

# overlap remember on top of itself, starting at Index 6000
# which is roughly half the length of the word
endOfRemember = 13393
offset        = 6000
for i in range( endOfRemember, 0, -1 ):
  sourceValue = getSampleValueAt( sound, i)
  destValue   = getSampleValueAt( sound, i+offset )
  setSampleValueAt( sound, i+offset, sourceValue + destValue ) 

blockingPlay( sound )
"""