I made a code to take a .mp3 archive path, transforms it into .wav and make this archive louder or quieter depending on the user's choice.
Here's the code:
import sys
import os
from os import path
from pydub import AudioSegment
input_file = input("Enter the path of your file: ")
assert os.path.exists(input_file), "I did not find the file at, " + str(input_file)
f = open(input_file, 'r+')
print("Hooray we found your file!")
# stuff you do with the file goes here
output_file = open("result.wav", "ab")
# convert mp3 file to wav file
sound = AudioSegment.from_mp3(input_file)
sound.export(output_file, format="wav")
f.close()
flag = input("Do you want to make your .wav louder/quieter?(Y/N) ")
if flag == 'Y':
sinal = input("Type '+' for making it louder or '-' for making it quieter: ")
if sinal == '+':
n = input("Type an int number for adjust the .wav in dB: ")
n = int(n)
sound = AudioSegment.from_wav("result.wav")
louder_sound = sound + n
louder_sound.export(output_file, "wav")
elif sinal == '-':
n = input("Type an int number for adjust the .wav in dB: ")
n = int(n)
sound = AudioSegment.from_wav("result.wav")
quieter_sound = sound - n
quieter_sound.export(output_file, "wav")
output_file.close()
I took some tutorials on web how to do it, I'm not sure if I need all these libs. Also, I managed to create the .wav archive, however I can't alter the sound of it.
For some reason, I can run the code when I choose add (+) dB to the .wav sound, however, when I choose to make the .wav quieter, I get an error. Also, when I add dB, the sound doesn't change. I cannot see why this is happening. Can someone give me a hand?
The error I get is the following:
Traceback (most recent call last):
File "C:/dev/IDE Pycharm/main.py", line 33, in <module>
quieter_sound = sound - n
File "C:\Users\matsa\anaconda3\envs\k35\lib\site-packages\pydub\audio_segment.py", line 382, in __sub__
return self.apply_gain(-arg)
TypeError: bad operand type for unary -: 'str'