Change the volume of a wav file in python

前端 未结 3 978
醉梦人生
醉梦人生 2020-12-25 15:36

I have a 2 seconds 16bit single channel 8khz wav file and I need to change its volume.

It should be quite straightforward, because changing the volume is the same as

相关标签:
3条回答
  • 2020-12-25 15:48

    I wrote a library to simplify this type of thing

    You can do that like so:

    from pydub import AudioSegment
    
    song = AudioSegment.from_wav("never_gonna_give_you_up.wav")
    
    # reduce volume by 10 dB
    song_10_db_quieter = song - 10
    
    # but let's make him *very* quiet
    song = song - 36
    
    # save the output
    song.export("quieter.wav", "wav")
    
    0 讨论(0)
  • 2020-12-25 15:58

    The code which makes sound louder plus filter low and high frequencies

    from pydub import AudioSegment
    
    audio_file = "first.mp3"
    
    song = AudioSegment.from_mp3(audio_file)
    
    new = song.low_pass_filter(1000)
    
    new1 = new.high_pass_filter(1000)
    
    # increae volume by 6 dB
    song_6_db_quieter = new1 + 6
    
    # save the output
    song_6_db_quieter.export("C://Users//User//Desktop//second.mp3", "mp3")
    
    0 讨论(0)
  • 2020-12-25 16:10

    As you can see in the comments of the question, there are several solutions, some more efficient.

    The problem was immediately detected by Jan Dvorak ("the * 5 part is clipping and overflowing") and the straightforward solution was:

    s = numpy.fromstring(s, numpy.int16) / 10 * 5
    

    In this case, this solution was perfect for me, just good enough.

    Thank you all folks!

    0 讨论(0)
提交回复
热议问题