maral
maral

Reputation: 123

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...

Upvotes: 12

Views: 38172

Answers (1)

Anil_M
Anil_M

Reputation: 11443

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')

Upvotes: 26

Related Questions