D_eM
Posts: 5
Joined: Mon Apr 08, 2019 2:16 am

Help required with multi-day script for mp3 playback

Mon Apr 08, 2019 4:48 am

Hi - I am new to Python and have been struggling to create a workable script for a friends project - am hoping someone can point me in the right direction!

The script is to control playback of mp3 files from certain directories on certain days. So, for example, on May 1 2019 at 10:00 I want to play all the mp3 files in directory 000 and I want them to repeat until 10:00 the next day. Then, on May 2 at 10:00 I want to play all the mp3 files from directory 001, May 3/003, May 4/004 etc.

I thought it would be quite straightforward but seem to be getting myself into a bit of a pickle. I've managed to use the crontab to play the files in one directory (using omxplayer) at 10:00 each day and I've cobbled together a script that plays the files in a loop and have looked into timedate and system functions but I don’t know where I should start to put this together.

I want to do it in an elegant and DRY fashion but am limited on the backend stuff (I do frontend mostly).

My initial thinking is that I need to have a lookup table to map the days to the correct directories - would that be right? Then I could, I presume, have something along the lines of ‘if day is x then run files in directory y’ with a control for resetting it at 10:00 the next day. My original plan for that was to just reboot the RPi using the crontab at 09:58 to a, halt the loop and b, rest everything for the new day.

Figure there must be a 'correct' way to do this as I just seem to be murdering the code by hacking bits and pieces together.

Also - the RPi won’t be online and will have wi-fi turned off - am using a real-time clock for timing.

I’ve been trying to figure all this out but hadn’t anticipated having to learn Python to quite the level that it seems is needed. Am learning some awesome stuff though - which is good as I've just bought my kids a Pi starter kit on the back of this.

Any help/pointers/snippets appreciated.

epoch1970
Posts: 5918
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Help required with multi-day script for mp3 playback

Mon Apr 08, 2019 9:05 am

How about this:
-31 directories with files, one for each day of the month
- at 10:00 every day, kill omxplayer if it is running, get the day-of-month, run omxplayer on the corresponding directory.

That would make the system work 24/7, except for a short interruption at 10:00 every day.
I would avoid rebooting needlessly since the Pi doesn’t have an RTC. The date might be wrong for some time after reboot.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

D_eM
Posts: 5
Joined: Mon Apr 08, 2019 2:16 am

Re: Help required with multi-day script for mp3 playback

Mon Apr 08, 2019 9:22 am

Hi - thanks for your reply.

I currently have pretty much that structure (ie a directory for each day but currently named 000, 001, 002 etc) and that's the theory I was working on - the problem is I can't work out how to script it.

Do you mean name each folder with the day of the month then get the day of the month and use that variable to identify the correct folder?

The approach is kinda what I figured but figuring and doing are different things!

I do have a RTC on the GPIO pins and that is working well so retaining the date seems to be OK. Assuming the battery lasts...

epoch1970
Posts: 5918
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Help required with multi-day script for mp3 playback

Mon Apr 08, 2019 2:34 pm

There is no day numbered "00".

I don't know the time-date libs available on Python but a simple call to the system "date" command will give you the day-of-month, e.g.

Code: Select all

me@jupyter:~$ date
Mon Apr  8 16:30:51 CEST 2019
me@jupyter:~$ date "+%d"
08
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
rpiMike
Posts: 1616
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Help required with multi-day script for mp3 playback

Tue Apr 09, 2019 2:15 pm

This is how I would implement it.

Wait until required time.
Work out folder name.
Get list of files.
Pop file off list and play using omxplayer-wrapper until list empty.
Repeat.

Code: Select all

import datetime,time,glob,signal,sys
from omxplayer.player import OMXPlayer

# pip3 install omxplayer-wrapper

def playerExit(code):
    print('exit',code)
    global playing
    playing=False

def playFile(file):
    global player,playing
    if player==None:
        player=OMXPlayer(file)
        player.exitEvent += lambda _, exit_code: playerExit(exit_code)
    else:
        player.load(file)
    print('Playing:',file)
    playing=True
    
def signalHandler(sig,frame):
    print('Ctrl+C pressed')
    quitPlayer()
    sys.exit(0)

def quitPlayer():
    if player!=None:
        player.quit()
        
signal.signal(signal.SIGINT,signalHandler)
        
player = None
playing = False
files = []
musicTime=datetime.datetime.strptime('10:00','%H:%M').time()
musicDateTime = datetime.datetime.combine(datetime.datetime.now().date(),musicTime)
#musicDateTime = musicDateTime + datetime.timedelta(days=1)
print('Next music datetime:',musicDateTime)
while True:
    now = datetime.datetime.now()
    if now>musicDateTime:
        folder = "{:03d}".format(now.day)
        files = glob.glob('/home/pi/musicfolders/'+folder+'/*.mp3')
        print(folder,files)
        
        musicDateTime += datetime.timedelta(days=1)
        print('Next music datetime:',musicDateTime)
        
    if len(files)>0 and playing==False:
        playFile(files.pop(0))
        
    time.sleep(1)



D_eM
Posts: 5
Joined: Mon Apr 08, 2019 2:16 am

Re: Help required with multi-day script for mp3 playback

Tue Apr 09, 2019 7:38 pm

Wow! This is great - thank you.

Just trying to follow this so I know what's going on.

When getting the folder name how is this working? I'm reading this as - {:03d} is saying there's a 3 digit number of the folder and format(now.day) is getting the number of the day - is that right? So I'd need to rename the folders something like 001.10 - if today's date is 10th - or am I misinterpreting that?

Code: Select all

now = datetime.datetime.now()
    if now>musicDateTime:
        folder = "{:03d}".format(now.day)
        files = glob.glob('/home/pi/musicfolders/'+folder+'/*.mp3')
        print(folder,files)

My folders don't need to be named with 3 numerals - can be anything - so thinking date would be better (as this project will go on over several months). So if I named the folders with the date (ie 2019-04-20) would that make things easier? Would the folder then just be:

Code: Select all

folder = format(now.year, now.month, now.day)

Thanks again.

User avatar
rpiMike
Posts: 1616
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Help required with multi-day script for mp3 playback

Tue Apr 09, 2019 10:05 pm

The folder name is currently just the day of the month (1 to 31) depending on the month padded with leading zeros up to 3 digits.

The 10th would be 010

Another way to format your full date string would be:

folder = now.strftime('%Y-%m-%d')

D_eM
Posts: 5
Joined: Mon Apr 08, 2019 2:16 am

Re: Help required with multi-day script for mp3 playback

Wed Apr 10, 2019 12:24 am

OK. Thanks Mike - will try this tonight.

I don't suppose you know of a way to adjust the playback volume to a speaker attached to 3.5mm jack via the terminal do you??

Have tried amixer set PCM -- -XYY using % and dB but doesn't affect the actual output. Have configured the config file to have output going through the audio jack etc - just can't get the volume to change when playing through omxplayer.

From what I can tell omxplayer seems to bypass the system volume setting but I need a way to reduce the volume once a day then raise it back up to the 'normal' level once a day. Was hoping to do so with cron.

Edit:

Found an entry in the help file - I can change the volume on a file basis with --vol -x (x being millibels). Still can't see a way to do at a system level though.

User avatar
rpiMike
Posts: 1616
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Help required with multi-day script for mp3 playback

Wed Apr 10, 2019 7:59 am

You should be able to change the volume with:

player.set_volume(0.5)

I think a sensible range is 0 to 1, the documentation says 0 to 10.

https://python-omxplayer-wrapper.readth ... omxplayer/

I assume your speaker is amplified?

D_eM
Posts: 5
Joined: Mon Apr 08, 2019 2:16 am

Re: Help required with multi-day script for mp3 playback

Wed Apr 10, 2019 10:25 am

Will try that - thanks.

Speaker is a UEBoom.

Return to “Python”