Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python : how to change audio volume?

I used winsound.Beep(frequency, duration) because I wanted to play sounds with a specified frequency. Now, I need to change the volume of these sounds. How can I do this? I tried to get help from pyaudio but...

like image 571
maral Avatar asked Apr 28 '17 11:04

maral


1 Answers

If you are open to external libraries, you can use pydub to manipulate audio (including volume ) easily. More details here.

Different audio formats such as wav, mp3, ogg , mp4,wma etc are available. Check here for more details.

Basically we convert audio to an audiosegment object and then manipulate it for various attributes using pydub.

pydub can be installed using:
pip install pydub #on 2.x and
pip3 install pydub # on 3.x

Here is an example:

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_mp3("your_song.mp3")

# boost volume by 6dB
louder_song = song + 6

# reduce volume by 3dB
quieter_song = song - 3

#Play song
play(louder_song)

#save louder song 
louder_song.export("louder_song.mp3", format='mp3')
like image 139
Anil_M Avatar answered Oct 17 '22 07:10

Anil_M