Is there a better way to get the open session's sample rate than opening the Session Setup dialog and reading the static text?
- Christian Scheuer @chrscheuer2020-02-23 20:03:50.116Z
At the moment I think that's the only way, unfortunately.
We do have some alpha-level features that could potentially read this directly, but they're not fully supported at the moment.Andrew Scheps @Andrew_Scheps
It works fine getting it from the Session Setup window, it just feels clumsy. Looking forward to Alpha turning to Beta!
- RIn reply toAndrew_Scheps⬆:Richard Furch @Richard_Furch
How do I read the sample rate from the session setup window? Which macro would I use?
Kitch Membery @Kitch2020-11-10 07:14:51.733Z
Hi Richard, please watch this video to learn how to use the "Click UI Element" action in SoundFlow to get text values from UI Elements in Pro Tools.
- In reply toKitch⬆:RRichard Furch @Richard_Furch
For anybody wishing to copy, here's a quick script that reports the sample rate of the session via variable sampleRate in a format 44100, 48000 etc and sampleRateString in a format 44.1 kHz, 48 kHz etc.
sf.ui.proTools.menuClick({ menuPath: ["Setup","Session"], }); var sampleRateString = sf.ui.proTools.windows.whoseTitle.is('Session Setup').first.groups.whoseTitle.is('Session Format').first.children.whoseRole.is("AXStaticText").allItems[2].value.invalidate().value; log(sampleRateString); if (sampleRateString=="44.1 kHz") {var sampleRate=44100;} if (sampleRateString=="48 kHz") {var sampleRate=48000;} if (sampleRateString=="96 kHz") {var sampleRate=96000;} if (sampleRateString=="88.2 kHz") {var sampleRate=88200;} log (sampleRate);
Kitch Membery @Kitch2020-11-30 02:36:28.117Z
Nice one Richard!
Thanks for reporting back and helping the community!
Here is a way to make it into a reusable function with a few tweaks :-)
function getSessionSampleRate() { if (!sf.ui.proTools.windows.whoseTitle.is('Session Setup').first.exists) { sf.ui.proTools.menuClick({ menuPath: ["Setup", "Session"], }); } const win = sf.ui.proTools.windows.whoseTitle.is('Session Setup').first; const sessionFormatPanel = win.groups.whoseTitle.is('Session Format').first; const sampleRateString = sessionFormatPanel.children.whoseRole.is("AXStaticText").allItems[2].value.invalidate().value; sf.ui.proTools.menuClick({ menuPath: ["Setup", "Session"], }); const sampleRate = Number(sampleRateString.split(' ')[0]) * 1000; return sampleRate; } const sampleRate = getSessionSampleRate(); log(sampleRate);
- RRichard Furch @Richard_Furch
Thank you.
A couple of questions:
- Why do you use const instead of var?
- and now as a function. Where do I put this code in order for the function to be available in all scripts?
Awesome
Kitch Membery @Kitch2020-11-30 03:03:51.334Z
Hi Richard,
To answer question 1;
var
is an old method of declaring a variable;Here is a great article that I found that walks through the differences between
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/var
,let
&const
.The short version is that it is best to declare a variable using
let
instead of var as let is scope specific (ie. cannot be accessed through function walls). And useconst
to declare variables that will not change after they have first been declared.I hope that make sense. :-)
samuel henriques @samuel_henriques
Hey Kitch, does it make sense to always use "const" untill you find you need to change the value? I've been using "let " as first option, should I change it to "const"?
Thank you
Kitch Membery @Kitch2020-11-30 06:04:28.772Z2020-11-30 06:11:07.456Z
It really depends on your intension. Variable and constants are essentially storage containers.
If you declare a variable with
let
you can then change it's value like thislet myVariable = "Before"; myVariable = "After"; log(myVariable); // This will log "After"
Note:(you only need to declare the variable once), if you paste this code into the SoundFlow editor you will notice the issue,
let myVariable = "Before"; let myVariable = "After"; //both myVariable's will be underlined because you cannot redeclare using let log(myVariable);
The same applies for
const
, But if you declare a variable withconst
you cannot change it, if you paste this code into the SoundFlow editor you will notice the issue,const myVariable = "Before"; myVariable = "After"; //myVariable will be underlined as an error. log(myVariable);
Variables & constants declared with
let
&const
cannot pass through function walls so they can be redeclared inside functions.But yes... It's best to use
const
as much as possible so that your intention of the variable is clear.Be sure to read the link I posted to Richard above. Which explains it much better (I think).
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/samuel henriques @samuel_henriques
Thanks for that @Kitch I had read the article before actually. I'm
learning as I go, so it takes me a bit of failing for these concepts to make sense in by brain. :)Kitch Membery @Kitch2020-11-30 09:47:10.294Z
You and me both mate. Keep up the good work. :-)
- In reply toRichard_Furch⬆:
Kitch Membery @Kitch2020-11-30 03:35:09.593Z2020-11-30 03:41:22.609Z
For question 2,
I should make a video for this. :-)
Its good practice to organize your code so that it is clean and reusable.
A great way to do this is to have your script broken up into functions that do just one thing each.
Here is an example, I have three simple functions
myFirstFunction
,mySecondFunction
&myThirdFunction
, Then I have amain
function where I call the individual functions.function myFirstFunction(){ log('This text is being logged from "myFirstFunction"') } function mySecondFunction(){ log('This text is being logged from "mySecondFunction"') } function myThirdFunction(){ log('This text is being logged from "myThirdFunction"') } function main() { myFirstFunction(); mySecondFunction(); myThirdFunction(); } main();
So in your situation you would do this (see the comments inside the code block);
function getSessionSampleRate() { if (!sf.ui.proTools.windows.whoseTitle.is('Session Setup').first.exists) { sf.ui.proTools.menuClick({ menuPath: ["Setup", "Session"], }); } const win = sf.ui.proTools.windows.whoseTitle.is('Session Setup').first; const sessionFormatPanel = win.groups.whoseTitle.is('Session Format').first; const sampleRateString = sessionFormatPanel.children.whoseRole.is("AXStaticText").allItems[2].value.invalidate().value; sf.ui.proTools.menuClick({ menuPath: ["Setup", "Session"], }); const sampleRate = Number(sampleRateString.split(' ')[0]) * 1000; return sampleRate; } function main() { //Add the code that occurs before getting the Session Sample Rate here; const sampleRate = getSessionSampleRate(); log(sampleRate); //Add the code that occurs after getting the Session Sample Rate here; } //This calls the main function; main();
Then if you need to reuse the function in another script you can easily copy and paste it.
Let me know if that makes sense :-)
Rock on!- RRichard Furch @Richard_Furch
Got it. Makes sense. Like referring to chapters in a book and jumping there.
Is there a way to make functions global? Like let's say sf.wait(); I don't have to INVENT it everytime I write a script. Does soundflow have libraries so I can add functions to it's vocabulary, and let's say call getSessionSampleRate from anywhere without having to embed the code all the time?Kitch Membery @Kitch2020-11-30 06:03:04.868Z
I get where you are coming from, and there are ways of linking scripts but I would not suggest working that way just yet. Better to keep the code all together for now.
Christian Scheuer @chrscheuer2020-12-02 16:05:07.374Z
@Richard_Furch if you do want to explore how to reuse functions across different scripts, here's a post that explains the fundamentals:
- In reply toKitch⬆:
Brandon Metcalf @Brandon_Metcalf
This is super cool. Great suggestion @Richard_Furch
I tend to call up the Session Setup box to check sample rate and bit depth prior to these 3 actions that I regularly perform:
- Bounce to Disk (or Bounce Mix as they call it now)
- Export clips as audio files
- Save session copy
99 times out of 100, the settings on the pop-up windows for those 3 actions will already match the session settings.. but those 1 out of 100 instances when I forget to change something I need to can be pretty painful!
How difficult would it be to create super-power versions of the existing Pro Tools functions, that will automatically check and then populate the correct session settings on the next windows for those 3 actions (Bounce, Export Audio Files, Save Session Copy)?
Follow up question - the Pro Tools key commands for those actions are so ingrained in my muscle memory. Is it possible for a Soundflow keyboard trigger to replace the standard Pro Tools keyboard shortcuts?
Chris Shaw @Chris_Shaw2020-12-03 02:04:33.378Z2020-12-03 02:47:36.360Z
Answer to your last question: Yes. If you set a keyboard script trigger with the same PT keyboard shortcut then the SF trigger overrides it. I have a script that automatically fills in the bounce window info (Bounce filename, sample rate, bit depth, include MP3, etc) and fills in the MP3 window automatically with the Artist Name, etc. I use cmd-B to trigger it which is the PT keyboard shortcut as well. That keyboard command now triggers my script instead of Bounce to Mix. If I ever need to use a normal Bounce to Mix I just use the mouse and select it from the widow.
Another thing I do is use shift-cmd-W to trigger a script which asks me if I want to back up the session before closing. There are a lot of possibilities…
- In reply toBrandon_Metcalf⬆:
Chris Shaw @Chris_Shaw2020-12-03 02:48:45.358Z
How difficult would it be to create super-power versions of the existing Pro Tools functions, that will automatically check and then populate the correct session settings on the next windows for those 3 actions (Bounce, Export Audio Files, Save Session Copy)?
Not that hard at all