#ifndef SOUNDFIELD_PACKET_LISTENER #define SOUNDFIELD_PACKET_LISTENER #include "osc/OscReceivedElements.h" #include "osc/OscPacketListener.h" #include "ip/UdpSocket.h" class SoundFieldPacketListener : public osc::OscPacketListener { protected: virtual void ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint ) { try{ // example of parsing single messages. osc::OscPacketListener // handles the bundle traversal. if( strcmp( m.AddressPattern(), "/noteOff" ) == 0 ){ osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); const char *messageFromIp = (arg++)->AsString(); int noteNumOff = (arg++)->AsInt32(); if( arg != m.ArgumentsEnd() ) throw osc::ExcessArgumentException(); //stop the note on the appropriate instrument for( int i = 0; i < numberOfTransmitSockets; i++) { if( strcmp( messageFromIp, ipAddrs[i] ) == 0 ) { soundFieldGens->ksInstruments[i]->markInactive(noteNumOff); i = numberOfTransmitSockets; } } }else if( strcmp( m.AddressPattern(), "/noteOn" ) == 0 ){ osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); const char *messageFromIp = (arg++)->AsString(); int noteNumOn = (arg++)->AsInt32(); int velocity = (arg++)->AsInt32(); if( arg != m.ArgumentsEnd() ) throw osc::ExcessArgumentException(); //start the note on the apropriate instrument for( int i = 0; i < numberOfTransmitSockets; i++) { if( strcmp( messageFromIp, ipAddrs[i] ) == 0 ) { soundFieldGens->ksInstruments[i]->pluckString(noteNumOn, velocity); i = numberOfTransmitSockets; } } } }catch( osc::Exception& e ){ // any parsing errors such as unexpected argument types, or // missing arguments get thrown as exceptions. std::cout << "error while parsing message: " << m.AddressPattern() << ": " << e.what() << "\n"; } } }; #endif