No internet connection
  1. Home
  2. How to

Set clip gain with preset values

By Andrew Sherman @Andrew_Sherman
    2022-04-11 09:56:10.346Z

    I'm starting a new thread here because I didn't want to complicate an existing thread started by @Kjartan_Kjartansson with a solution by @raphaelsepulveda.

    @raphaelsepulveda I hope you're well! I'm asking if there is a way to modify your script (pasted below for convenience).

    With this script it doesn't work as expected if I select a clip (or multiple clips). I'm looking for a version that does not really use the playhead method, but rather pastes the clip gain onto whichever clips I have selected.

    Also it should not require a clip on the clip gain track at the same playhead position as the selected clips.

    My ideal setup would be: a single mono track called "Clip Gain" that is deactivated, muted and not visible in the timeline (hidden). This has 3 separate clips on it at 0sec, 5sec and 10sec.
    Clip 1: "0Db". Clip 2: "-4Db". Clip 3: "-15Db". If it's not possible to have the track invisible/hidden, then deactivated would be next best. Also if it needs to be 3 separate tracks then that's fine as well.

    The script would then reference one of these clips on the track (say -4), and paste that clip gain value onto the selected clips on the other track/s.

    Could you give me any pointers on how to approach this?

    // Original script by @raphaelsepulveda
    /**@param { string } clipGainTrackName */
    function copyPasteClipGainFromTrack(clipGainTrackName) {
        // Activate Pro Tools
        sf.ui.proTools.appActivateMainWindow();
    
        // Refresh Main Window
        sf.ui.proTools.mainWindow.invalidate();
    
        // Get Copy Clip Gain menu item
        let copyClipGainMenuItem = sf.ui.proTools.getMenuItem("Edit", "Copy Special", "Clip Gain");
    
        // Error Handling: If no clip is present in current playhead position:
        if (!copyClipGainMenuItem.isEnabled) {
            // Throw error
            throw `No clip present on playhead position.`
        };
    
        // Get currently selected track
        const selectedTrack = sf.ui.proTools.selectedTrack;
    
        // Select track containing desired Clip Gain
        sf.ui.proTools.trackSelectByName({ names: [clipGainTrackName], deselectOthers: true });
    
        // Force menu item state to refresh by pressing an arbitrary key
        // This is to be able to read current menu item state in the next step
        sf.keyboard.press({ keys: "down" });
    
        // Error Handling: If no clip is present in current playhead position:
        if (!copyClipGainMenuItem.isEnabled) {
            // Re-select originally selected track
            selectedTrack.trackSelect();
    
            // Throw error
            throw `No clip present in track ${clipGainTrackName} on playhead position.`
        };
    
        // Copy Clip Gain from clip in track
        copyClipGainMenuItem.elementClick();
    
        // Re-select originally selected track
        selectedTrack.trackSelect();
    
        // Select entire clip in original track
        sf.keyboard.press({
            keys: "alt+tab,shift+tab",
        });
    
        // Paste Clip Gain
        sf.ui.proTools.menuClick({
            menuPath: ["Edit", "Paste"],
        });
    };
    
    copyPasteClipGainFromTrack('GAIN A');
    
    Solved in post #2, click to view
    • 8 replies
    1. Raphael Sepulveda @raphaelsepulveda2022-04-11 16:10:13.645Z2022-04-11 16:18:30.339Z

      Hey @Andrew_Sherman, thanks for creating a new thread for this!

      Unfortunately, I can't get this to perform the exact way you'd like but here's a workaround using a modified version of my script:

      The setup is simple, create 3 tracks, one for each clip with the desired Clip Gain (0, -4, and -15), then create a Group Clip in each of these tracks that run across the whole session and adjust the clip gain in each to the desired value. These tracks can be inactive but not hidden. They can be anywhere in the session though and we do not need to have them on screen for this to work.

      Now, fill out the script below with the name of the track that contains the Clip Gain you want to copy from, in this example, it's "Gain -4". I highly suggest making a Template out of this to make it easier to do the different variations for every track (let me know if you need help with that):

      /**@param {{ clipGainTrackName: string }} arg  */
      function copyPasteClipGainFromTrack({ clipGainTrackName }) {
          sf.ui.proTools.appActivateMainWindow();
          sf.ui.proTools.mainWindow.invalidate();
      
          let copyClipGainMenuItem = sf.ui.proTools.getMenuItem("Edit", "Copy Special", "Clip Gain");
          let separateClipMenuItem = sf.ui.proTools.getMenuItem("Edit", "Separate Clip");
      
          // Error Handling: If no clip is present in current playhead position:
          if (!separateClipMenuItem.isEnabled) {
              throw `No clip present on playhead position.`
          }
      
          const selectedTrackNames = sf.ui.proTools.selectedTracks.names
      
          sf.ui.proTools.trackSelectByName({ names: [clipGainTrackName], deselectOthers: true });
      
          // Force menu item state to refresh by pressing an arbitrary key
          // This is to be able to read current menu item state in the next step
          sf.keyboard.press({ keys: "left" });
      
          if (!copyClipGainMenuItem.isEnabled) {
              // Re-select originally selected track
              sf.ui.proTools.trackSelectByName({ names: selectedTrackNames });
      
              throw `No clip present in track "${clipGainTrackName}" on playhead position.`
          }
      
          // Copy Clip Gain from clip in track
          copyClipGainMenuItem.elementClick();
      
          // Re-select originally selected track
          sf.ui.proTools.trackSelectByName({ names: selectedTrackNames });
      
          // Paste Clip Gain
          sf.ui.proTools.menuClick({
              menuPath: ["Edit", "Paste"],
          });
      }
      
      copyPasteClipGainFromTrack({
          clipGainTrackName: 'Gain -4'
      });
      

      Now select the clips in the session and run the script!

      This will work with several tracks selected as well but remember that since we're limited to selecting clips by the timeline selection, this will paste the clip gain to any other partial clips that happen to be within it, so I'd be careful not to make broad selections.

      If everything works, it should look like this:

      Hopefully that helps!

      ReplySolution
      1. AAndrew Sherman @Andrew_Sherman
          2022-04-11 18:04:08.091Z

          This is great, thank you Raphael. I'm going to work with this and make some changes and I might come back to you if I get stuck on something. Much appreciated

          1. AAndrew Sherman @Andrew_Sherman
              2022-04-11 20:18:46.732Z

              Hi @raphaelsepulveda ,

              I've made some progress on this but I'm getting a bit stuck when there is no existing track; it's not pasting the clip gain onto the selected clips.

              Can you let me know where I'm going wrong? I went wild with the functions, like a kid with a new toy!

              Also, if it's possible for the timeline view to reset back to what it was (zoom level) before the command that would be ideal. Any general improvements/tweaks to the script are welcome.

              
              
              const gainValueA = -4;
              const gainA = "Gain " + gainValueA;
              
              // Create a new mono track
              function trackNew() {
              
                  sf.ui.proTools.menuClick({
                      menuPath: ['Track', 'New...']
                  });
              
                  sf.ui.proTools.dialogWaitForManual({ dialogTitle: 'New Tracks' }).dialog;
              
                  sf.ui.proTools.focusedWindow.getFirstWithTitle("Create").elementClick();
              }
              
              // Mute track
              function trackMute() {
                  sf.ui.proTools.invalidate();
                  sf.ui.proTools.trackGetByName({ name: gainA, makeVisible: true }).track.trackSetMute({
                      targetValue: "Enable",
                  });
              }
              
              // Make track inactive
              function trackInactive() {
                  sf.ui.proTools.invalidate();
                  sf.ui.proTools.menuClick({ menuPath: ['Track', 'Make Inactive'] });
              }
              
              // Select from beginning to end of timeline
              function selectTime() {
                  // Go to beginning
                  sf.ui.proTools.transportEnsureCluster();
                  const trannsportCluster = sf.ui.proTools.mainWindow.transportViewCluster;
                  const transportButtons = trannsportCluster.groups.whoseTitle.is("Normal Transport Buttons").first;
                  transportButtons.buttons.whoseTitle.is("Return to Zero").first.elementClick();
                  // Select to end
                  sf.keyboard.press({ keys: "shift+tab", fast: true, repetitions: 5 });
              }
              
              // Create new clip group
              function clipGroup() {
                  sf.ui.proTools.invalidate();
                  sf.ui.proTools.menuClick({
                      menuPath: ["Clip", "Group"],
                  });
              }
              
              // Set clip gain
              // Assumes default increment of 1/2 Db
              function clipGain() {
              
                  if (gainValueA > 0) {
                      sf.keyboard.press({
                          keys: "ctrl+shift+up",
                          repetitions: (gainValueA) * 2,
                          fast: true,
                      });
                  } else {
                      sf.keyboard.press({
                          keys: "ctrl+shift+down",
                          repetitions: (gainValueA) * -2,
                          fast: true,
                      });
                  }
              
              }
              
              // Paste Clip gain
              /**@param {{ clipGainTrackName: string }} arg  */
              function copyPasteClipGainFromTrack({ clipGainTrackName }) {
                  sf.ui.proTools.appActivateMainWindow();
                  sf.ui.proTools.mainWindow.invalidate();
              
                  let copyClipGainMenuItem = sf.ui.proTools.getMenuItem("Edit", "Copy Special", "Clip Gain");
                  let separateClipMenuItem = sf.ui.proTools.getMenuItem("Edit", "Separate Clip");
              
                  // Error Handling: If no clip is present in current playhead position:
                  if (!separateClipMenuItem.isEnabled) {
                      throw `No clip present on playhead position.`
                  }
              
                  const selectedTrackNames = sf.ui.proTools.selectedTracks.names
              
                  sf.ui.proTools.trackSelectByName({ names: [clipGainTrackName], deselectOthers: true });
              
                  // Force menu item state to refresh by pressing an arbitrary key
                  // This is to be able to read current menu item state in the next step
                  sf.keyboard.press({ keys: "left" });
              
                  if (!copyClipGainMenuItem.isEnabled) {
                      // Re-select originally selected track
                      sf.ui.proTools.trackSelectByName({ names: selectedTrackNames });
              
                      throw `No clip present in track "${clipGainTrackName}" on playhead position.`
                  }
              
                  // Copy Clip Gain from clip in track
                  copyClipGainMenuItem.elementClick();
              
                  // Re-select originally selected track
                  sf.ui.proTools.trackSelectByName({ names: selectedTrackNames });
              
                  // Paste Clip Gain
                  sf.ui.proTools.menuClick({
                      menuPath: ["Edit", "Paste"],
                  });
              }
              
              sf.ui.proTools.appActivate();
              
              // If track doesn't exist, then make one
              
              if (!sf.ui.proTools.trackNames.some(c => c.substr(0, 20) === "Gain " + gainValueA)) {
              
                  trackNew();
              
                  // Rename track
                  sf.ui.proTools.invalidate();
                  sf.ui.proTools.selectedTrack.trackRename({
                      renameFunction: oldName => gainA,
                  });
              
                  trackMute();
                  trackInactive();
                  selectTime();
                  clipGroup();
                  clipGain();
                  copyPasteClipGainFromTrack({
                      clipGainTrackName: gainA
                  });
              
              } else {
              
                  // Paste clip gain
                  copyPasteClipGainFromTrack({
                      clipGainTrackName: gainA
                  });
              
              }
              
              
              1. Nice!

                I can see a couple spots where there could be trouble, so let me suggest a slightly different approach.

                You can cut down time and lower complexity by creating a Track Preset for the Clip Gain track instead of creating it from scratch. Just make sure that when you do so, tick the box that says "Include Audio and MIDI Clips", that way the Clip Group is saved as well. Create one for each of your Clip Tracks.

                With that set up, you can use something like my "Create New Track with Track Preset" script from my Utilities package in the store, to recall that preset.

                Using this method will also retain your zoom level.

                Here's how that code would look like:

                /**@param {{ clipGainTrackName: string }} arg  */
                function copyPasteClipGainFromTrack({ clipGainTrackName }) {
                    sf.ui.proTools.appActivateMainWindow();
                    sf.ui.proTools.mainWindow.invalidate();
                
                    let copyClipGainMenuItem = sf.ui.proTools.getMenuItem("Edit", "Copy Special", "Clip Gain");
                    let separateClipMenuItem = sf.ui.proTools.getMenuItem("Edit", "Separate Clip");
                
                    // Error Handling: If no clip is present in current playhead position:
                    if (!separateClipMenuItem.isEnabled) {
                        throw `No clip present on playhead position.`
                    }
                
                    const selectedTrackNames = sf.ui.proTools.selectedTracks.names;
                
                    sf.ui.proTools.trackSelectByName({ names: [clipGainTrackName], deselectOthers: true });
                
                    // Force menu item state to refresh by pressing an arbitrary key
                    // This is to be able to read current menu item state in the next step
                    sf.keyboard.press({ keys: "left" });
                
                    if (!copyClipGainMenuItem.isEnabled) {
                        // Re-select originally selected track
                        sf.ui.proTools.trackSelectByName({ names: selectedTrackNames });
                
                        throw `No clip present in track "${clipGainTrackName}" on playhead position.`
                    }
                
                    // Copy Clip Gain from clip in track
                    copyClipGainMenuItem.elementClick();
                
                    // Re-select originally selected track
                    sf.ui.proTools.trackSelectByName({ names: selectedTrackNames });
                
                    // Paste Clip Gain
                    sf.ui.proTools.menuClick({
                        menuPath: ["Edit", "Paste"],
                    });
                }
                
                function main({ clipGainTrackName }) {
                    sf.ui.proTools.appActivateMainWindow();
                
                    // If track doesn't exist, then make one
                    if (!sf.ui.proTools.trackNames.some(trackName => trackName === clipGainTrackName)) {
                        const selectedTrackNames = sf.ui.proTools.selectedTracks.names;
                
                        //Calling command "RS_Create New Track with Track Preset" from package "Raphael Sepulveda Utilities"
                        sf.soundflow.runCommand({
                            commandId: 'THIS WILL BE UNIQUE TO YOUR ACCOUNT',
                            props: {
                                menuPath1: ["Track Presets", "Clip Gain Presets"],
                                menuPath2: [clipGainTrackName],
                            }
                        });
                
                        sf.ui.proTools.trackSelectByName({ names: selectedTrackNames });
                    }
                
                    copyPasteClipGainFromTrack({
                        clipGainTrackName
                    });
                }
                
                main({
                    clipGainTrackName: "Gain -4"
                });
                

                To use my "Create New Track with Track Preset" template in script form:

                • Install package
                • Create a Macro and "Add Action". Search for "RS_Create New Track with Track Preset".
                • Fill with the adequate menu paths
                • On the top right corner of the command, click "..." and then "Copy as Javascript"
                • Paste into the script
                • Change the value of menuPath2 to [clipGainTrackName], like I did on the script above.
                • (The reason we have to do this from scratch instead of just using what I wrote is because the commandID will be different for your account)
                1. AAndrew Sherman @Andrew_Sherman
                    2022-04-12 20:17:43.285Z

                    Thank you Raphael, I'm looking forward to trying this out and getting my head around it. Look like those track presets are going to be very handy.

                    1. AAndrew Sherman @Andrew_Sherman
                        2022-04-13 12:11:22.478Z

                        Hi Raphael,

                        I'm having some difficulty installing your utilities package; I have tried installing it (sound SoundFlow says its installed), but it's not visible anywhere and when i search for the command it's not there either. I tried uninstalling and installing again.

                        1. Mmm, that is strange. Did you try restarting SoundFlow completely after installing the package?

                          1. AAndrew Sherman @Andrew_Sherman
                              2022-04-13 18:25:31.881Z

                              Hi Raphael,

                              Now it's showing for me - perhaps it just needed some time to load or something.