# change RPI SOUND GUI # python 2 + PYGAME lib # kll engineering 12/2014 V0.3 # thanks to help from FORUM # http://www.raspberrypi.org/forums/viewtopic.php?f=32&t=92827 # best start from terminal with: # python /home/pi/kll/python_audio/soundselect_GUI.py # so you see OS result info, what you not see in IDLE import pygame from pygame.locals import * import sys import subprocess FPS = 20 # frames per second, the general speed of the program WINDOWWIDTH = 160 # size of window's width in pixels WINDOWHEIGHT = 140 # size of windows' height in pixels #_______________________GAME WINDOW COLORS # R G B GRAY = (100, 100, 100) GRAY1 = (210,210,210) NAVYBLUE = ( 60, 60, 100) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = ( 0, 255, 0) BLUE = ( 0, 0, 255) YELLOW = (255, 255, 0) ORANGE = (255, 128, 0) PURPLE = (255, 0, 255) CYAN = ( 0, 255, 255) CYAN2 = ( 0, 180, 220) BLACK = ( 0, 0, 0) LIGHTBGCOLOR = GRAY BOXCOLOR = WHITE KEYCOLOR = PURPLE PYGAMETHEME = False if (PYGAMETHEME == True) : BGCOLOR = NAVYBLUE HIGHLIGHTCOLOR = BLUE NOTECOLOR = BLACK CAPSCOLOR = ORANGE TXTCOL = WHITE SLIDERCOL = CYAN else : BGCOLOR = WHITE HIGHLIGHTCOLOR = GRAY NOTECOLOR = GRAY1 CAPSCOLOR = GRAY TXTCOL = BLACK SLIDERCOL = CYAN2 mysysVOL=0.8 # ! i don't know how to do a read back from system first?? slrange = 1.0 # 100%range for VOL SL_top = WINDOWHEIGHT - 40 SL_long = 100 SL_left = (WINDOWWIDTH - SL_long)/2 SL_high = 15 SL_rangetxt ="" Sel_top = 5 Sel_long = 100 Sel_left = (WINDOWWIDTH - Sel_long)/2 Sel_high = 20 mySel=0 # ! i don't know how to do a read back from system first?? #______________________________________________________________________ def drawinputslider(): global mysysVOL,slrange,SL_left,SL_top,SL_long,SL_high,SL_rangetxt myfont = pygame.font.SysFont("monospace",16,bold=True) SLset_long = int(SL_long * mysysVOL/slrange) SL_rangetxt ="0 .. %.1f" % (slrange) DISPLAYSURF.blit(myfont.render(SL_rangetxt,1,TXTCOL),(SL_left,SL_top-18)) pygame.draw.rect(DISPLAYSURF, NOTECOLOR, (SL_left, SL_top, SL_long, SL_high)) pygame.draw.rect(DISPLAYSURF, CAPSCOLOR, (SL_left, SL_top, SLset_long, SL_high)) slidertxt="VOL %.2f" % (mysysVOL) DISPLAYSURF.blit(myfont.render(slidertxt,1,CAPSCOLOR),(SL_left,SL_top+SL_high+5)) #______________________________________________________________________ def drawselectslider(): global Sel_top,Sel_long,Sel_left,Sel_high myfont = pygame.font.SysFont("monospace",16,bold=True) pygame.draw.rect(DISPLAYSURF, NOTECOLOR, (Sel_left, Sel_top, Sel_long, Sel_high)) DISPLAYSURF.blit(myfont.render("HDMI",1,TXTCOL),(Sel_left+10,Sel_top)) pygame.draw.rect(DISPLAYSURF, NOTECOLOR, (Sel_left, Sel_top+Sel_high, Sel_long, Sel_high)) DISPLAYSURF.blit(myfont.render("Aout",1,TXTCOL),(Sel_left+10,Sel_top+Sel_high)) pygame.draw.rect(DISPLAYSURF, NOTECOLOR, (Sel_left, Sel_top+Sel_high+Sel_high, Sel_long, Sel_high)) DISPLAYSURF.blit(myfont.render("default",1,TXTCOL),(Sel_left+10,Sel_top+Sel_high+Sel_high)) #______________________________________________________________________ def getSliderPixel(x, y): global mysysVOL,slrange,SL_left,SL_top,SL_long,SL_high,SL_rangetxt myfont = pygame.font.SysFont("monospace",16,bold=True) boxRect = pygame.Rect(SL_left-3,SL_top, SL_long+6,SL_high) if boxRect.collidepoint(x, y): pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR, (SL_left - 5, SL_top - 5, SL_long + 10,SL_high + 10), 3) # make a slider pygame.draw.rect(DISPLAYSURF, SLIDERCOL, (x - 5,SL_top,10,15), 2) return (1) return (None) #______________________________________________________________________ def getSelPixel(x, y): global Sel_top,Sel_long,Sel_left,Sel_high,mySel mySel=0 myfont = pygame.font.SysFont("monospace",16,bold=True) boxRect = pygame.Rect(Sel_left,Sel_top, Sel_long,3*Sel_high) if boxRect.collidepoint(x, y): pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR, (Sel_left - 5, Sel_top - 5, Sel_long + 10,3*Sel_high + 10), 3) # draw rect over select texts topoffset = y - Sel_top if topoffset < Sel_high : mySel = 1 if topoffset > Sel_high : mySel = 2 if topoffset > 2*Sel_high : mySel = 3 pygame.draw.rect(DISPLAYSURF, SLIDERCOL, (Sel_left,Sel_top+Sel_high*(mySel-1), Sel_long, Sel_high), 1) return (mySel) return (None) def playsound() : #os.system("aplay /home/pi/python_games/badswap.wav") # now use pygame mixer for play sound if ( pygame.mixer.get_busy() ): print "mixer stop" pygame.mixer.stop() sound=pygame.mixer.Sound('/home/pi/python_games/badswap.wav') print "play WAV" sound.play() # (1) play 2 times #______________________________________________________________________ def main(): global FPSCLOCK, DISPLAYSURF,mysysVOL,SL_left pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) pygame.display.set_caption("Audio System") mousex = 0 # used to store x coordinate of mouse event mousey = 0 # used to store y coordinate of mouse event firstSelection = None # stores the (x, y) of the first box clicked. DISPLAYSURF.fill(BGCOLOR) drawinputslider() drawselectslider() while True: mouseClicked = False DISPLAYSURF.fill(BGCOLOR) # drawing the window drawinputslider() drawselectslider() for event in pygame.event.get(): if event.type == QUIT: # close window[x] QUIT event pygame.mixer.quit() pygame.display.quit() pygame.quit() sys.exit() elif event.type == MOUSEMOTION: mousex, mousey = event.pos # elif event.type == MOUSEBUTTONUP: # DOWN??? elif event.type == MOUSEBUTTONDOWN: # DOWN??? mousex, mousey = event.pos mouseClicked = True SL_undermouse = getSliderPixel(mousex, mousey) if (SL_undermouse != None ): if ( mouseClicked ) : mysysVOL = (mousex - SL_left)/100.0 if (mysysVOL < 0.0 ) : mysysVOL = 0.0 if (mysysVOL > 1.0 ) : mysysVOL = 1.0 VOL = -10240 + int(mysysVOL * 10600) print "set VOL %s" % (VOL) subprocess.call(['amixer','cset','numid=1','--',str(VOL)]) playsound() Sel_undermouse = getSelPixel(mousex, mousey) if (Sel_undermouse != None ): if ( mouseClicked ) : #print Sel_undermouse if ( Sel_undermouse == 1 ) : # HDMI subprocess.call(['amixer','cset','numid=3','2']) print "HDMI" playsound() if ( Sel_undermouse == 2 ) : # A OUT subprocess.call(['amixer','cset','numid=3','1']) print "A Out" playsound() if ( Sel_undermouse == 3 ) : # DEFAULT subprocess.call(['amixer','cset','numid=3','0']) print "default" playsound() pygame.display.update() FPSCLOCK.tick(FPS) pygame.mixer.quit() pygame.display.quit() pygame.quit() sys.exit() #______________________________________________________________________ if __name__ == '__main__': main()