| 33 | } |
| 34 | |
| 35 | stChannelID CNoteChannelQueue::Trigger(int Note, stChannelID Channel) |
| 36 | { |
| 37 | const auto AddNote = [&] (int Index) -> stChannelID { |
| 38 | m_iCurrentNote[Index] = Note; |
| 39 | m_iNoteState[Note] = note_state_t::HOLD; |
| 40 | int p = 0; |
| 41 | for (const auto &x : m_iNotePriority) |
| 42 | if (x.second > p) |
| 43 | p = x.second; |
| 44 | m_iNotePriority[Note] = p + 1; |
| 45 | m_iNoteChannel[Note] = Channel; |
| 46 | return m_iChannelMapID[Index]; |
| 47 | }; |
| 48 | |
| 49 | if (!m_iNoteState.count(Note)) { |
| 50 | int Pos = -1; |
| 51 | for (int i = 0; i < m_iChannelCount; ++i) |
| 52 | if (m_iChannelMapID[i] == Channel) { Pos = i; break; } |
| 53 | for (int i = 0; i < m_iChannelCount; ++i) { |
| 54 | if (!m_bChannelMute[Pos] && m_iCurrentNote[Pos] == (unsigned)-1) |
| 55 | return AddNote(Pos); |
| 56 | if (++Pos >= m_iChannelCount) Pos = 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!m_iNoteState.count(Note)) { |
| 61 | int p = 0x7FFFFFFF; |
| 62 | int c = -1; |
| 63 | for (int i = 0; i < m_iChannelCount; ++i) { |
| 64 | if (m_bChannelMute[i]) |
| 65 | continue; |
| 66 | int n = m_iCurrentNote[i]; |
| 67 | if (n && m_iNoteState[n] == note_state_t::RELEASE) { |
| 68 | int pnew = m_iNotePriority[n]; |
| 69 | if (pnew < p) { |
| 70 | p = pnew; c = i; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | if (c == -1) for (int i = 0; i < m_iChannelCount; ++i) { |
| 75 | if (m_bChannelMute[i]) |
| 76 | continue; |
| 77 | int n = m_iCurrentNote[i]; |
| 78 | if (n && m_iNoteState[n] == note_state_t::HOLD) { |
| 79 | int pnew = m_iNotePriority[n]; |
| 80 | if (pnew < p) { |
| 81 | p = pnew; c = i; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | if (c != -1) { |
| 86 | Cut(m_iCurrentNote[c], { }); |
| 87 | return AddNote(c); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | for (int i = 0; i < m_iChannelCount; ++i) |
| 92 | if (m_iCurrentNote[i] == (unsigned)Note && m_iNoteChannel[m_iCurrentNote[i]] == Channel) |