No internet connection
  1. Home
  2. How to

Python <> Sound Flow

By Udi Simhon @Udi_Simhon
    2024-03-12 20:48:24.704Z

    Hi, I'm trying to get these two platforms to communicate, but I'm stuck.
    for instance, to find a way i can execute SF scripts from Python and vice versa.

    Do you have any links or resources to help me out?

    Thanks,

    Udi

    Solved in post #5, click to view
    • 5 replies
    1. Dustin Harris @Dustin_Harris
        2024-03-13 13:52:29.954Z

        I haven't called SoundFlow from python, but I have called python scripts from soundflow, which is pretty simple:

        function runPythonScript({ scriptPath, option1, option2, inputPath, outputPath }) {
            const result = sf.system.exec({ commandLine: `arch -arm64 python3 "${scriptPath}" -option_1 ${option1} -option_2 ${option2} '${inputPath}' '${outputPath}'` });
            while (!result) sf.wait({ intervalMs: 50 });
            if (result.exitCode > 0) throw `Error Processing Python Script\nExit Code: ${result.exitCode}\nStandardError:\n${result.standardError}`;
        }
        

        I recently switch to using a virtual environment for it too but I can't seem to find the sf.system.exec call for that, but it would be whatever commands you need to execute in the terminal :)

        1. And for the other way around:

          1. In reply toDustin_Harris:
            UUdi Simhon @Udi_Simhon
              2024-03-20 09:43:09.182Z

              Thanks @Dustin_Harris !
              I'm not sure where to write the path to the .py file.
              also, can you explain the 'option1, option2, inputPath, outputPath' use?

              1. Dustin Harris @Dustin_Harris
                  2024-03-20 13:25:07.928Z

                  Hi @Udi_Simhon
                  I made that function with all of those parameters because I need to pass those inputs to my python script (which processes some audio) so here's an example of how I use it:

                  function runPythonScript({ scriptPath, option1, option2, inputPath, outputPath }) {
                      const result = sf.system.exec({ commandLine: `arch -arm64 python3 "${scriptPath}" -option_1 ${option1} -option_2 ${option2} '${inputPath}' '${outputPath}'` });
                      while (!result) sf.wait({ intervalMs: 50 });
                      if (result.exitCode > 0) throw `Error Processing Python Script\nExit Code: ${result.exitCode}\nStandardError:\n${result.standardError}`;
                  }
                  
                  const myPythonScript = "~/Desktop/myFavoritePythonScript.py";
                  const speed = 2.5;
                  const pitch = 1.1;
                  const fileToProcess = "~/Desktop/someWaveFile.wav";
                  const processedFile = "~/Desktop/someWaveFile_Processed.wav";
                  
                  runPythonScript({
                      scriptPath: myPythonScript,
                      option1: speed,
                      option2: pitch,
                      inputPath: fileToProcess,
                      outputPath: processedFile,
                  });
                  

                  so, the python script I'm running is made to accept those parameters as well of course, but your needs may vary; you might not need any of the parameters.
                  Another important note: I'm on an M1 system, so I'm specifying arch -arm64 to ensure python is running all of its code and modules with the arm64 versions.

                  Reply1 LikeSolution
                  1. Dustin Harris @Dustin_Harris
                      2024-03-20 13:31:23.991Z

                      if you only need to run the python script and don't need to pass the python script any parameters, you can use this simpler function instead:

                      function runPythonScript({ scriptPath }) {
                          const result = sf.system.exec({ commandLine: `arch -arm64 python3 "${scriptPath}"` });
                          while (!result) sf.wait({ intervalMs: 50 });
                          if (result.exitCode > 0) throw `Error Processing Python Script\nExit Code: ${result.exitCode}\nStandardError:\n${result.standardError}`;
                      }