Last active
November 7, 2015 16:09
-
-
Save Rampoina/d73694a23ae678786a28 to your computer and use it in GitHub Desktop.
usb midi driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <libusb.h> | |
#include <cstdlib> | |
#include <RtMidi.h> | |
using namespace std; | |
int main() { | |
RtMidiOut *midiout = 0; | |
std::vector<unsigned char> message; | |
// RtMidiOut constructor | |
try { | |
midiout = new RtMidiOut(); | |
} | |
catch ( RtMidiError &error ) { | |
error.printMessage(); | |
exit( EXIT_FAILURE ); | |
} | |
midiout->openVirtualPort(); | |
message.push_back( 0 ); | |
message.push_back( 0 ); | |
message.push_back( 0 ); | |
libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices | |
libusb_device_handle *dev_handle; //a device handle | |
libusb_context *ctx = NULL; //a libusb session | |
int r; //for return values | |
ssize_t cnt; //holding number of devices in list | |
r = libusb_init(&ctx); //initialize the library for the session we just declared | |
if(r < 0) { | |
cout<<"Init Error "<<r<<endl; //there was an error | |
return 1; | |
} | |
libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation | |
cnt = libusb_get_device_list(ctx, &devs); //get the list of devices | |
if(cnt < 0) { | |
cout<<"Get Device Error"<<endl; //there was an error | |
return 1; | |
} | |
cout<<cnt<<" Devices in list."<<endl; | |
dev_handle = libusb_open_device_with_vid_pid(ctx, 0xf055, 0x0069); //these are vendorID and productID I found for my usb device | |
if(dev_handle == NULL) | |
cout<<"Cannot open device"<<endl; | |
else | |
cout<<"Device Opened"<<endl; | |
libusb_free_device_list(devs, 1); //free the list, unref the devices in it | |
unsigned char *data = new unsigned char[4]; //data to read | |
int actual; //used to find out how many bytes were read/written | |
if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached | |
cout<<"Kernel Driver Active"<<endl; | |
if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it | |
cout<<"Kernel Driver Detached!"<<endl; | |
} | |
r = libusb_claim_interface(dev_handle, 1); | |
if(r < 0) { | |
cout<<"Cannot Claim Interface"<<endl; | |
cout << "R: " << libusb_error_name(r) << endl; | |
return 1; | |
} | |
cout<<"Claimed Interface"<<endl; | |
while (1) { | |
r = libusb_bulk_transfer(dev_handle, (0x82 | LIBUSB_ENDPOINT_IN), data, 4, &actual, 0); | |
message[0] = data[1]; | |
message[1] = data[2]; | |
message[2] = data[3]; | |
midiout->sendMessage( &message ); | |
} | |
r = libusb_release_interface(dev_handle, 1); //release the claimed interface | |
if(r!=0) { | |
cout<<"Cannot Release Interface"<<endl; | |
return 1; | |
} | |
cout<<"Released Interface"<<endl; | |
libusb_close(dev_handle); //close the device we opened | |
libusb_exit(ctx); //needs to be called to end the | |
delete[] data; //delete the allocated memory for data | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment