Tags

, , , , , , , , , , ,

Arkwood wants to be a rap star. Problem is, he is a scrawny little Belgian with acne. ‘Not to worry,’ I stated confidently, ‘I will create you a bling rap video with a foxy voice.’ Armed with my Raspberry Pi and the Python programming language, I set about turning my buddy into a pimp dope ass…

import pygame
from speech import Speech
from screen import Screen
from random import randint

speech = Speech()
screen = Screen()

# set up ma slang
slang_list = [ 
"beanpies",
"cold hoopin' it",
"gangsta lean",
"homie",
"in the house"
]

# set up da beats
pygame.mixer.init()
pygame.mixer.music.load("CrazyRap.MP3")
pygame.mixer.music.play()

#loop da beats
while pygame.mixer.music.get_busy():

    # get next lyric from the mic
    lyric = speech.speech_to_text('/home/pi/PiAUISuite/VoiceCommand/speech-recog.sh')

    # get first word of lyric
    word = lyric.partition(' ')[0]

    # show video still in browser
    screen.display_images(word, 1)

    # obtain some slang, dear boy
    slang = slang_list[randint(0, len(slang_list) - 1)]

    # sing out the slang 'n' lyric google-style
    speech.text_to_speech(slang + ' ' + lyric)

Okay. So first up I created a list of slang, sourced from the The Rap Dictionary website. After all, neither Arkwood nor I are from the street.

Next, I used the recommended Pygame to play my beats. I made the beats myself, you know, using Magix Music Maker to program the drums:

magic_music_maker_rap

Now we can start doing the really nifty stuff. I use Google Speech To Text service to convert Arkwood’s terrible voice into text.

We then grab the first word of the lyric and launch a browser with a matching Google image. These images are to be the backbone of Arkwood’s MTV video campaign.

Finally we add a random piece of slang to our lyric, and use the sexy voice of Google Text To Speech service to ‘sing’ the hip hop song Ice Ice Baby by Vanilla Ice.

I played Arkwood’s video to my maid Monita and she was utterly speechless. Me thinks a star has been born!

pi_hothiphopvideo

Here’s the code that handles the Google services…

from subprocess import Popen, PIPE, call
import urllib
 
class Speech(object):
 
    # converts speech to text
    def speech_to_text(self, filepath):
        try:
            # utilise PiAUISuite to turn speech into text
            text = Popen(['sudo', filepath], stdout=PIPE).communicate()[0]
 
            # tidy up text
            text = text.replace('"', '').strip()
 
            # debug
            print(text)

            return text
        except:
            print ("Error translating speech")
 
    # converts text to speech
    def text_to_speech(self, text):
        try:
            # truncate text as google only allows 100 chars
            text = text[:100]
 
            # encode the text
            query = urllib.quote_plus(text)
 
            # build endpoint
            endpoint = "http://translate.google.com/translate_tts?tl=en&q=" + query
 
            # debug
            print(endpoint)
 
            # get google to translate and mplayer to play
            call(["mplayer", endpoint], shell=False, stdout=PIPE, stderr=PIPE)
        except:
            print ("Error translating text")

…and the code that handles the browser images

import requests
from BeautifulSoup import BeautifulSoup
import webbrowser

class Screen(object):

    # displays google images
    def display_images(self, search_term, amount):
        try:
            # check params
            if (amount < 1):
                return

            #build url
            url = "https://www.google.co.uk/search?q=image+{}&tbm=isch".format(search_term)
             
            #get webpage
            request = requests.get(url)
            soup = BeautifulSoup(request.text)    

            # get images
            images = []
            for img in soup.findAll('img'):
                images.append(img['src'])

            # ensure enough images
            if amount > len(images):
                amount = len(images)

            # display each image in a browser
            for i in range(0, amount):
                webbrowser.open(images[i], new=0 )
        except:
            print ("Error displaying images")

Now, if you don’t mind, my client and I have a pool party to attend. Worthing Recreation Centre.