123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #include "MIDIToneGenerator.h"
- USB_ClassInfo_MIDI_Device_t Keyboard_MIDI_Interface =
- {
- .Config =
- {
- .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,
- .DataINEndpoint =
- {
- .Address = MIDI_STREAM_IN_EPADDR,
- .Size = MIDI_STREAM_EPSIZE,
- .Banks = 1,
- },
- .DataOUTEndpoint =
- {
- .Address = MIDI_STREAM_OUT_EPADDR,
- .Size = MIDI_STREAM_EPSIZE,
- .Banks = 1,
- },
- },
- };
- static const uint8_t SineTable[256] =
- {
- 128, 131, 134, 137, 140, 143, 146, 149, 152, 156, 159, 162, 165, 168, 171, 174,
- 176, 179, 182, 185, 188, 191, 193, 196, 199, 201, 204, 206, 209, 211, 213, 216,
- 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 237, 239, 240, 242, 243, 245,
- 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 254, 254, 253, 252, 252, 251, 250, 249, 248, 247,
- 246, 245, 243, 242, 240, 239, 237, 236, 234, 232, 230, 228, 226, 224, 222, 220,
- 218, 216, 213, 211, 209, 206, 204, 201, 199, 196, 193, 191, 188, 185, 182, 179,
- 176, 174, 171, 168, 165, 162, 159, 156, 152, 149, 146, 143, 140, 137, 134, 131,
- 128, 124, 121, 118, 115, 112, 109, 106, 103, 99, 96, 93, 90, 87, 84, 81,
- 79, 76, 73, 70, 67, 64, 62, 59, 56, 54, 51, 49, 46, 44, 42, 39,
- 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 18, 16, 15, 13, 12, 10,
- 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 8,
- 9, 10, 12, 13, 15, 16, 18, 19, 21, 23, 25, 27, 29, 31, 33, 35,
- 37, 39, 42, 44, 46, 49, 51, 54, 56, 59, 62, 64, 67, 70, 73, 76,
- 79, 81, 84, 87, 90, 93, 96, 99, 103, 106, 109, 112, 115, 118, 121, 124,
- };
- static DDSNoteData NoteData[MAX_SIMULTANEOUS_NOTES];
- int main(void)
- {
- SetupHardware();
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- GlobalInterruptEnable();
- for (;;)
- {
- MIDI_EventPacket_t ReceivedMIDIEvent;
- if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent))
- {
- if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_ON)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0))
- {
- DDSNoteData* LRUNoteStruct = &NoteData[0];
-
- for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
- {
-
- if (!(NoteData[i].Pitch))
- {
-
- LRUNoteStruct = &NoteData[i];
- break;
- }
- else if (NoteData[i].LRUAge >= LRUNoteStruct->LRUAge)
- {
-
- LRUNoteStruct = &NoteData[i];
- }
- NoteData[i].LRUAge++;
- }
-
- LRUNoteStruct->Pitch = ReceivedMIDIEvent.Data2;
- LRUNoteStruct->TableIncrement = (uint32_t)(BASE_INCREMENT * SCALE_FACTOR) +
- ((uint32_t)(BASE_INCREMENT * NOTE_OCTIVE_RATIO * SCALE_FACTOR) *
- (ReceivedMIDIEvent.Data2 - BASE_PITCH_INDEX));
- LRUNoteStruct->TablePosition = 0;
- LRUNoteStruct->LRUAge = 0;
-
- LEDs_SetAllLEDs(LEDS_LED1);
- }
- else if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_OFF)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0))
- {
- bool FoundActiveNote = false;
-
- for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
- {
- if (NoteData[i].Pitch == ReceivedMIDIEvent.Data2)
- NoteData[i].Pitch = 0;
- else if (NoteData[i].Pitch)
- FoundActiveNote = true;
- }
-
- if (!(FoundActiveNote))
- LEDs_SetAllLEDs(LEDS_NO_LEDS);
- }
- }
- MIDI_Device_USBTask(&Keyboard_MIDI_Interface);
- USB_USBTask();
- }
- }
- ISR(TIMER0_COMPA_vect, ISR_BLOCK)
- {
- uint16_t MixedSample = 0;
-
- for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
- {
-
- if (NoteData[i].Pitch)
- {
-
- uint8_t TableIndex = (NoteData[i].TablePosition >> 24);
-
- MixedSample += SineTable[TableIndex];
- NoteData[i].TablePosition += NoteData[i].TableIncrement;
- }
- }
-
- OCR3A = (MixedSample <= 0xFF) ? MixedSample : 0xFF;
- }
- void SetupHardware(void)
- {
- #if (ARCH == ARCH_AVR8)
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- clock_prescale_set(clock_div_1);
- #endif
-
- LEDs_Init();
- USB_Init();
-
- TIMSK0 = (1 << OCIE0A);
- OCR0A = (VIRTUAL_SAMPLE_TABLE_SIZE / 8);
- TCCR0A = (1 << WGM01);
- TCCR0B = (1 << CS01);
-
- DDRC |= (1 << 6);
-
- TCCR3A = ((1 << WGM31) | (1 << COM3A1) | (1 << COM3A0));
- TCCR3B = ((1 << WGM32) | (1 << CS30));
- }
- void EVENT_USB_Device_Connect(void)
- {
- LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
-
- DDRC |= (1 << 6);
- }
- void EVENT_USB_Device_Disconnect(void)
- {
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
-
- for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
- NoteData[i].Pitch = 0;
-
- DDRC &= ~(1 << 6);
- }
- void EVENT_USB_Device_ConfigurationChanged(void)
- {
- bool ConfigSuccess = true;
- ConfigSuccess &= MIDI_Device_ConfigureEndpoints(&Keyboard_MIDI_Interface);
- LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
- }
- void EVENT_USB_Device_ControlRequest(void)
- {
- MIDI_Device_ProcessControlRequest(&Keyboard_MIDI_Interface);
- }
|