I coded a Python program to sing the BTS song "Butter"

with pygame and gTTS API

Subscribe to my newsletter and never miss my upcoming articles

It was my dream for a very long time to program something to sing. Recently , I found a way to achieve that goal. Though it's not an optimized idea, I was kinda thrilled while doing it. FYI , I am a big fan of BTS, and when their song "Butter" was released I felt like I had to do something.

Pre-requisite:

1) Install Python . It is better to install a version of 3.7.7 or greater than that.

2) Install Pygame. Run the commands written below on your command prompt.

pip install pygame

3) Install gTTS API on your command prompt.

     pip install gTTS

     pip install gTTS--upgrade
     pip install gTTS-token --upgrade

4) I downloaded the "Butter" lyric and saved it as "lyrics.txt". You can get the file here

5) Then also downloaded an instrumental file of "Butter" and converted it as .wav format online and trimmed the first 12 sec and it wasn't the part of the song.The file is here.

Keep your .wav file , .txt and .py file under the same folder.

Now write some code.

1) First , import gTTS , set language to English, and open and read the .txt file.

from gtts import gTTS

language = 'en'
with open('lyrics.txt') as f:
    lines = f.readlines()

2)The "lines" variable is saving the text as a list. Now we need to convert the list file to a string and save it in a variable name "text".

text=""
for x in lines:
 text+=x 
print(text) #will print the lyrics

3) Now we need to convert the text into speech. We convert the lyrics into a speech and save it in a variable named "output" .

output = gTTS(text=text, lang=language,slow=False)

4) Now we will save the speech as an mp3 file on our local storage.

 output.save("butter.mp3")

The lyrics speech file will be saved as "butter.mp3" file on the same location where the .py is located. You can play and listen to it.

5)After that , we will add some music. My .wav instrumental file name is "Butter - BTS instrumental.wav".

from pygame import mixer  # Load the popular external library

sound = mixer.Sound('Butter - BTS instrumental.wav')
sound.play()

6) Lastly we will load and play the "butter.mp3" file automatically in our code and close the file that we opened at the start.

mixer.init()
mixer.music.load('butter.mp3')
mixer.music.play(1,0.0)
f.close()

Now if you run your code both the sound and the lyrics will play at the same time and there you have your own singer bot. You can make it sing any lyrics you want to. You just need to change the .wav and the .txt file.

Get Full code :

 from gtts import gTTS

language = 'en'
with open('lyrics.txt') as f:
    lines = f.readlines()

#print(len(lines))

text=""
for x in lines:
 text+=x     

#print(lines)
print(text)
output = gTTS(text=text, lang=language,slow=False)




output.save("butter.mp3")
from pygame import mixer  # Load the popular external library

sound = mixer.Sound('Butter - BTS instrumental.wav')
sound.play()

mixer.init()
mixer.music.load('butter.mp3')
mixer.music.play(1,0.0)

##sound.stop()
f.close()

No Comments Yet