Page 1 of 1

Music Play Lsts

Posted: Sun Mar 23, 2025 12:21 pm
by Phollyer
I've been ripping my CD's and "online" music to MP3's lately, and I discovered that there is NO "good" play list creator, sow yesterday I wrote this. It's rather down and dirty but works.

My Goal was a Music Thumb Drive Loaded with a Couple thousand songs, Categorized by Folders inside a "Music" folder, All Play Lists are at the drive Root, one for EACH FOLDER which contains music directly, or nested inside folders. Empty Folders are Ignored. Play List Name is Is Folder Name plus Count of contained songs. Lists are Randomized, See comments to remove if desired. Music should be MP3, WAV, or OGG, if you have others see call to recursive procedure, extension parameter is Comma Separated List, add More.

Readme has expected folder structures:


The Read Me:

Code: Select all

;==========================================================
;===
;===   <<<<  READ ME  >>>>
;===
;===   Program "ThumbDrivePlayList.exe"
;===========================================================

;  This Program EXPECTS The Following Folder Structure:
;
;   \Some Folder (like Root)
;          ThumbDrivePlayList.exe
;          READ ME.txt
;          {The Play Lists are Generated Here}
;          \Music   <-- Root Golder of your Music Library
;                       Put NO Music in THIS Folder!!
;                       It Should Contain ONLY Folders
;                  \Folk Music  <-- Categorize Your Music Into Folders
;                         \Neal Young - Harvest
;                                \Songs
;                                \Songs
;                         \Neal Young - After The Gold Rush
;                                \Songs
;                                \Songs
;                         \Karen Carpenter
;                                \Songs
;                                \Songs
;                  \ClassicRock
;                         \STYX
;                                \Crystal Ball
;                                       \Songs
;                                       \Songs
;                                \Cornerstone
;                                       \Songs
;                                       \Songs
;                                \Pieces of Eight
;                                       \Songs
;                                       \Songs
;                  \Awesome Songs
;                         \I'm A Believer
;                         \Let It Be
;                         \Mademouselle
;
;======================================================================
;   This System WILL Produce a PlayList FOR EACH Folder Containing Music, 
;   and "Parent Folder" containing folders with Music (recursively).
;
;   Music is defined as Files with "mp3", "wav", or "ogg"
;
;   ALL Generated Play Lists will be Generated in the Folder with the EXE.
;   All Play Lists are built SHUFFLED.


The Code:

Code: Select all

;==============================================
;===  Create Playlists
;===
;===
;==============================================

Structure Album
  Folder.s
  Name.s
EndStructure
Structure Category
  Name.s
  Folder.s
  List Music.Album()
EndStructure


MusicPath$ = "Music"
PlayListPath$ = GetCurrentDirectory()
NewList MyMusic.Category()

Result = MessageRequester("Regenerate Playlists", "Do You Want to Regenerate the Playlists?", #PB_MessageRequester_YesNo)
If Result = #PB_MessageRequester_No
  End
EndIf

;Directory Test
Directory$ = GetCurrentDirectory()+"Music/"
Dir = ExamineDirectory(#PB_Any, Directory$, "*.*")
If Not IsDirectory(Dir)
  MessageRequester("Directory Check", "Missing Folder 'Music', Process Aborted!", #PB_MessageRequester_Error)
  FinishDirectory(Dir)
  End
EndIf

;Clear OLD Lists
Directory$ = GetCurrentDirectory()
If ExamineDirectory(0, Directory$, "*.m3u")
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      Ext$ = GetExtensionPart(DirectoryEntryName(0))
      If LCase(Ext$) = "m3u" 
        DeleteFile(Directory$+GetFilePart(DirectoryEntryName(0)))
      EndIf
    EndIf
  Wend
  FinishDirectory(0)
EndIf

;================================================
;Recursive Folder Reading Loading Lists of Folders$, Files$
;All Fully Pathed; Matching to EXT Pattern$ pb,pbi,pbf (No Quotes)
;================================================
Procedure RecursiveFolderFiles(StartingFolder$, Pattern$, List lstFolders.s(), List lstFiles.s(), WantFiles=#True) 
  Protected DIR, k, i, Name$, EXT$
  If Right(StartingFolder$,1)<>"/"
    StartingFolder$ = StartingFolder$ +"/"
  EndIf
  DIR = ExamineDirectory(#PB_Any, StartingFolder$, "*.*")
  While NextDirectoryEntry(DIR)
    If DirectoryEntryType(DIR) = #PB_DirectoryEntry_Directory
      Name$ = DirectoryEntryName(DIR)
      
      ;Igniore ALL "."Something Files
      If Left(Name$, 1) <> "." 
        
        AddElement(lstFolders())
        lstFolders() = StartingFolder$+Name$+"/"
        
        RecursiveFolderFiles(StartingFolder$+Name$+"/", Pattern$, lstFolders(), lstFiles(), WantFiles) 
      EndIf
      
    Else ; FILE
      
      If Not WantFiles
        Continue
      EndIf
      
      k = CountString(Pattern$,",")+1
      
      For i = 1 To k
        EXT$ = LCase(StringField(Pattern$, i, ","))
        
        If EXT$ = LCase(GetExtensionPart(DirectoryEntryName(DIR)))
          AddElement(lstFiles())
          lstFiles() = StartingFolder$+DirectoryEntryName(DIR)
        EndIf
      Next
    EndIf
  Wend
  FinishDirectory(DIR)
EndProcedure

;===================================================
;===  Generate Play Lists
;===================================================
NewList lstFolders.s()
NewList lstFiles.s()
RecursiveFolderFiles(MusicPath$, "mp3,ogg,wav", lstFolders(), lstFiles(), #True) 


For X = 0 To ListSize(lstFolders())-1
  SelectElement(lstFolders(), X)
  Debug lstFolders()
Next

For X = 0 To ListSize(lstFiles())-1
  SelectElement(lstFiles(), X)
  Debug lstFiles()
Next      
;======================================
;===  Build Lists
;======================================
;For Each Folder Build a List
;======================================
NewList PlayList.s()
For X = 0 To ListSize(lstFolders())-1
  SelectElement(lstFolders(), X)
  ClearList(PlayList())
  For Y = 0 To ListSize(lstFiles())-1
    SelectElement(lstFiles(), Y)
    If FindString(lstFiles(), lstFolders()) > 0
      AddElement(PlayList())
      PlayList() = lstFiles()
    EndIf
  Next
  If ListSize(PlayList()) > 0
    ;Write Out List / Comment Out to Keep In Order
    ;==============================================
    RandomizeList(PlayList())
    ;Create File
    K = CountString(lstFolders(), "/")
    ListName$ = StringField(lstFolders(), K, "/")
    
    Debug lstFolders()+ListName$ + ".m3u"
    
    ;====================================
    OpenFile(0, PlayListPath$ + ListName$ + " (" + Str(ListSize(PlayList())) + ")"  + ".m3u")
    For Y = 0 To ListSize(PlayList())-1
      SelectElement(PlayList(), Y)
      ;Remove Base Path -- Optional
      ;WriteStringN(0, ReplaceString(PlayList(), lstFolders(), ""))
      ;=============================================================
      ;Linux Machine
      ;Song$ = ReplaceString(PlayList(), "/home/pete/Music/", "../")
      ;=============================================================
      ;Thumb Drive
      ;Song$ = ReplaceString(PlayList(), "/home/pete/", "")
      ;=============================================================
      Song$=PlayList()
      WriteStringN(0, Song$)
      ;Debug PlayList()
    Next
    CloseFile(0)
  EndIf  
  ;====================================
  ClearList(PlayList())
Next

MessageRequester("Complete","Process is complete!")