Scan user sound files

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1214
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Scan user sound files

Post by WilliamL »

You can see what you have in your sound folder. This code could be modified compile a list of just about anything in any folder even recursively. The code is from the Help file. The Cocoa sound routines are from wilbert.

Code: Select all

EnableExplicit
ExamineDesktops()

#ShowFilesWnd=1
#ShowFilesEfid=1

Global totlist
Global Dim SoundName.s(100)
Global Dim SoundAddress(100)
Define event,Dir$

;-sound section
Procedure Sound_Load(FileName.s)
  ProcedureReturn CocoaMessage(0,CocoaMessage(0,0,"NSSound alloc"), "initWithContentsOfFile:$", @FileName, "byReference:", #YES)
EndProcedure

Procedure Sound_Catch(*MemoryAddress, Size)
  Protected Result.i = CocoaMessage(0, 0, "NSData dataWithBytes:", *MemoryAddress, "length:", Size)
  If Result : Result = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"), "initWithData:", Result) : EndIf
  ProcedureReturn Result
EndProcedure

Procedure Sound_SetVolume(SoundObject, Volume.f)
  CocoaMessage(0, SoundObject, "setVolume:@", @Volume)
EndProcedure

Procedure Sound_Play(SoundObject, Loop = 0)
  Protected currentTime.d
  CocoaMessage(0, SoundObject, "setLoops:", Loop)
  CocoaMessage(0, SoundObject, "setCurrentTime:@", @currentTime)
  ProcedureReturn CocoaMessage(0, SoundObject, "play")
EndProcedure

Procedure Sound_Stop(SoundObject)
  ProcedureReturn CocoaMessage(0, SoundObject, "stop")
EndProcedure

Procedure Sound_IsPlaying(SoundObject)
  ProcedureReturn CocoaMessage(0, SoundObject, "isPlaying")
EndProcedure

Procedure Sound_Release(SoundObject)
  CocoaMessage(0, SoundObject, "stop")
  CocoaMessage(0, SoundObject, "release")
EndProcedure
;-end of coca sound routines

Procedure ShellSort()
    Define Limit,Switch,bar
    Define Offset=totlist/2
    While Offset>0
        Limit=totlist-Offset
        Switch=1 ; _True
        While Switch
            Switch=0 ;_False
            For bar=1 To Limit
                If UCase(SoundName(bar))>UCase(SoundName(bar+Offset))
                    Swap SoundName(bar),SoundName(bar+Offset)
                    Swap SoundAddress(bar),SoundAddress(bar+Offset)
                    Switch=bar
                EndIf
            Next
            Limit=Switch-Offset
        Wend
        Offset=Offset/2
    Wend
EndProcedure

Procedure ScanDirectory(Dir$)
    Protected Dir.i
    Dir = ExamineDirectory(#PB_Any, Dir$, "*.*")
    If Dir
        While NextDirectoryEntry(Dir)
            If DirectoryEntryName(Dir) <> "." And DirectoryEntryName(Dir) <> ".."
                If DirectoryEntryType(Dir) = #PB_DirectoryEntry_File
                    If FindString(DirectoryEntryName(Dir),".mp3",#PB_String_NoCase)>0 Or FindString(DirectoryEntryName(Dir),".aif",#PB_String_NoCase)>0 Or FindString(DirectoryEntryName(Dir),".aiff",#PB_String_NoCase)>0
                        totlist+1
                        SoundName(totlist)=Dir$+DirectoryEntryName(Dir) ; put data in AddElement
                        SoundAddress(totlist)=Sound_Load(Soundname(totlist))
                    EndIf
                Else
                    ;ScanDirectory(Dir$ + DirectoryEntryName(Dir)+"/") ; rem out if you want non-resursive
                EndIf
            EndIf
        Wend
        FinishDirectory(Dir)
    EndIf
EndProcedure

Procedure Speak(snd)
    Sound_SetVolume(SoundAddress(snd), 0.8) ; lower is softer
    Sound_Play(SoundAddress(snd))
EndProcedure

Procedure ShowFiles()
    Define w=300,h=totlist*20
    If h>DesktopHeight(0)-55 : h=DesktopHeight(0)-55 : EndIf
    Define cnt,xtab,ytab,fw,lh=25
    
    If OpenWindow(#ShowFilesWnd,0,0,w,h,"List of files",#PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
        ListViewGadget(#showfilesefid,0,0,WindowWidth(#ShowFilesWnd),WindowHeight(#ShowFilesWnd))
        For cnt=1 To totlist
            AddGadgetItem(#showfilesefid,-1,Str(cnt)+". "+GetFilePart(SoundName(cnt)))
        Next
   EndIf
EndProcedure

Procedure DoShowFiles(event)
    Define id,event
    Select event
    Case #PB_Event_CloseWindow
        End
    Case #PB_Event_Gadget
        id=EventGadget()
        Select id
        Case #showfilesefid ; click on listview
            If EventType()=#PB_EventType_LeftClick
                Speak(GetGadgetState(#showfilesefid)+1)
            EndIf
        EndSelect
    EndSelect
EndProcedure

dir$=PathRequester("Find user sounds...","/vering2/Library/Sounds")
ScanDirectory(dir$)
ShellSort()
ShowFiles()

Repeat
    event = WaitWindowEvent()
    Select EventWindow()
    Case #showfileswnd
        DoShowFiles(event)
    EndSelect
ForEver
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1