Skip to content

Commit

Permalink
***Rewrite indentation and need errors*
Browse files Browse the repository at this point in the history
ührung taped creation xo pancakes Amendment function*~
  • Loading branch information
ak5k committed Feb 8, 2024
1 parent 2c3e9bf commit 3408f19
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 109 deletions.
128 changes: 76 additions & 52 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ bool AudioEngine::isPlaying() const
double AudioEngine::beatTime() const
{
const auto sessionState = mLink.captureAppSessionState();
return sessionState.beatAtTime(mLink.clock().micros(),
mSharedEngineData.quantum);
return sessionState.beatAtTime(
mLink.clock().micros(), mSharedEngineData.quantum
);
}

void AudioEngine::setTempo(double tempo)
Expand Down Expand Up @@ -131,8 +132,10 @@ int FindTempoTimeSigMarkerByPosition(ReaProject* proj, double targetPos)

while (true)
{
if (!GetTempoTimeSigMarker(proj, idx, &pos, &measure, &beat, &bpm,
&timesig_num, &timesig_denom, &lineartempo))
if (!GetTempoTimeSigMarker(
proj, idx, &pos, &measure, &beat, &bpm, &timesig_num, &timesig_denom,
&lineartempo
))
break; // No more tempo/time signature markers

if (pos == targetPos)
Expand All @@ -147,8 +150,9 @@ int FindTempoTimeSigMarkerByPosition(ReaProject* proj, double targetPos)
}

// Function to find a region or marker by name containing a specific substring
int FindRegionOrMarkerByNameContaining(ReaProject* proj,
const std::string& substring)
int FindRegionOrMarkerByNameContaining(
ReaProject* proj, const std::string& substring
)
{
int idx = 0;
bool isRegion;
Expand All @@ -159,8 +163,9 @@ int FindRegionOrMarkerByNameContaining(ReaProject* proj,
while (true)
{
const char* namebuf;
if (!EnumProjectMarkers2(proj, idx, &isRegion, &pos, &regEnd, &namebuf,
&markerId))
if (!EnumProjectMarkers2(
proj, idx, &isRegion, &pos, &regEnd, &namebuf, &markerId
))
{
break; // No more markers or regions
}
Expand Down Expand Up @@ -210,38 +215,43 @@ double GetNextFullMeasureTimePosition()
int measures3 = 0;
int beats3 = 0;

int measurePosition =
(int)TimeMap2_timeToBeats(0, currentPosition, &measures3, &beats3, NULL,
NULL); // Convert the current position to beats
int measurePosition = (int)TimeMap2_timeToBeats(
0, currentPosition, &measures3, &beats3, NULL,
NULL
); // Convert the current position to beats
(void)measurePosition;
return TimeMap2_beatsToTime(
0, beats3,
&measures3); // Convert the next full measure position to time
&measures3
); // Convert the next full measure position to time
}

int SetLaunchPrerollRegion()
{
double projectLength = GetProjectLength(0); // Get the length of the project
int measures3 = 0;
int beats3 = 0;
double num_measures =
TimeMap2_timeToBeats(0, projectLength, &measures3, &beats3, NULL,
NULL); // Convert the project length to measures/beats
double num_measures = TimeMap2_timeToBeats(
0, projectLength, &measures3, &beats3, NULL,
NULL
); // Convert the project length to measures/beats
auto region_start = TimeMap2_beatsToTime(0, beats3, &measures3);
auto pos = GetNextFullMeasureTimePosition();

num_measures =
TimeMap2_timeToBeats(0, region_start, &measures3, &beats3, NULL,
NULL); // Convert the project length to measures/beats
num_measures = TimeMap2_timeToBeats(
0, region_start, &measures3, &beats3, NULL,
NULL
); // Convert the project length to measures/beats
auto region_end = TimeMap2_beatsToTime(0, beats3, &measures3);

(void)pos;
(void)num_measures;
// Add the region to the project
int isRegion = true; // We want to create a region, not a marker
int color = 0; // The color of the region (0 = default color)
return AddProjectMarker2(0, isRegion, region_start, region_end,
"reablink pre-roll", -1, color);
return AddProjectMarker2(
0, isRegion, region_start, region_end, "reablink pre-roll", -1, color
);
}

void ClearReablinkDummyObjects()
Expand Down Expand Up @@ -273,8 +283,9 @@ void ClearReablinkDummyObjects()
}
}

void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
const std::size_t numSamples)
void AudioEngine::audioCallback(
const std::chrono::microseconds hostTime, const std::size_t numSamples
)
{
static bool quantized_launch{false};
auto frame_time = GetFrameTime();
Expand Down Expand Up @@ -312,15 +323,18 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
int timesig_num = 0;
int timesig_denom = 0;
double tempo = 0;
TimeMap_GetTimeSigAtTime(0, pos_target, &timesig_num, &timesig_denom,
&tempo);
TimeMap_GetTimeSigAtTime(
0, pos_target, &timesig_num, &timesig_denom, &tempo
);
target_region_idx = AddProjectMarker2(
0, true, pos_target, pos_target + (60. / tempo * engineData.quantum),
"reablink target", -1, 0);
"reablink target", -1, 0
);
auto target_tempo_idx = FindTempoTimeSigMarker(0, pos_target);
SetTempoTimeSigMarker(0, target_tempo_idx, pos_target, 0, 0,
sessionState.tempo(), timesig_num, timesig_denom,
false);
SetTempoTimeSigMarker(
0, target_tempo_idx, pos_target, 0, 0, sessionState.tempo(),
timesig_num, timesig_denom, false
);
preroll_region_idx = SetLaunchPrerollRegion();
sessionState.requestBeatAtStartPlayingTime(0, engineData.quantum);
auto beat_now = sessionState.beatAtTime(hostTime, engineData.quantum);
Expand All @@ -337,8 +351,10 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
}
}

SetTempoTimeSigMarker(0, -1, pos_preroll, 0, 0, sessionState.tempo(),
timesig_num, timesig_denom, false);
SetTempoTimeSigMarker(
0, -1, pos_preroll, 0, 0, sessionState.tempo(), timesig_num,
timesig_denom, false
);
FindTempoTimeSigMarker(0, pos_preroll);

MediaTrack* track = GetTrack(0, 0); // Get the first track
Expand All @@ -347,14 +363,16 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,

CreateNewMIDIItemInProj(
track, pos_preroll,
pos_preroll + (60. / sessionState.tempo() * engineData.quantum), 0);
pos_preroll + (60. / sessionState.tempo() * engineData.quantum), 0
);

int measures{0};
TimeMap2_timeToBeats(0, pos_preroll, &measures, 0, 0, 0);
auto pos_preroll_start = TimeMap2_beatsToTime(0, beat_offset, &measures);
SetEditCurPos(pos_preroll_start +
(hostTime - mLink.clock().micros()).count() / 1.0e6,
false, false);
SetEditCurPos(
pos_preroll_start + (hostTime - mLink.clock().micros()).count() / 1.0e6,
false, false
);

quantized_launch = true;
}
Expand All @@ -378,9 +396,9 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
ClearReablinkDummyObjects();
Undo_EndBlock("ReaBlink", -1);
}
else if (isPuppet && !mIsPlaying && !sessionState.isPlaying() &&
GetPlayState() & 1)
else if (isPuppet && !mIsPlaying && !sessionState.isPlaying() && GetPlayState() & 1)
{
ClearReablinkDummyObjects();
OnStopButton();
}

Expand Down Expand Up @@ -461,8 +479,9 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
}
else
{
sessionState.requestBeatAtTime(fmod(beat, 1.), hostTime,
4. / timesig_denom);
sessionState.requestBeatAtTime(
fmod(beat, 1.), hostTime, 4. / timesig_denom
);
}
}
qn_prev = qn_abs;
Expand All @@ -488,8 +507,10 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
{
limit_denom = 1.0;
}
static auto limit = std::max(frame_time / limit_denom,
buf_len_time / limit_denom); // seconds
static auto limit = std::max(
frame_time / limit_denom,
buf_len_time / limit_denom
); // seconds

if (!isMaster && isPuppet && mLink.numPeers() > 0 && !quantized_launch &&
(sessionState.beatAtTime(hostTime, engineData.quantum) < 0 ||
Expand All @@ -498,25 +519,27 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
abs(reaper_phase_current - link_phase_current) < 0.5 &&
GetToggleCommandState(40620) == 0)
{
limit = std::max(frame_time / limit_denom / 2,
buf_len_time / limit_denom / 2); // seconds
limit = std::max(
frame_time / limit_denom / 2,
buf_len_time / limit_denom / 2
); // seconds
if (reaper_phase_time > link_phase_time && Master_GetPlayRate(0) >= 1)
{
Main_OnCommand(40525, 0);
Main_OnCommand(40525, 0);
}
else if (reaper_phase_time < link_phase_time &&
Master_GetPlayRate(0) <= 1)
else if (reaper_phase_time < link_phase_time && Master_GetPlayRate(0) <= 1)
{
Main_OnCommand(40524, 0);
Main_OnCommand(40524, 0);
}
}
else if (!isMaster && isPuppet && mLink.numPeers() > 0 &&
abs(diff) < limit && Master_GetPlayRate(0) != 1)
else if (!isMaster && isPuppet && mLink.numPeers() > 0 && abs(diff) < limit && Master_GetPlayRate(0) != 1)
{
limit = std::max(frame_time / limit_denom,
buf_len_time / limit_denom); // seconds
limit = std::max(
frame_time / limit_denom,
buf_len_time / limit_denom
); // seconds
Main_OnCommand(40521, 0);
}
else if ((mLink.numPeers() == 0 || isMaster) && abs(diff) > limit)
Expand Down Expand Up @@ -544,15 +567,16 @@ void AudioEngine::audioCallback(const std::chrono::microseconds hostTime,
GetSet_LoopTimeRange(false, false, &loop_start, &loop_end, false);
auto loop_end_beat = TimeMap2_timeToBeats(0, loop_end, 0, 0, 0, 0);
auto beat = TimeMap2_timeToBeats(0, r_pos, 0, 0, 0, 0);
if ((r_pos > loop_start && r_pos < loop_end) &&
abs(beat - loop_end_beat) < 1)
if ((r_pos > loop_start && r_pos < loop_end) && abs(beat - loop_end_beat) < 1)
{
new_tempo = hostBpm;
}
(void)measures;
}
if (!SetTempoTimeSigMarker(0, ptidx, timepos, measurepos, beatpos,
new_tempo, timesig_num, timesig_denom, 0))
if (!SetTempoTimeSigMarker(
0, ptidx, timepos, measurepos, beatpos, new_tempo, timesig_num,
timesig_denom, 0
))
{
sessionState.setTempo(new_tempo, hostTime);
}
Expand Down
Loading

0 comments on commit 3408f19

Please sign in to comment.