Skip to content

Commit f82f23e

Browse files
Update MIDI note add method
1 parent 06b2eb8 commit f82f23e

File tree

8 files changed

+192
-45
lines changed

8 files changed

+192
-45
lines changed

src/audioCore/source/SourceInternalContainer.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ bool SourceInternalContainer::isForked() const {
238238
}
239239

240240
int SourceInternalContainer::addNote(
241-
int track, double startTime, double endTime,
241+
int track, double startTime, double endTime, uint8_t channel,
242242
uint8_t pitch, uint8_t vel, const juce::String& lyrics) {
243243
/** Check Type */
244244
if (this->type != SourceType::MIDI) { return -1; }
@@ -250,7 +250,7 @@ int SourceInternalContainer::addNote(
250250

251251
/** Add Note */
252252
int result = this->midiData->addNote(
253-
track, startTime, endTime,
253+
track, startTime, endTime, channel,
254254
pitch, vel, lyrics);
255255

256256
/** Set Flag */
@@ -284,10 +284,32 @@ int SourceInternalContainer::setNoteTime(
284284
return result;
285285
}
286286

287+
bool SourceInternalContainer::setNoteChannel(
288+
int track, int index, uint8_t channel) {
289+
/** Check Type */
290+
if (this->type != SourceType::MIDI) { return false; }
291+
292+
/** Init MIDI */
293+
if (!this->midiData) {
294+
this->initMidiData();
295+
}
296+
297+
/** Set Note Pitch */
298+
bool result = this->midiData->setNoteChannel(
299+
track, index, channel);
300+
301+
/** Set Flag */
302+
if (result) {
303+
this->changed();
304+
}
305+
306+
return result;
307+
}
308+
287309
bool SourceInternalContainer::setNotePitch(
288310
int track, int index, uint8_t pitch) {
289311
/** Check Type */
290-
if (this->type != SourceType::MIDI) { return -1; }
312+
if (this->type != SourceType::MIDI) { return false; }
291313

292314
/** Init MIDI */
293315
if (!this->midiData) {
@@ -309,7 +331,7 @@ bool SourceInternalContainer::setNotePitch(
309331
bool SourceInternalContainer::setNoteVelocity(
310332
int track, int index, uint8_t vel) {
311333
/** Check Type */
312-
if (this->type != SourceType::MIDI) { return -1; }
334+
if (this->type != SourceType::MIDI) { return false; }
313335

314336
/** Init MIDI */
315337
if (!this->midiData) {
@@ -331,7 +353,7 @@ bool SourceInternalContainer::setNoteVelocity(
331353
bool SourceInternalContainer::setNoteLyrics(
332354
int track, int index, const juce::String& lyrics) {
333355
/** Check Type */
334-
if (this->type != SourceType::MIDI) { return -1; }
356+
if (this->type != SourceType::MIDI) { return false; }
335357

336358
/** Init MIDI */
337359
if (!this->midiData) {
@@ -353,7 +375,7 @@ bool SourceInternalContainer::setNoteLyrics(
353375
bool SourceInternalContainer::removeNote(
354376
int track, int index) {
355377
/** Check Type */
356-
if (this->type != SourceType::MIDI) { return -1; }
378+
if (this->type != SourceType::MIDI) { return false; }
357379

358380
/** Init MIDI */
359381
if (!this->midiData) {

src/audioCore/source/SourceInternalContainer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ class SourceInternalContainer {
5151
bool isForked() const;
5252

5353
public:
54-
int addNote(int track, double startTime, double endTime,
54+
int addNote(int track, double startTime, double endTime, uint8_t channel,
5555
uint8_t pitch, uint8_t vel, const juce::String& lyrics = "");
5656
int setNoteTime(int track, int index,
5757
double startTime, double endTime);
58+
bool setNoteChannel(int track, int index, uint8_t channel);
5859
bool setNotePitch(int track, int index, uint8_t pitch);
5960
bool setNoteVelocity(int track, int index, uint8_t vel);
6061
bool setNoteLyrics(int track, int index, const juce::String& lyrics);

src/audioCore/source/SourceItem.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,12 @@ const SourceMIDITemp::Misc SourceItem::getMIDIMisc(int track, int index) const {
446446
return this->container->getMIDIMisc(track, index);
447447
}
448448

449-
int SourceItem::addNote(int track, double startTime, double endTime,
449+
int SourceItem::addNote(int track, double startTime, double endTime, uint8_t channel,
450450
uint8_t pitch, uint8_t vel, const juce::String& lyrics) {
451451
if (!this->container) { return -1; }
452452

453453
int result = this->container->addNote(
454-
track, startTime, endTime,
454+
track, startTime, endTime, channel,
455455
pitch, vel, lyrics);
456456

457457
if (result >= 0) {
@@ -473,8 +473,20 @@ int SourceItem::setNoteTime(int track, int index,
473473
return result;
474474
}
475475

476+
bool SourceItem::setNoteChannel(int track, int index, uint8_t channel) {
477+
if (!this->container) { return false; }
478+
479+
bool result = this->container->setNoteChannel(
480+
track, index, channel);
481+
482+
if (result) {
483+
this->invokeCallback();
484+
}
485+
return result;
486+
}
487+
476488
bool SourceItem::setNotePitch(int track, int index, uint8_t pitch) {
477-
if (!this->container) { return -1; }
489+
if (!this->container) { return false; }
478490

479491
bool result = this->container->setNotePitch(
480492
track, index, pitch);
@@ -486,7 +498,7 @@ bool SourceItem::setNotePitch(int track, int index, uint8_t pitch) {
486498
}
487499

488500
bool SourceItem::setNoteVelocity(int track, int index, uint8_t vel) {
489-
if (!this->container) { return -1; }
501+
if (!this->container) { return false; }
490502

491503
bool result = this->container->setNoteVelocity(
492504
track, index, vel);
@@ -498,7 +510,7 @@ bool SourceItem::setNoteVelocity(int track, int index, uint8_t vel) {
498510
}
499511

500512
bool SourceItem::setNoteLyrics(int track, int index, const juce::String& lyrics) {
501-
if (!this->container) { return -1; }
513+
if (!this->container) { return false; }
502514

503515
bool result = this->container->setNoteLyrics(
504516
track, index, lyrics);
@@ -510,7 +522,7 @@ bool SourceItem::setNoteLyrics(int track, int index, const juce::String& lyrics)
510522
}
511523

512524
bool SourceItem::removeNote(int track, int index) {
513-
if (!this->container) { return -1; }
525+
if (!this->container) { return false; }
514526

515527
bool result = this->container->removeNote(
516528
track, index);

src/audioCore/source/SourceItem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ class SourceItem final {
8282
const SourceMIDITemp::Misc getMIDIMisc(int track, int index) const;
8383

8484
public:
85-
int addNote(int track, double startTime, double endTime,
85+
int addNote(int track, double startTime, double endTime, uint8_t channel,
8686
uint8_t pitch, uint8_t vel, const juce::String& lyrics = "");
8787
int setNoteTime(int track, int index,
8888
double startTime, double endTime);
89+
bool setNoteChannel(int track, int index, uint8_t channel);
8990
bool setNotePitch(int track, int index, uint8_t pitch);
9091
bool setNoteVelocity(int track, int index, uint8_t vel);
9192
bool setNoteLyrics(int track, int index, const juce::String& lyrics);

src/audioCore/source/SourceMIDITemp.cpp

Lines changed: 126 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,34 @@ void SourceMIDITemp::removeEvents(int track, double startTime, double timeLength
8181

8282
/** For Each Note */
8383
for (int i = 0; i < eventList.size(); i++) {
84-
auto ptr = eventList.getUnchecked(i);
85-
86-
/** Note On Item */
87-
if (auto ptrNote = dynamic_cast<Note*>(ptr)) {
88-
noteOnObjectTemp.push({ ptrNote->endSec, i });
89-
}
90-
91-
/** Insert Note Off Marker */
92-
if (i < eventList.size() - 1) {
93-
auto ptrNext = eventList.getUnchecked(i + 1);
94-
if (!noteOnObjectTemp.empty()) {
95-
auto& [firstNoteEndTime, firstNoteIndex] = noteOnObjectTemp.top();
96-
if (ptr->timeSec <= firstNoteEndTime && ptrNext->timeSec > firstNoteEndTime) {
97-
if (auto ptrNote = dynamic_cast<Note*>(eventList.getUnchecked(firstNoteIndex))) {
98-
auto noteOff = std::make_unique<NoteOffMarker>();
99-
noteOff->channel = ptrNote->channel;
100-
noteOff->timeSec = firstNoteEndTime;
101-
102-
noteOff->eventOnIndex = firstNoteIndex;
103-
ptrNote->eventOffIndex = i + 1;
104-
105-
noteOff->eventIndex = ptrNote->eventOffIndex;
106-
noteOff->eventInListIndex = -1;
84+
if (auto ptr = eventList.getUnchecked(i)) {
85+
/** Note On Item */
86+
if (auto ptrNote = dynamic_cast<Note*>(ptr)) {
87+
noteOnObjectTemp.push({ ptrNote->endSec, i });
88+
}
10789

108-
eventList.insert(ptrNote->eventOffIndex, std::move(noteOff));
90+
/** Insert Note Off Marker */
91+
if (i < eventList.size() - 1) {
92+
if (auto ptrNext = eventList.getUnchecked(i + 1)) {
93+
if (!noteOnObjectTemp.empty()) {
94+
auto& [firstNoteEndTime, firstNoteIndex] = noteOnObjectTemp.top();
95+
if (ptr->timeSec <= firstNoteEndTime && ptrNext->timeSec > firstNoteEndTime) {
96+
if (auto ptrNote = dynamic_cast<Note*>(eventList.getUnchecked(firstNoteIndex))) {
97+
auto noteOff = std::make_unique<NoteOffMarker>();
98+
noteOff->channel = ptrNote->channel;
99+
noteOff->timeSec = firstNoteEndTime;
100+
101+
noteOff->eventOnIndex = firstNoteIndex;
102+
ptrNote->eventOffIndex = i + 1;
103+
104+
noteOff->eventIndex = ptrNote->eventOffIndex;
105+
noteOff->eventInListIndex = -1;
106+
107+
eventList.insert(ptrNote->eventOffIndex, std::move(noteOff));
108+
}
109+
noteOnObjectTemp.pop();
110+
}
109111
}
110-
noteOnObjectTemp.pop();
111112
}
112113
}
113114
}
@@ -547,9 +548,102 @@ void SourceMIDITemp::addMIDIMessages(
547548
this->updateIndexs(track);
548549
}
549550

550-
int SourceMIDITemp::addNote(int track, double startTime, double endTime,
551+
int SourceMIDITemp::addNote(int track, double startTime, double endTime, uint8_t channel,
551552
uint8_t pitch, uint8_t vel, const juce::String& lyrics) {
552-
/** TODO */
553+
/** Limit Track Index */
554+
if (track < 0 || track >= this->eventList.size()) { return -1; }
555+
556+
/** Limit Time */
557+
if (endTime <= startTime) { return -1; }
558+
559+
/** Get Insert Index */
560+
auto& list = this->eventList.getReference(track);
561+
int startIndex = SourceMIDITemp::linearSearchInsert(list, 0, startTime);
562+
563+
/** Create Note Start Event */
564+
auto note = std::make_unique<Note>();
565+
note->channel = channel;
566+
note->timeSec = startTime;
567+
note->endSec = endTime;
568+
note->pitch = pitch;
569+
note->vel = vel;
570+
note->lyrics = lyrics;
571+
572+
/** Insert Into Event List */
573+
note->eventIndex = startIndex;
574+
list.insert(startIndex, std::move(note));
575+
576+
/** Update Event Index In Event List */
577+
for (int i = 0; i < list.size(); i++) {
578+
if (i != startIndex) {
579+
auto ptr = list.getUnchecked(i);
580+
if (ptr->eventIndex >= startIndex) {
581+
ptr->eventIndex++;
582+
}
583+
584+
if (auto pNote = dynamic_cast<Note*>(ptr)) {
585+
if (pNote->eventOffIndex >= startIndex) {
586+
pNote->eventOffIndex++;
587+
}
588+
}
589+
590+
else if (auto pNoteOff = dynamic_cast<NoteOffMarker*>(ptr)) {
591+
if (pNoteOff->eventOnIndex >= startIndex) {
592+
pNoteOff->eventOnIndex++;
593+
}
594+
}
595+
}
596+
}
597+
598+
/** Get Note Off Insert Index */
599+
int endIndex = SourceMIDITemp::linearSearchInsert(
600+
list, startIndex, endTime);
601+
602+
/** Create Note End Event */
603+
auto noteOff = std::make_unique<NoteOffMarker>();
604+
noteOff->channel = channel;
605+
noteOff->timeSec = endTime;
606+
noteOff->eventOnIndex = startIndex;
607+
608+
/** Insert Note End Into Event List */
609+
noteOff->eventIndex = endIndex;
610+
list.insert(endIndex, std::move(noteOff));
611+
612+
/** Update Note On */
613+
if (auto pNote = dynamic_cast<Note*>(list[startIndex])) {
614+
pNote->eventOffIndex = endIndex;
615+
}
616+
617+
/** Update Event Index In Event List */
618+
for (int i = 0; i < list.size(); i++) {
619+
if (i != endIndex) {
620+
auto ptr = list.getUnchecked(i);
621+
if (ptr->eventIndex >= endIndex) {
622+
ptr->eventIndex++;
623+
}
624+
625+
if (auto pNote = dynamic_cast<Note*>(ptr)) {
626+
if (pNote->eventIndex != startIndex &&
627+
pNote->eventOffIndex >= endIndex) {
628+
pNote->eventOffIndex++;
629+
}
630+
}
631+
632+
else if (auto pNoteOff = dynamic_cast<NoteOffMarker*>(ptr)) {
633+
if (pNoteOff->eventOnIndex >= endIndex) {
634+
pNoteOff->eventOnIndex++;
635+
}
636+
}
637+
}
638+
}
639+
640+
/** Rebuild Index Temp */
641+
this->updateIndexs(track);
642+
643+
/** Return Note Index */
644+
if (auto pNote = dynamic_cast<Note*>(list[startIndex])) {
645+
return pNote->eventInListIndex;
646+
}
553647
return -1;
554648
}
555649

@@ -559,6 +653,11 @@ int SourceMIDITemp::setNoteTime(int track, int index,
559653
return -1;
560654
}
561655

656+
bool SourceMIDITemp::setNoteChannel(int track, int index, uint8_t channel) {
657+
/** TODO */
658+
return -1;
659+
}
660+
562661
bool SourceMIDITemp::setNotePitch(int track, int index, uint8_t pitch) {
563662
/** TODO */
564663
return false;

src/audioCore/source/SourceMIDITemp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ class SourceMIDITemp final {
8181
NoteOnTemp& noteOnTemp, int& indexTemp, LyricsItem& lyricsTemp);
8282

8383
public:
84-
int addNote(int track, double startTime, double endTime,
84+
int addNote(int track, double startTime, double endTime, uint8_t channel,
8585
uint8_t pitch, uint8_t vel, const juce::String& lyrics = "");
8686
int setNoteTime(int track, int index,
8787
double startTime, double endTime);
88+
bool setNoteChannel(int track, int index, uint8_t channel);
8889
bool setNotePitch(int track, int index, uint8_t pitch);
8990
bool setNoteVelocity(int track, int index, uint8_t vel);
9091
bool setNoteLyrics(int track, int index, const juce::String& lyrics);

src/audioCore/source/SourceManager.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,13 @@ const juce::Array<SourceMIDITemp::Misc> SourceManager::getMIDIMiscList(uint64_t
464464
return {};
465465
}
466466

467-
int SourceManager::addNote(uint64_t ref, int track, double startTime, double endTime,
467+
int SourceManager::addNote(uint64_t ref,
468+
int track, double startTime, double endTime, uint8_t channel,
468469
uint8_t pitch, uint8_t vel, const juce::String& lyrics) {
469470
juce::ScopedReadLock locker(audioLock::getSourceLock());
470471
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
471472
return ptr->addNote(
472-
track, startTime, endTime, pitch, vel, lyrics);
473+
track, startTime, endTime, channel, pitch, vel, lyrics);
473474
}
474475
return -1;
475476
}
@@ -484,6 +485,15 @@ int SourceManager::setNoteTime(uint64_t ref, int track, int index,
484485
return -1;
485486
}
486487

488+
bool SourceManager::setNoteChannel(uint64_t ref, int track, int index, uint8_t channel) {
489+
juce::ScopedReadLock locker(audioLock::getSourceLock());
490+
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
491+
return ptr->setNoteChannel(
492+
track, index, channel);
493+
}
494+
return false;
495+
}
496+
487497
bool SourceManager::setNotePitch(uint64_t ref, int track, int index, uint8_t pitch) {
488498
juce::ScopedReadLock locker(audioLock::getSourceLock());
489499
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {

0 commit comments

Comments
 (0)