Hi all...
I have made the karaoke player with python pygame.
Following are the steps in it
1)I have installed wheezy Linux on Raspberry Pi, which was already in the NOOBS.
2) For working with Wolfson PI AUDIO CARD, I have used Kernel,modules and scripts given by ragnar.jensen at
after installing it you will need to execute scripts for each devices in home/pi/use_case_scripts
3)This player uses 3 types of file for one song:
MP3 File
Lyrics File
Timing File
5)Following is the code for my Application:
import pygame, Buttons
import subprocess
import os
from threading import Thread
from pygame.locals import *
from pygame import mixer
pygame.init()
class Play_Button:
def __init__(self):
self.main()
#Create a display
def display(self):
self.screen = pygame.display.set_mode((600,450),0,32)
pygame.display.set_caption("player")
#Update the display and show the button
def update_display(self,some_text):
self.screen.fill((81,209,247))
screen=pygame.display.set_mode((600,450),0, 24)
img=pygame.image.load("/home/pi/Kariyoke_Machine/play music/bg-img.jpg").convert()
create=pygame.font.SysFont("comicsansms",40)
f=create.render(some_text,True,(0, 0, 0),(255, 255, 255))
screen.blit(img,(0, 0))
screen.blit(f,(200, 20))
#Parameters: surface, color, x, y, length, height, width, text, text_color
self.Prev_Button.create_button(self.screen, (255,142,35), 100, 135, 100, 100, 0, "Prev", (255,255,255))
self.Play_Button.create_button(self.screen, (255,142,35), 300, 135, 100, 100, 0, "Play/Pause", (255,255,255))
self.Next_Button.create_button(self.screen, (255,142,35), 500, 135, 100, 100, 0, "Next", (255,255,255))
pygame.display.flip()
pygame.display.update()
#Run the loop
def main(self):
pygame.init()
#os.system("arecord|aplay")
aplay_arec=subprocess.Popen(["arecord","|","aplay"])
i=0
a={}
for filename in os.listdir("/home/pi/Kariyoke_Machine/songs/"):
a[i]=filename
print a[i]
i=i+1
i=0;
play=0
self.Play_Button = Buttons.Button()
self.Next_Button = Buttons.Button()
self.Prev_Button = Buttons.Button()
self.display()
self.update_display("Welcome")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == MOUSEBUTTONDOWN:
if self.Play_Button.pressed(pygame.mouse.get_pos()):
if play:
print "Stoped"
mixer.music.stop()
play=0
else:
text_to_print="Playing " + str(a[i])
print text_to_print
self.update_display(text_to_print)
mixer.music.stop()
mixer.init()
mixer.music.load('/home/pi/Kariyoke_Machine/songs/'+a[i])
lyrics_file="/home/pi/Kariyoke_Machine/lyrics_and_timings/"+str(i)+"l.txt"
timings_file="/home/pi/Kariyoke_Machine/lyrics_and_timings/"+str(i)+"t.txt"
if os.path.isfile(lyrics_file):
subtitles_file = open(lyrics_file, 'r')
subtitles_time = open(timings_file, 'r')
loop_counter=int(subtitles_time.readline());
print loop_counter
subtitles_list=[]
timings_list=[]
for x in range(0,loop_counter):
next_subtitles=subtitles_file.readline()
timing=int(subtitles_time.readline())
subtitles_list.append(next_subtitles)
timings_list.append(timing)
mixer.music.play()
for x in range(0,loop_counter):
ev = pygame.event.poll()
if ev.type == pygame.MOUSEBUTTONDOWN:
print "Breaking Loop"
break;
#print "I am in for Loop"
#event=pygame.event.get()
pygame.time.wait(timings_list[x])
text_to_print=subtitles_list[x]
self.update_display(text_to_print)
play=1
pygame.display.flip()
else:
mixer.music.play()
play=1
pygame.display.flip()
elif self.Prev_Button.pressed(pygame.mouse.get_pos()):
i=i-1
if i<0:
i = len (a)
text_to_print="Playing " + str(a[i])
print text_to_print
self.update_display(text_to_print)
mixer.music.stop()
mixer.init()
mixer.music.load('/home/pi/Kariyoke_Machine/songs/'+a[i])
lyrics_file="/home/pi/Kariyoke_Machine/lyrics_and_timings/"+str(i)+"l.txt"
timings_file="/home/pi/Kariyoke_Machine/lyrics_and_timings//"+str(i)+"t.txt"
if os.path.isfile(lyrics_file):
subtitles_file = open(lyrics_file, 'r')
subtitles_time = open(timings_file, 'r')
loop_counter=int(subtitles_time.readline());
print loop_counter
subtitles_list=[]
timings_list=[]
for x in range(0,loop_counter):
next_subtitles=subtitles_file.readline()
timing=int(subtitles_time.readline())
subtitles_list.append(next_subtitles)
timings_list.append(timing)
mixer.music.play()
for x in range(0,loop_counter):
#print "I am in for Loop"
#event=pygame.event.get()
pygame.time.wait(timings_list[x])
text_to_print=subtitles_list[x]
self.update_display(text_to_print)
else:
mixer.music.play()
elif self.Next_Button.pressed(pygame.mouse.get_pos()):
i=i+1;
if i > len(a):
i=0
text_to_print="Playing " + str(a[i])
print text_to_print
self.update_display(text_to_print)
mixer.music.stop()
mixer.init()
mixer.music.load('/home/pi/Kariyoke_Machine/songs/'+a[i])
lyrics_file="/home/pi/Kariyoke_Machine/lyrics_and_timings/"+str(i)+"l.txt"
timings_file="/home/pi/Kariyoke_Machine/lyrics_and_timings/"+str(i)+"t.txt"
if os.path.isfile(lyrics_file):
subtitles_file = open(lyrics_file, 'r')
subtitles_time = open(timings_file, 'r')
loop_counter=int(subtitles_time.readline());
print loop_counter
subtitles_list=[]
timings_list=[]
for x in range(0,loop_counter):
next_subtitles=subtitles_file.readline()
timing=int(subtitles_time.readline())
subtitles_list.append(next_subtitles)
timings_list.append(timing)
mixer.music.play()
for x in range(0,loop_counter):
#print "I am in for Loop"
#event=pygame.event.get()
pygame.time.wait(timings_list[x])
text_to_print=subtitles_list[x]
self.update_display(text_to_print)
else:
mixer.music.play()
if __name__ == '__main__':
obj =Play_Button()
6) You need to execute:
arecord|aplay
in separate terminal.
7)To player just go to Kariyoke_machine Folder in Home and execute Karaoke_Player.m
8)Enjoy !!
- Demonstration Video of the project:
Karaoke Machine With Raspberry Pi and Wolfson PI. - YouTube
- Following is the source code for the Project:
https://drive.google.com/file/d/0B5TjchJFpKR1M0RwUGtWQTNKa0E/edit?usp=sharing
I am trying to improve it in some aspects.Lets see what happens next .
I would like to thanks to Element14 for giving this wonderful opportunity !
