Hmm. Here is one suggestion:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(".")
Set fc = f.Files
Set ff = f.SubFolders
For Each f1 in ff
note f1.name
Next
note " "
For Each f1 in fc
note f1.name
Next
Set ff = nothing
Set fso = nothing
Set f = nothing
Set fc = nothing
This code will, at least on my system, and I presume on any others with VBscript installed, read the directory given in "GetFolder", and return a list of files and directories.
This could be used in two ways, if added to the plugin. Method 1: Benefits - You only need to update the list of files once each time you run the plugin, by adding a call to it in the "onplugininstall" function, though, I make it a separate one here, so you can also call it from an alias, if you add new sounds, etc.
sub OnPluginInstall()
call UpdateList("","","")
end sub
redim file_list()
sub UpdateList(a, b, c)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(".")
Set fc = f.Files
count = 0
for all f1 in fc
count = count + 1
next
redim file_list(count)
count = 0
for all f1 in fc
'Yes, some twits are dumb enough to encode an .mpg with only audio, then wonder why
'it won't play in things like winamp..., hence the support for looking for .mpg or .mpeg.
'This is mostly just an example of how to handle extensions longer than three characters,
'assuming the code even works right.
dot = instr(".",f1.name)
if instr(mid(f1.name,dot,len(f1.name)-dot, ".mid.wav.wma.mpg.mpeg") then
file_list(count) = f1.name
count = count + 1
end if
next
end sub
Then change the code for playback to do:
' play sound - directory in variable
for x = 0 to ubound(file_list)
if file_list(x) = sSound then
world.Sound (world.getvariable ("msp_path") & sSound)
else
note "Sound not installed in directory " & world.getvariable ("msp_path") & "!"
end if
The "other" method would be to instead do this:
' play sound - directory in variable
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(".")
Set fc = f.Files
success = 0
for all f1 in fc
if f1.name = sSound then
world.Sound (world.getvariable ("msp_path") & sSound)
success = 1
end if
next
if success = 0 then
note "Sound not installed in directory " & world.getvariable ("msp_path") & "!"
end if
Set ff = nothing
Set fso = nothing
Set fc = nothing
Obviously the second version is far shorter, and can handle new sounds, but it has to create the system object and read the directory "every single time" it wants to play something.
Still, its probably less complicated to add, especially since I don't provide the alias needed to call the "update" directly. The only real disadvantage, other than maybe some minor lag as the directory gets read each time, is that, since no copy of the list is kept, you can't add code to simply show all of the sound files you have installed. Note also, if you wanted to make things more robust, you would also want to make the ".mid.wav.wma" part, in the first example, so its stored in a variable/MXP element, so that it could be adjusted to include other end types.
Oh, the other reason to use the second version, even if the first has some advantages, is simply that I am 99.9% sure that the second version will work as written. The first method **should** work, but I haven't tested the code to make sure its bug free. ;) lol
Feel free to adjust one or the other to work better Nick. Since there are major advantages to version #1, especially if you could port it to Lua, where I presume the entire data structure for the file list could be loaded into a table.
---
Been a while since I wrote anything like this. Kind of fun to get back into it again. lol |