MP3 player

Showcase your Organiser hardware developments here
Post Reply
MartinP
Posts: 55
Joined: Wed Jan 04, 2023 7:51 pm

MP3 player

Post by MartinP »

I've been working on an MP3 player based on the DF player module, since Daren posted about one a while back. I have it to the point where it works, but it needs some development, just thought I'd share it 'though.
Psion Org MP3 player_web.jpg
The serial communication to the DF player is a bit awkward and restrictive, for example you can't read much info about a track except a file number and the numbers only give the order in which files were written to the SD card, so don't provide the folder where the track is.
So to identify a track I have a Psion data file that relates the track number to the track name. And on the PC I have some Python code (which I found on the web at https://github.com/Neutrino-1/Arduino-Soundpod) which writes tracks onto the SD card. I modified the code to build a Psion data file with an index to the track names. If you want to change the music on the SD card you have to delete everything and re-write, or the index will be wrong.
DF player helper.PNG
I'm using it with the CommsLink I built previously, I modified that with a potential divider on the output to give 3.3V to the DFplayer and the buffer chip I'm using provides a level shift to 5V on the input to the Psion. The MP3 player is sitting on a second board above the CommsLink, which keeps it small and neat. The buttons on it are for previous and next track, or a long press changes the volume. One nice thing about it is that it's powered separately from the Psion, so the Psion can switch off and the MP3 player keeps going, you can then wake the Psion up later to check or change a track.

The software is all in a single OPL script (shown below), but I think it would be neater if I break it up into separate functions, as I'm having to use quite a few GOTO and IF statements, which could be removed. The advantage of how it is at the moment is that I can view the whole thing in one script on the PC. I know about the GOSUB option for one of the PC translators, but I was trying to keep it simple.
Any comments on the code and how it could be, or the hardware are most welcome.
MartinP

Code: Select all

MP3:
rem for DF player, by Martin Prest 2023
rem set HAND, TEOL and REOL to NONE in COMMS SETUP
LOCAL M%,C%(8),I%,L%,IN%(10),DL%,DH%,CKL%,CKH%,Q%,S%,D%,CK,F%
Q%=0 :rem set Query to none
L%=8 :rem length of command (in bytes)
LSET:(9600,0,8,1,0,0,0)
C%(1) = $7E :rem start of packet
C%(2) = $FF :rem version
C%(3) = 6   :rem length
C%(4) = 0   :rem command
C%(5) = 0   :rem response 0-no, 1-yes
C%(6) = 0   :rem data-hi
C%(7) = 0   :rem data-lo
C%(8) = $EF :rem end
rem setup
C%(4)=6 :rem volume
C%(7)=15 :rem max is 30
S%=0 :rem 1-do or 0-don't run SETUP1
GOTO SEND::

SETUP1::
C%(4)=$0F :rem play folder + track - stops current track
C%(6)=2 :rem folder 2
C%(7)=1 :rem track 1
S%=0
GOTO SEND::

START::
CLS
M%=MENU("NEXT,PREV,STOP,VOL+,VOL-,VOL?,TRACK?,MODE?,REPT,EQ?,FOLDERS?,TRACKS?,PAUSE,PLAY")
IF M%=0 :STOP
ELSEIF M%=1
 C%(4)=1 :rem play next track
ELSEIF M%=2
 C%(4)=2 :rem play prev track
ELSEIF M%=3
 C%(4)=$16 :rem stop
ELSEIF M%=4
 C%(4)=4 :rem vol+
ELSEIF M%=5
 C%(4)=5 :rem vol-
ELSEIF M%=6
 C%(4)=$43 :rem vol?
 Q%=1 :rem Vol query
ELSEIF M%=7
 C%(4)=$4C :rem Track?
 Q%=2 :rem Track query
ELSEIF M%=8
 C%(4)=$45 :rem Mode?
 Q%=3 :rem Mode query
ELSEIF M%=9
 M%=MENU("STOP,ON")
 IF M%=0 :GOTO START:: :ENDIF
 C%(4)=$11 :rem set play repeat all 
 C%(7)=M%-1 :rem repeat: 0-stop, 1-on
ELSEIF M%=10
 C%(4)=$44 :rem Equaliser?
 Q%=4 :rem EQ query
ELSEIF M%=11
 C%(4)=$4F :rem No. of folders?
 Q%=5 :rem folder query
ELSEIF M%=12
 C%(4)=$4E :rem No. of tracks in folder? -stops play
 CLS
 PRINT"Folder:"
 INPUT F%
 CLS
 C%(7)=F% :rem folder
 Q%=6 :rem tracks query
ELSEIF M%=13
 C%(4)=$0E :rem pause
ELSEIF M%=14
 C%(4)=$0D :rem play -does nothing?
ENDIF

SEND::
IF Q%>0
 L%=7 :rem set command length for query
ENDIF
I%=1
WHILE I%<=L%
 LPRINT CHR$(C%(I%))
 I%=I%+1
ENDWH
C%(7)=0 :rem reset data-lo
IF S%=1
 GOTO SETUP1::
ENDIF
IF Q%=0 :GOTO START:: :ENDIF

rem query
IN%(1)=ASC(TRIG$:(1,1,CHR$(239))) :rem send last byte of packet to trigger response
I%=2
WHILE I%<=10
 IN%(I%)=ASC(LINPUT$:(1,1))
 I%=I%+1
ENDWH
DH%=IN%(6)
DL%=IN%(7)
CKH%=IN%(8)
CKL%=IN%(9)
CK = 65535-IN%(2)-IN%(3)-IN%(4)-IN%(5)-IN%(6)-IN%(7)+1
IF CK-CKL%-FLT(CKH%)*256.=0
 rem PRINT "CHECKSUM OK"
ELSE
 PRINT "CHECKSUM BAD!"
 GET
 GOTO START::
ENDIF
IF Q%=2 :GOTO TRACK::
ELSEIF Q%=3 :GOTO MODE::
ELSEIF Q%=4 :GOTO EQ::
ELSEIF Q%=5 :GOTO FDRS::
ELSEIF Q%=6 :GOTO TRKS::
ENDIF

rem Q%=1 Vol?
PRINT "VOL:",DL%
GET
GOTO DONE::

TRACK::
PRINT "TRACK:",DL%+256*DH%
GET
OPEN "A:music",A,track$
POSITION DL%+256.*DH%
D%=DISP(-1,"")
CLOSE
GOTO DONE::

MODE::
PRINT "Mode:",DL% :rem 0-all, 1-folder, 2-single, 3-random
GET
GOTO DONE::

EQ::
PRINT "EQ:",DL% :rem 0-norm, 1-Pop, 2-Rock, 3-Jazz, 4-Classic, 5-base
GET
GOTO DONE::

FDRS::
PRINT "Folders:",DL%
GET
GOTO DONE::

TRKS::
PRINT "Tracks:",DL%+256.*DH%
GET

DONE::
Q%=0 :rem reset Query to none
L%=8 :rem restore full command length
GOTO START::

EDIT: updated code with hex values using $ prefix
You do not have the required permissions to view the files attached to this post.
Last edited by MartinP on Mon Apr 03, 2023 4:57 pm, edited 2 times in total.
amenjet
Posts: 314
Joined: Tue Jan 03, 2023 7:54 pm

Re: MP3 player

Post by amenjet »

interesting. I don't see a speaker in the photos, is this line out/headphones only?

As you are using a comms link, this is a serially controlled device? I seem to remember that it is from previous posts about this.

It's a shame about the track handling and lack of control over such things. have you thought about a Pico plus SD card? It might be fast enough to PWM audio out, and then you'd have complete control over the track files, with names etc. It'd be bigger than that module, though.

Andrew
MartinP
Posts: 55
Joined: Wed Jan 04, 2023 7:51 pm

Re: MP3 player

Post by MartinP »

Andrew,
Although the player is able to drive a 3W speaker, I didn't see much point in a small speaker with poor sound quality. I intend to plug it into external speakers, that way the sound is good, I can't tell much difference between it and other MP3 players I've used.

The communication is serial UART at 9600 baud and 3.3V so that's quite easy to use.

I have thought about using a Pico instead, but that would need software written for it in C, which is fine, but with the DF player all the software can be in OPL which makes it quite appealing, I think. Also, it's small, self contained, and very cheap (less than £4), so I wanted to see what could be done with it. Although I haven't tried this yet, there's an option to read from a USB memory stick in addition to the SD card, so as I have a stack of old USB drives, it would provide a good use for those.
MartinP
amenjet
Posts: 314
Joined: Tue Jan 03, 2023 7:54 pm

Re: MP3 player

Post by amenjet »

Yes, the Pico idea would need a fair amount of code. Is the module you are using reprogrammable in any way? Could the firmware be improved?

Andrew
MartinP
Posts: 55
Joined: Wed Jan 04, 2023 7:51 pm

Re: MP3 player

Post by MartinP »

The DF Player firmware is not reprogrammable, see further info here: https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299
A pico could provide some improvements, but would require putting together various bits of code from the web.
Martin P.
Daren
Posts: 78
Joined: Tue Jan 03, 2023 10:03 pm

Re: MP3 player

Post by Daren »

Great stuff Martin! I will be interested to try this out when I get the time, in the meantime a few ideas/thoughts.

It would be cool to have playlists and maybe random playback within a folder/playlist/globally?
I like the idea of the handling of file names taken care of by the Psion.
There are a few PC utilities which can output the names of files in a folder as text, handy for making the titles, a utility called bulk renamer could also be useful here - maybe not needed with your DF helper?
Some UDGs for the transport controls might be nice, maybe a few characters from the standard set like the arrows can be used for volume, prev/next etc.
Maybe have playlists stored on datapaks - not sure about max number of files on DF or Psion, or indeed how many are practical to have, but maybe a consideration?

But regardless, a really interesting project already, well done mate!
MartinP
Posts: 55
Joined: Wed Jan 04, 2023 7:51 pm

Re: MP3 player

Post by MartinP »

Daren,
Thanks for the positive feedback. I took a look at that bulk rename software and it seems useful. I did think about playlists and the player already has firmware to do random play or folder repeat play. To do a custom playlist would require the Psion to keep checking for the end of a track, which would prevent it from doing anything else at the same time, but maybe that's ok. Perhaps there's some way to generate an interrupt based on serial input, but that's not a standard part of OPL.
Martin P.
Lostgallifreyan
Posts: 90
Joined: Thu Jan 12, 2023 8:25 pm
Contact:

Re: MP3 player

Post by Lostgallifreyan »

I'll be watching this at times. I bought a tiny MP3 player board on Ebay once, it's still in its anti-static wrap! If the Organiser can make proper use of it, I will be doing it.
okto
Posts: 61
Joined: Sun Apr 23, 2023 5:14 am

Re: MP3 player

Post by okto »

Very cool stuff. I've been thinking about something similar.
Post Reply