Snippet : How to filter files in a browser
-
Hi everyone,
I thought about sharing something with you all, and adding my humble contribution to our snippet library.
I'm currently working on a new project that will include midi files. I built a custom browser to list and play them but I needed a way to allow the user to sort these files, either by type, tempo, length, etc...
So this snippet is built to sort midi files, but obviously you'll be able to modify it to sort other file types.It's not that complex but I tried (read: banged my head) to find the best and most condensed way to do this, so here's my method.
If you got a better solution, feel free to share !const MIDIPlayer1 = Synth.getMidiPlayer("MIDI Player1"); const midiFiles = MIDIPlayer1.getMidiFileList(); const lblDisplayFiles = Content.getComponent("lblDisplayFiles"); // I'll leave you the task of handling how you want to add filters to this array... :) // For this example you can do it manually, but you can add as many filters as you want. const filterArray = ["2bars"]; /* -------------- We'll assume a file-naming system like this : "FileName_Type_Feel_Tempo_Length" i.e.: "MidiLoop1_Groove_Straight_120bpm_2bars.mid" What we want is to get all files that match one or more words that we'll call 'filters' The function goes like this : 1 - We'll push the midiFiles array into a local array f. 2 - The result of each filtering iteration i will be stored into another array into f. 3 - We'll iterate through f as many times as there are filters. i.e. if there are 3 filters, we'll end up with : f[0] = allFiles; f[1] = allFiles - filteredFiles_1; f[2] = filteredFiles_1 - filteredFiles_2; f[3] = filteredFiles_2 - filteredFiles_3; filterResult = filteredFiles_3; -------------- */ inline function filterFilesLoop(files, filters) { // Start with f[0] = all midi files: so we begin to filter all available files. local f = [midiFiles]; // This will contain the filtered files that we want the function to return. local filterResult = []; // Check: if there are no filters if(filters.length == 0) { filterResult = midiFiles; } else { for(i = 0; i < filters.length; i++) { // Add as many empty arrays as filters f.push([]); // If the filter exists if(isDefined(filters[i])) { // Go through each file of f[i] to check if the file contains the filter for(j = 0; j < f[i].length; j++) { // if the file contains the filter if(f[i][j].contains(filters[i])) // Add the file to the next array, which will be analyzed further to filter the files even more f[i+1].push(f[i][j]); } } // the resulting array must be the last one, no matter how much filters are used filterResult = f[i+1]; } } lblDisplayFiles.set("text", trace(filterResult)); return filterResult; } // Calling the function... const filteredFiles = filterFilesLoop(midiFiles, filterArray);
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