t_CKBOOL MidiInManager::open( MidiIn * min, t_CKINT device_num ) { // see if port not already open if( device_num >= (t_CKINT)the_mins.capacity() || !the_mins[device_num] ) { // allocate the buffer CBufferAdvance * cbuf = new CBufferAdvance; if( !cbuf->initialize( BUFFER_SIZE, sizeof(MidiMsg) ) ) { if( !min->m_suppress_output ) EM_error2( 0, "MidiIn: couldn't allocate CBuffer for port %i...", device_num ); delete cbuf; return FALSE; } // allocate RtMidiIn * rtmin = new RtMidiIn; try { rtmin->openPort( device_num ); rtmin->setCallback( cb_midi_input, cbuf ); } catch( RtError & err ) { if( !min->m_suppress_output ) { // print it EM_error2( 0, "MidiIn: couldn't open MIDI port %i...", device_num ); err.getMessage(); // const char * e = err.getMessage().c_str(); // EM_error2( 0, "...(%s)", err.getMessage().c_str() ); } delete cbuf; return FALSE; } // resize? if( device_num >= (t_CKINT)the_mins.capacity() ) { t_CKINT size = the_mins.capacity() * 2; if( device_num >= size ) size = device_num + 1; the_mins.resize( size ); the_bufs.resize( size ); } // put cbuf and rtmin in vector for future generations the_mins[device_num] = rtmin; the_bufs[device_num] = cbuf; } // set min min->min = the_mins[device_num]; // found min->m_buffer = the_bufs[device_num]; // get an index into your (you are min here) own buffer, // and a free ticket to your own workshop min->m_read_index = min->m_buffer->join( (Chuck_Event *)min->SELF ); min->m_device_num = (t_CKUINT)device_num; // done return TRUE; }
//----------------------------------------------------------------------------- // name: cb_hid_input // desc: called by device implementations to push a message onto the buffer //----------------------------------------------------------------------------- void HidInManager::push_message( HidMsg & msg ) { // find the queue if( the_matrix[msg.device_type][msg.device_num] != NULL ) { CBufferAdvance * cbuf = the_matrix[msg.device_type][msg.device_num]->cbuf; if( cbuf != NULL ) // queue the thing cbuf->put( &msg, 1 ); } }
//----------------------------------------------------------------------------- // name: cb_midi_output // desc: call back //----------------------------------------------------------------------------- void MidiInManager::cb_midi_input( double deltatime, std::vector<unsigned char> * msg, void * userData ) { unsigned int nBytes = msg->size(); CBufferAdvance * cbuf = (CBufferAdvance *)userData; MidiMsg m; if( nBytes >= 1 ) m.data[0] = msg->at(0); if( nBytes >= 2 ) m.data[1] = msg->at(1); if( nBytes >= 3 ) m.data[2] = msg->at(2); // put in the buffer, make sure not active sensing if( m.data[2] != 0xfe ) { cbuf->put( &m, 1 ); } }
//----------------------------------------------------------------------------- // name: open() // desc: opens the device of specified type and id //----------------------------------------------------------------------------- t_CKBOOL PhyHidDevIn::open( t_CKINT type, t_CKUINT number ) { // int temp; // check if( device_type != CK_HID_DEV_NONE ) { // log EM_log( CK_LOG_WARNING, "PhyHidDevIn: open() failed -> already open!" ); return FALSE; } if( type <= CK_HID_DEV_NONE || type >= CK_HID_DEV_COUNT ) { // log EM_log( CK_LOG_WARNING, "PhyHidDevIn: open() failed -> invalid device type %d", type ); return FALSE; } if( !default_drivers[type].open ) { EM_log( CK_LOG_WARNING, "PhyHidDevIn: open() failed -> %s does not support open", default_drivers[type].driver_name ); return FALSE; } if( default_drivers[type].open( ( int ) number ) ) { EM_log( CK_LOG_WARNING, "PhyHidDevIn: open() failed -> invalid %s number %d", default_drivers[type].driver_name, number ); return FALSE; } // allocate the buffer cbuf = new CBufferAdvance; if( !cbuf->initialize( BUFFER_SIZE, sizeof(HidMsg), HidInManager::m_event_buffer ) ) { // log EM_log( CK_LOG_WARNING, "PhyHidDevIn: open operation failed: cannot initialize buffer" ); this->close(); return FALSE; } device_type = type; device_num = number; return TRUE; }