Announcement

Collapse
No announcement yet.

Random wav file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Random wav file

    HELP!

    I'd like a script to play a random .wav file from a specified directory. How can this be done?
    Regards, Bob

    #2
    Here's a script that plays a random mp3 or wma - you can modify as needed for .wav files - it's terribly inefficient with a large number of files:

    option explicit

    dim fs, root, myFiles(3000), ndx, randomNdx

    set fs = createObject("scripting.filesystemobject")
    set root = fs.getFolder("e:\My Music")

    'load all files into array
    getMusicFiles fs, root
    randomNdx = GetRandomNumber(1,ndx)

    hs.writeLog "randomMusic","found " & ndx & " songs. playing:" & myFiles(randomNdx)
    hs.launch myFiles(randomNdx)
    set fs = nothing


    Function GetRandomNumber(lowerbound,upperbound)
    Randomize
    GetRandomNumber = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    End Function

    sub getMusicFiles(fs, folder)

    dim s, objFile, colFiles

    if folder.subFolders.count = 0 then 'grab all files in this folder
    Set colFiles = folder.Files
    For Each objFile in colFiles
    if(fs.getExtensionName(objFile)<>"mp3" and _
    fs.getExtensionName(objFile)<>"MP3" and _
    fs.getExtensionName(objFile)<>"wma" and _
    fs.getExtensionName(objFile)<>"WMA") then
    '
    else
    ndx=ndx+1
    myFiles(ndx) = fs.getAbsolutePathName(objFile)
    'display fs.getAbsolutePathName(objFile)
    end if
    Next
    exit sub
    end if

    dim subFolder

    for each subFolder in folder.subFolders
    getMusicFiles fs, subFolder
    next
    end sub

    sub display(s)
    response.send s + "<br>"
    end sub
    HS4Pro on a Raspberry Pi4
    54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
    Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI

    HSTouch Clients: 1 Android

    Comment


      #3
      Thanks Rob,

      I cut and pasted the script (then edited for location, file type, etc.) but it won't work. Can you see what I may be doing wrong.
      Attached Files
      Regards, Bob

      Comment


        #4
        ah - try this one instead - I simplified it greatly.
        Change the path and un-comment the hs.launch()

        try it out and let us know...

        Thanks
        Rob
        Attached Files
        HS4Pro on a Raspberry Pi4
        54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
        Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI

        HSTouch Clients: 1 Android

        Comment


          #5
          Rob,

          Thanks, that did the trick.
          Regards, Bob

          Comment


            #6
            I don't know if anyone is interested, but I took Rob's revised script and revised it further for HS 2.x. I took one of our boys' educational applications on birds and copied the 800 bird sound files (all WAV's) to a dedicated folder. When I call this script, it plays one at random. This revised methodology executes about 4x faster, down to one second for 800 files on a 933MHz machine.

            --David
            Code:
              sub main()
              
              	dim fs, root, ndx, randomNdx
              	dim objFile, colFiles, strFileName
              
              	ndx=0
              	set fs = createObject("scripting.filesystemobject")
              	set root = fs.getFolder("C:\Program Files\HomeSeer 2\Sounds\Birds")
              	Set colFiles = root.Files
              
              	randomNdx = cint(GetRandomNumber(1,cint(colFiles.count)))
              
              	For Each objFile in colFiles
              
              	 if ndx=randomNdx then
              		if lcase(fs.getExtensionName(objFile))="wav" then
              			strFileName = fs.getAbsolutePathName(objFile)
              		end if
              		exit for
              	 end if
              	  ndx=ndx+1
              	Next
              
              	hs.writeLog "RandomBirdSound","Found " & ndx & " files. Playing:" & strFileName
              		hs.mediafilename = strFileName
              		hs.mediaplay
              	
              end sub
              
              Function GetRandomNumber(lowerbound,upperbound)
              	Randomize
              	GetRandomNumber = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
              End Function

            Comment


              #7
              How often do you play these bird sounds?
              💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

              Comment


                #8
                Lol!

                Comment


                  #9
                  Umm, well, I just got the script running. Heh, heh. I don't really have anything set up yet. My thoughts were to have some random 'environmental' sounds going off now and then (random intervals) to bring the outdoors, in. I have no idea what the wife is going to think of this idea, nor how long it will last. Maybe I'll only run it on the weekends to preserve my life. If we lived in DC, I'd have to substitute the bird environmental sounds for gunshots and sirens.

                  --David

                  Comment


                    #10
                    David,
                    How are you going to setup up a random event? This actually may not be such a bad idea.
                    💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

                    Comment


                      #11
                      Greg,

                      Well, I haven't really thought this through, but my first thought is to set up an event to run at intervals, say every 3 or 7 minutes, and when run, have a weighted determination of whether that particular time will be one in which a random environmental sound is played. I'm not sure if I can programmatically change the interval at which recurring events are fired, but I might periodically change that setting, if possible.

                      Do you have any ideas on the matter?

                      --David

                      Comment

                      Working...
                      X