Playing with sound


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # Autthors: Gurkirat, Addison
  2. # Due Wednesday October 12th
  3. import sound
  4. import time
  5.  
  6. def rem_vocals(snd):
  7. #Return a copy of sound with vocals removed
  8.  
  9. new_snd = sound.copy(snd)
  10. for sample in new_snd:
  11. left = sound.get_left(sample)
  12. right = sound.get_right(sample)
  13. delete_vocals = (left - right)/2.0
  14. sound.set_left(sample,int(delete_vocals))
  15. sound.set_right(sample,int(delete_vocals))
  16. return new_snd
  17. def fade_in (snd, fade_length):
  18. #Return a copy of sound with the beginning faded. Number of samples faded is defined by fade_length
  19.  
  20. '''Return original sound snd with selected number of samples faded in via new_snd'''
  21. new_snd2 = sound.copy(snd)
  22. samps_to_fade = fade_length - 1
  23. #required due to samples starting at 0, not 1
  24. index = 0
  25. for samp in new_snd2:
  26. index = sound.get_index(samp)
  27. if index <= samps_to_fade:
  28. fade_factor = 1.0 *index/fade_length
  29. #Tracks number of samples done, is also used as the fade factor
  30. left2 = (sound.get_left(samp))*fade_factor
  31. right2 = (sound.get_right(samp))*fade_factor
  32. sound.set_left(samp, int(left2))
  33. sound.set_right(samp,int(right2))
  34. return new_snd2
  35. def fade_out (snd,fade_length):
  36. #Return a copy of sound with the end faded. Number of samples faded is defined by fade_length
  37.  
  38. '''Return original sound snd with selected number of samples faded out via new_snd'''
  39. new_snd=sound.copy(snd)
  40. fade_point= len(snd)-fade_length-1
  41. #where to start fading
  42. samp_todo = fade_length
  43. #tracks the number of untouched samples
  44. fade_factor=1.0
  45. samp_index=0
  46. for samp in new_snd:
  47. samp_index=sound.get_index(samp)
  48. if samp_index>=fade_point:
  49. # if we have reached the point to begin fading,then below
  50. if samp_index == len(snd)-1:
  51. fade_factor = 0
  52. #if at last sample, fade factor is set to zero
  53. fade_factor= ((samp_todo*1.0)/fade_length)
  54. left = int((sound.get_left(samp))*fade_factor)
  55. right = int((sound.get_right(samp))*fade_factor)
  56. sound.set_left(samp, int(left))
  57. sound.set_right(samp,int(right))
  58. samp_todo-= 1
  59.  
  60. #Tracks number of samples done,starts at one and ends at zero
  61. return new_snd
  62. def fade(snd, fade_length):
  63. #Returns a copy of sound with the beginning and ending faded.Number of samples faded is defined by fade_legnth
  64.  
  65. new_snd=sound.copy(snd)
  66. new_snd=fade_in(new_snd,fade_length)
  67. new_snd=fade_out(new_snd,fade_length)
  68. return new_snd
  69. '''Return original sound snd with selected number of samples
  70. faded in and out via new_snd'''
  71. #new_snd = sound.copy(snd)
  72. #samps_to_fade_in = fade_length - 1
  73. #samp_todo = fade_length
  74. ##tracks the number of untouched samples
  75. #fade_point_fade_out= len(snd)-fade_length
  76. #index = 0
  77. #fade_factor = 1.0
  78. #for samp in new_snd:
  79. #samp_index=sound.get_index(samp)
  80. #if samp_index <= samps_to_fade_in:
  81. #fade_factor = 1.0 *samp_index/fade_length
  82. #left2 = (sound.get_left(samp))*fade_factor
  83. #right2 = (sound.get_right(samp))*fade_factor
  84. #sound.set_left(samp, int(left2))
  85. #sound.set_right(samp,int(right2))
  86. #if samp_index>=fade_point_fade_out :
  87. #if samp_index == len(snd)-1:
  88. #fade_factor = 0
  89. #left = int((sound.get_left(samp))*fade_factor)
  90. #right = int((sound.get_right(samp))*fade_factor)
  91. #sound.set_left(samp, int(left))
  92. #sound.set_right(samp,int(right))
  93. #samp_todo-=1
  94. #fade_factor= ((samp_todo*1.0)/fade_length)
  95. #return new_snd
  96.  
  97. def left_to_right (snd, pan_length):
  98. #Returns a copy of sound with panning applied to it. Left starts at 0,right starts at max
  99. '''Return original sound snd with selected samples panned to the right
  100. and others to the left via new_snd'''
  101. new_snd= sound.copy(snd)
  102. samps_to_pan = pan_length - 1
  103. index = 0
  104. fade_factor_left = 0.0
  105. fade_factor_right=0.0
  106. for samp in new_snd :
  107. if index <= samps_to_pan :
  108. left = sound.get_left(samp)
  109. right = sound.get_right(samp)
  110. avg= (left+right)/2.0
  111. fade_factor_left = ((index*1.0)/samps_to_pan)
  112. fade_factor_right= 1-fade_factor_left
  113. left = int(avg*fade_factor_left)
  114. right = int(avg*fade_factor_right)
  115. sound.set_values(samp,left,right)
  116. index+=1
  117. return new_snd
  118.  
  119. if __name__ == "__main__":
  120. snd = sound.load_sound("love.wav")
  121. #sound.play(snd)
  122. #sound.play(rem_vocals(snd))
  123. #sound.play(fade_in(snd, 50000))
  124. #sound.play(fade_out(snd, 70000))
  125. #sound.play(left_to_right(snd, 500000))
  126. sound.play(fade(snd,50000))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.