Exemple #1
0
void MIDIProcessor::handleIncomingMidiMessage(juce::MidiInput * /*device*/,
  const juce::MidiMessage& message) {
  if (message.isController()) {
    const auto channel =
      static_cast<unsigned short int>(message.getChannel()); // 1-based
    const auto control =
      static_cast<unsigned short int>(message.getControllerNumber());
    const auto value =
      static_cast<unsigned short int>(message.getControllerValue());
    if (nrpn_filter_.ProcessMidi(channel, control, value)) { //true if nrpn piece
      if (nrpn_filter_.IsReady(channel)) { //send when finished
        for (const auto listener : listeners_)
          listener->handleMidiCC(channel, nrpn_filter_.GetControl(channel),
          nrpn_filter_.GetValue(channel));
        nrpn_filter_.Clear(channel);
      }
    }
    else //regular message
      for (const auto listener : listeners_)
        listener->handleMidiCC(channel, control, value);
  }
  else if (message.isNoteOn()) {
    for (const auto listener : listeners_) {
		  listener->handleMidiNote(message.getChannel(), message.getNoteNumber());
	  }
  }
  else if (message.isPitchWheel()) {
    const auto value =
      static_cast<unsigned short int>(message.getPitchWheelValue());
	  for (auto listener : listeners_) {
		  listener->handlePitchWheel(message.getChannel(), value);
	  }
  }
}
//===============================================================================================
void MainComponent::handleIncomingMidiMessage(juce::MidiInput *source, const juce::MidiMessage &message)
{
    // if we want to print the data to the console, we are going to have to lock the message thread
#ifdef DEBUG
    const MessageManagerLock mmLock; // should do it
    const uint8* data = message.getRawData();
    log("Received MIDI: " + String(data[0]) + " " + String(data[1]) + " " + String(data[2]) + "\n", console);
    try {
#endif
    // essentially have to switch on the channel to pass it to the right string
    // would make sense to have a map of strings by channel
    // as this is potentially a bottleneck
    // depending how much midi we are piping through
    // for now the ugly linear version
    if (!analysisThread->isThreadRunning())
        for (int i = 0; i < swivelStrings.size(); i++)
        {
            if (swivelStrings[i]->getMidiChannel() == message.getChannel())
                midiOutBox->getSelectedOutput()->sendMessageNow(swivelStrings[i]->transform(message));
        }
    //midiOutBox->getSelectedOutput()->sendMessageNow(message);
        
#ifdef DEBUG
    } catch (std::logic_error const &e) {
        std::cerr << e.what();
    }
#endif
}
void SeaboardVisualiser::seaboardDidGetNoteOff(const juce::MidiMessage &message)
{
	int channel = message.getChannel();
	String childName = kChannelNo + String(channel);
	ValueTree removeMe = theSeaboardData.getChildWithName(kChannelNo + String(channel));
	// We check if the channel number does have a current note (if the seabaord data tree has a child tree named after the relevant channel). If so, we remove it.
	if (removeMe.isValid())
	{
		theSeaboardData.removeChild(removeMe, 0);
	}
}
void SeaboardVisualiser::seaboardDidGetAftertouch(const juce::MidiMessage & message)
{
	int channel = message.getChannel();
	float aftertouch = message.getAfterTouchValue() / 127.f; //Normalise the aftertouch value between 0 and 1.
	String childName = kChannelNo + String(channel);
	
	ValueTree noteTree = theSeaboardData.getChildWithName(childName);
	if (noteTree.isValid())
	{
		noteTree.setProperty(kAftertouch, aftertouch, 0);
	}
}
void SeaboardVisualiser::seaboardDidGetPitchBend(const juce::MidiMessage & message)
{
	int channel = message.getChannel();
	float pitchBend = (message.getPitchWheelValue() - 8192) / 8192.f; //Normalise the pitch bend value between -1 and 1.
	String childName = kChannelNo + String(channel);
	
	ValueTree noteTree = theSeaboardData.getChildWithName(childName);
	if (noteTree.isValid())
	{
		noteTree.setProperty(kPitchBend, pitchBend, 0);
	}
}
// When this Seaboard::Listener object receives midi messages, we overwrite the following methods and implement our desired response.
void SeaboardVisualiser::seaboardDidGetNoteOn(const juce::MidiMessage &message)
{
	/*
	 We will set up a ValueTree object and add it as a child to the SeaboardData value tree. Its name will be set to the channel number that it is assigned to.
	 */
	
	// Get the relevant midi note information from the incoming message
	int channel = message.getChannel();
	int note = message.getNoteNumber();
	
	// Setup the name for the new child ValueTree structure.
	String childName = kChannelNo + String(channel);
	ValueTree channelInfo(childName);
	
	// Add the note information to the ValueTree structure.
	channelInfo.setProperty(kNoteNo, note, 0);
	channelInfo.addListener(this);
	
	// Add the note value tree as a child to the seaboard data value tree.
	theSeaboardData.addChild(channelInfo, -1, 0);
}