Skip to content

Commit da30a0a

Browse files
committed
implement #27
added support for randomized sleep command over specified time range
1 parent d920df8 commit da30a0a

8 files changed

Lines changed: 176 additions & 17 deletions

File tree

‎Engine/BaseSleepCommand.h‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* mTroll MIDI Controller
3+
* Copyright (C) 2009-2010,2018,2024 Sean Echevarria
4+
*
5+
* This file is part of mTroll.
6+
*
7+
* mTroll is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* mTroll is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* Let me know if you modify, extend or use mTroll.
21+
* Original project site: http://www.creepingfog.com/mTroll/
22+
* Contact Sean: "fester" at the domain of the original project site
23+
*/
24+
25+
#ifndef BaseSleepCommand_h__
26+
#define BaseSleepCommand_h__
27+
28+
#include "IPatchCommand.h"
29+
#include "IMidiOut.h"
30+
#ifdef _WINDOWS
31+
#include <windows.h>
32+
#define SLEEP Sleep
33+
#undef TextOut // stupid unicode support defines TextOut to TextOutW
34+
#else
35+
#define SLEEP sleep
36+
#endif // _WINDOWS
37+
38+
39+
40+
class BaseSleepCommand : public IPatchCommand
41+
{
42+
public:
43+
BaseSleepCommand() = default;
44+
45+
virtual void Exec() override
46+
{
47+
SLEEP(GetSleepAmount()); // amount in milliseconds
48+
}
49+
50+
protected:
51+
virtual int GetSleepAmount() = 0;
52+
};
53+
54+
#endif // BaseSleepCommand_h__

‎Engine/EngineLoader.cpp‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* mTroll MIDI Controller
3-
* Copyright (C) 2007-2015,2018,2020-2023 Sean Echevarria
3+
* Copyright (C) 2007-2015,2018,2020-2024 Sean Echevarria
44
*
55
* This file is part of mTroll.
66
*
@@ -63,6 +63,7 @@
6363
#include "ControllerInputMonitor.h"
6464
#include "RepeatingMomentaryPatch.h"
6565
#include "RepeatingTogglePatch.h"
66+
#include "SleepRandomCommand.h"
6667

6768

6869
#ifdef _MSC_VER
@@ -73,6 +74,7 @@
7374
static PatchBank::PatchState GetLoadState(const std::string & tmpLoad);
7475
static PatchBank::PatchSyncState GetSyncState(const std::string & tmpLoad);
7576
static PatchBank::SecondFunctionOperation GetSecondFuncOp(const std::string & tmp);
77+
static void ReadTwoIntValues(std::string tmp, int & val1, int & val2);
7678

7779
EngineLoader::EngineLoader(ITrollApplication * app,
7880
IMidiOutGenerator * midiOutGenerator,
@@ -896,6 +898,29 @@ EngineLoader::LoadPatches(TiXmlElement * pElem)
896898
}
897899
continue;
898900
}
901+
else if (patchElement == "SleepRandom")
902+
{
903+
std::string sleepAmtStr;
904+
if (childElem->GetText())
905+
sleepAmtStr = childElem->GetText();
906+
int sleepMinAmt = 0;
907+
int sleepMaxAmt = 0;
908+
::ReadTwoIntValues(sleepAmtStr, sleepMinAmt, sleepMaxAmt);
909+
if (0 < sleepMinAmt && 0 < sleepMaxAmt)
910+
{
911+
if (group == "B")
912+
cmds2.push_back(std::make_shared<SleepRandomCommand>(sleepMinAmt, sleepMaxAmt));
913+
else
914+
cmds.push_back(std::make_shared<SleepRandomCommand>(sleepMinAmt, sleepMaxAmt));
915+
}
916+
else if (mTraceDisplay)
917+
{
918+
std::strstream traceMsg;
919+
traceMsg << "Error loading config file: no (or invalid) times specified in SleepRandom in patch " << patchName << '\n' << std::ends;
920+
mTraceDisplay->Trace(std::string(traceMsg.str()));
921+
}
922+
continue;
923+
}
899924
else if (patchElement == "localExpr")
900925
continue;
901926
else if (patchElement == "globalExpr")
@@ -3008,3 +3033,23 @@ GetSecondFuncOp(const std::string & secFuncOp)
30083033
else
30093034
return PatchBank::sfoManual;
30103035
}
3036+
3037+
void
3038+
ReadTwoIntValues(std::string tmp, int & val1, int & val2)
3039+
{
3040+
if (tmp.empty())
3041+
return;
3042+
3043+
int tokenPos = tmp.find_first_of(" ,:;");
3044+
if (std::string::npos == tokenPos)
3045+
return;
3046+
3047+
val1 = ::atoi(tmp.c_str());
3048+
3049+
tmp = tmp.substr(tokenPos + 1);
3050+
while ((tokenPos = tmp.find_first_of(" ,:;")) != std::string::npos)
3051+
tmp = tmp.substr(tokenPos + 1);
3052+
3053+
if (!tmp.empty())
3054+
val2 = ::atoi(tmp.c_str());
3055+
}

‎Engine/SleepCommand.h‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* mTroll MIDI Controller
3-
* Copyright (C) 2009-2010,2018 Sean Echevarria
3+
* Copyright (C) 2009-2010,2018,2024 Sean Echevarria
44
*
55
* This file is part of mTroll.
66
*
@@ -25,28 +25,21 @@
2525
#ifndef SleepCommand_h__
2626
#define SleepCommand_h__
2727

28-
#include "IPatchCommand.h"
29-
#include "IMidiOut.h"
30-
#ifdef _WINDOWS
31-
#include <windows.h>
32-
#define SLEEP Sleep
33-
#undef TextOut // stupid unicode support defines TextOut to TextOutW
34-
#else
35-
#define SLEEP sleep
36-
#endif // _WINDOWS
28+
#include "BaseSleepCommand.h"
3729

3830

39-
class SleepCommand : public IPatchCommand
31+
class SleepCommand : public BaseSleepCommand
4032
{
4133
public:
42-
SleepCommand(int sleepAmt) :
34+
SleepCommand(int sleepAmt) noexcept :
4335
mSleepAmt(sleepAmt)
4436
{
4537
}
4638

47-
virtual void Exec() override
39+
protected:
40+
int GetSleepAmount() noexcept override
4841
{
49-
SLEEP(mSleepAmt); // amount in milliseconds
42+
return mSleepAmt;
5043
}
5144

5245
private:

‎Engine/SleepRandomCommand.h‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* mTroll MIDI Controller
3+
* Copyright (C) 2024 Sean Echevarria
4+
*
5+
* This file is part of mTroll.
6+
*
7+
* mTroll is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* mTroll is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* Let me know if you modify, extend or use mTroll.
21+
* Original project site: http://www.creepingfog.com/mTroll/
22+
* Contact Sean: "fester" at the domain of the original project site
23+
*/
24+
25+
#ifndef SleepRandomCommand_h__
26+
#define SleepRandomCommand_h__
27+
28+
#include <random>
29+
#include "BaseSleepCommand.h"
30+
31+
32+
class SleepRandomCommand : public BaseSleepCommand
33+
{
34+
public:
35+
SleepRandomCommand(int minSleepAmt, int maxSleepAmt) :
36+
mDistribution(minSleepAmt, maxSleepAmt)
37+
{
38+
}
39+
40+
protected:
41+
int GetSleepAmount() override
42+
{
43+
return mDistribution(mGenerator);
44+
}
45+
46+
private:
47+
SleepRandomCommand();
48+
49+
private:
50+
std::default_random_engine mGenerator;
51+
std::uniform_int_distribution<int> mDistribution;
52+
};
53+
54+
#endif // SleepRandomCommand_h__

‎docs/changelog.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
### mTroll MIDI controller changelog / process milestones
22

3+
#### 2024.02.xx
4+
- Added a [SleepRandom](docs.md#patchCommands) patch command.
5+
36
#### 2023.05.28
47
- Fix for bad sysex stream from Echoplex when using CME U2MIDI Pro interface
58
- Rearrange Echoplex local param data display

‎docs/docs.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ The `RefirePedal` command can be used to resend the current value of an expressi
196196

197197
The `Sleep` command can be used to pause a command sequence for an amount of time specified in milliseconds. Note that the actual amount of time paused will not exactly match the time specified since the OS is not a real-time system, but it will be close enough that variances shouldn't matter. The Sleep command pauses everything in mTroll: MIDI output, MIDI input and the UI. Example of a 5 second sleep: `<Sleep group="A">5000</Sleep>`
198198

199+
The `SleepRandom` command can be used to pause a command sequence for a random amount of time between minimum and maximum amounts of time specified in milliseconds. Note that the actual amount of time paused will not exactly match the time specified since the OS is not a real-time system, but it will be close enough that variances shouldn't matter. The RandomSleep command pauses everything in mTroll: MIDI output, MIDI input and the UI. Example of a random sleep between 500 and 1000 milliseconds: `<SleepRandom group="A">500, 1000</SleepRandom>`
200+
199201
The `AxeProgramChange` command is an Axe-Fx specific version of the generic `ProgramChange` command. When you use this command, you do not have to use bank select messages. You can use the actual Axe-Fx preset number. mTroll will translate the preset into the correct bank select and program change. Afterwards, mTroll will sync up all `AxeToggle` and `AxeMomentary` patches. It will also display the name of the preset in the main window. Example code to load Axe-Fx preset 361: `<AxeProgramChange program="361"/>`
200202

201203
The `EdpProgramChange` command is an Echoplex Digital Pro specific version of the generic `ProgramChange` command. When you use this command, mTroll queries and displays global and local state after sending the requested program change.

‎mTrollQt/mTrollQt.vcxproj‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
<UseOfAtl>false</UseOfAtl>
2222
<CharacterSet>Unicode</CharacterSet>
2323
<WholeProgramOptimization>true</WholeProgramOptimization>
24-
<PlatformToolset>v142</PlatformToolset>
24+
<PlatformToolset>v143</PlatformToolset>
2525
</PropertyGroup>
2626
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2727
<ConfigurationType>Application</ConfigurationType>
2828
<UseOfAtl>false</UseOfAtl>
2929
<CharacterSet>Unicode</CharacterSet>
30-
<PlatformToolset>v142</PlatformToolset>
30+
<PlatformToolset>v143</PlatformToolset>
3131
</PropertyGroup>
3232
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
3333
<ImportGroup Label="ExtensionSettings">
@@ -215,6 +215,7 @@
215215
<ClInclude Include="..\Engine\AxeFx3EffectIds.h" />
216216
<ClInclude Include="..\Engine\AxeFxModel.h" />
217217
<ClInclude Include="..\Engine\AxeMomentaryPatch.h" />
218+
<ClInclude Include="..\Engine\BaseSleepCommand.h" />
218219
<ClInclude Include="..\Engine\CompositeTogglePatch.h" />
219220
<ClInclude Include="..\Engine\ControllerInputMonitor.h" />
220221
<ClInclude Include="..\Engine\ControllerTogglePatch.h" />
@@ -227,6 +228,7 @@
227228
<ClInclude Include="..\Engine\RepeatingMomentaryPatch.h" />
228229
<ClInclude Include="..\Engine\RepeatingPatch.h" />
229230
<ClInclude Include="..\Engine\RepeatingTogglePatch.h" />
231+
<ClInclude Include="..\Engine\SleepRandomCommand.h" />
230232
<ClInclude Include="..\midi\SleepShort.h" />
231233
<ClInclude Include="..\winUtil\WinDark.h" />
232234
<ClInclude Include="AboutDlg.h" />

‎mTrollQt/mTrollQt.vcxproj.filters‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@
341341
<ClInclude Include="..\Engine\ControllerTogglePatch.h">
342342
<Filter>Engine</Filter>
343343
</ClInclude>
344+
<ClInclude Include="..\Engine\SleepRandomCommand.h">
345+
<Filter>Header Files</Filter>
346+
</ClInclude>
347+
<ClInclude Include="..\Engine\BaseSleepCommand.h">
348+
<Filter>Header Files</Filter>
349+
</ClInclude>
344350
</ItemGroup>
345351
<ItemGroup>
346352
<ResourceCompile Include="win\mTrollQt.rc">

0 commit comments

Comments
 (0)