Skip to content

Commit

Permalink
Add change seq track MIDI track action
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed May 30, 2024
1 parent dd06d11 commit 2e6a669
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/translates/zh-CN/alert.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ countries: cn
"Remove Track" = "移除轨道"
"Remove Label" = "移除标签"
"Remove Instrument" = "移除乐器"
"MIDI Track Selector" = "MIDI 轨道选择器"

"Discard unsaved changes and exit?" = "放弃未保存的更改并退出?"
"Discard unsaved changes and continue?" = "放弃未保存的更改并继续?"
Expand Down Expand Up @@ -52,4 +53,5 @@ countries: cn
"Remove the track from mixer. Continue?" = "从混音器中移除轨道。继续?"
"Remove the label from time line. Continue?" = "从时间线中移除标签。继续?"
"Remove the instrument from sequencer track. Continue?" = "从音序器轨道移除乐器。继续?"
"Remove the track from sequencer. Continue?" = "从音序器移除轨道。继续?"
"Remove the track from sequencer. Continue?" = "从音序器移除轨道。继续?"
"Select a MIDI track in the list:" = "从列表中选择一个 MIDI 轨道:"
1 change: 1 addition & 0 deletions app/translates/zh-CN/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ countries: cn
"Bypass Instrument" = "旁路乐器"
"Instrument Offline" = "乐器离线"
"Offline" = "离线"
"Select MIDI Track" = "选择 MIDI 轨道"

"Type:" = "类型:"
"Instrument:" = "乐器:"
Expand Down
41 changes: 41 additions & 0 deletions src/audioCore/action/ActionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2056,3 +2056,44 @@ bool ActionSetEffect::undo() {
}
ACTION_RESULT(false);
}

ActionSetSequencerMIDITrack::ActionSetSequencerMIDITrack(
int track, int midiTrack)
: ACTION_DB{ track, midiTrack } {}

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

ACTION_WRITE_TYPE(ActionSetSequencerMIDITrack);
ACTION_WRITE_DB();

if (auto graph = AudioCore::getInstance()->getGraph()) {
if (auto track = graph->getSourceProcessor(ACTION_DATA(track))) {
ACTION_DATA(oldMIDITrack) = track->getCurrentMIDITrack();
track->setCurrentMIDITrack(ACTION_DATA(midiTrack));

this->output("Set seq MIDI track: [" + juce::String(ACTION_DATA(track)) + "] " + juce::String{ ACTION_DATA(midiTrack) } + "\n");
ACTION_RESULT(true);
}
}
this->output("Can't set seq MIDI track: [" + juce::String(ACTION_DATA(track)) + "] " + juce::String{ ACTION_DATA(midiTrack) } + "\n");
ACTION_RESULT(false);
}

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

ACTION_WRITE_TYPE_UNDO(ActionSetSequencerMIDITrack);
ACTION_WRITE_DB();

if (auto graph = AudioCore::getInstance()->getGraph()) {
if (auto track = graph->getSourceProcessor(ACTION_DATA(track))) {
track->setCurrentMIDITrack(ACTION_DATA(oldMIDITrack));

this->output("Undo set seq MIDI track: [" + juce::String(ACTION_DATA(track)) + "] " + juce::String{ ACTION_DATA(midiTrack) } + "\n");
ACTION_RESULT(true);
}
}
this->output("Can't undo set seq MIDI track: [" + juce::String(ACTION_DATA(track)) + "] " + juce::String{ ACTION_DATA(midiTrack) } + "\n");
ACTION_RESULT(false);
}
23 changes: 23 additions & 0 deletions src/audioCore/action/ActionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1103,3 +1103,26 @@ class ActionSetEffect final : public ActionUndoableBase {

JUCE_LEAK_DETECTOR(ActionSetEffect)
};

class ActionSetSequencerMIDITrack final : public ActionUndoableBase {
public:
ActionSetSequencerMIDITrack() = delete;
ActionSetSequencerMIDITrack(
int track, int midiTrack);

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

private:
ACTION_DATABLOCK{
const int track;
const int midiTrack;

int oldMIDITrack = 0;
} ACTION_DB;

JUCE_LEAK_DETECTOR(ActionSetSequencerMIDITrack)
};
7 changes: 7 additions & 0 deletions src/audioCore/graph/SeqSourceProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,13 @@ int SeqSourceProcessor::getCurrentMIDITrack() const {
return this->currentMIDITrack;
}

int SeqSourceProcessor::getTotalMIDITrackNum() const {
if (this->midiData) {
return this->midiData->getNumTracks();
}
return 0;
}

void SeqSourceProcessor::setRecording(bool recording) {
if (recording) {
this->prepareRecord();
Expand Down
1 change: 1 addition & 0 deletions src/audioCore/graph/SeqSourceProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class SeqSourceProcessor final : public juce::AudioProcessorGraph,
bool isMIDIValid() const;
void setCurrentMIDITrack(int trackIndex);
int getCurrentMIDITrack() const;
int getTotalMIDITrackNum() const;

void setRecording(bool recording);
bool getRecording() const;
Expand Down
18 changes: 18 additions & 0 deletions src/audioCore/quickAPI/QuickGet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,24 @@ namespace quickAPI {
return {};
}

int getSeqTrackMIDITrackNum(int index) {
if (auto graph = AudioCore::getInstance()->getGraph()) {
if (auto track = graph->getSourceProcessor(index)) {
return track->getTotalMIDITrackNum();
}
}
return 0;
}

int getSeqTrackCurrentMIDITrack(int index) {
if (auto graph = AudioCore::getInstance()->getGraph()) {
if (auto track = graph->getSourceProcessor(index)) {
return track->getCurrentMIDITrack();
}
}
return 0;
}

int getMixerTrackNum() {
if (auto graph = AudioCore::getInstance()->getGraph()) {
return graph->getTrackNum();
Expand Down
2 changes: 2 additions & 0 deletions src/audioCore/quickAPI/QuickGet.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ namespace quickAPI {
const juce::String getSeqTrackDataRefMIDI(int index);
const std::tuple<double, juce::AudioSampleBuffer> getSeqTrackAudioData(int index);
const juce::MidiMessageSequence getSeqTrackMIDIData(int index);
int getSeqTrackMIDITrackNum(int index);
int getSeqTrackCurrentMIDITrack(int index);

int getMixerTrackNum();
const juce::String getMixerTrackName(int index);
Expand Down
3 changes: 2 additions & 1 deletion src/audioCore/recovery/ActionType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ enum class ActionType : unsigned int {
ActionSetSequencerTrackName,
ActionSetSequencerTrackColor,
ActionSetSequencerTrackMute,
ActionSetEffect
ActionSetEffect,
ActionSetSequencerMIDITrack
};
7 changes: 6 additions & 1 deletion src/ui/component/sequencer/SeqTrackComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void SeqTrackComponent::instrMenuShow() {
}

enum SeqMenuActionType {
Add = 1, Remove1
Add = 1, Remove1, MIDITrack
};

void SeqTrackComponent::menuShow() {
Expand All @@ -520,6 +520,10 @@ void SeqTrackComponent::menuShow() {
this->remove();
break;
}
case SeqMenuActionType::MIDITrack: {
CoreActions::setSeqMIDITrackGUI(this->index);
break;
}
}
}

Expand Down Expand Up @@ -578,6 +582,7 @@ juce::PopupMenu SeqTrackComponent::createMenu() const {

menu.addItem(SeqMenuActionType::Add, TRANS("Add"));
menu.addItem(SeqMenuActionType::Remove1, TRANS("Remove"));
menu.addItem(SeqMenuActionType::MIDITrack, TRANS("Select MIDI Track"));

return menu;
}
Expand Down
51 changes: 51 additions & 0 deletions src/ui/misc/CoreActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ void CoreActions::setSeqRec(int index, bool rec) {
ActionDispatcher::getInstance()->dispatch(std::move(action));
}

void CoreActions::setSeqMIDITrack(int index, int midiTrack) {
auto action = std::unique_ptr<ActionBase>(new ActionSetSequencerMIDITrack{ index, midiTrack });
ActionDispatcher::getInstance()->dispatch(std::move(action));
}

void CoreActions::removeSeq(int index) {
auto action = std::unique_ptr<ActionBase>(new ActionRemoveSequencerTrack{ index });
ActionDispatcher::getInstance()->dispatch(std::move(action));
Expand Down Expand Up @@ -916,6 +921,20 @@ void CoreActions::setSeqAudioOutputToMixerGUI(int index, int mixerIndex, bool ou
trackName, mixerName, true);
}

void CoreActions::setSeqMIDITrackGUI(int index) {
/** Callback */
auto callback = [index](int midiTrack) {
CoreActions::setSeqMIDITrack(index, midiTrack);
};

/** Get Total Track */
int totalMIDITrack = quickAPI::getSeqTrackMIDITrackNum(index);
int currentMIDITrack = quickAPI::getSeqTrackCurrentMIDITrack(index);

/** Ask For MIDI Track */
CoreActions::askForMIDITrackAsync(callback, totalMIDITrack, currentMIDITrack);
}

void CoreActions::removeSeqGUI(int index) {
if (index <= -1) { return; }

Expand Down Expand Up @@ -1423,3 +1442,35 @@ void CoreActions::askForTempoGUIAsync(
callback(isTempo, tempo, numerator, denominator);
}), true);
}

void CoreActions::askForMIDITrackAsync(
const std::function<void(int)>& callback,
int totalNum, int defaltTrack) {
/** Get Index List */
juce::StringArray indexItemList;
for (int i = 0; i < totalNum; i++) {
indexItemList.add(juce::String{ i });
}

/** Create Selector */
auto selectorWindow = new juce::AlertWindow{
TRANS("MIDI Track Selector"), TRANS("Select a MIDI track in the list:"),
juce::MessageBoxIconType::QuestionIcon };
selectorWindow->addButton(TRANS("OK"), 1);
selectorWindow->addButton(TRANS("Cancel"), 0);
selectorWindow->addComboBox(TRANS("Track"), indexItemList);

/** Show Selector Async */
auto combo = selectorWindow->getComboBoxComponent(TRANS("Track"));
combo->setSelectedItemIndex(defaltTrack);
selectorWindow->enterModalState(true, juce::ModalCallbackFunction::create(
[combo, callback, totalNum](int result) {
if (result != 1) { return; }

int index = combo->getSelectedItemIndex();
if (index < 0 || index >= totalNum) { return; }

callback(index);
}
), true);
}
5 changes: 5 additions & 0 deletions src/ui/misc/CoreActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class CoreActions final {
static void setSeqSolo(int index);
static void setSeqMuteAll(bool mute);
static void setSeqRec(int index, bool rec);
static void setSeqMIDITrack(int index, int midiTrack);
static void removeSeq(int index);

static void addTempoLabel(double time, double tempo);
Expand Down Expand Up @@ -142,6 +143,7 @@ class CoreActions final {
static void setSeqNameGUI(int index);
static void setSeqAudioOutputToMixerGUI(int index, int mixerIndex, bool output,
const juce::Array<std::tuple<int, int>>& links);
static void setSeqMIDITrackGUI(int index);
static void removeSeqGUI(int index);

static void addLabelGUI(double time);
Expand Down Expand Up @@ -184,4 +186,7 @@ class CoreActions final {
bool defaultIsTempo = true,
double defaultTempo = 120.0, int defaultNumerator = 4, int defaultDenominator = 4,
bool switchable = true);
static void askForMIDITrackAsync(
const std::function<void(int)>& callback,
int totalNum, int defaltTrack = 0);
};

0 comments on commit 2e6a669

Please sign in to comment.