void PianoKeyBoard::createKeys() { keys_->removeAllObjects(); float x = 0; for (int i=lowNote_; i<=highNote_; ++i) { PianoKey *key = new PianoKey(); KEYTYPE type = getKeyType(i); Point pt = Point::ZERO ; Rect rect; if (isBlackKey(type)) { pt.x = x - whiteKeyWidth_ + getBlackKeyOffset(i) ; } else { pt.x = x ; x += whiteKeyWidth_ ; } if (isBlackKey(type)) { rect = Rect(pt.x+20, pt.y+17, blackKeyWidth_, blackKeyHeight_); } else { int offset = int(x) - (int(pt.x) + int(whiteKeyWidth_)) ; offset = (float)1.0/2; rect = Rect(pt.x+20, pt.y+17, whiteKeyWidth_+offset, whiteKeyHeight_); } key->note_ = i; key->type_ = type; key->rect_ = rect; key->position_ = pt; key->down_ = false; key->matching_ = false; keys_->setObject(key, itoa(i)); key->release(); } }
void MidiBlackKeyFilter::processMidiEvents (VstMidiEventVec *inputs, VstMidiEventVec *outputs, VstInt32 sampleFrames) { const int listenchannel = FLOAT_TO_CHANNEL(param[kInChannel]); int BlackChannel = FLOAT_TO_CHANNEL(param[kBlackChannel]); int WhiteChannel = FLOAT_TO_CHANNEL(param[kWhiteChannel]); // process incoming events for (unsigned int i=0;i<inputs[0].size();i++) { //copying event "i" from input (with all its fields) VstMidiEvent tomod = inputs[0][i]; unsigned int status = tomod.midiData[0] & 0xf0; // scraping channel const int channel = tomod.midiData[0] & 0x0f; // isolating channel const int data1 = tomod.midiData[1] & 0x7f; const int data2 = tomod.midiData[2] & 0x7f; // make zero-velocity noteons look like "real" noteoffs if (status==MIDI_NOTEON && data2==0) status=MIDI_NOTEOFF; if (channel == listenchannel || listenchannel == -1) { if (status == MIDI_NOTEON) { if (isBlackKey(data1)) { if (BlackChannel==ANY_CHANNEL) BlackChannel = channel; tomod.midiData[0] = MIDI_NOTEON | BlackChannel; tomod.midiData[2] = midiLimit(roundToInt((float)data2 * 2.f * param[kBlackVelocity])); } else { if (WhiteChannel==ANY_CHANNEL) WhiteChannel = channel; tomod.midiData[0] = MIDI_NOTEON | WhiteChannel; tomod.midiData[2] = midiLimit(roundToInt((float)data2 * 2.f * param[kWhiteVelocity])); } if (tomod.midiData[2]>0) playingOnChannel[data1][channel] = tomod.midiData[0]&0x0f; } else if (status == MIDI_NOTEOFF) { if (isBlackKey(data1)) { if (BlackChannel==ANY_CHANNEL) BlackChannel = channel; tomod.midiData[0] = MIDI_NOTEOFF | BlackChannel; } else { if (WhiteChannel==ANY_CHANNEL) WhiteChannel = channel; tomod.midiData[0] = MIDI_NOTEOFF | WhiteChannel; } playingOnChannel[data1][channel] = -1; } } outputs[0].push_back(tomod); } }
void PianoKeyBoard::calculateKeySize(cocos2d::Size size) { int whiteKeyCount = 0 ; for ( int i=lowNote_; i<=highNote_; ++i ) { if ( !isBlackKey(getKeyType(i))) { ++whiteKeyCount ; } } whiteKeyWidth_ = (size.width-42) / whiteKeyCount ; whiteKeyHeight_ = 243 ; blackKeyWidth_ = whiteKeyWidth_ * 16 /27 ; blackKeyHeight_ = 124 ; }
bool PizKeyboardComponent::mouseDownOnKey(int midiNoteNumber, const MouseEvent &e) { midiKeyboardEditor* editor = ((midiKeyboardEditor*)(this->getParentComponent())); if (e.mods.isAltDown()) { editor->getFilter()->setParameter(kHidePanel,1.f-editor->getFilter()->getParameter(kHidePanel)); return false; } int l = isBlackKey(midiNoteNumber) ? this->getBlackNoteLength() : getHeight(); float velocity = editor->getFilter()->getParameter(kUseY)>=0.5f ? (float)e.getMouseDownY()/(float)l : editor->getFilter()->getParameter(kVelocity); if (e.mods.isCtrlDown() || e.mods.isMiddleButtonDown()) { s->allNotesOff(this->getMidiChannel()); _keysPressed.clear(); return false; } else if (e.mods.isShiftDown()) { if (!e.mods.isPopupMenu()) setKeyPressBaseOctave(midiNoteNumber/12); grabKeyboardFocus(); repaint(); return false; } else if (e.mods.isPopupMenu()!=toggle) { if (s->isNoteOn(this->getMidiChannel(),midiNoteNumber)) { s->noteOff(this->getMidiChannel(),midiNoteNumber); } else { s->noteOn(this->getMidiChannel(),midiNoteNumber,velocity); } return false; } else { if (!s->isNoteOn(this->getMidiChannel(),midiNoteNumber)) { //s->noteOn(this->getMidiChannel(),midiNoteNumber,velocity); this->setVelocity(velocity,false); return true; } else { s->noteOff(this->getMidiChannel(),midiNoteNumber); return false; } } return true; }
bool Piano::isWhiteKey( int key ) { return !isBlackKey( key ); }