Skip to content

Commit

Permalink
Add seq block split actions
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Jun 2, 2024
1 parent 161c3bf commit e302cda
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/audioCore/action/ActionOther.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,43 @@ bool ActionSynth::doAction() {
this->error("Can't synth: [" + juce::String{ this->index } + "]\n");
return false;
}

ActionSplitSequencerBlock::ActionSplitSequencerBlock(
int track, int block, double time)
: ACTION_DB{ track, block, time } {}

bool ActionSplitSequencerBlock::doAction() {
ACTION_UNSAVE_PROJECT();

ACTION_WRITE_TYPE(ActionSplitSequencerBlock);
ACTION_WRITE_DB();

if (auto graph = AudioCore::getInstance()->getGraph()) {
if (auto track = graph->getSourceProcessor(ACTION_DATA(track))) {
if (track->splitSeq(ACTION_DATA(block), ACTION_DATA(time))) {
this->output("Split seq block: [" + juce::String(ACTION_DATA(track)) + ", " + juce::String{ ACTION_DATA(block) } + "]\n");
ACTION_RESULT(true);
}
}
}
this->output("Can't split seq block: [" + juce::String(ACTION_DATA(track)) + ", " + juce::String{ ACTION_DATA(block) } + "]\n");
ACTION_RESULT(false);
}

bool ActionSplitSequencerBlock::undo() {
ACTION_UNSAVE_PROJECT();

ACTION_WRITE_TYPE_UNDO(ActionSplitSequencerBlock);
ACTION_WRITE_DB();

if (auto graph = AudioCore::getInstance()->getGraph()) {
if (auto track = graph->getSourceProcessor(ACTION_DATA(track))) {
if (track->stickSeqWithNext(ACTION_DATA(block))) {
this->output("Undo split seq block: [" + juce::String(ACTION_DATA(track)) + ", " + juce::String{ ACTION_DATA(block) } + "]\n");
ACTION_RESULT(true);
}
}
}
this->output("Can't undo split seq block: [" + juce::String(ACTION_DATA(track)) + ", " + juce::String{ ACTION_DATA(block) } + "]\n");
ACTION_RESULT(false);
}
21 changes: 21 additions & 0 deletions src/audioCore/action/ActionOther.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,24 @@ class ActionSynth final : public ActionBase {

JUCE_LEAK_DETECTOR(ActionSynth)
};

class ActionSplitSequencerBlock final : public ActionUndoableBase {
public:
ActionSplitSequencerBlock() = delete;
ActionSplitSequencerBlock(
int track, int block, double time);

bool doAction() override;
bool undo() override;
const juce::String getName() override {
return "Split Sequencer Track Block";
};

private:
ACTION_DATABLOCK{
const int track, block;
const double time;
} ACTION_DB;

JUCE_LEAK_DETECTOR(ActionSplitSequencerBlock)
};
8 changes: 8 additions & 0 deletions src/audioCore/graph/SeqSourceProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ const SourceList::SeqBlock SeqSourceProcessor::getSeq(int index) const {
return this->srcs.get(index);
}

bool SeqSourceProcessor::splitSeq(int index, double time) {
return this->srcs.split(index, time);
}

bool SeqSourceProcessor::stickSeqWithNext(int index) {
return this->srcs.stickWithNext(index);
}

void SeqSourceProcessor::setTrackName(const juce::String& name) {
this->trackName = name;

Expand Down
2 changes: 2 additions & 0 deletions src/audioCore/graph/SeqSourceProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SeqSourceProcessor final : public juce::AudioProcessorGraph,
void removeSeq(int index);
int getSeqNum() const;
const SourceList::SeqBlock getSeq(int index) const;
bool splitSeq(int index, double time);
bool stickSeqWithNext(int index);

void setTrackName(const juce::String& name);
const juce::String getTrackName() const;
Expand Down
50 changes: 50 additions & 0 deletions src/audioCore/graph/SourceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,56 @@ void SourceList::remove(int index) {
}
}

bool SourceList::split(int index, double time) {
juce::ScopedWriteLock locker(audioLock::getAudioLock());

if (index >= 0 && index < this->list.size()) {
auto [startTime, endTime, offset] = this->list.getUnchecked(index);
if (startTime < time && time < endTime) {
this->list.remove(index);

this->list.insert(index, { startTime, time, offset });
this->list.insert(index + 1, { time, endTime, offset });

/** Callback */
UICallbackAPI<int, int>::invoke(
UICallbackType::SeqBlockChanged, this->index, index);
UICallbackAPI<int, int>::invoke(
UICallbackType::SeqBlockChanged, this->index, index + 1);

return true;
}
}

return false;
}

bool SourceList::stickWithNext(int index) {
juce::ScopedWriteLock locker(audioLock::getAudioLock());

if (index >= 0 && index < this->list.size() - 1) {
auto [startTimeFirst, endTimeFirst, offsetFirst] = this->list.getUnchecked(index);
auto [startTimeSecond, endTimeSecond, offsetSecond] = this->list.getUnchecked(index + 1);
if (juce::approximatelyEqual(offsetFirst, offsetSecond) &&
juce::approximatelyEqual(endTimeFirst, startTimeSecond)) {
this->list.remove(index + 1);
this->list.remove(index);

this->list.insert(index, { startTimeFirst, endTimeSecond, offsetFirst });

/** Callback */
UICallbackAPI<int, int>::invoke(
UICallbackType::SeqBlockChanged, this->index, index);
UICallbackAPI<int, int>::invoke(
UICallbackType::SeqBlockChanged, this->index, index + 1);

return true;
}
}

return false;
}

void SourceList::clearGraph() {
juce::ScopedWriteLock locker(audioLock::getAudioLock());
this->list.clear();
Expand Down
3 changes: 3 additions & 0 deletions src/audioCore/graph/SourceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class SourceList final : public Serializable {
int add(const SeqBlock& block);
void remove(int index);

bool split(int index, double time);
bool stickWithNext(int index);

void clearGraph();

public:
Expand Down
1 change: 1 addition & 0 deletions src/audioCore/recovery/ActionType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum class ActionType : unsigned int {
ActionAddTempoBeat,

ActionSave = 0x0101,
ActionSplitSequencerBlock,

ActionRemoveMixerTrack = 0x0201,
ActionRemoveMixerTrackSend,
Expand Down

0 comments on commit e302cda

Please sign in to comment.