Hi (Again),

I'm inching towards squashing the (known) bugs in my Music Player and Bells 
software.

By and large the Flask code is doing exactly what it should do and displays 
the current status of the two programs and also sends the correct commands to 
them when the control buttons are pressed.

I have a bug which I'm sure is something to do with the fact that I am calling 
the mpg123 player a second time having previously killed it.

I have attached two files to this message (since I understand that the list now 
accepts attachments as long as they aren't executable).

The file 'Minster_Music_Software_Fragment.txt' contains the three functions 
that are used in this scenario and the file 'Console_Output.txt' shows what is 
written to the terminal from the point where I launch it to the point where I 
hit Ctrl-C to get out of it.  I have edited this to remove all the messaging 
between the Webserver and the Music program, so only the relevant data 
remains.

Here is what is supposed to happen:

1.  At program Startup the sockets code sets up all connections.  From then 
until Step 5 below all messaging works fine.

2.  The function mp3_player_start() starts the mpg123 player with a List 
containing the filenames in the current playlist.  (Most of this code was 
written 3+ years ago and is launched at program startup.)

3.  While the contents of List is being played the software waits for the 
mpg123 player to exit at the end of the Playlist, whereupon the Playlist 
Number is incremented and the next Playlist is passed to mpg123, and so on for 
ever.

4.  When a "Stop Music" command is received from the Webserver, the function 
mp3_player_stop() is called and the old Playlist Number is stored to ensure 
that the music can start again at the same place.   Then the Playlist Number 
is set to zero before the mpg123 player is killed.   This means that the 
mp3_player.wait() in the function mp3_player_start() returns and because the 
Playlist Number is zero the program breaks out of the loop.  This all appears 
to work fine.

5.  When a "Start Music" command is received from the Webserver, the function 
restartmp3() is called, the old Playlist Number is restored and the function 
mp3_player_start() is called again.  The music starts as it should, but this 
is the point where it all goes wrong, because the Music Player software then 
stops responding to commands from the Webserver.

To me it seems that the software suddenly starts ignoring interrupts because 
the command should be detected after no more than 1 second when apscheduler 
calls the command checking function which works fine up to that point.

Is there anything obviously wrong with what I've done (apart from my rather 
noddy coding techniques that is)?

(If the list rejects these attachments, I'll put them up on my website.)

-- 



                Terry Coles
def mp3_player_start():                          #  Setup and run the MP3 Player
    global playlists, playlist_index, mp3_player, Status

    print("")
    print("Entry into mp3_player_start()")
    print("Playlist Number = " +str(playlist_index))
    print("")

    while playlist_index != 0:
        print ("Playlist Number = " + str(playlist_index))
        print ("Playlist Length = " + str(len(playlists)))

        Status[9] = str(playlist_index)                     # Current Playlist

        mp3_subdir = playlists[playlist_index - 1]          # directory 
containing mp3 files for the current Playlist

        playlist = sorted(glob.glob(mp3_subdir + '/*.[Mm][Pp]3'))   # Create a 
list of files to be played in the current directory
        Status[10] = playlist

        mpg_list = ['mpg123']

        args = mpg_list + playlist

        print (args)

        mp3_player = subprocess.Popen(args)

        mp3_wait = mp3_player.wait()
        if not mp3_wait:
            print('MP3 Player: mpg123 failed:%#x\n' % mp3_wait)
        mp3_player = None

        if playlist_index == 0:
            print("Break from Playlist Loop")
            break

        next_playlist()
        
def mp3_player_stop():                           #  Setup and run the MP3 Player
    global mp3_player, playlist_index, last_playlist_index, Status

    last_playlist_index = playlist_index
    
    playlist_index = 0                          # Not Playing Flag.
    
    mp3_player.terminate()
    mp3_wait = mp3_player.wait()
    if not mp3_wait:
        print ('MP3 Player: mpg123 failed:%#x\n' % mp3_wait)
    mp3_player = None
    
    print("Music Stopped")
    
    Status[10] = "Not Playing"

def restartmp3():                           # Restart the MP3 Player if the 
Wedding Sequence is completed.
    global playlist_index, last_playlist_index

    playlist_index  = last_playlist_index   # Restore the Plylist Number to 
what it was last time the Player was stopped.
    
    mp3_player_start()                      # (mp3_player_start() increments 
the index.)

pi@minster-music:~ $ python3 minstermusic.py 
5 second delay to allow Bells Pi to start up
Minster Software Version 0.2.4 (15/02/2021)
System Time:  2021-02-15 12:17:50.587983
System startup sequence initiated.
System startup sequence initiated.
Initialising connection to Webserver sockets server, please wait...
Waiting for peers to connect...
Connected to peer (Webserver Socket).
Connected to peer (Bells Pi Sockets Client).


CPU Usage (%): 9.6
Memory Used (MB): 73
CPU Temperature (Deg C): 38.1


Bells Pi Status Request Sent

Bells Pi Status Received

Entry into mp3_player_start()
Playlist Number = 1

Playlist Number = 1
Playlist Length = 6
['mpg123', 'Music/Playlist1/01 Marche Episcopale.mp3', 'Music/Playlist1/02 
Choral - Herzlich - BWV 727.mp3', 'Music/Playlist1/03 Choral - Herr Jesu 
Christ.mp3', 'Music/Playlist1/04 Choral Du Veilleur BWV 645.mp3']
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
        version 1.25.10; written and copyright by Michael Hipp and others
        free software (LGPL) without any warranty but with best wishes

Directory: Music/Playlist1/

Terminal control enabled, press 'h' for listing of keys and functions.

Playing MPEG stream 1 of 4: 01 Marche Episcopale.mp3 ...

MPEG 1.0 L III cbr160 44100 j-s

Title:   Marche Episcopale                              Artist: Louis Vierne    
                              
Comment: 24++                                           Album:  Pieces D'orgue 
Pour Des Mariages              
Genre:   Classical, 

Webserver Status Request Received

Stop Music

mpg123: death by SIGTERM

[0:50] Decoding of 01 Marche Episcopale.mp3 finished.
MP3 Player: mpg123 failed:0x0

MP3 Player: mpg123 failed:0x0

Break from Playlist Loop
Music Stopped

Status sent to Webserver
Start Music

Entry into mp3_player_start()
Playlist Number = 1

Playlist Number = 1
Playlist Length = 6
['mpg123', 'Music/Playlist1/01 Marche Episcopale.mp3', 'Music/Playlist1/02 
Choral - Herzlich - BWV 727.mp3', 'Music/Playlist1/03 Choral - Herr Jesu 
Christ.mp3', 'Music/Playlist1/04 Choral Du Veilleur BWV 645.mp3']
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
        version 1.25.10; written and copyright by Michael Hipp and others
        free software (LGPL) without any warranty but with best wishes

Directory: Music/Playlist1/

Terminal control enabled, press 'h' for listing of keys and functions.

Playing MPEG stream 1 of 4: 01 Marche Episcopale.mp3 ...

MPEG 1.0 L III cbr160 44100 j-s

Title:   Marche Episcopale                              Artist: Louis Vierne    
                              
Comment: 24++                                           Album:  Pieces D'orgue 
Pour Des Mariages              
Genre:   Classical, 
Traceback (most recent call last):
  File "minstermusic.py", line 595, in <module>
    time.sleep(2**31-1)
KeyboardInterrupt

-- 
  Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00
  Check to whom you are replying
  Meetings, mailing list, IRC, ...  http://dorset.lug.org.uk
  New thread, don't hijack:  mailto:dorset@mailman.lug.org.uk

Reply via email to