Forum

    • Register
    • Login
    • Search
    • Categories

    Get list of midi files

    Scripting Forum
    3
    19
    153
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan Korneff
      Dan Korneff last edited by

      Hey guys,
      I could use some pointers with the FileSystem.
      I'm trying to use the File Browser to populate an array with a list midi files within a directory.
      here's what I'm doing, but it's returning an empty array:

      //hold file list
      var fileList = [];
      
      const var Browse = Content.getComponent("Browse");
      
      inline function onBrowseControl(component, value)
      {
          if(value) //if button clicked
          {
              //open file broswer
              FileSystem.browseForDirectory("", function()
                  {
                      //push midi list into array 
                      fileList.push(FileSystem.findFiles("", "*.mid", false));
      
                  });
          }
      	
      };
      
      Content.getComponent("Browse").setControlCallback(onBrowseControl);
      
      HiseSnippet 932.3ocsV0uiZbCD2KGaTXaIpQJO.q3ufpqvRa9RJppW.NpPM2ETIMpRQUolc8dXgWajs26BJ5d25SPeV5aPxX6E1kKjSIHUy+v7wuw+73YFuSkhXhRIjHuFuZ8JBx6a8mslqWLbAlxQSFg7tm+YXklHCcpFrdEVoHIHOui9UiBuF0Q10+8KCvLLOlTpBgdsfFSdAMipK0N8jeixXiwIjWQyp38COYRrfOTvD4.eNxOBsBGuDeA4brwsZ9Hu6bZBUKjyzXMQA9LPjrd1BwUbm+ulpnyYDiPezLHPN0ngKnrjoaNqJDxq9zxS9QtS9C7OilP2puLC7cVCgkHplC7psKkpuCk5+4nzXAKwDfOC87pPu5N5ce+YwR5JcoEC29F+Ib3xIECo8pzx4Kp1+Vyen.7fq6lgWRFKAgsHZ+3nniCeTTTmm0LnYPudK.VElRYjPFUoaFbIVZEeAHE9ygu4ur9AWRfnw1.o3JEArrYOtfnGJxVI3fP6VNysbQmxYTNILMmGqoBdnf6LafJEr1wavcLDZVNoSyf22LHDVzz1NMg85QSCmmq0.9XFMdIIw4RgmlUudhUDt6TLWJTWQjkFGCZmsFJmy5N2t6iExQTIIFtAW2tUqi2xu1cJQcicnbmVkqVDlYJNL4qPJWKBwRId8m57lzXWCl1U3QJkmXDU1su022EhmgGXlhzwk5pFmq67Lq70MCZzL3Zq8aO62UYTaSxCwL1bnmp8MR9tso7WkKoyEZxKgrQv6CZDbcP3MMkltWaEAlQj60rowWda.ayyylSjapE13HzMra61c9xZ2hcYnJNJ3S3T8KgREmbYCY+8zPhJRwv+9iIivZroGsPG32JhTSMzwaD4RXfmqisg+HhZoVrx5aw8BPYs058J5mGXKmQTXuuqu6VA8tsCE+6kmrdqv+7lST3KIS3SkD3R0zmSUmIxfvhkfad0Pe5bCX5kHImg06NRyL6tv.j32Y1gY9.WQ0qqNa+qXNWzdmys2z5WHcuu+TpNdw94as8vW354+a9V7pQS+SSSg4Gkjst+3+7Peh3qfJ+tHWS4WbFVKoP8h+44YyfGNiI.S3bBC1Henfv.0JGYjMYlYDdhU3CvpvXeirWgw9aLhxvwRwaicsjl2ktqUCvIt8I4Fv2F.xg8Q11T.meT2HjYh3aiiMohe.399w7iG.le5.v7vC.yiN.LO9.v7jC.ySuULluT444ZQlqMATL8T67JOuS4XnJyVQh9H7XmydB
      

      Dan Korneff - Producer / Mixer / Audio Nerd

      1 Reply Last reply Reply Quote 0
      • d.healey
        d.healey last edited by d.healey

        Browse for directory returns the selected directory - except in your snippet where you've decide not to return anything in your callback function 😉 and then you can search that directory for files.

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        1 Reply Last reply Reply Quote 1
        • Dan Korneff
          Dan Korneff last edited by

          @d-healey Thank you so much!

          //hold file list
          var fileList = [];
          
          const var Browse = Content.getComponent("Browse");
          
          inline function onBrowseControl(component, value)
          {
              if(value) //if button clicked
              {
                  //open file broswer
                  FileSystem.browseForDirectory("", function(dir)
                      {
                          //push midi list into array
                          fileList.push(FileSystem.findFiles(dir, "*.mid", false));
          
                      });
              }
          	
          };
          
          Content.getComponent("Browse").setControlCallback(onBrowseControl);
          

          Dan Korneff - Producer / Mixer / Audio Nerd

          1 Reply Last reply Reply Quote 0
          • Dan Korneff
            Dan Korneff last edited by

            And one last tweak. The above code puts an array of files inside of an array...
            This will just populate the array with files:

            //hold file list
            var fileList = [];
            
            const var Browse = Content.getComponent("Browse");
            
            inline function onBrowseControl(component, value)
            {
                if(value) //if button clicked
                {
                    //open file broswer
                    FileSystem.browseForDirectory("", function(dir)
                        {
                            //push midi list into array
                            local midiFiles = FileSystem.findFiles(dir, "*.mid", false);
                            
                            for(m in midiFiles)
                            {
                                fileList.push(m);
                            }
            
                        });
                }
            	
            };
            
            Content.getComponent("Browse").setControlCallback(onBrowseControl);
            

            Dan Korneff - Producer / Mixer / Audio Nerd

            ustk 1 Reply Last reply Reply Quote 0
            • ustk
              ustk @Dan Korneff last edited by ustk

              @dustbro Then why not just:

              fileList = FileSystem.findFiles(dir, "*.mid", false);
              

              Or if you want to keep a local var (if ever you have a good reason)

              fileList = midiFiles;
              

              I cannot help pressing F5 in the forum...
              Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

              Dan Korneff 2 Replies Last reply Reply Quote 0
              • Dan Korneff
                Dan Korneff @ustk last edited by Dan Korneff

                @ustk said in Get list of midi files:

                @dustbro Then why not just:

                fileList = FileSystem.findFiles(dir, "*.mid", false);
                

                That works too!
                Next question. The array of files are Objects. Tips on converting to string so I can load in the MidiPlayer?
                The files in the array looks like:

                "File: E:\\Company\\Groove 06 - 155 bpm\\Pattern01.mid"
                

                I'm calling

                MIDIPlayer.setFile(fileList[0], 1, 1);
                

                and getting an error:

                API Call with undefined parameter 0
                

                I guess technically I just need to remove

                File: 
                

                from the file name

                Dan Korneff - Producer / Mixer / Audio Nerd

                1 Reply Last reply Reply Quote 0
                • Dan Korneff
                  Dan Korneff @ustk last edited by

                  @ustk @d-healey Any tips on using replace() ?
                  I'm trying to snip the File: part off of the file name.
                  using:

                  fileList = fileList.replace("File: ", "");
                  

                  but I'm getting the error:

                  Unknown function 'replace'
                  

                  Dan Korneff - Producer / Mixer / Audio Nerd

                  1 Reply Last reply Reply Quote 0
                  • d.healey
                    d.healey last edited by

                    replace works on strings, fileList is an array so no replace function there. You'd need to loop through your array of objects and run replace on each one.

                    Libre Wave - Freedom respecting instruments and effects
                    My Patreon - HISE tutorials
                    YouTube Channel - Public HISE tutorials

                    Dan Korneff 1 Reply Last reply Reply Quote 0
                    • Dan Korneff
                      Dan Korneff @d.healey last edited by

                      @d-healey here's my attempt:

                      //hold file list
                      var fileList = [];
                      const var formattedNames = [];
                      
                      const var Browse = Content.getComponent("Browse");
                      
                      inline function onBrowseControl(component, value)
                      {
                          if(value) //if button clicked
                          {
                              //open file broswer
                              FileSystem.browseForDirectory("", function(dir)
                                  {
                                      //push midi list into array
                                      fileList = FileSystem.findFiles(dir, "*.mid", false);
                                      
                                      for (i = 0; i < fileList.length; i++)
                                      {
                                          var newName = fileList[i];
                                          newName = newName.replace("File: ", "");
                          
                                          formattedNames.push(newName);
                                      }
                                      
                                  });
                          }
                      	
                      };
                      
                      Content.getComponent("Browse").setControlCallback(onBrowseControl);
                      

                      getting this error:

                      function not found
                      at
                       newName = newName.replace("File: ", "");

                      Dan Korneff - Producer / Mixer / Audio Nerd

                      1 Reply Last reply Reply Quote 0
                      • d.healey
                        d.healey last edited by

                        filelist is an array of objects. So filelist[i] is an object not a string. Try Console.print(trace(filelist[i])); to see what is contained within your object.

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        Dan Korneff 1 Reply Last reply Reply Quote 0
                        • Dan Korneff
                          Dan Korneff @d.healey last edited by

                          @d-healey said in Get list of midi files:

                          Console.print(trace(filelist[i]));

                          It returns

                          Interface: undefined
                          

                          Dan Korneff - Producer / Mixer / Audio Nerd

                          d.healey 1 Reply Last reply Reply Quote 0
                          • d.healey
                            d.healey @Dan Korneff last edited by

                            @dustbro If you're going to copy and paste things that I write you should check that I've written it correctly 😉 Your variable is called fileList, not filelist.

                            Libre Wave - Freedom respecting instruments and effects
                            My Patreon - HISE tutorials
                            YouTube Channel - Public HISE tutorials

                            Dan Korneff 1 Reply Last reply Reply Quote 0
                            • Dan Korneff
                              Dan Korneff @d.healey last edited by Dan Korneff

                              @d-healey said in Get list of midi files:

                              fileList

                              giphy.gif

                              I've been sitting at the computer for 13 hours... maybe I need a break.

                              Now that I'm using variables that actually exist in my project, it returns empty:

                              Interface: 
                              

                              Also, thanks for the help. 👍

                              Dan Korneff - Producer / Mixer / Audio Nerd

                              1 Reply Last reply Reply Quote 0
                              • d.healey
                                d.healey last edited by

                                What do you get with Console.print(fileList.length);?

                                Libre Wave - Freedom respecting instruments and effects
                                My Patreon - HISE tutorials
                                YouTube Channel - Public HISE tutorials

                                Dan Korneff 1 Reply Last reply Reply Quote 0
                                • Dan Korneff
                                  Dan Korneff @d.healey last edited by Dan Korneff

                                  @d-healey 15, which is the amount of files in my test folder

                                  Dan Korneff - Producer / Mixer / Audio Nerd

                                  1 Reply Last reply Reply Quote 0
                                  • d.healey
                                    d.healey last edited by d.healey

                                    Try this in place of your loop

                                    for (f in fileList)
                                    {
                                    	Console.print(f.toString(File.FullPath));
                                    }
                                    

                                    I also notice you're using some vars, always use local in inline functions, and in on init use reg or const. 99% of the time var should not be used except in paint routines, mouse callbacks, timer callbacks, and occasionally a few other places.

                                    Libre Wave - Freedom respecting instruments and effects
                                    My Patreon - HISE tutorials
                                    YouTube Channel - Public HISE tutorials

                                    Dan Korneff 2 Replies Last reply Reply Quote 1
                                    • Dan Korneff
                                      Dan Korneff @d.healey last edited by

                                      @d-healey said in Get list of midi files:

                                      Try this in place of your loop

                                      Now we're talking! that returns the file path correctly

                                      Dan Korneff - Producer / Mixer / Audio Nerd

                                      d.healey 1 Reply Last reply Reply Quote 0
                                      • d.healey
                                        d.healey @Dan Korneff last edited by

                                        @dustbro Yeah it's late here 🙂 the objects in your array are all file objects so you have access to all of the File class functions, like toString()

                                        Libre Wave - Freedom respecting instruments and effects
                                        My Patreon - HISE tutorials
                                        YouTube Channel - Public HISE tutorials

                                        1 Reply Last reply Reply Quote 0
                                        • Dan Korneff
                                          Dan Korneff @d.healey last edited by

                                          @d-healey said in Get list of midi files:

                                          99% of the time var should not be used

                                          Thanks for this advice as well.

                                          Dan Korneff - Producer / Mixer / Audio Nerd

                                          1 Reply Last reply Reply Quote 1
                                          • First post
                                            Last post

                                          3
                                          Online

                                          977
                                          Users

                                          6.6k
                                          Topics

                                          60.6k
                                          Posts