andrewm@0: /*
andrewm@0: TouchKeys: multi-touch musical keyboard control software
andrewm@0: Copyright (c) 2013 Andrew McPherson
andrewm@0:
andrewm@0: This program is free software: you can redistribute it and/or modify
andrewm@0: it under the terms of the GNU General Public License as published by
andrewm@0: the Free Software Foundation, either version 3 of the License, or
andrewm@0: (at your option) any later version.
andrewm@0:
andrewm@0: This program is distributed in the hope that it will be useful,
andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0: GNU General Public License for more details.
andrewm@0:
andrewm@0: You should have received a copy of the GNU General Public License
andrewm@0: along with this program. If not, see .
andrewm@0:
andrewm@0: =====================================================================
andrewm@0:
andrewm@0: MIDIKeyPositionMapping.cpp: handles generating MIDI data out of continuous
andrewm@0: key position.
andrewm@0: */
andrewm@0:
andrewm@0: #include "MIDIKeyPositionMapping.h"
andrewm@0: #include "../TouchKeys/MidiOutputController.h"
andrewm@0:
andrewm@0: // Class constants
andrewm@0: const int MIDIKeyPositionMapping::kDefaultMIDIChannel = 0;
andrewm@0: const float MIDIKeyPositionMapping::kDefaultAftertouchScaler = 127.0 / 0.03; // Default aftertouch sensitivity: MIDI 127 = 0.03
andrewm@0: const float MIDIKeyPositionMapping::kMinimumAftertouchPosition = 0.99; // Position at which aftertouch messages start
andrewm@0: const float MIDIKeyPositionMapping::kDefaultPercussivenessScaler = 1.0 / 300.0; // Default scaler from percussiveness feature to MIDI
andrewm@0: const key_velocity MIDIKeyPositionMapping::kPianoKeyVelocityForMaxMIDI = scale_key_velocity(40.0); // Press velocity for MIDI 127
andrewm@0: const key_velocity MIDIKeyPositionMapping::kPianoKeyReleaseVelocityForMaxMIDI = scale_key_velocity(-50.0); // Release velocity for MIDI 127
andrewm@0:
andrewm@0:
andrewm@0: // Main constructor takes references/pointers from objects which keep track
andrewm@0: // of touch location, continuous key position and the state detected from that
andrewm@0: // position. The PianoKeyboard object is strictly required as it gives access to
andrewm@0: // Scheduler and OSC methods. The others are optional since any given system may
andrewm@0: // contain only one of continuous key position or touch sensitivity
andrewm@0: MIDIKeyPositionMapping::MIDIKeyPositionMapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node* touchBuffer,
andrewm@0: Node* positionBuffer, KeyPositionTracker* positionTracker)
andrewm@0: : Mapping(keyboard, factory, noteNumber, touchBuffer, positionBuffer, positionTracker), noteIsOn_(false),
andrewm@0: midiChannel_(kDefaultMIDIChannel), lastAftertouchValue_(0), midiPercussivenessChannel_(-1)
andrewm@0: {
andrewm@0: setAftertouchSensitivity(1.0);
andrewm@0: }
andrewm@0:
andrewm@0: // Copy constructor
andrewm@0: MIDIKeyPositionMapping::MIDIKeyPositionMapping(MIDIKeyPositionMapping const& obj)
andrewm@0: : Mapping(obj), noteIsOn_(obj.noteIsOn_), aftertouchScaler_(obj.aftertouchScaler_),
andrewm@0: midiChannel_(obj.midiChannel_), lastAftertouchValue_(obj.lastAftertouchValue_),
andrewm@0: midiPercussivenessChannel_(obj.midiPercussivenessChannel_)
andrewm@0: {
andrewm@0:
andrewm@0: }
andrewm@0:
andrewm@0: MIDIKeyPositionMapping::~MIDIKeyPositionMapping() {
andrewm@0: try {
andrewm@0: disengage();
andrewm@0: }
andrewm@0: catch(...) {
andrewm@0: std::cerr << "~MIDIKeyPositionMapping(): exception during disengage()\n";
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: // Turn off mapping of data. Remove our callback from the scheduler
andrewm@0: void MIDIKeyPositionMapping::disengage() {
andrewm@0: Mapping::disengage();
andrewm@0: if(noteIsOn_) {
andrewm@0: generateMidiNoteOff();
andrewm@0: }
andrewm@0: noteIsOn_ = false;
andrewm@0: }
andrewm@0:
andrewm@0: // Reset state back to defaults
andrewm@0: void MIDIKeyPositionMapping::reset() {
andrewm@0: Mapping::reset();
andrewm@0: noteIsOn_ = false;
andrewm@0: }
andrewm@0:
andrewm@0: // Set the aftertouch sensitivity on continuous key position
andrewm@0: // 0 means no aftertouch, 1 means default sensitivity, upward
andrewm@0: // from there
andrewm@0: void MIDIKeyPositionMapping::setAftertouchSensitivity(float sensitivity) {
andrewm@0: if(sensitivity <= 0)
andrewm@0: aftertouchScaler_ = 0;
andrewm@0: else
andrewm@0: aftertouchScaler_ = kDefaultAftertouchScaler * sensitivity;
andrewm@0: }
andrewm@0:
andrewm@0: // Trigger method. This receives updates from the TouchKey data or from state changes in
andrewm@0: // the continuous key position (KeyPositionTracker). It will potentially change the scheduled
andrewm@0: // behavior of future mapping calls, but the actual OSC messages should be transmitted in a different
andrewm@0: // thread.
andrewm@0: void MIDIKeyPositionMapping::triggerReceived(TriggerSource* who, timestamp_type timestamp) {
andrewm@0: if(who == 0)
andrewm@0: return;
andrewm@0: if(who == positionTracker_) {
andrewm@0: if(!positionTracker_->empty()) {
andrewm@0: KeyPositionTrackerNotification notification = positionTracker_->latest();
andrewm@0:
andrewm@0: // New message from the key position tracker. Might be time to start or end MIDI note.
andrewm@0: if(notification.type == KeyPositionTrackerNotification::kNotificationTypeFeatureAvailableVelocity && !noteIsOn_) {
andrewm@0: cout << "Key " << noteNumber_ << " velocity available\n";
andrewm@0: generateMidiNoteOn();
andrewm@0: noteIsOn_ = true;
andrewm@0: }
andrewm@0: else if(notification.type == KeyPositionTrackerNotification::kNotificationTypeFeatureAvailableReleaseVelocity && noteIsOn_) {
andrewm@0: cout << "Key " << noteNumber_ << " release velocity available\n";
andrewm@0: generateMidiNoteOff();
andrewm@0: noteIsOn_ = false;
andrewm@0: }
andrewm@0: else if(notification.type == KeyPositionTrackerNotification::kNotificationTypeFeatureAvailablePercussiveness) {
andrewm@0: cout << "Key " << noteNumber_ << " percussiveness available\n";
andrewm@0: generateMidiPercussivenessNoteOn();
andrewm@0: }
andrewm@0: }
andrewm@0: }
andrewm@0: else if(who == touchBuffer_) {
andrewm@0: // TODO: New touch data is available from the keyboard
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: // Mapping method. This actually does the real work of sending OSC data in response to the
andrewm@0: // latest information from the touch sensors or continuous key angle
andrewm@0: timestamp_type MIDIKeyPositionMapping::performMapping() {
andrewm@0: if(!engaged_)
andrewm@0: return 0;
andrewm@0:
andrewm@0: timestamp_type currentTimestamp = keyboard_.schedulerCurrentTimestamp();
andrewm@0:
andrewm@0: // Calculate the output features as a function of input sensor data
andrewm@0: if(positionBuffer_ == 0) {
andrewm@0: // No buffer -> all 0
andrewm@0: }
andrewm@0: else if(positionBuffer_->empty()) {
andrewm@0: // No samples -> all 0
andrewm@0: }
andrewm@0: else if(noteIsOn_) {
andrewm@0: // Generate aftertouch messages based on key position, if the note is on and
andrewm@0: // if the position exceeds the aftertouch threshold. Note on and note off are
andrewm@0: // handled directly by the trigger thread.
andrewm@0: key_position latestPosition = positionBuffer_->latest();
andrewm@0: int aftertouchValue;
andrewm@0:
andrewm@0: if(latestPosition < kMinimumAftertouchPosition)
andrewm@0: aftertouchValue = 0;
andrewm@0: else {
andrewm@0: aftertouchValue = (int)((key_position_to_float(latestPosition) - kMinimumAftertouchPosition) * aftertouchScaler_);
andrewm@0: if(aftertouchValue < 0)
andrewm@0: aftertouchValue = 0;
andrewm@0: if(aftertouchValue > 127)
andrewm@0: aftertouchValue = 127;
andrewm@0: }
andrewm@0:
andrewm@0: if(aftertouchValue != lastAftertouchValue_) {
andrewm@0: if(keyboard_.midiOutputController() != 0) {
andrewm@0: keyboard_.midiOutputController()->sendAftertouchPoly(0, midiChannel_, noteNumber_, aftertouchValue);
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: lastAftertouchValue_ = aftertouchValue;
andrewm@0: }
andrewm@0:
andrewm@0: // Register for the next update by returning its timestamp
andrewm@0: nextScheduledTimestamp_ = currentTimestamp + updateInterval_;
andrewm@0: return nextScheduledTimestamp_;
andrewm@0: }
andrewm@0:
andrewm@0: // Generate a MIDI Note On from continuous key data
andrewm@0: void MIDIKeyPositionMapping::generateMidiNoteOn() {
andrewm@0: if(positionTracker_ == 0)
andrewm@0: return;
andrewm@0:
andrewm@0: std::pair velocityInfo = positionTracker_->pressVelocity();
andrewm@0:
andrewm@0: // MIDI Velocity now available. Send a MIDI message if relevant.
andrewm@0: if(keyboard_.midiOutputController() != 0) {
andrewm@0: float midiVelocity = 0.5;
andrewm@0: if(!missing_value::isMissing(velocityInfo.second))
andrewm@0: midiVelocity = (float)velocityInfo.second / (float)kPianoKeyVelocityForMaxMIDI;
andrewm@0: if(midiVelocity < 0.0)
andrewm@0: midiVelocity = 0.0;
andrewm@0: if(midiVelocity > 1.0)
andrewm@0: midiVelocity = 1.0;
andrewm@0: keyboard_.midiOutputController()->sendNoteOn(0, midiChannel_, noteNumber_, (unsigned char)(midiVelocity * 127.0));
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: // Generate a MIDI Note Off from continuous key data
andrewm@0: void MIDIKeyPositionMapping::generateMidiNoteOff() {
andrewm@0: if(positionTracker_ == 0)
andrewm@0: return;
andrewm@0:
andrewm@0: std::pair velocityInfo = positionTracker_->releaseVelocity();
andrewm@0:
andrewm@0: // MIDI release velocity now available. Send a MIDI message if relevant
andrewm@0: if(keyboard_.midiOutputController() != 0) {
andrewm@0: float midiReleaseVelocity = 0.5;
andrewm@0: if(!missing_value::isMissing(velocityInfo.second))
andrewm@0: midiReleaseVelocity = (float)velocityInfo.second / (float)kPianoKeyReleaseVelocityForMaxMIDI;
andrewm@0: if(midiReleaseVelocity < 0.0)
andrewm@0: midiReleaseVelocity = 0.0;
andrewm@0: if(midiReleaseVelocity > 1.0)
andrewm@0: midiReleaseVelocity = 1.0;
andrewm@0: keyboard_.midiOutputController()->sendNoteOff(0, midiChannel_, noteNumber_, (unsigned char)(midiReleaseVelocity * 127.0));
andrewm@0:
andrewm@0: // Also turn off percussiveness note if enabled
andrewm@0: if(midiPercussivenessChannel_ >= 0)
andrewm@0: keyboard_.midiOutputController()->sendNoteOff(0, midiPercussivenessChannel_, noteNumber_, (unsigned char)(midiReleaseVelocity * 127.0));
andrewm@0: }
andrewm@0:
andrewm@0: lastAftertouchValue_ = 0;
andrewm@0: }
andrewm@0:
andrewm@0: void MIDIKeyPositionMapping::generateMidiPercussivenessNoteOn() {
andrewm@0: if(positionTracker_ == 0 || midiPercussivenessChannel_ < 0)
andrewm@0: return;
andrewm@0:
andrewm@0: KeyPositionTracker::PercussivenessFeatures features = positionTracker_->pressPercussiveness();
andrewm@0: std::cout << "found percussiveness value of " << features.percussiveness << std::endl;
andrewm@0:
andrewm@0: // MIDI Velocity now available. Send a MIDI message if relevant.
andrewm@0: if(keyboard_.midiOutputController() != 0) {
andrewm@0: float midiPercVelocity = 0.0;
andrewm@0: if(!missing_value::isMissing(features.percussiveness))
andrewm@0: midiPercVelocity = features.percussiveness * kDefaultPercussivenessScaler;
andrewm@0: if(midiPercVelocity < 0.0)
andrewm@0: midiPercVelocity = 0.0;
andrewm@0: if(midiPercVelocity > 1.0)
andrewm@0: midiPercVelocity = 1.0;
andrewm@0: keyboard_.midiOutputController()->sendNoteOn(0, midiPercussivenessChannel_, noteNumber_, (unsigned char)(midiPercVelocity * 127.0));
andrewm@0: }
andrewm@0: }