Building my own mp3 player in MMBasic

Hi there,

I’m trying to build my own MP3 player.

Everything works fine as long as I know the list of titles in advance.

However, when dealing with unknown folders, I have a question:

How can I store the filenames from a folder into an array in MMBasic?

I’ve tried using ChatGPT and Copilot, but neither provided a working solution.

Try the PicoMite manual. AI is kinda useless for esoteric information right now.

Here’s a simple code snippet.

dim file$(25)
f1=1
f$=Dir$("*.mp3",FILE)
Do While f$<>""
   file$(f1)=f$
   f$=Dir$()
   f1=f1+1
Loop
2 Likes

wow thank you very much - it works !!

here is my state of work :slight_smile:
the Bitrate of the mp3 is importent for the correct lenght suggestion

in this code i used a 320kbit mp3

The left and right arrow switch trough the tracks

'music
x=1
y=0
tmp$=""
Dim mp3$(100)

f1=1
f$=Dir$("*.mp3",FILE)
Do While f$<>""
   mp3$(f1)=f$
   f$=Dir$()
   f1=f1+1
Loop

Do while x <  f1
  CLS
  Print "########################"
  Print "####### SiD Music ######"
  Print "########################"
  Print " "
  Print mp3$(x) + ".mp3"
  Open mp3$(x)+".mp3" For input As #1
  y = Lof(1)
  Close #1
  Play mp3 mp3$(x)
  z=Cint(((y*8)/320000))

  Do While z > 0
     k$ = Inkey$

    If k$ <> "" Then
      Select Case Asc(k$)
        Case 131: Play stop
                  z=0
        Case 130: Play stop
                  x = x-2
                  z=0
        Case 32: Play stop
      End Select
    End If

    Pause 100
    z=z-1
    min = Int(z / 60)
    sec = z Mod 60
    If sec < 10 Then
      tmp$ = Str$(min)+":0"+Str$(sec)+"  "
    Else
      tmp$ = Str$(min)+":"+Str$(sec)+"   "
    EndIf
    Print @(0,60) tmp$
    Pause (900)
   Loop
  x=x+1
Loop
5 Likes

3 Likes

I made a few changes to your mp3 player, so I’m posting the code here in case you or others are interested.

  1. Reads .mp3 files from b:/mp3/ (to avoid cluttering the root directory)
  2. Randomizes the list of mp3 files (because listening in order may be boring)
  3. “space” stops the player and exits (before it just muted it but kept going)

I also found out that OPTION CPUSPEED 200000 is required with the PI Pico 2w, in case others get an error about insufficient CPU speed.

'music
x=1
y=0
tmp$=""
Dim mp3$(100)

Drive "b:"
Chdir "b:/mp3"

f1=1
f$=Dir$("*.mp3",FILE)
Do While f$<>""
   mp3$(f1)=f$
   f$=Dir$()
   f1=f1+1
Loop

Randomize
For i=1 To f1 - 1
   k$ = mp3$(i)
   j = Rnd(f1 - 1) + 1
   mp3$(i) = mp3$(j)
   mp3$(j) = k$
Next

Do While x <  f1
  CLS
  Print "########################"
  Print "####### SiD Music ######"
  Print "########################"
  Print " "
  Print mp3$(x)
  Open mp3$(x) For input As #1
  y = Lof(1)
  Close #1
  Play stop
  Pause 1000
  Play mp3 mp3$(x)
  z=Cint(((y*8)/320000))

  Do While z > 0
     k$ = Inkey$

    If k$ <> "" Then
      Select Case Asc(k$)
        Case 131: Play stop
                  z=0
        Case 130: Play stop
                  x = x-2
                  z=0
        Case 32: Play stop
                 x=f1
                 z=0
      End Select
    End If

    Pause 100
    z=z-1
    min = Int(z / 60)
    sec = z Mod 60
    If sec < 10 Then
      tmp$ = Str$(min)+":0"+Str$(sec)+"  "
    Else
      tmp$ = Str$(min)+":"+Str$(sec)+"   "
    EndIf
    Print @(0,60) tmp$
    Pause (900)
   Loop
  x=x+1
Loop

Enjoy!

3 Likes

Hi :slight_smile:
Thanks for your reply. I reworked my code myself a few days ago and added a path variable for the music folder too. I added a Cover view and a better refresh (with no blinking while changing tracks).

Furthermore, I don’t like random music shuffling, but I like to have the choice, so I take your random function as a commentary - so everybody can decide to use it or not.

You are right here

adcockm
said you can go up to 380000 with a Pico 2 - and he is right.

My new Code with

'music
x=1
y=0
tmp$=""
Dim mp3$(100)
path$ = "b:\music\"


f1=1
f$=Dir$(path$ + "*.mp3",FILE)
cover$ = "cover.bmp"

Do While f$<>""
   mp3$(f1)=path$+f$
   f$=Dir$()
   f1=f1+1
Loop


'Randomize
'For i=1 To f1 - 1
'   k$ = mp3$(i)
'   j = Rnd(f1 - 1) + 1
'   mp3$(i) = mp3$(j)
'   mp3$(j) = k$
'Next


CLS
On error skip 1
Load image(path$ + cover$)
Print "########################"
Print "####### SiD Music ######"
Print "########################"
Do While x < f1
  Print @(0,40) mp3$(x)
  Open mp3$(x) For input As #1
  y = Lof(1)
  Close #1
  Play mp3 mp3$(x)
  z=Cint(((y*8)/320000))

  Do While z > 0
     k$ = Inkey$

    If k$ <> "" Then
      Select Case Asc(k$)
        Case 131: Play stop
                  z=0
        Case 130: Play stop
                  x = x-2
                  z=0
        Case 32: Play stop
                        CLS
	                x=f1
                        z=0
      End Select
    End If

    Pause 100
    z=z-1
    min = Int(z / 60)
    sec = z Mod 60
    If sec < 10 Then
      tmp$ = Str$(min)+":0"+Str$(sec)+"  "
    Else
      tmp$ = Str$(min)+":"+Str$(sec)+"   "
    EndIf
    Print @(0,60) tmp$
    Pause (900)
   Loop
  Play stop
  x=x+1
Loop
2 Likes

And a new version.

Now you can choose to shuffle or not from the program :slight_smile:
Press 1 toggle between shuffle and no shuffle - the choice is saved in a txt file in the music folder.

To avoid the false length calculation, a bitrate.txt file with 1 line (e.g. 320000) for the bitrate of the music folder can be used.

Next step will be to shown the folder structure to choose the music folder from the app.

'music
x=1
y=0
tmp$=""
Dim mp3$(100)
path$ = "b:\music2\"
shuffle$ = "false"
bitrate$ = "320000"


f1=1
f$=Dir$(path$ + "*.mp3",FILE)
cover$ = "cover.bmp"

Do While f$<>""
   mp3$(f1)=path$+f$
   f$=Dir$()
   f1=f1+1
Loop

On error skip 3
Open (path$ + "bitrate.txt") For input As #1
Line Input #1, bitrate$
Close #1
On error abort

On error skip 3
Open (path$ + "shuffle.txt") For input As #1
Line Input #1, shuffle$
Close #1
On error abort

If shuffle$ = "true" Then
 doshuffle
EndIf

Sub doshuffle
 Randomize
 For i=1 To f1 - 1
 k$ = mp3$(i)
 j = Int(Rnd*(f1 - 1)) + 1
 mp3$(i) = mp3$(j)
 mp3$(j) = k$
 Next
End Sub

Sub unshuffle
  f1=1
  f$=Dir$(path$ + "*.mp3",FILE)
  Do While f$<>""
    mp3$(f1)=path$+f$
    f$=Dir$()
    f1=f1+1
  Loop
End Sub

Sub writeshuffle
 Open path$ + "shuffle.txt" For OUTPUT As #1
 If shuffle$ = "true" Then
  Print #1, "false"
Print @(0,36)"########################"
 shuffle$ = "false"
 unshuffle
 Else
  Print #1, "true"
Print @(0,36)"###############shuffle##"
  shuffle$ = "true"
  doshuffle
 EndIf
 Close #1
End Sub

CLS
On error skip 1
Load image(path$ + cover$)
On error abort
Print "########################"
Print "####### SiD Music ######"
Print "########################"
Do While x < f1
 If shuffle$ = "true" Then
  Print @(0,36)"###############shuffle##"
 Else
  Print @(0,36)"########################"
 EndIf
 Print @(0,55) mp3$(x) + "             "
 Open mp3$(x) For input As #2
 y = Lof(2)
 Close #2
On error skip 1
 Play mp3 mp3$(x)
On error abort
 z=Cint(((y*8)/Val(bitrate$)))

 Do While z > 0
  k$ = Inkey$

 If k$ <> "" Then
  Select Case Asc(k$)
  Case 131: Play stop
     z=0
  Case 130: Play stop
     x = x-2
     z=0
  Case 32: Play stop
     CLS
     x=f1
     z=0
  Case 49
     x=0
     writeshuffle
  End Select
 End If

    Pause 100
    z=z-1
    min = Int(z / 60)
    sec = z Mod 60
    If sec < 10 Then
      tmp$ = Str$(min)+":0"+Str$(sec)+"  "
    Else
      tmp$ = Str$(min)+":"+Str$(sec)+"   "
    EndIf
    Print @(0,65) tmp$
    Pause (900)
   Loop
  Play stop
  x=x+1
Loop

3 Likes

I read the manual :wink: and found the interrupt for MP3 Playback.

  • no Bitrate Infos are needed
  • skip small files (i have some ._filename.mp3 in some folders)
'music
x=1
y=0
tmp$=""
Dim mp3$(100)
path$ = "b:\music3\"
shuffle$ = "false"

f1=1
f$=Dir$(path$ + "*.mp3",FILE)
cover$ = "cover.bmp"

Do While f$<>""
   mp3$(f1)=path$+f$
   f$=Dir$()
   f1=f1+1
Loop

On error skip 3
Open (path$ + "shuffle.txt") For input As #1
Line Input #1, shuffle$
Close #1
On error abort

If shuffle$ = "true" Then
 doshuffle
EndIf

Sub doshuffle
 Randomize
 For i=1 To f1 - 1
 k$ = mp3$(i)
 j = Int(Rnd*(f1 - 1)) + 1
 mp3$(i) = mp3$(j)
 mp3$(j) = k$
 Next
End Sub

Sub unshuffle
  f1=1
  f$=Dir$(path$ + "*.mp3",FILE)
  Do While f$<>""
    mp3$(f1)=path$+f$
    f$=Dir$()
    f1=f1+1
  Loop
End Sub

Sub nextSong
playSong$ = "false"
End Sub

Sub writeshuffle
 Open path$ + "shuffle.txt" For OUTPUT As #1
 If shuffle$ = "true" Then
  Print #1, "false"
Print @(0,36)"########################"
 shuffle$ = "false"
 unshuffle
 Else
  Print #1, "true"
Print @(0,36)"###############shuffle##"
  shuffle$ = "true"
  doshuffle
 EndIf
 Close #1
End Sub

CLS
On error skip 1
Load image(path$ + cover$)
On error abort
Print "########################"
Print "####### SiD Music ######"
Print "########################"
Do While x < f1
 If shuffle$ = "true" Then
  Print @(0,36)"###############shuffle##"
 Else
  Print @(0,36)"########################"
 EndIf
 Print @(0,55) mp3$(x) + "             "
 Open mp3$(x) For input As #2
 y = Lof(2)
 Close #2
 On error skip 1
 Play mp3 mp3$(x) ,nextSong
 On error abort
 playSong$ = "true"

  Do While playSong$ = "true" and y > 10000
  k$ = Inkey$

 If k$ <> "" Then
  Select Case Asc(k$)
  Case 131: Play stop
     z=0
  Case 130: Play stop
     x = x-2
     z=0
  Case 32: Play stop
     CLS
     x=f1
     z=0
  Case 49
     x=0
     writeshuffle
  End Select
 End If

    Pause (100)
    z=z+1
    min = Int(z / 60)
    sec = z Mod 60
    If sec < 10 Then
      tmp$ = Str$(min)+":0"+Str$(sec)+"  "
    Else
      tmp$ = Str$(min)+":"+Str$(sec)+"   "
    EndIf
    Print @(0,65) tmp$
    Pause (900)
   Loop
  Play stop
  z=0
  x=x+1
Loop
5 Likes