| 465 | #define ACT(v) (v.keydown ? v.midi_note : -1) |
| 466 | |
| 467 | int DexedAudioProcessor::chooseNote(uint8_t pitch) { |
| 468 | // order of preference: |
| 469 | // 1. a note that is not playing |
| 470 | // 2. a note with its key up, playing the same pitch |
| 471 | // 3. a note with its key up, playing a different pitch |
| 472 | // 4. a note with its key down, playing the same pitch |
| 473 | // 5. a note with its key down, playing a different pitch |
| 474 | // break ties by preferring note with least recent keydown |
| 475 | int bestNote = currentNote; |
| 476 | int bestScore = -1; |
| 477 | int note = currentNote; |
| 478 | for (int i=0; i<MAX_ACTIVE_NOTES; i++) { |
| 479 | int score = 0; |
| 480 | if ( !voices[note].dx7_note->isPlaying() ) score += 4; |
| 481 | if ( !voices[note].keydown ) score += 2; |
| 482 | if ( voices[note].midi_note == pitch ) score += 1; |
| 483 | if ( (score > bestScore) || (score == bestScore && voices[note].keydown_seq < voices[bestNote].keydown_seq) ) { |
| 484 | bestNote = note; |
| 485 | bestScore = score; |
| 486 | } |
| 487 | note = (note + 1) % MAX_ACTIVE_NOTES; |
| 488 | } |
| 489 | return bestNote; |
| 490 | } |
| 491 | |
| 492 | void DexedAudioProcessor::keydown(uint8_t channel, uint8_t pitch, uint8_t velo) { |
| 493 | if ( velo == 0 ) { |