Friday, March 3, 2017

Play Sound In Python

Hi friends this time i gave you a source code that play sound in python.........

1. # Autthors: Vivek Kumar.
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 fa
19.
20. '''Return original sound snd with selected number of samples faded in
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
37.
38. '''Return original sound snd with selected number of samples faded out
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
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,
99. '''Return original sound snd with selected samples panned to the right
00. and others to the left via new_snd'''
01. new_snd= sound. copy(snd )
02. samps_to_pan = pan_length - 1
03. index = 0
04. fade_factor_left = 0.0
05. fade_factor_right= 0.0
06. for samp in new_snd :
07. if index <= samps_to_pan :
08. left = sound.get_left (samp)
09. right = sound.get_right (samp)
10. avg= ( left+right) /2.0
11. fade_factor_left = (( index* 1.0 )/samps_to_pan )
12. fade_factor_right= 1-fade_factor_left
13. left = int (avg*fade_factor_left )
14. right = int (avg*fade_factor_right )
15. sound.set_values ( samp,left,right )
16. index+= 1
17. return new_snd
18.
19. if __name__ == "__main__" :
20. snd = sound.load_sound ( "love.wav" )
21. #sound.play(snd)
22. #sound.play(rem_vocals(snd))
23. #sound.play(fade_in(snd, 50000))
24. #sound.play(fade_out(snd, 70000))
25. #sound.play(left_to_right(snd, 500000))
26. sound.play (fade( snd, 50000 ))

if you find this title helpful then share it on fb or google+ for helping more people......Thank you

No comments:

Post a Comment