
Rem Roy_Music Player_5
Rem For Android
Rem With OliBasic
Rem May/June 2019
Rem Version 0.02
Rem By Roy Shepherd

rem Credits: Gilles, JuergenS, Danny, Gregor, EPr, Carlos Caldas. Many thanks for help with this app.

! Debug.on
globals.all 
locals.off

di_height = 1100 % set to my Device
di_width = 720

gr.open 255,0,0,0
gr.orientation 1 % Portrait 
pause 3000

 
gr.screen screenWidth, screenHeight
scale_x = screenWidth / di_width
scale_y = screenHeight / di_height
gr.scale scale_x, scale_y

gr.scale.touch scale_x, scale_y, 0,0,1

!-------------------------------------------------
! Let Basic see the functions before they are called
!-------------------------------------------------
gosub Functions

gosub Initialise
gosub LoadSounds
gosub LoadImages
gosub DrawButtons
call SplashScreen()
call Render()
 
do 

	call Buttons()
	gosub DancingBars
until 0
!------------------------------------------------
! Double tap back key to end
!------------------------------------------------
onBackKey:
	backKeyHit = BackKey(backKeyHit)
back.resume 

!------------------------------------------------
! Do every seconed 
!------------------------------------------------
onTimer:
	call UpDateSongTime()
	call UpdateClock()
	!call Banner()
	audio.isdone songEnded
	if  songPlaying & songEnded then call GetNextSong()
	WakeLock 1
timer.resume

!------------------------------------------------
! Do once at first run of app
!------------------------------------------------
Initialise:
	
	timer.set 1000
	
	!Global variables 
	dataPath$ = "musicPlayer/"
	internalPath$ = "/../../Music"
	SDpath$ = "" : path$ =""
	! if no Music folder then make one
	 file.exists pathPresent,internalPath$
    if ! pathPresent then file.mkdir internalPath$
	
	list.create s,ldir
	tick = time() : backKeyHit = 1 % for double tap back key to exit
    FALSE = 0 : TRUE = 1 : buzz = TRUE
	loadingData = TRUE
    red = 0 : green = 99 : blue = 254
    sound = TRUE  : buzz = TRUE
	songRepeat = FALSE : songShuffle = FALSE
	songPlaying = FALSE : songLength = 0
	thisSong = 0 : numOfSongs = 0
	songToPlay = 0 : songEnded = False
	stopStartSong = FALSE 
	externalSD = FALSE
	animation = TRUE
	 _pauseSong = FALSE 
	answer = 0 : filename$ = ""
	songBanner$ = ""
	call LoadData()
	
	!SetUp background
	gr.color 255,red,green,blue,1
	gr.rect background, 0, 0, 800,1200
	!gr.get.value background, "paint", pbg
	
	 !For when a button is tapped or a song
	gr.color 0,0,0,0
    gr.point collision, - 1, - 1 
	gr.point butCollision, -1, -1
	gr.color 255,255,255,255

	 array.load buzzGame[], 1, 100
	 
return
!-------------------------------------------------
! Load, scale and draw the bitmaps. Draw top rectangle 
!Draw the bars 
!-------------------------------------------------
LoadImages:
	!colout pallet
	gr.bitmap.load palletPrt, dataPath$ + "colourPallet.png"
	if cp =-1 then call ImageLoadError("colourPallet")
	gr.bitmap.draw colourPallet, palletPrt,210,500
	gr.hide colourPallet
	
	!music panel
	gr.bitmap.load p, dataPath$ + "musicPanel2.png"
	if p =-1 then call ImageLoadError("musicPanel")
	!gr.bitmap.scale panel,p,720,250
	gr.bitmap.draw musicPanel, p,0,809
	
	!pause button
	gr.bitmap.load bp, dataPath$ + "butPause.png"
	if bp =-1 then call ImageLoadError("butPause")
	gr.bitmap.draw imaPause, bp, 293,957
	!gr.hide imaPause
	
	!Draw top box
	gr.color 255,100,100,100,1
	gr.rect top,0,0,720,60 : gr.set.stroke 4
	
	!Draw menu button (three dots)
	gr.color 0,255,255,255,1
	gr.rect butMenu,650,0,720,50
	gr.color 255
	gr.point null, 670,20
	gr.point null, 670,30
	gr.point null, 670,40
	
	!Draw text in top box
	gr.set.stroke 1
	gr.text.size 25 : gr.text.align 2
	gr.text.draw singer, 360,20, "Song"
	!gr.text.draw title, 360,50,"Title"
	
	! Display the Folders
	gr.bitmap.load in, dataPath$ + "InternalFolder.png"
	if in =-1 then call ImageLoadError("InternalFolder")
	gr.bitmap.draw imaInternal, in, 50,50
	
	gr.bitmap.load sd, dataPath$ + "SDCardFolder.png"
	if sd =-1 then call ImageLoadError("SDCardFolder")
	gr.bitmap.draw imaSDCard, sd, 520,50
	externalSD = IsThereSDCard()
	if !externalSD then gr.hide imaSDCard else SDcard$ = RemovableCard$()
	
	!Do the clock
	 gr.text.draw musicClock, 40,50, left$(using$(,"%tT",time()),5)
	
	!Text played so far 
	gr.color 255,255,255,255
	gr.text.size 20  : gr.text.align 1
	gr.text.draw txtSoFar,25,860, int$(0)
	
	!Print Repeat
	gr.text.draw r,220,835,"Repeat"
	!Print Shuffle 
	gr.text.draw s,440,835,"Shuffle"
	
	!Text Duration of song
	gr.text.align 3
	gr.text.draw txtDuration, 695,860, int$(0)
	gr.text.align 1
	
	!Draw the bars 
	List.create n, animBars
	dim bars[8], barMove[8]
	c = 1
	for x = 160 to 520 step 50
		barMove[c] = floor(600 * rnd())
		gr.color 255, floor(200 * rnd() + 50), floor(200 * rnd() + 50), floor(200 * rnd() + 50)
		gr.rect bars[c], x, 810, x + 48,810 - barMove[c] - 10
		c ++
	next
	
	List.add.array animBars, bars[1,8]
	gr.group.list theBars,animBars
	if ! animation then gr.hide theBars
return 

!-------------------------------------------------
! Draw the buttons and Time played so far and Duration of song, text
! Draw the seek bar
!-------------------------------------------------
DrawButtons:
	gr.color 0,255,255,255,0
	!Repeat button
	gr.rect butRepeat,205,832,310,885
	!Shuffle button
	gr.rect butShuffle,425,832,530,885
	!Previoue button
	gr.rect butPrevioue,70,985,150,1052
	!Back 10 seconeds button
	gr.rect butBackTen,185,985,265,1052
	!Pause button
	gr.rect butPause,300,960,433,1075
	!Play button
	gr.rect butPlay,300,960,433,1075
	!Forward ten seconed
	gr.rect butForwardTen,470,980,550,1052
	!Next button
	gr.rect butNext,570,980,650,1052
	!Seak button
	gr.rect butSeak,90,900,645,920
	!Repeat light on 
	gr.color 255,0,255,0,1
	gr.circle greenRepeatLight, 190,860, 5 : gr.hide greenRepeatLight
	!Shuffle light onl 
	gr.circle greenShuffleLight, 410,860,5 : gr.hide greenShuffleLight
	
	!Draw the seek Bar
	gr.color 255.0,0,0,1
	gr.rect null,95,895,645,915
	gr.color 255,255,255,255,0
	gr.rect null,94,895,645,915
	gr.color 255,255,255,0,1
	gr.rect seek1, 95,896,95,914
	gr.rect seek2, 90,890,100,918,5,5
	gr.color 0
	gr.rect butSeek,95,895,645,915
	gr.color 255
	
return

!-------------------------------------------------
! The bars move up and down
!-------------------------------------------------
DancingBars:
	if !songPlaying then return
	if ! animation then return
	
	for x = 1 to 8 
		barMove[x] = floor(600 * rnd())
	next
	array.shuffle bars[]
	for x = 1 to 8
		gr.touch t,null,null
		if t then f_n.break
		gr.modify bars[x],"bottom",810, "top",810 - barMove[x]
		call Render()
	next
	if t then return
	for x = 1 to 25
		gr.touch t,null,null
		if t then f_n.break
		pause 1
	next
	
return

!-------------------------------------------------
! Load sounds
!-------------------------------------------------
LoadSounds:
	soundpool.open 1
	 soundpool.load click, dataPath$ + "click.wav"
    if ! click then call SoundLoadError("click") 
	array.load buzzGame[], 1, 100
return 

!-------------------------------------------------
! All the functions for the app start here
!-------------------------------------------------
Functions:

!-------------------------------------------------
! Select a song
!-------------------------------------------------
fn.def SelectSong$(p$,card)
	fex = FALSE
	_pauseCalled = FALSE
	if songPlaying &  ! _pauseSong then call SongPause() : _pauseCalled = TRUE
	path$ = p$
	 DO
		path$ = GetDir$(path$,ldir,card)
		DIALOG.SELECT answer,ldir,path$
		if !answer then d_u.break
		LIST.GET ldir,answer,filename$
		IF filename$=".."
		  IF ENDS_WITH("/..",path$)|path$=""
			path$+="/.."
		  ELSE
			path$ =MID$(path$,1,IS_IN("/",path$,-1)-1)
		  ENDIF
		ELSEIF ENDS_WITH("(d)",filename$)
		  path$+="/"+MID$(filename$,1,LEN(filename$)-3)
		ELSE
		  FILE.EXISTS fex,path$+"/"+filename$
		ENDIF
	  UNTIL fex 
		call PlaySound(click)
	  if _pauseCalled then call SongPause()
	 if !answer then fn.rtn path$
	 
	songToPlay = answer	  
	 call PlaySong(songToPlay)
	 fn.rtn path$
fn.end

!------------------------------------------------
!Play Selected Songs
!------------------------------------------------
fn.def PlaySong(PlayThisSong)
	songPlaying = FALSE
	if _pauseSong then call SongPause() 
	!songBanner$ = filename$ + " "
	gr.modify singer,"text",left$(filename$,50) : call Render()
	audio.stop
	AUDIO.LOAD PlayThisSong,path$+"/"+ filename$

	stopStartSong = PlayThisSong % needed for pausing song

	audio.play PlayThisSong
	audio.length songLength, PlayThisSong 
	songPlaying = TRUE
	call SongDuration(songLength)
	
fn.end 

!------------------------------------------------
!Get the next song, see if repeat or shuffle is on
!------------------------------------------------
fn.def GetNextSong()
	!	?stopStartSong
	audio.stop
	audio.release stopStartSong
	
	!if repeat is on, repeat last song
	if songRepeat then 
		 LIST.GET ldir,answer,filename$
		call PlaySong(answer)
	endif
	if songRepeat then fn.rtn FALSE 
	
	!if shuffle is on. Pick a new song at random, but not the last one that was played
	if songShuffle then 
		 LIST.GET ldir,answer,filename$
		do 
			s = floor(numOfSongs * rnd() + 2)
		until s <> answer
		answer = s 
		 LIST.GET ldir,answer,filename$
		call PlaySong(answer)
	endif 
	if songShuffle then fn.rtn FALSE
	
	! repeat and shuffle are off. Pick the next song in the play list.
	if answer <= numOfSongs then 
		answer ++ 
	else 
		answer = 2
	endif
	 LIST.GET ldir,answer,filename$
	
	call PlaySong(answer)

fn.end 

!------------------------------------------------
!Display the song length 
!------------------------------------------------
fn.def SongDuration(songLength) 
	if ! songPlaying then fn.rtn FALSE
	songLength$ = ConvertMilliseconds$(songLength) 
	gr.modify txtDuration,"text",songLength$
	call Render()

fn.end 

!------------------------------------------------
! Update how far along the song as played
!------------------------------------------------
fn.def UpDateSongTime()
	if ! songPlaying then fn.rtn FALSE
	audio.position.current ptn
	playedSoFar$ = ConvertMilliseconds$(ptn)
	
	gr.modify txtSoFar,"text",playedSoFar$
	call UpDateSeekBar()
	call Render()
fn.end 

!------------------------------------------------
! Show how far along the song as played with the seak bar
!Thank to JuergenS for the help with the maths 
!------------------------------------------------
fn.def UpDateSeekBar() 
	if ! songPlaying then fn.rtn FALSE	
	audio.position.current ptn
	seek = ptn / songLength * 550 
	gr.modify seek1,"right",95 + seek
	gr.modify seek2,"left",90 + seek,"right",100 + seek

fn.end 

!------------------------------------------------
! Update the clock
!------------------------------------------------
fn.def UpdateClock()
	gr.modify musicClock, "text", left$(using$(,"%tT",time()),5)
fn.end 
 
!------------------------------------------------
! Convert Milliseconds to hours, minuet, and seconed
! Thanks to Dms for this code
!------------------------------------------------
fn.def ConvertMilliseconds$(milli)
	secs = milli / 1000
	  while secs >= 60
		 secs = secs - 60
		 mins = mins + 1
	  repeat
	  while mins >= 60
		mins = mins - 60
		hrs = hrs + 1
		
	  repeat  
	
	  if hrs > 0 then
		converted$ = int$(hrs) + ":" + int$(mins) + ":" + int$(secs)
	  else
		converted$ = int$(mins) + ":" + int$(secs) 
	  endif    
	  fn.rtn converted$
fn.end 

!------------------------------------------------
! Look if a button is prest and acted accordingly 
!------------------------------------------------
fn.def Buttons()
	buttonTapped = FALSE
	gr.touch t, tx, ty
	if t then buttonTapped = TRUE
       
	if buttonTapped then 
		 gr.modify butCollision, "x", tx, "y", ty
		 
		 if gr_collision(butCollision, butMenu) then	call Menu() % Menu button top right
		 
		 if gr_collision(butCollision,imaInternal) then 
			call PlaySound(click) 
			internalPath$ = SelectSong$(internalPath$,1)
		endif
		 
		 if  gr_collision(butCollision,imaSDCard) then 
			call PlaySound(click) 
			!call RemovableCard$()
			if externalSD then SDpath$ = SelectSong$(SDpath$,2) else dialog.message"No SD Card Found","",ok,"OK" : call PlaySound(click) 
		endif 
		 
		if  gr_collision(butCollision, butRepeat) then call PlaySound(click) : call SetRepeat()
		if  gr_collision(butCollision, butShuffle) then call PlaySound(click) : call SetShuffle()
		
		if gr_collision(butCollision,butSeek) then call PlaySound(click) : call MoveSeekBar()
		
		if gr_collision(butCollision,butForwardTen) then call PlaySound(click) : call ForwardTen()
		if gr_collision(butCollision,butBackTen) then call PlaySound(click) : call BackTen()
		
		if gr_collision(butCollision,butPrevioue) then call PlaySound(click) : call PreviousSong()
		if gr_collision(butCollision,butNext) then call PlaySound(click) : call NextSong()
		
		if gr_collision(butCollision,butPause) then call PlaySound(click) : call SongPause()
		
	endif 
	
fn.end 

!------------------------------------------------
! Toddle repeat on/off
!------------------------------------------------
fn.def SetRepeat()
	if ! songRepeat then
		songRepeat = TRUE 
		gr.show greenRepeatLight
		gr.hide greenShuffleLight
		songShuffle = FALSE
	else
		songRepeat = FALSE 
		gr.hide greenRepeatLight
	endif
	call Render()
	call Wait()
fn.end 

!------------------------------------------------
! Toddle shuffle on/off
!------------------------------------------------
fn.def SetShuffle()
	
	if numOfSongs < 3 then dialog.message"To few songs to shuffle",,OK,"ok" : fn.rtn FALSE  
	if !songShuffle then 
		songShuffle = TRUE
		gr.show greenShuffleLight 
		gr.hide greenRepeatLight 
		songRepeat = False 
	else 
		songShuffle = FALSE 
		gr.hide greenShuffleLight 
	endif 
	call Render()
	call Wait()
fn.end

!------------------------------------------------
! Move the song on ten seconds 
!------------------------------------------------
fn.def ForwardTen()
	 if !songPlaying then fn.rtn FALSE
	 audio.position.current prt
	prt += 10000
	if prt > songLength then prt = songLength - 1000
	Audio.position.seek prt
	call Wait()
fn.end 

!------------------------------------------------
! Move the song back ten seconds 
!------------------------------------------------
fn.def BackTen()
	 if !songPlaying then fn.rtn FALSE
	  audio.position.current prt
	prt -= 10000
	
	if prt <= 0 then prt = 1000
	Audio.position.seek prt
	call Wait()
fn.end 

!------------------------------------------------
! User drags the seek bar to move the song forward or back
!Thanks to Gilles for the MAP function to move the seek bar
!------------------------------------------------
fn.def MoveSeekBar()

	if !songPlaying then fn.rtn FALSE
	do
	gr.touch t, tx, ty
		npos =MAP(tx, 95, 648, 0, songLength)      % manual  move the seek bar
        audio.position.seek npos
		call UpDateSeekBar()
		call Render()
		pause 50
	until ! t
		
fn.end 

!------------------------------------------------
! Move up the song list
!------------------------------------------------
fn.def PreviousSong()
	if !songPlaying then fn.rtn FALSE
	songPlaying = FALSE
	audio.stop
	audio.release stopStartSong
	
	if answer > 2 then 
		answer --
	else 
		answer = numOfSongs + 1 
	endif
	call Wait()
	pause 500
	 LIST.GET ldir,answer,filename$
	 gr.modify singer,"text",filename$ : call Render()
	call PlaySong(answer )
	
fn.end 

!------------------------------------------------
! Move down the song list
!------------------------------------------------
fn.def NextSong()
	if !songPlaying then fn.rtn FALSE
	songPlaying = FALSE
	audio.stop
	audio.release stopStartSong
	
	print"in NextSong, numOfSongs = ";numOfSongs
	print"in NextSong, answer = ";answer
	
	 if answer <= numOfSongs then 
		answer ++
	else 
		answer = 2 
	endif 
	call Wait()
	pause 500
	 LIST.GET ldir,answer,filename$
	 gr.modify singer,"text",filename$ : call Render()
	call PlaySong(answer)
fn.end 

!------------------------------------------------
! Pause and un-pause the current song
!------------------------------------------------
fn.def SongPause()
	 if ! stopStartSong then fn.rtn FALSE
	 if ! _pauseSong then 
		 songPlaying = False
		 gr.hide imaPause : call Render() 
		 audio.pause
		  _pauseSong = TRUE
		 call Wait()
	else 
		 gr.show imaPause : call Render() 
		 audio.play stopStartSong 
		songPlaying = TRUE
		_pauseSong = FALSE		
		 call Wait()
	 endif

fn.end 

!------------------------------------------------
! Select a new background colour
!------------------------------------------------
fn.def NewBackground()
	if animation then gr.hide theBars
	gr.show colourPallet
	
	call Render() 
	do : gr.touch t,tx,ty : until t
	call Wait()
	
	gr.modify collision, "x", tx, "y", ty 
	if gr_collision(collision,colourPallet) then 
		tx -= 210 : ty -= 500
		gr.get.bmpixel palletPrt,tx, ty, alpha, red, green, blue
		gr.color 255,red,green,blue
		GR.PAINT.GET myPaint
		GR.MODIFY background,"paint", myPaint
	endif 
	
	gr.hide colourPallet
	if animation then gr.show theBars
	call Render()
fn.end

!------------------------------------------------
! Wait for user to lift finger off screen
!------------------------------------------------
fn.def Wait()
	do 
		gr.touch t,null,null 
	until ! t
fn.end 

!------------------------------------------------
! if Play sound (ptr)
!------------------------------------------------
  fn.def PlaySound(ptr)
	call Buzzing()
      if sound then soundpool.play s,ptr,0.99,0.99,1,0,1
  fn.end
  
!------------------------------------------------
! if buzz is true then vibrate
!------------------------------------------------
  fn.def buzzing()
	if buzz then vibrate buzzGame[], -1
  fn.end 
  
!------------------------------------------------
! Render if game not in background
!------------------------------------------------
  fn.def Render()
	if ! background() then gr.render
 fn.end
 
!------------------------------------------------
! Menu for sound (button click), buzz etc
!------------------------------------------------ 
  fn.def Menu()
	_pauseCalled = FALSE
	if ! _pauseSong then call SongPause() : _pauseCalled = TRUE
    call PlaySound(click) 
	if sound then s$ = "Off" else s$ = "On"
	if buzz then b$ = "Off" else b$ = "On"
	if animation then a$ = "Off" else a$ = "On"
	array.load menu$[],"Turn Button Click " + s$,"Turn Vibrate " + b$, "Turn Animation " + a$, ~
									"New Background Colour","About","Help","Exit"
	 dialog.select menu,menu$[],"Menu"
	call PlaySound(click) 
	 sw.begin menu
		
		sw.case 1
			if sound = TRUE then sound = FALSE else sound = TRUE
			sw.break
			
		sw.case 2
			if buzz = TRUE then buzz = FALSE else buzz = TRUE
			sw.break
			
		sw.case 3
			if animation = TRUE then 
				animation = FALSE 
				gr.hide theBars
			else 
				animation = TRUE 
				gr.show theBars 
			endif
			call Render()
		sw.break
			
		sw.case 4
			call NewBackground()
		sw.break
			
		sw.case 5
			call About()
		sw.break
		
		sw.case 6
			call Help()
		sw.break
				
		sw.case 7
			dialog.message "Exit Music Player ",,yn,"Yes" ,"No"
			if yn = 1 then 
				audio.stop
				call SaveData()
				call PlaySound(click)
				audio.stop
				if songPlaying then audio.release stopStartSong
				pause 1000
				exit
			endif 
			
			sw.break
			
	sw.end
	if  _pauseCalled then call SongPause() 
	call PlaySound(click) 
  fn.end 
  
  
!------------------------------------------------
! Display the about box
!------------------------------------------------
fn.def About()
	p$=chr$(10)+chr$(10) % Do a paragraph brake	
	Dialog.Message "About:",~
		" Music Player: for Android" + p$ + ~
		" With Oli Basic" + p$ + ~
		" May/June 2019." + p$ +~
		" Version 0.02" + p$ + ~ 
		" Author: Roy Shepherd"+ p$ + ~ 
		"Credits:"+ p$ + ~ 
		"Gregor"+p$ + ~
		"Danny"+p$ +~
		"JuergenS"+p$ +~
		"Gilles" + p$ + ~
		"EPr" + p$ + ~
		"Carlos Caldas",ok, "OK"
		
fn.end

!------------------------------------------------
! Display Music Player help
!------------------------------------------------
fn.def Help()

	gr.bitmap.load hp, dataPath$ + "help1.png"
	if hp =-1 then 
		Dialog.Message "Help:","Can't find Help file",ok,"OK"
	else
		Popup "Tap to Exit", ,, TRUE
		gr.bitmap.draw help, hp,0,0
		call Render()
		do 
			gr.touch t,null,null 
		until t

		call Wait() 
		gr.bitmap.delete hp 
		gr.hide help
		call Render()
	endif
fn.end

!------------------------------------------------
! Display the splash screen for a short time
!------------------------------------------------
fn.def SplashScreen()
	gr.bitmap.load splash, dataPath$ + "splash.png"
	if splash = - 1 then ImageLoadError("splash")
	gr.bitmap.scale ms,splash,di_width,di_height
	gr.bitmap.draw splash,ms,0,0 
	call Render()
	pause 2000
	gr.bitmap.delete ms
	gr.hide splash
	call Render()
	loadingData = False
fn.end

!------------------------------------------------
! load Sound files not found
!------------------------------------------------
    fn.def SoundLoadError(e$)
        dialog.message "Sound file " + e$ + " not found", "App will end",OK,"OK" 
        end
		
    fn.end
    
!------------------------------------------------
! Load image file not found
!------------------------------------------------
    fn.def ImageLoadError(e$)
        dialog.message "Image file " + e$ + " not found", "App will end",OK,"OK" 
        end
	
    fn.end

!------------------------------------------------
! Gilles function to move the seek bar
!------------------------------------------------
	Fn.def map( v, in_min, in_max, out_min, out_max )    %  'v' conversion between boundary...
	  Fn.rtn (v-in_min)*(out_max-out_min)/(in_max-in_min)+out_min
	Fn.end
	
!------------------------------------------------
! Double tap Back Key to end
!------------------------------------------------
    fn.def BackKey(backKeyHit)
        inTime = 3 
        if backKeyHit = 3 then backKeyHit = 1
        if backKeyHit = 1 then 
            outTime = time() - tick 
            outTime /= 1000 
            if outTime > inTime then backKeyHit = 0
        endif
        backKeyHit ++
        gr.screen screenWidth, screenHeight
        if backKeyHit = 1 then tick = time() : popup "Press again to Exit",, + (screenHeight / 2)
        
        if backKeyHit = 2 then 
            tock = time() - tick
            tock /= 1000
            if tock <= inTime then call SaveData() : pause 1000 : exit
			!if tock <= inTime then end
        endif
        fn.rtn backKeyHit
    fn.end

!------------------------------------------------
! Get the directory path
!------------------------------------------------	
FN.DEF GetDir$(path$,ldir,card)
	
	if card = 1 & path$="/../../../.." then path$ = "/../../Music"  % Internal storage
	if card = 2 & path$ = "file://" then path$ = "file:///storage" % External SD
	FILE.DIR path$,files$[]
	 LIST.CLEAR ldir
	 LIST.ADD ldir,".."
	 ARRAY.LENGTH nfiles,files$[]

	 FOR i=1 TO nfiles
		  IF ENDS_WITH(".mp3",LOWER$(files$[i]))|ENDS_WITH("(d)",LOWER$(files$[i])) THEN
			LIST.ADD ldir,files$[i]
		  ENDIF
	 NEXT i
	 numOfSongs = nfiles
	 fn.rtn path$
FN.END

!------------------------------------------------
! If there a Removable SD card fitted 
!------------------------------------------------
fn.def IsThereSDCard()
	file.root fp$ , "_SdRemovable" % Start point of the public part of the external file system at a removable SD card.
	myMusicPath$ = "file://" + fp$

	if myMusicPath$ <> "file://"  then 
		externalSD = TRUE 
	else 
		externalSD = FALSE
		
	endif 
	fn.rtn externalSD
fn.end

!------------------------------------------------
! Get path for Removable sd Card
!------------------------------------------------
fn.def RemovableCard$()
	file.root fp$ , "_SdRemovable" % Start point of the public part of the external file system at a removable SD card.
	SDPath$ = "file://" + fp$

	if SDPath$ <> "file://"  then 
		externalSD = TRUE 
		!path$ = myMusicPath$
	else 
		externalSD = FALSE
		!path$ = "/../../Music"
	endif 
	fn.rtn SDPath$
fn.end

!------------------------------------------------
! See if the background colour as been changed from the default 
! if so set background colour
!------------------------------------------------
fn.def CheakBackground()
	
	if red = 0 & green = 99 & blue = 254 then fn.rtn FALSE
	gr.color 255,red,green,blue, , pbg
fn.end

!------------------------------------------------
! Song info banner
!------------------------------------------------
fn.def Banner()
	if songPlaying then 
		songBanner$ = MID$(songBanner$, 2) + LEFT$(songBanner$, 1)
		gr.modify singer,"text",songBanner$ 
	endif
fn.end

!------------------------------------------------
! Save the High Score and the game settings 
!------------------------------------------------
fn.def SaveData()
    file.exists pathPresent,dataPath$+"MusicData.txt"
    if ! pathPresent then file.mkdir dataPath$

		text.open w,hs,dataPath$+"MusicData.txt"
		
		text.writeln hs,int$(sound)
		text.writeln hs,int$(buzz)
		text.writeln hs,int$(animation)
		text.writeln hs,int$(red)
		text.writeln hs,int$(green)
		text.writeln hs,int$(blue)
		text.close hs
fn.end

!------------------------------------------------
! Load the High Score and the game settings 
!------------------------------------------------
fn.def LoadData()
    file.exists pathPresent,dataPath$+"MusicData.txt"
    if pathPresent then
		text.open r,hs,dataPath$+"MusicData.txt"
		
		text.readln hs,sound$
		text.readln hs,buzz$
		text.readln hs,animation$
		text.readln hs,red$
		text.readln hs,green$
		text.readln hs,blue$
		text.close hs

		sound = val(sound$)
		buzz = val(buzz$)
		animation = val(animation$)
		red = val(red$)
		green = val(green$)
		blue = val(blue$)
		call CheakBackground()
    endif
fn.end
    
return

