User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

python PYGAME HDMI sound

Sun Nov 30, 2014 12:19 pm

i try to get python to create and play a sound using the PYGAME lib
( a test installing pyaudio made me install RPI again because it killed Sonic Pi ...)
-a- i think it is very slow, up to a second delay??
-b- i used audio output and even VNC operation ok,
but when i try the HDMI TV the sound did not work.
i started Sonic Pi, used that HDMI button, close it and restarted my python code and it worked too.
what are the python commands to force / select between HDMI / audio output?
( a search "HDMI python" here, what did not give results )
blog http://kll.engineering-news.org/kllfusi ... icle_id=80
code http://kll.engineering-news.org/kllfusi ... load_id=44

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: python PYGAME HDMI sound

Sun Nov 30, 2014 12:46 pm

I don't know if this is related but if you're using the pygame.mixer.Sound object then this will load the sound into a buffer before you're able to play it, which will cause delays. Using the Music object will stream sounds instead so you don't get this delay.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: python PYGAME HDMI sound

Sun Nov 30, 2014 12:59 pm

yes, first i fill a stereo array (tmp) in a loop with calc,
then make a sound from it "sound = pygame.sndarray.make_sound(tmp)"
then play it "sound.play(loops=x)"
and i try already to reduce the math as much as possible.
but funny, if the delay comes from the math and array copy it should depend linear to the length of the array, but i do not feel that??

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: python PYGAME HDMI sound

Mon Dec 01, 2014 2:38 pm

working test example for change HDMI / audio jack from python: http://kll.engineering-news.org/kllfusi ... ect_py.txt

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: python PYGAME HDMI sound

Mon Dec 01, 2014 3:50 pm

I'm not familiar with sndarray at all so can't really comment on whether or not that's slowing down your code.

One thing you should try and move away from is using "range(len( ))" and then looking up items via that index when looping through a list e.g. instead of:

Code: Select all

    for i in range(0, len(menu_items)):
        print i+1,":", menu_items[i]
you can do:

Code: Select all

    for i, item in enumerate(menu_items):
        print i+1,":", item
It won't make much difference to your code, but if you start asking for help on StackOverflow or elsewhere then it's definitely something that they'll comment on.

Also, the preference is to use the subprocess module rather than "os.system".
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
paddyg
Posts: 2359
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: python PYGAME HDMI sound

Mon Dec 01, 2014 7:47 pm

It's a bit off topic and maybe pedantic but you're not using i at all so you might as well

Code: Select all

def menu():
    print ('''select from
1:  HDMI
2:  analog
3:  Exit''')
   return int(input("Enter choice : "))
but the point about using enumerate is worth following up as you will see it often in examples and it's generally slicker code.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: python PYGAME HDMI sound

Tue Dec 02, 2014 2:07 am

thanks:
yes, the menu was just a dirty test, also it gives errors when you not input numbers...
elParaguayo wrote: Also, the preference is to use the subprocess module rather than "os.system".
right, i started with that, but not get it working, possibly syntax?
subprocess.call(["amixer","cset numid=3 2"])

revised:
http://kll.engineering-news.org/kllfusi ... ect_py.txt
but i needed it anyhow for the keyboard synth made with python 2, using the PYGAME lib ( what does not like Tk inter ),
so i made a new GUI for it:
http://kll.engineering-news.org/kllfusi ... GUI_py.txt
Image
pls copy that code to
/home/pi/python_audio/soundselect_GUI.py
and you can use it also from the Desktop, make a new Desktop file :
/home/pi/Desktop/soundselect_GUI.desktop

Code: Select all

[Desktop Entry]
Name=Audio
Comment=Python 2 PYGAME System Sound
Exec=lxterminal -e '/home/pi/python_audio/soundselect_GUI.sh'
Icon=/usr/share/pixmaps/qjackctl.xpm
Terminal=false
MultipleArgs=false
Type=Application
Categories=Application;Development;
StartupNotify=true 
and a executable script
/home/pi/python_audio/soundselect_GUI.sh

Code: Select all

#!/bin/bash
	cd /home/pi/python_audio/
	python /home/pi/python_audio/soundselect_GUI.py 
Last edited by KLL on Wed Dec 03, 2014 7:27 am, edited 2 times in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: python PYGAME HDMI sound

Tue Dec 02, 2014 9:21 am

I think the subprocess syntax would need to be:

Code: Select all

subprocess.call(["amixer","cset", "numid=3", "2"])
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: python PYGAME HDMI sound

Wed Dec 03, 2014 7:21 am

+ + thanks, very good, now "subprocess" works too.
updated above
- - but the timing of pygame mixer is still very questionable,
_ i try to use the mixer.pre_init(..) and play with the buffer=""
and i see something like:
after press one key, the delay is still high,
if i press up to 4 keys ( one after an other ) the delay for the first sound is still long,
but the other 3 notes are played after the first without delay!
means the delay could not be caused by math and array work.
problem is that the timing inside the program could be checked by diagnostic prints,
but the audible delay i can not measure.

solved:
when i make all the sounds first the play with keyboard and mouse has good response.

it is possible to make a array [0 ..87] of sounds and play the items:

Code: Select all

    mySounds=[]     # reset for append    
    for k in range(1,89) :
        mySounds.append(makesnd(k,myamp,mypan)) # point 0 .. 87
        sys.stdout.write('.')  
and later play it with

Code: Select all

        mySounds[pkey-1].play(loops=myloops) 
but that takes min 105 sec for all 88 "piano" keys

when i try to load MIDI files to this sounds instead of a stereo signal array it did not work.

Return to “Python”