QB64 Project Site

Unseen Machine's QB64 Project Site

Pure MP3

This section is dedicated to my work on my mp3 player. PureMp3. 

I am currently working on a .03.1 version which will have vastly improved capabilities as well as looking and working a lot better. For the time being here is the source code for PureMp3 v.02.1

Pure Mp3 v.02.1

REM PureMp3 - Version .02.1 - By Unseen Machine

DIM SHARED tracknum AS INTEGER, mp3cnt AS INTEGER, updateflag AS INTEGER, arl AS INTEGER, btnval AS INTEGER, h AS LONG
DIM SHARED ltrcnt AS INTEGER, bckspcval AS INTEGER, playval AS INTEGER
DIM SHARED cnt AS INTEGER, mastervol AS DOUBLE, clr AS INTEGER, keydown(127) AS LONG
DIM SHARED fnt AS LONG, scrn AS LONG, shuffle AS INTEGER
DIM SHARED btnl AS INTEGER, btnr AS INTEGER, oldvol AS DOUBLE
DIM SHARED listbox AS INTEGER, ctrack AS INTEGER

_TITLE "PURE Mp3 Version.02.1"
fnt = _LOADFONT("C:\windows\fonts\comic.ttf", 16)

mastervol = .4

CALL createMp3
CALL drawscreen
CALL mp3count
DIM SHARED shuffleflag(1 TO mp3cnt) AS INTEGER
DIM SHARED mp3s(1 TO mp3cnt) AS STRING
CALL loadmp3array
CALL volpaint


updateflag = 1: arl = 4: tracknum = 0

ON ERROR GOTO errorhandler

RANDOMIZE TIMER

OPEN "c:\mp3list.txt" FOR INPUT AS #1

DO

        REDIM SHARED track(1 TO 10) AS STRING
        REDIM SHARED trackname(1 TO 10) AS STRING
        CALL loadlist
        CALL userinput
        _DELAY .01
        _LIMIT 30

LOOP

errorhandler:
LOCATE 18, 10: PRINT SPACE$(320)
LOCATE 18, 10: PRINT "An unknown error has occurred...restarting in 5 seconds."
SLEEP 5
RUN

REM SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS SUBS

SUB volpaint

DIM lcnt AS INTEGER, vlx AS INTEGER, vcomp AS DOUBLE, bclr AS INTEGER

vcomp = 0: lcnt = 0: vlx = 640

DO

        vcomp = vcomp + .1
        IF lcnt <= 4 THEN clr = 2
        IF lcnt >= 5 AND lcnt <= 7 THEN clr = 6
        IF lcnt >= 8 THEN clr = 4

        IF vcomp <= mastervol THEN

                PAINT (vlx, 320), clr, 7

        ELSEIF vcomp > mastervol THEN

                PAINT (vlx, 320), 0, 7

        END IF

        lcnt = lcnt + 1
        vlx = vlx + 16

LOOP UNTIL lcnt = 10

END SUB


SUB loadlist
DIM ucnt AS INTEGER

_FONT fnt

IF updateflag = 2 THEN

        CLOSE #1
        IF shuffle = 0 THEN

                tracknum = tracknum - 20
                IF tracknum < 1 THEN tracknum = mp3cnt - 10

        END IF

        OPEN "c:\mp3list.txt" FOR INPUT AS #1
        ucnt = 0

        DO

                LINE INPUT #1, mp3file$
                ucnt = ucnt + 1

        LOOP UNTIL ucnt = tracknum

        updateflag = 1

END IF

IF updateflag = 1 THEN

        cnt = 0

        DO
                IF EOF(1) THEN

                        CLOSE #1
                        OPEN "c:\mp3list.txt" FOR INPUT AS #1
                        tracknum = 0
                        cnt = 0

                END IF

                cnt = cnt + 1
                LINE INPUT #1, mp3file$
                trackname(cnt) = mp3file$

                IF tracknum < mp3cnt THEN tracknum = tracknum + 1

                ltrcnt = LEN(trackname(cnt))

                DO

                        ltrcnt = ltrcnt - 1
                        ltr$ = MID$(trackname(cnt), ltrcnt, 1)
                        IF ltr$ = "\" THEN
                                bckspcval = ltrcnt
                                EXIT DO
                        END IF

                LOOP UNTIL ltrcnt = 0

                track(cnt) = RIGHT$(mp3file$, (LEN(mp3file$) - ltrcnt))
                COLOR 15
                LOCATE 3 + cnt, 90: PRINT SPACE$(120)
                LOCATE 3 + cnt, 90: PRINT LEFT$(track(cnt), 55)

                IF mp3cnt < 10 THEN IF cnt = mp3cnt THEN EXIT DO

        LOOP UNTIL cnt = 10

END IF

updateflag = 0

END SUB

SUB shufflehandler

IF shuffle = 1 THEN

        DO

                scnt = 0
                DO

                        scnt = scnt + 1
                        tracknum = INT(RND * mp3cnt) + 1

                LOOP UNTIL shuffleflag(tracknum) = 0 OR scnt = mp3cnt

                IF scnt = mp3cnt THEN REDIM shuffleflag(mp3cnt) AS INTEGER ELSE shuffleflag(tracknum) = 1

        LOOP UNTIL shuffleflag(tracknum) = 1
        updateflag = 2
        CALL loadlist

END IF

END SUB

SUB userinput
LOCATE 18, 10: PRINT SPACE$(320)
LOCATE 18, 10: PRINT "Track details : "; trackname(arl - 3)
DO

        _LIMIT 30

        tname$ = trackname(arl - 3)
        btnl = 90: btnr = 170
        cnt = 0: btnval = 0
        oldvol = mastervol

        DO WHILE _MOUSEINPUT

                'LOCATE 1, 1: PRINT "Mouse X : "; _MOUSEX; "Mouse Y : "; _MOUSEY

                IF _MOUSEBUTTON(1) = -1 THEN

                        IF _MOUSEX >= 465 AND _MOUSEX <= 525 THEN

                                'delete button - only apples to track list - use remove for playlist
                                IF _MOUSEY >= 76 AND _MOUSEY <= 96 THEN

                                        CLOSE #1
                                        OPEN "c:\MP3LIST.TXT" FOR OUTPUT AS #1

                                        FOR a = 1 TO mp3cnt

                                                IF mp3s(a) <> trackname(arl - 3) THEN PRINT #1, mp3s(a)

                                        NEXT
                                        CLOSE #1

                                        mp3cnt = 0
                                        CALL mp3count
                                        CALL loadmp3array
                                        updateflag = 2: tracknum = 1
                                        CALL loadlist

                                END IF

                                'shuffle button
                                IF _MOUSEY >= 205 AND _MOUSEY <= 225 THEN

                                        IF shuffle = 0 THEN

                                                shuffle = 1
                                                COLOR 4: LOCATE 14, 475: PRINT "Shuffle": COLOR 7

                                        ELSE

                                                shuffle = 0
                                                COLOR 15: LOCATE 14, 475: PRINT "Shuffle": COLOR 7

                                        END IF

                                END IF

                        END IF

                        'minus and plus volume buttons
                        IF _MOUSEY >= 300 AND _MOUSEY <= 340 THEN

                                IF _MOUSEX >= 564 AND _MOUSEX <= 624 THEN

                                        IF mastervol > 0 THEN mastervol = mastervol - .1

                                ELSEIF _MOUSEX >= 800 AND _MOUSEX <= 860 THEN

                                        IF mastervol < 1 THEN mastervol = mastervol + .1

                                END IF

                        END IF

                        IF mastervol >= 0 AND mastervol <= 1 AND mastervol <> oldvol THEN

                                _SNDVOL h&, mastervol
                                CALL volpaint

                        END IF

                        REM standard buttons
                        IF _MOUSEY >= 300 AND _MOUSEY <= 340 THEN

                                DO
                                        cnt = cnt + 1

                                        IF _MOUSEX >= btnl AND _MOUSEX <= btnr THEN

                                                btnval = cnt
                                                EXIT DO

                                        ELSE

                                                btnl = btnl + 90: btnr = btnr + 90

                                        END IF

                                LOOP UNTIL cnt = 5

                        END IF

                END IF

                IF btnval > 0 THEN CALL buttonhandler

        LOOP

        kb$ = INKEY$

        SELECT CASE kb$

                CASE CHR$(0) + CHR$(72)

                        LOCATE arl, 70: PRINT "  "
                        IF arl > 4 THEN
                                arl = arl - 1
                        ELSE
                                arl = 13
                                updateflag = 2
                                EXIT DO
                        END IF

                CASE CHR$(0) + CHR$(80)

                        LOCATE arl, 70: PRINT "  "
                        IF arl < 13 THEN
                                arl = arl + 1
                        ELSE
                                arl = 4
                                updateflag = 1
                                EXIT DO
                        END IF

                CASE CHR$(13) 'play/stop

                        IF playval = 1 THEN btnval = 3
                        IF playval = 0 THEN btnval = 2
                        CALL buttonhandler

                CASE CHR$(43) 'volup

                        oldvol = mastervol
                        IF mastervol < 1 THEN mastervol = mastervol + .1
                        IF mastervol <= 1 THEN _SNDVOL h&, mastervol
                        CALL volpaint

                CASE CHR$(45) 'voldown

                        oldvol = mastervol
                        IF mastervol > 0 THEN mastervol = mastervol - .1
                        IF mastervol > 0 THEN _SNDVOL h&, mastervol
                        CALL volpaint

                CASE CHR$(0) + CHR$(77) 'next track button

                        btnval = 5
                        CALL buttonhandler

                CASE CHR$(0) + CHR$(75) 'previuos track button

                        btnval = 4
                        CALL buttonhandler

                CASE CHR$(27)

                        SYSTEM

                CASE CHR$(32) 'space bar - pause/unpause

                        btnval = 1
                        CALL buttonhandler

                CASE "M" 'mute

                        mastervol = 0
                        _SNDVOL h&, mastervol
                        CALL volpaint

                CASE "m"

                        mastervol = 0
                        _SNDVOL h&, mastervol
                        CALL volpaint

                CASE "s" 'shuffle

                        IF shuffle = 0 THEN

                                shuffle = 1
                                COLOR 4: LOCATE 14, 475: PRINT "Shuffle": COLOR 7

                        ELSE

                                shuffle = 0
                                COLOR 15: LOCATE 14, 475: PRINT "Shuffle": COLOR 7

                        END IF

                CASE "S"

                        IF shuffle = 0 THEN

                                shuffle = 1
                                COLOR 4: LOCATE 14, 475: PRINT "Shuffle": COLOR 7

                        ELSE

                                shuffle = 0
                                COLOR 15: LOCATE 14, 475: PRINT "Shuffle": COLOR 7

                        END IF

        END SELECT

        LOCATE arl, 70: PRINT CHR$(175)

        IF tname$ <> trackname$(arl - 3) THEN
                LOCATE 18, 10: PRINT SPACE$(320)
                LOCATE 18, 10: PRINT "Track details : "; LEFT$(trackname(arl - 3), 120)
        END IF

        IF _SNDPLAYING(h&) = 0 AND playval = 1 AND shuffle = 0 THEN

                LOCATE arl, 70: PRINT "  "

                IF arl < 13 THEN

                        arl = arl + 1

                ELSE

                        arl = 4
                        updateflag = 1
                        EXIT DO

                END IF

                CALL playmp3

        ELSEIF _SNDPLAYING(h&) = 0 AND playval = 1 AND shuffle = 1 THEN

                CALL shufflehandler
                CALL playmp3

        END IF

LOOP
END SUB


SUB buttonhandler

IF btnval = 1 THEN 'pause

        IF _SNDPLAYING(h&) = -1 THEN

                _SNDPAUSE h&
                playval = 0

        ELSEIF _SNDPAUSED(h&) = -1 THEN

                _SNDPLAY (h&)
                playval = 1

        END IF

ELSEIF btnval = 2 THEN 'play

        IF _SNDPAUSED(h&) <> -1 THEN

                IF mastervol = 0 THEN mastervol = .1
                _SNDSTOP h&
                CALL playmp3
                CALL volpaint
                playval = 1

        ELSE

                _SNDPLAY (h&)
                playval = 1

        END IF

ELSEIF btnval = 3 THEN 'stop

        _SNDSTOP h&
        playval = 0

ELSEIF btnval = 4 THEN 'previous


        IF shuffle = 0 THEN

                LOCATE arl, 70: PRINT "  "

                IF arl > 4 THEN

                        arl = arl - 1

                ELSE

                        arl = 13
                        updateflag = 2
                        CALL loadlist

                END IF

        ELSE

                CALL shufflehandler

        END IF

        IF playval = 1 THEN CALL playmp3

ELSEIF btnval = 5 THEN 'next


        IF shuffle = 0 THEN

                LOCATE arl, 70: PRINT "  "

                IF arl < 13 THEN

                        arl = arl + 1

                ELSE
                        arl = 4
                        updateflag = 1
                        CALL loadlist

                END IF

        ELSE

                CALL shufflehandler

        END IF

        IF playval = 1 THEN CALL playmp3

END IF

END SUB

SUB drawscreen

SCREEN _NEWIMAGE(1000, 400, 9): CLS

DIM cnt AS INTEGER, btnx AS INTEGER, vcnt AS INTEGER, vx AS INTEGER
button$ = "l40 u40 r80 d40 l40"
btnx = 130: vx = 640


COLOR 7

DO

        PSET (vx, 337): DRAW "l6u34r12d34l6"
        vx = vx + 16
        vcnt = vcnt + 1

LOOP UNTIL vcnt = 10

PSET (594, 340), 3: DRAW "L30u40r60d40l30 bu20 nl10nr10"
PSET (830, 340), 3: DRAW "L30u40r60d40l30bu20 nl10nr10nu10d10"

DO

        IF cnt = 0 THEN

                PSET (btnx, 340), 2: DRAW "Bu10bl8 u20r5d20l5 br15 l5u20r5d20"

        ELSEIF cnt = 1 THEN

                PSET (btnx, 340), 2: DRAW "Bu10bl5 u20f10g10"

        ELSEIF cnt = 2 THEN

                PSET (btnx, 340), 12: DRAW "Bu10bl10 u20r20d20l20 "
                PAINT (btnx, 325), 4, 12

        ELSEIF cnt = 3 THEN

                PSET (btnx, 340), 2: DRAW "Bu10br8 u20g10u10g10f10u10f10"

        ELSEIF cnt = 4 THEN

                PSET (btnx, 340), 2: DRAW "Bu10bl8 u20f10u10f10g10u10g10"

        END IF

        PSET (btnx, 340), 3: DRAW button$

        btnx = btnx + 90
        cnt = cnt + 1

LOOP UNTIL cnt = 5

_FONT fnt

by = 66
FOR i = 1 TO 6

        PSET (495, by), 3: DRAW "L30 u20 r60d20l30"
        by = by + 32

NEXT
COLOR 8
LOCATE 4, 480: PRINT "  Add"
LOCATE 8, 480: PRINT "  New"
LOCATE 10, 480: PRINT " Load "
LOCATE 12, 480: PRINT "Delete"
COLOR 15
LOCATE 6, 475: PRINT "Remove"
LOCATE 14, 475: PRINT "Shuffle"
END SUB

SUB createMp3
SCREEN 0
CLS

REM check for music list file if none exist create one from mp3 search

OPEN "C:\mp3list.txt" FOR RANDOM AS #1

IF LOF(1) > 1 THEN

        CLOSE #1

ELSE

        CLOSE #1
        OPEN "C:\mp3crt.bat" FOR OUTPUT AS #1
        PRINT #1, "ECHO OFF"
        PRINT #1, "CLS"
        PRINT #1, "ECHO Creating mp3 file list"
        PRINT #1, "cd\"
        PRINT #1, "cmd /c dir *.mp3 /b /s > C:\mp3list.txt"
        CLOSE #1
        PRINT "Searching for Mp3's..."
        SHELL _HIDE "c:\mp3crt.bat"
        KILL "c:\mp3crt.bat"

END IF

END SUB

SUB mp3count
'count mp3 files

OPEN "c:\mp3list.txt" FOR INPUT AS #1

DO

        LINE INPUT #1, mp3file$
        mp3cnt = mp3cnt + 1

LOOP UNTIL EOF(1)

CLOSE #1
END SUB

SUB loadmp3array

OPEN "c:\mp3list.txt" FOR INPUT AS #1

FOR i = 1 TO mp3cnt

        LINE INPUT #1, mp3file$
        mp3s(i) = mp3file$

NEXT
CLOSE #1
END SUB

SUB playmp3
IF mastervol <= 0 THEN mastervol = .1
_SNDSTOP h&
_SNDCLOSE h&
_DELAY .05
h& = _SNDOPEN(trackname(arl - 3), "vol,pause")
_SNDVOL h&, mastervol
IF h& THEN _SNDPLAY h&
playval = 1
END SUB