Wait in a script
You could use the timeMs function to get the current time, assign that value to a variable when you want to start waiting, then put something like this within the Process procedure:
IF ((timeMs - startTime) >= waitTime) THEN
...
IF ((timeMs - startTime) >= waitTime) THEN
...
Bjørn S
don't hesitate to use the search function of this forum too..
Here a post about this question
http://www.sensomusic.com/forums/viewtopic.php?id=2619
Here a post about this question
http://www.sensomusic.com/forums/viewtopic.php?id=2619
Thanks Bjørn,
Well I tried and failed... One question, what kind of unit is "(timeMs - startTime)", seconds, milliseconds ? What is the difference betwen Time and TimeMs ?
Here's what I did : I wanted to have my output on 1 for few seconds but it doesn't work... Thanks
Var In1 : tParameter;
Var OutA : tParameter;
// initialisation : create parameters
procedure init;
begin
In1 := Createparam('In', ptButton);
OutA := CreateParam ('Out',pttextfield);
SetStringValue (OutA, '0' );
End;
// Global variables
Var MyTime : Tdatetime;
//////////////////////////////
// main proc
//////////////////////////////
Procedure Process;
begin
MyTime := TimeMs;
If (In1=1) Then
Begin
While ((TimeMs-MyTime <5000)) do SetStringValue (OutA, '1');
End
Else
begin
SetStringValue (OutA, '0');
End;
end;
Well I tried and failed... One question, what kind of unit is "(timeMs - startTime)", seconds, milliseconds ? What is the difference betwen Time and TimeMs ?
Here's what I did : I wanted to have my output on 1 for few seconds but it doesn't work... Thanks
Var In1 : tParameter;
Var OutA : tParameter;
// initialisation : create parameters
procedure init;
begin
In1 := Createparam('In', ptButton);
OutA := CreateParam ('Out',pttextfield);
SetStringValue (OutA, '0' );
End;
// Global variables
Var MyTime : Tdatetime;
//////////////////////////////
// main proc
//////////////////////////////
Procedure Process;
begin
MyTime := TimeMs;
If (In1=1) Then
Begin
While ((TimeMs-MyTime <5000)) do SetStringValue (OutA, '1');
End
Else
begin
SetStringValue (OutA, '0');
End;
end;
Sorry that my first reply was a bit short, but I didn't have time to elaborate.
Anyway, you can see an example in the topic Nay refers to where I use the timeMs function. timeMs returns the elapsed time since I-don't-know-when in milliseconds as a float. According to the reference it returns an Extended value, but as fas as I know you can't define a variable of type Extended in a script, so I use a Single, which is more than good enough anyway. Unless you want to to calculations based on days, I don't see much use for TDateTime within a script.
I'd like to add that something like TurboPascal's Delay doesn't really make much sense in a Usine script as things are processed in blocks where each block's length is defined by the sampling frequency and the block size. Running at eg 44.1 kHz with a block size of 128 will give a block duration of 2.902 milliseconds.
As you probably already know, there are four predefined procedures called from the script engine:
- Init is called once when the script is ninialized/compiled
- Destroy is called once when the script is exited, and is only really needed if you've created some objects that needs to be removed from memory.
- Callback is called once for every change to the parameters, including right after initialization if there are anything connected to the inputs.
- Process is called once for every block.
All callbacks within a block are executed before Process. You don't need all of these (very few scripts need Destroy, for example), and if you can get away without Process, the script doesn't use any CPU after initialization unless there's a callback.
In most situations where you would want some action to be delayed, you start the "countdown" within a callback (startTime := timeMs), but as Callback is only called when there are some change in the parameter values, Process has to be used to check the time elapsed and do whatever needs to be done. If you only want to check that some time has elapsed between two callbacks, you can even skip Process.
In your script you have a check - IF (In1 = 1) - that will never work, unless you create OutA before In1, and then it would always be true. TParameter values is acually just integers where the first CreateParam returns 0, the next 1, and so on. Use GetValue(In1) to get the value, preferably within Callback.
Anyway, you can see an example in the topic Nay refers to where I use the timeMs function. timeMs returns the elapsed time since I-don't-know-when in milliseconds as a float. According to the reference it returns an Extended value, but as fas as I know you can't define a variable of type Extended in a script, so I use a Single, which is more than good enough anyway. Unless you want to to calculations based on days, I don't see much use for TDateTime within a script.
I'd like to add that something like TurboPascal's Delay doesn't really make much sense in a Usine script as things are processed in blocks where each block's length is defined by the sampling frequency and the block size. Running at eg 44.1 kHz with a block size of 128 will give a block duration of 2.902 milliseconds.
As you probably already know, there are four predefined procedures called from the script engine:
- Init is called once when the script is ninialized/compiled
- Destroy is called once when the script is exited, and is only really needed if you've created some objects that needs to be removed from memory.
- Callback is called once for every change to the parameters, including right after initialization if there are anything connected to the inputs.
- Process is called once for every block.
All callbacks within a block are executed before Process. You don't need all of these (very few scripts need Destroy, for example), and if you can get away without Process, the script doesn't use any CPU after initialization unless there's a callback.
In most situations where you would want some action to be delayed, you start the "countdown" within a callback (startTime := timeMs), but as Callback is only called when there are some change in the parameter values, Process has to be used to check the time elapsed and do whatever needs to be done. If you only want to check that some time has elapsed between two callbacks, you can even skip Process.
In your script you have a check - IF (In1 = 1) - that will never work, unless you create OutA before In1, and then it would always be true. TParameter values is acually just integers where the first CreateParam returns 0, the next 1, and so on. Use GetValue(In1) to get the value, preferably within Callback.
Bjørn S
Thanks a lot chap,
That's very interesting... Indeed, I was distracted and wrote "if (n1=1) " instead of "if getvalue (n1)=1", now it works as expected...
I haven't been programming for 20 years, and I had forgotten how fun it is. I forgot almost everything, so I have to search a lot on the web, and Usine Scripting seems to be a bit different from the Turbo Pascal.
I need to wait during my script because I want to rename files randomly named by the sampler, but when I clear the sampler list, I have to wait the file isn't used any longer by usine to be allowed by windows to rename the file (I don't know how to check if a file is used by a program)...
Thanks
Kenan
That's very interesting... Indeed, I was distracted and wrote "if (n1=1) " instead of "if getvalue (n1)=1", now it works as expected...
I haven't been programming for 20 years, and I had forgotten how fun it is. I forgot almost everything, so I have to search a lot on the web, and Usine Scripting seems to be a bit different from the Turbo Pascal.
I need to wait during my script because I want to rename files randomly named by the sampler, but when I clear the sampler list, I have to wait the file isn't used any longer by usine to be allowed by windows to rename the file (I don't know how to check if a file is used by a program)...
Thanks
Kenan
You can consider Usine scripting as a subset of TP or Delphi with some extras. A subset since it doesn't contain everything, the extras being some special functions, variables and declarations together with the four procedures called by the engine.Ken29 wrote:Usine Scripting seems to be a bit different from the Turbo Pascal.
I remember that I had to struggle a bit myself when I started scripting because most of my programming experience is with more sequential/batch-wise programs: Start at the top, do this and that, end... Which is all good when a script is doing whatever it's supposed to do within a single block, but not in cases like this.
Bjørn S
Who is online
Users browsing this forum: No registered users and 93 guests
