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: 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; }