This is early stages of my project, the idea of this project is to be able to mix a guitar with windows sounds in reverse such as instrumental background music or trance music perhaps or maybe another fellow guitarist you may have downloaded from the internet. Microphone or guitar pin is p19 I would use a microphone for drums:) and that it for the moment, the code makes the mbed act as usb speaker that excepts a guitar or microphone input, but with a twist it all in reverse like a guitar reverse effects pedal but only you can mix anything you can get from the internet or any windows sound.
USBDevice/USBMIDI/USBMIDI.cpp@0:7610d342c76e, 2012-01-08 (annotated)
- Committer:
- mbed2f
- Date:
- Sun Jan 08 17:28:24 2012 +0000
- Revision:
- 0:7610d342c76e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed2f | 0:7610d342c76e | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
mbed2f | 0:7610d342c76e | 2 | * |
mbed2f | 0:7610d342c76e | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed2f | 0:7610d342c76e | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed2f | 0:7610d342c76e | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed2f | 0:7610d342c76e | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed2f | 0:7610d342c76e | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed2f | 0:7610d342c76e | 8 | * |
mbed2f | 0:7610d342c76e | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed2f | 0:7610d342c76e | 10 | * substantial portions of the Software. |
mbed2f | 0:7610d342c76e | 11 | * |
mbed2f | 0:7610d342c76e | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed2f | 0:7610d342c76e | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed2f | 0:7610d342c76e | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed2f | 0:7610d342c76e | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed2f | 0:7610d342c76e | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed2f | 0:7610d342c76e | 17 | */ |
mbed2f | 0:7610d342c76e | 18 | |
mbed2f | 0:7610d342c76e | 19 | #include "stdint.h" |
mbed2f | 0:7610d342c76e | 20 | #include "USBMIDI.h" |
mbed2f | 0:7610d342c76e | 21 | #include "USBBusInterface.h" |
mbed2f | 0:7610d342c76e | 22 | |
mbed2f | 0:7610d342c76e | 23 | |
mbed2f | 0:7610d342c76e | 24 | USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) { |
mbed2f | 0:7610d342c76e | 25 | midi_evt = NULL; |
mbed2f | 0:7610d342c76e | 26 | USBDevice::connect(); |
mbed2f | 0:7610d342c76e | 27 | } |
mbed2f | 0:7610d342c76e | 28 | |
mbed2f | 0:7610d342c76e | 29 | void USBMIDI::write(MIDIMessage m) { |
mbed2f | 0:7610d342c76e | 30 | USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK); |
mbed2f | 0:7610d342c76e | 31 | } |
mbed2f | 0:7610d342c76e | 32 | |
mbed2f | 0:7610d342c76e | 33 | |
mbed2f | 0:7610d342c76e | 34 | void USBMIDI::attach(void (*fptr)(MIDIMessage)) { |
mbed2f | 0:7610d342c76e | 35 | midi_evt = fptr; |
mbed2f | 0:7610d342c76e | 36 | } |
mbed2f | 0:7610d342c76e | 37 | |
mbed2f | 0:7610d342c76e | 38 | |
mbed2f | 0:7610d342c76e | 39 | bool USBMIDI::EP2_OUT_callback() { |
mbed2f | 0:7610d342c76e | 40 | uint8_t buf[64]; |
mbed2f | 0:7610d342c76e | 41 | uint16_t len; |
mbed2f | 0:7610d342c76e | 42 | readEP(EPBULK_OUT, buf, &len, 64); |
mbed2f | 0:7610d342c76e | 43 | |
mbed2f | 0:7610d342c76e | 44 | if (midi_evt != NULL) { |
mbed2f | 0:7610d342c76e | 45 | for (int i=0; i<len; i+=4) { |
mbed2f | 0:7610d342c76e | 46 | midi_evt(MIDIMessage(buf+i)); |
mbed2f | 0:7610d342c76e | 47 | } |
mbed2f | 0:7610d342c76e | 48 | } |
mbed2f | 0:7610d342c76e | 49 | |
mbed2f | 0:7610d342c76e | 50 | // We reactivate the endpoint to receive next characters |
mbed2f | 0:7610d342c76e | 51 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
mbed2f | 0:7610d342c76e | 52 | return true; |
mbed2f | 0:7610d342c76e | 53 | } |
mbed2f | 0:7610d342c76e | 54 | |
mbed2f | 0:7610d342c76e | 55 | |
mbed2f | 0:7610d342c76e | 56 | |
mbed2f | 0:7610d342c76e | 57 | // Called in ISR context |
mbed2f | 0:7610d342c76e | 58 | // Set configuration. Return false if the |
mbed2f | 0:7610d342c76e | 59 | // configuration is not supported. |
mbed2f | 0:7610d342c76e | 60 | bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) { |
mbed2f | 0:7610d342c76e | 61 | if (configuration != DEFAULT_CONFIGURATION) { |
mbed2f | 0:7610d342c76e | 62 | return false; |
mbed2f | 0:7610d342c76e | 63 | } |
mbed2f | 0:7610d342c76e | 64 | |
mbed2f | 0:7610d342c76e | 65 | // Configure endpoints > 0 |
mbed2f | 0:7610d342c76e | 66 | addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK); |
mbed2f | 0:7610d342c76e | 67 | addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
mbed2f | 0:7610d342c76e | 68 | |
mbed2f | 0:7610d342c76e | 69 | // We activate the endpoint to be able to receive data |
mbed2f | 0:7610d342c76e | 70 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
mbed2f | 0:7610d342c76e | 71 | return true; |
mbed2f | 0:7610d342c76e | 72 | } |
mbed2f | 0:7610d342c76e | 73 | |
mbed2f | 0:7610d342c76e | 74 | |
mbed2f | 0:7610d342c76e | 75 | uint8_t * USBMIDI::stringIinterfaceDesc() { |
mbed2f | 0:7610d342c76e | 76 | static uint8_t stringIinterfaceDescriptor[] = { |
mbed2f | 0:7610d342c76e | 77 | 0x0c, //bLength |
mbed2f | 0:7610d342c76e | 78 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
mbed2f | 0:7610d342c76e | 79 | 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio |
mbed2f | 0:7610d342c76e | 80 | }; |
mbed2f | 0:7610d342c76e | 81 | return stringIinterfaceDescriptor; |
mbed2f | 0:7610d342c76e | 82 | } |
mbed2f | 0:7610d342c76e | 83 | |
mbed2f | 0:7610d342c76e | 84 | uint8_t * USBMIDI::stringIproductDesc() { |
mbed2f | 0:7610d342c76e | 85 | static uint8_t stringIproductDescriptor[] = { |
mbed2f | 0:7610d342c76e | 86 | 0x16, //bLength |
mbed2f | 0:7610d342c76e | 87 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
mbed2f | 0:7610d342c76e | 88 | 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio |
mbed2f | 0:7610d342c76e | 89 | }; |
mbed2f | 0:7610d342c76e | 90 | return stringIproductDescriptor; |
mbed2f | 0:7610d342c76e | 91 | } |
mbed2f | 0:7610d342c76e | 92 | |
mbed2f | 0:7610d342c76e | 93 | |
mbed2f | 0:7610d342c76e | 94 | uint8_t * USBMIDI::configurationDesc() { |
mbed2f | 0:7610d342c76e | 95 | static uint8_t configDescriptor[] = { |
mbed2f | 0:7610d342c76e | 96 | // configuration descriptor |
mbed2f | 0:7610d342c76e | 97 | 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50, |
mbed2f | 0:7610d342c76e | 98 | |
mbed2f | 0:7610d342c76e | 99 | // The Audio Interface Collection |
mbed2f | 0:7610d342c76e | 100 | 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor |
mbed2f | 0:7610d342c76e | 101 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor |
mbed2f | 0:7610d342c76e | 102 | 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors |
mbed2f | 0:7610d342c76e | 103 | 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor |
mbed2f | 0:7610d342c76e | 104 | |
mbed2f | 0:7610d342c76e | 105 | // MIDI IN JACKS |
mbed2f | 0:7610d342c76e | 106 | 0x06, 0x24, 0x02, 0x01, 0x01, 0x00, |
mbed2f | 0:7610d342c76e | 107 | 0x06, 0x24, 0x02, 0x02, 0x02, 0x00, |
mbed2f | 0:7610d342c76e | 108 | |
mbed2f | 0:7610d342c76e | 109 | // MIDI OUT JACKS |
mbed2f | 0:7610d342c76e | 110 | 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, |
mbed2f | 0:7610d342c76e | 111 | 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00, |
mbed2f | 0:7610d342c76e | 112 | |
mbed2f | 0:7610d342c76e | 113 | // OUT endpoint descriptor |
mbed2f | 0:7610d342c76e | 114 | 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, |
mbed2f | 0:7610d342c76e | 115 | 0x05, 0x25, 0x01, 0x01, 0x01, |
mbed2f | 0:7610d342c76e | 116 | |
mbed2f | 0:7610d342c76e | 117 | // IN endpoint descriptor |
mbed2f | 0:7610d342c76e | 118 | 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, |
mbed2f | 0:7610d342c76e | 119 | 0x05, 0x25, 0x01, 0x01, 0x03, |
mbed2f | 0:7610d342c76e | 120 | }; |
mbed2f | 0:7610d342c76e | 121 | return configDescriptor; |
mbed2f | 0:7610d342c76e | 122 | } |