# Posted By

svleeuwen on 11/08/11

# Statistics

Viewed 233 times
Favorited by 0 user(s)

# Playing with sound

/ Published in: Python
`# Autthors: Gurkirat, Addison    # Due Wednesday October 12th    import sound    import time     def rem_vocals(snd):    #Return a copy of sound with vocals removed     new_snd = sound.copy(snd)    for sample in new_snd:    left = sound.get_left(sample)    right = sound.get_right(sample)    delete_vocals = (left - right)/2.0    sound.set_left(sample,int(delete_vocals))    sound.set_right(sample,int(delete_vocals))    return new_snd    def fade_in (snd, fade_length):    #Return a copy of sound with the beginning faded. Number of samples faded is defined by fade_length     '''Return original sound snd with selected number of samples faded in via new_snd'''    new_snd2 = sound.copy(snd)    samps_to_fade = fade_length - 1    #required due to samples starting at 0, not 1    index = 0    for samp in new_snd2:    index = sound.get_index(samp)    if index <= samps_to_fade:    fade_factor = 1.0 *index/fade_length    #Tracks number of samples done, is also used as the fade factor    left2 = (sound.get_left(samp))*fade_factor    right2 = (sound.get_right(samp))*fade_factor    sound.set_left(samp, int(left2))    sound.set_right(samp,int(right2))    return new_snd2    def fade_out (snd,fade_length):    #Return a copy of sound with the end faded. Number of samples faded is defined by fade_length     '''Return original sound snd with selected number of samples faded out via new_snd'''    new_snd=sound.copy(snd)    fade_point= len(snd)-fade_length-1    #where to start fading    samp_todo = fade_length    #tracks the number of untouched samples    fade_factor=1.0    samp_index=0    for samp in new_snd:    samp_index=sound.get_index(samp)    if samp_index>=fade_point:    # if we have reached the point to begin fading,then below    if samp_index == len(snd)-1:    fade_factor = 0    #if at last sample, fade factor is set to zero    fade_factor= ((samp_todo*1.0)/fade_length)    left = int((sound.get_left(samp))*fade_factor)    right = int((sound.get_right(samp))*fade_factor)    sound.set_left(samp, int(left))    sound.set_right(samp,int(right))    samp_todo-= 1     #Tracks number of samples done,starts at one and ends at zero    return new_snd    def fade(snd, fade_length):    #Returns a copy of sound with the beginning and ending faded.Number of samples faded is defined by fade_legnth     new_snd=sound.copy(snd)    new_snd=fade_in(new_snd,fade_length)    new_snd=fade_out(new_snd,fade_length)    return new_snd    '''Return original sound snd with selected number of samples    faded in and out via new_snd'''    #new_snd = sound.copy(snd)    #samps_to_fade_in = fade_length - 1    #samp_todo = fade_length    ##tracks the number of untouched samples    #fade_point_fade_out= len(snd)-fade_length    #index = 0    #fade_factor = 1.0    #for samp in new_snd:    #samp_index=sound.get_index(samp)    #if samp_index <= samps_to_fade_in:    #fade_factor = 1.0 *samp_index/fade_length    #left2 = (sound.get_left(samp))*fade_factor    #right2 = (sound.get_right(samp))*fade_factor    #sound.set_left(samp, int(left2))    #sound.set_right(samp,int(right2))    #if samp_index>=fade_point_fade_out :    #if samp_index == len(snd)-1:    #fade_factor = 0    #left = int((sound.get_left(samp))*fade_factor)    #right = int((sound.get_right(samp))*fade_factor)    #sound.set_left(samp, int(left))    #sound.set_right(samp,int(right))    #samp_todo-=1    #fade_factor= ((samp_todo*1.0)/fade_length)    #return new_snd     def left_to_right (snd, pan_length):    #Returns a copy of sound with panning applied to it. Left starts at 0,right starts at max    '''Return original sound snd with selected samples panned to the right    and others to the left via new_snd'''    new_snd= sound.copy(snd)    samps_to_pan = pan_length - 1    index = 0    fade_factor_left = 0.0    fade_factor_right=0.0    for samp in new_snd :    if index <= samps_to_pan :    left = sound.get_left(samp)    right = sound.get_right(samp)    avg= (left+right)/2.0    fade_factor_left = ((index*1.0)/samps_to_pan)    fade_factor_right= 1-fade_factor_left    left = int(avg*fade_factor_left)    right = int(avg*fade_factor_right)    sound.set_values(samp,left,right)    index+=1    return new_snd     if __name__ == "__main__":    snd = sound.load_sound("love.wav")    #sound.play(snd)    #sound.play(rem_vocals(snd))    #sound.play(fade_in(snd, 50000))    #sound.play(fade_out(snd, 70000))    #sound.play(left_to_right(snd, 500000))    sound.play(fade(snd,50000))`