Hi All,
This is my second blog post after getting all kits from Element14, I tried to give maximum time from my schedule as I am a working professional.
In Fact, I have implemented Karaoke in two ways.
1. One which I am describing in this blog, which uses full capabilities of Raspberry Pi and PiFace display to have complete Karaoke player in one Box.
2. Other implementation i did is to control and display lyrics of Karaoke player to a remote mobile browser. From where i can play/stop song and respective lyrics will get displayed on Mobile browser. This implementation I am trying to integrate with Walfson Audio Card, as there i cannot stack PiFace display. Hope this will get completed soon.
As a working guy, within the Time I get, I tried to implement Karaoke Player with using most of the capabilities of PiFace display and tried to make it portable. Now my Raspberry Karaoke machine is totally portable solution which is independent of TV/Monitor/Mobile. In short to use this Karaoke machine there is no need to depend on TV/Monitor or any other device to see the lyrics. You can now see lyrics on PiFace display only, which is synced with the Song.
Overview of implementation-
As the main aim of the PiFace display is to make Raspberry Pi a portable board, I prefered to avoid monitor/TV to make a Karaoke Player. In the current implementation the Lyrics (.lrc) and respective .mp3 files are saved in a directory over Raspberry pi.
When the Player is started, it will show you the list of .mp3 files present in the directory. You can scroll through the list (switch 3 is used to scroll through songs list) to select song. When you come on the required song you can select it (switch 5 is used to select song) to play song and lyrics on PiFace Display. This implementation is completely in Python3, the program that is running on Raspberry Pi is a true Karaoke player which takes .lrc file as input and syncs it with the song playing. The lyrics will be played line by line on PiFace display, and i made sure that the lyrics should get displayed even if it is more than 32 characters (limitation of Display). To record sound I bought a USB sound card and Soldered a Mic to it. When we play a song the "arecord" will start recording it to a .wav file. Also one of feature of this Karaoke player is this doesnt use Karaoke Songs to play. I filtered out vocals from the song to make it Karaoke song, hence you can use any song to play with it.
Before going to detail you can have a look on video I made to demo it.
Raspberry Pi Karaoke Player demo
or you can find on youtube
https://www.youtube.com/watch?v=6Ata9oPuf5s&feature=youtu.be
Details about implementation-
For implementation details, I am giving more specific details those are hard to find on internet. So that we can use these information for any further implementation.
- Processing on existing .mp3 files
In order to make the Karaoke more generic, instead of searching for Karaoke I found out the way by which you can cancel vocals. I used the "SOX" utility.
-Sox - SoX reads and writes audio files in most popular formats and can optionally apply effects to them.
In order to cancel vocal (This doesn't cancel vocals 100% but up to extend, but little vocals could be useful for guidelines to a singer) we can use option "oops" to sox.
To install sox on your device use
sudo apt-get install sox
Now only installing sox is not able to play mp3 files, hence you need to install libs for that using
sudo apt-get install libsox-fmt-mp3
Now you should be able to play any mp3 file using
play <path_to_mp3_file>
This will play mp3 file to audio out device.
If you have multiple Audio Out cards then you can use option to specify which card "sox" should use.
AUDIODEV=hw:0 play <path_to_mp3_file> //where hw:0 is card 0.
Now if you want to make the .mp3 file to listen as Karaoke song use following,
AUDIODEV=hw:0 play <path_to_mp3_file> oops
the option "oops" will cancel the vocals from the song.
Now this utility i used in my python program so that i can convert any song to karaoke song.
Setting up PiFace -
To setup PiFace to Raspberry Pi, I referred the official page documentation which you can find here
http://www.piface.org.uk/guides/setting_up_pifacecad/
hence I am not giving much details about same on blog to make it short.
Python Program-
The Python program given below does following job
1. Listing the .mp3 files from the given directory.
2. Display the .mp3 file names on PiFace LCD one by one.
3. Allows switch 3 to scroll through list.
4. Allows switch 5 to select song to play.
5. Selects .lrc file with respect to .mp3 file that is selected.
6. Reads .lrc file using file handling and then syncs it with the song and records sounds to test.wave file.
7. Sending the synced lyrics to PiFace Display while playing song.
8. Allows switch 2 to stop the song and select another song from list.
Following is my python program karaoke.py
#!/usr/bin/python3 #Project : Basic python based Karaoke player #Author : Shrenik Shikhare #Date : 24 April 2014 (C) #Description: This program reads time information and lyrics from .lrc file and also plays song along with lyrics # automatically. #Use ./karaoke.py #dependencies : sox utility in linux and piFace libs. #TODO: test with mplayer import pifacecad as p import time import os import sys cad = p.PiFaceCAD() cad.lcd.cursor_off() cad.lcd.write("RaspKaraoke\nFrom Shrenik") listbuff= [[0 for x in range(30)] for x in range(10)] while(1): listindex=0 #List the directory files to show the list and store it into buffer to access them for file in os.listdir("./"): if file.endswith(".mp3"): fname=str(file+"\n") listbuff[listindex]=str(file) listindex=listindex+1 #print file listindex1=0 os.system('clear') print ("Following songs are found in Current Directory") os.system('pwd') #list filenames cad.lcd.clear() for listindex1 in range(0,listindex): listtitles= listbuff[listindex1] print ("[",listindex1+1,"]" ,listbuff[listindex1]) cad.lcd.clear() cad.lcd.write(listtitles) while(not(cad.switches[2].value == 1 or cad.switches[4].value ==1)): if(cad.switches[4].value == 1): break if(cad.switches[4].value==1): break #while(not(sys.stdin.read(1)=='a')): # break #n=input("Select song to play -> ") #print listbuff(n) n=listindex1#n-1 print ("Playing",listbuff[n]) #if (n==0): #open mp3 relative lyrics with same file name and .lrc extension f = open(listbuff[n].replace(".mp3",".lrc"),"r") #f = open(str(sys.argv[1]),"r") buff=f.readline() os.system('AUDIODEV=hw:0 play -q ' + listbuff[n]+' oops &') os.system('arecord -D plughw:1 -r 16000 test.wav &') while (buff[1]!='0'): lyrics_start_pos=f.tell()#save first line position to recover read line buff=f.readline() #print buff #restore lyrics start position f.seek(lyrics_start_pos) prev_time_in_ms = 0 compensate_time=500 while 1: if(cad.switches[1].value == 1):#to stop the song, not much efficient as its not interrupt driven os.system('killall play') os.system('killall arecord') break buff=f.readline() if not f:break # Check for EOF #Check for blank lines in file if len(buff.strip())==0: buff=f.readline() break print (buff) else: #read time information from .lrc file minute = 10*(ord(buff[1])-48)+(ord(buff[2])-48) seconds = 10*(ord(buff[4])-48)+(ord(buff[5])-48) hundredms = 10*(ord(buff[7])-48)+(ord(buff[8])-48) #print "minutes [",minute,"] sec [",seconds,"] hundredms [",hundredms #conver time format in millisecond to pass to time.sleep() function current_time_in_ms=(60*minute*1000)+(1000*seconds)+(10*hundredms) if(current_time_in_ms>500): current_time_in_ms=current_time_in_ms-compensate_time#500ms to display next line .5 second early compensate_time=compensate_time+600 #print current_time_in_ms #calculate delay between two lines delay = current_time_in_ms - prev_time_in_ms+0.00 #save current delay for next reference prev_time_in_ms = current_time_in_ms delay_time_in_sec=delay/1000 #put delay time.sleep(delay_time_in_sec) #print read lyrics line print (buff) #for j in range(11,len(buff)): buff=buff[10:] cad.lcd.clear() cad.lcd.write(buff) cad.lcd.write('\n') if (len(buff)>16): x=16 #go on next line if line is more than 16 characters print (len(buff)); if (len(buff)<32 and len(buff)>16): while buff[x]!='\n': cad.lcd.write(buff[x]) x=x+1 else: for x in range(16,31): cad.lcd.write(buff[x]) x=x+1 if (len(buff)>31 and len(buff)<48):#Clear display if line is more than 32 characters and display remaining characters cad.lcd.clear() compensate_time=compensate_time+800 #Compensating time that goes in between two display refresh while buff[x]!='\n': cad.lcd.write(buff[x]) x=x+1 else: if len(buff)>47: cad.lcd.clear() for x in range(32,47): cad.lcd.write(buff[x]) x=x+1
After running this python program you should be able to run the song from PiFace Display, and you can enjoy Karaoke with this.
If you have any queries feel free to ask me. I would like to answer them. Let me know if something I missed.
This is just a different approach to define Karaoke player. I hope you all liked it.
Thanks,
Shrenik Shikhare.