Guitar and vocals on Raspberry Pi

Tags

, , , , , , , , , , , , ,

If you know Arkwood, you will know that he plays the melodica. Well, now he wants to play the guitar too. ‘I love to strum those groovy chords, but I’d really dig a tutorial.’ No problem, I told him, and began coding some Python using the fantastic and free Python Tools for Visual Studio.

from speech import Speech
import pygame
import requests
from BeautifulSoup import BeautifulSoup
from time import sleep

speech = Speech()
 
# set up guitar chords
pygame.mixer.init()
guitar_a = pygame.mixer.Sound("52384__guitarguy1985__a.wav")
guitar_b = pygame.mixer.Sound("52385__guitarguy1985__b7.wav")
guitar_e = pygame.mixer.Sound("52388__guitarguy1985__e.wav")
 
# get song
def get_song(song_title):
        request = requests.get("http://www.e-chords.com/chords/ramones/" + song_title.replace(" ", "-"))
        soup = BeautifulSoup(request.text)    
        return soup.find('pre', attrs={'class':'core'})
 
# play guitar
def play_guitar(chord):
    if ('A' in chord):
        guitar_a.play()
        print("Chord A")
    elif ('B' in chord):
        guitar_b.play()
        print("Chord B")
    elif ('E' in chord):
        guitar_e.play()
        print("Chord E")

# get song title from microphone
print ("State a Ramones song title")
song_title = speech.speech_to_text('/home/pi/PiAUISuite/VoiceCommand/speech-recog.sh')
 
# get song from e-chords
song = get_song(song_title)
 
# play each line of song
for line in song:
     
    # check line has content
    line = str(line).strip('{}\r\n \t')
    if (not line):
        continue
 
    # either play guitar or sing lyric
    if (line.startswith('<u>')):
        play_guitar(line)
        sleep(1)
    else:
        speech.text_to_speech(line)

The program is real simple. First we use Google’s Speech To Text service, so that we can speak into the microphone attached to our Raspberry Pi and provide a song title.

Then we use the E-Chords website to obtain the necessary chords and lyrics for the requested song. Beautiful Soup python library handles the HTML for us.

We can now loop over every line in the song. If we find some musical notes then we can use Pygame to play the appropriate chords. If we find some lyrics then we can use Google’s Text To Speech service to ‘sing’ the words.

I must say, it worked out brilliant for Ramones song I Wanna Be Sedated:

pi_guitarvocals

Granted, the distorted guitar chords weren’t perfectly in time with the vocals. What the hell, it’s all rock ‘n’ roll baby yeah! Hm.

Now, you can record your own guitar sounds if you want, or you may wish to source them for free from Freesound. I used Distorted Guitar Chords by guitarguy1985.

Here’s the code that makes use of Google’s Speech To Text and Text To Speech 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")

Big question – what does Arkwood think about my endeavours? Well, I’ll let you know once he’s finished listening to it. Right now he’s too busy making the sign of the horns and headbanging like a deranged cow.

Ciao!

Follow

Get every new post delivered to your Inbox.

Join 45 other followers