0

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'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Kodora
  • 1
  • 1
  • You want `n = int(n)`. Or `n = int(input(...))`. `int(n)` on its own does nothing. `int(n)` returns the integer value of `n`, but you're not assigning the result to anything. – Random Davis Sep 14 '21 at 18:05
  • I tried 'n = int(n)' and the code still don't change the dB – Kodora Sep 15 '21 at 06:14
  • You have the same issue in multiple spots. But are you even getting the same error or is it a completely different issue you're having now? – Random Davis Sep 16 '21 at 14:30

0 Answers0