Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Andrew

On 06/11/16 16:54, Terry Coles wrote:


Andrew,

The list doesn't accept attachments, can you send it to my address instead of
the list?



Ok, have done.

It was based on a few lines from the video player example which was 
linked to on the Ubuntu Wiki page:

https://bazaar.launchpad.net/~jderose/+junk/gst-examples/view/head:/video-player-1.0


If I remember correctly, there didn't used to be a way to tell GStreamer 
which audio output device to use, it always used the default. I'm not 
sure if that has changed, and it might not be a problem for you anyway.



--


Andrew.



--
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Terry Coles
On Sunday, 6 November 2016 16:46:16 GMT Andrew wrote:
> seem to just work on Ubuntu 16.04. I've attached a simple player program

Andrew,

The list doesn't accept attachments, can you send it to my address instead of 
the list?

-- 



Terry Coles

-- 
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Andrew

Hi Terry,

A completely different and probably better way to play audio in Python 
is by using GStreamer.


A while ago I wrote a python program to play sound effects using 
GStreamer. I've had to re-create it for Python 3 as the original didn't 
seem to just work on Ubuntu 16.04. I've attached a simple player program 
which shows how to play, pause and seek to the beginning. Set the 
filename and when running it press enter and it'll do the next step.


Personally I've only use it with FLAC files, but it should play anything 
gstreamer can play.


This page was useful:
https://wiki.ubuntu.com/Novacut/GStreamer1.0

--

Andrew.

--
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Ralph Corderoy
Hi Terry,

Will wrote:
> import os
> import time
> import subprocess
>
> mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), 
> os.listdir('.')))

In case you're not familiar with lambda yet, that can also be written
using a generator expression that came with later Pythons.

sorted(f for f in os.listdir(".") if f.lower().endswith(".mp3"))

Both trust that foo.mp3 isn't a directory, that you're happy playing
./.mp3, etc., but then it might be easiest to throw f at mpg321 and
handle its complaint.

> blackhole = open(os.devnull, 'w')

If you're using Python 3.3 or later then there's subprocess.DEVNULL to
save you opening this yourself.

> player = subprocess.Popen(['mpg321', '-q', file],
> stdin=subprocess.PIPE, stdout=blackhole, stderr=blackhole)

If you don't need to communicate with mpg321 after it's started then you
may find subprocess.run() easier;  it has a timeout parameter.  Either
way, you may want to consider any handling of the documented exceptions.

Cheers, Ralph.

-- 
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Terry Coles
On Sunday, 6 November 2016 14:37:53 GMT William R Sowerbutts wrote:
> Whipped this quick demo up; it will play at most five seconds of each MP3
> file in the current directory. Tested with both mpg321 and mpg123.

Will,

Thanks for this.  I had got as far as getting Popen to play the music, (it 
took me a while to understand the docs), but I hadn't got round to looking at 
how to handle the terminate code yet :-)

Excellent work.  I'll make sure that the Trustees at the Model Town know that 
I had help from this List.  :-)

--

Terry

-- 
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread William R Sowerbutts
Terry

Whipped this quick demo up; it will play at most five seconds of each MP3 file
in the current directory. Tested with both mpg321 and mpg123.

Will

---
#!/usr/bin/env python

import os
import time
import subprocess

mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), 
os.listdir('.')))
blackhole = open(os.devnull, 'w')
for file in mp3_files:
print("Playing %s ..." % file)
# launch the player
player = subprocess.Popen(['mpg321', '-q', file], stdin=subprocess.PIPE, 
stdout=blackhole, stderr=blackhole)
# play at most 5 seconds, or until end of file, whichever is first:
wait_until = time.time() + 5 
while time.time() < wait_until:
player.poll() # check to see if terminated
if player.returncode != None:
break
time.sleep(0.1)
# kill the player if it's still running
if player.returncode == None:
player.terminate()
player.wait()
---

On Sun, Nov 06, 2016 at 01:44:45PM +, Terry Coles wrote:
>On Sunday, 6 November 2016 13:10:19 GMT William R Sowerbutts wrote:
>> Instead of os.system(), which will blocks until the called process
>> completes, use the "subprocess" module -- subprocess.Popen() -- which runs
>> the player process in parallel with your script. The ".pid" member variable
>> of the process object gives you the process ID. You can .send_signal() to
>> signal it (and .terminate() which uses this to send it a SIGTERM).
>
>I'll give that I try.  Thanks.
>
>> You can probably send keypresses to stdin with this method also, I expect it
>> has a key you can press to quit.
>
>I don't believe mpg321 has an interactive mode; I can start it with all sorts 
>of options, but 
>once it's going.
>
>However, I gather that mpg123 is the player available on piCore ( which I 
>intend to use 
>for this) and that does have an Option to accept keyboard inputs.
>
>I'll see where this takes me.
>
>-- 
>
>
>
>   Terry Coles
>-- 
>Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
>Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
>New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
>Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

_
William R Sowerbutts  w...@sowerbutts.com
"Carpe post meridiem"   http://sowerbutts.com
 main(){char*s=">#=0> ^#X@#@^7=",c=0,m;for(;c<15;c++)for
 (m=-1;m<7;putchar(m++/6%3/2?10:s[c]-31&1<

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Terry Coles
On Sunday, 6 November 2016 13:10:19 GMT William R Sowerbutts wrote:
> Instead of os.system(), which will blocks until the called process
> completes, use the "subprocess" module -- subprocess.Popen() -- which runs
> the player process in parallel with your script. The ".pid" member variable
> of the process object gives you the process ID. You can .send_signal() to
> signal it (and .terminate() which uses this to send it a SIGTERM).

I'll give that I try.  Thanks.

> You can probably send keypresses to stdin with this method also, I expect it
> has a key you can press to quit.

I don't believe mpg321 has an interactive mode; I can start it with all sorts 
of options, but 
once it's going.

However, I gather that mpg123 is the player available on piCore ( which I 
intend to use 
for this) and that does have an Option to accept keyboard inputs.

I'll see where this takes me.

-- 



Terry Coles
-- 
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Tim

On 06/11/16 12:34, Terry Coles wrote:

Hi,

By now I think everyone knows that I'm doing some work on the Wimborne Model
Town again; this year includes getting the bells to work in the model Minster.

As part of this, I'm looking to replace the aging CD/MP3 player that currently
sits on the floor inside the model and plays (amongst other things) the Wedding
March.  To this end, I'm currently coding a simple MP3 player in Python, which
will run on a Pi Zero.  It will select a directory full of files and play them
in a loop until a staff member presses a button to select a new directory or
to stop playback.

So far, I can play any selected directory using mpg321 in an os.system call.

My question is about stopping playback, so that a new selection can be made.
mpg321 has a fair number of command-line argument, but none to stop playback
or quit.  I think that I might be able do this by sending a signal (I've done
this from the OS), but I haven't found a way to obtain the PID of the mpg321
instance from within my script yet.  That method also seems a bit 'harsh'.

Has anyone got any suggestions?
  


Hi Terry


I have a script that kills Firefox and the line that does the work is

p=$(pidof firefox)
kill $p

Would it work to kill mpg321?

Like you said it does seem a little heavy handed way to stop a program 
(in my case where I just want to start it again afresh)


Tim


--
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

Re: [Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread William R Sowerbutts
Terry

Instead of os.system(), which will blocks until the called process completes, 
use the "subprocess" module -- subprocess.Popen() -- which runs the player 
process in parallel with your script. The ".pid" member variable of the 
process object gives you the process ID. You can .send_signal() to signal it 
(and .terminate() which uses this to send it a SIGTERM).

You can probably send keypresses to stdin with this method also, I expect it 
has a key you can press to quit.

Will

On Sun, Nov 06, 2016 at 12:34:48PM +, Terry Coles wrote:
>Hi,
>
>By now I think everyone knows that I'm doing some work on the Wimborne Model 
>Town again; this year includes getting the bells to work in the model Minster.
>
>As part of this, I'm looking to replace the aging CD/MP3 player that currently 
>sits on the floor inside the model and plays (amongst other things) the 
>Wedding 
>March.  To this end, I'm currently coding a simple MP3 player in Python, which 
>will run on a Pi Zero.  It will select a directory full of files and play them 
>in a loop until a staff member presses a button to select a new directory or 
>to stop playback.
>
>So far, I can play any selected directory using mpg321 in an os.system call.
>
>My question is about stopping playback, so that a new selection can be made.  
>mpg321 has a fair number of command-line argument, but none to stop playback 
>or quit.  I think that I might be able do this by sending a signal (I've done 
>this from the OS), but I haven't found a way to obtain the PID of the mpg321 
>instance from within my script yet.  That method also seems a bit 'harsh'.
>
>Has anyone got any suggestions?
> 
>-- 
>
>
>
>   Terry Coles
>
>-- 
>Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
>Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
>New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
>Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR

_
William R Sowerbutts  w...@sowerbutts.com
"Carpe post meridiem"   http://sowerbutts.com
 main(){char*s=">#=0> ^#X@#@^7=",c=0,m;for(;c<15;c++)for
 (m=-1;m<7;putchar(m++/6%3/2?10:s[c]-31&1<

[Dorset] Ending Playback in mpg321 from Within Python

2016-11-06 Thread Terry Coles
Hi,

By now I think everyone knows that I'm doing some work on the Wimborne Model 
Town again; this year includes getting the bells to work in the model Minster.

As part of this, I'm looking to replace the aging CD/MP3 player that currently 
sits on the floor inside the model and plays (amongst other things) the Wedding 
March.  To this end, I'm currently coding a simple MP3 player in Python, which 
will run on a Pi Zero.  It will select a directory full of files and play them 
in a loop until a staff member presses a button to select a new directory or 
to stop playback.

So far, I can play any selected directory using mpg321 in an os.system call.

My question is about stopping playback, so that a new selection can be made.  
mpg321 has a fair number of command-line argument, but none to stop playback 
or quit.  I think that I might be able do this by sending a signal (I've done 
this from the OS), but I haven't found a way to obtain the PID of the mpg321 
instance from within my script yet.  That method also seems a bit 'harsh'.

Has anyone got any suggestions?
 
-- 



Terry Coles

-- 
Next meeting:  Bournemouth, Tuesday, 2016-12-06 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR