Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Backup #2
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <ncurses.h>
- #define NOTE_NULL 0x0
- #define NOTE_RESET 0x1
- #define NOTE_ON 0x2
- #define NOTE_OFF 0x3
- #define NOTE_C 0x4
- #define NOTE_Cs 0x5
- #define NOTE_Db 0x5
- #define NOTE_D 0x6
- #define NOTE_Ds 0x7
- #define NOTE_Eb 0x7
- #define NOTE_E 0x8
- #define NOTE_F 0x9
- #define NOTE_Fs 0xA
- #define NOTE_Gb 0xA
- #define NOTE_G 0xB
- #define NOTE_Gs 0xC
- #define NOTE_Ab 0xC
- #define NOTE_A 0xD
- #define NOTE_As 0xE
- #define NOTE_Bb 0xE
- #define NOTE_B 0xF
- #define EFF_ARPEGGIO 0x0
- #define EFF_PITCH_SLIDE_UP 0x1
- #define EFF_PITCH_SLIDE_DOWN 0x2
- #define EFF_PORTAMENTO 0x3
- #define EFF_VIBRATO 0x4
- #define EFF_WAVEFORM 0x5
- #define EFF_DUTY_CYCLE 0x6
- #define EFF_TREMOLO 0x7
- #define EFF_PWM 0x8
- #define EFF_PITCH 0x9
- #define EFF_VOLUME_SLIDE_UP 0xA
- #define EFF_VOLUME_SLIDE_DOWN 0xB
- #define EFF_VOLUME 0xC
- #define EFF_ENVELOPE_ATTACK_DECAY 0xD
- #define EFF_ENVELOPE_SUSTAIN_RELEASE 0xE
- #define EFF_FINE_TUNE 0xF
- FILE *out;
- char serialPort[255];
- typedef struct
- {
- unsigned char enable;
- unsigned char effect;
- unsigned char argument;
- } Effect;
- typedef struct
- {
- unsigned char note;
- unsigned char octave;
- unsigned char volume;
- unsigned char applyInstrument;
- unsigned char instrument;
- } NoteCommand;
- typedef struct
- {
- NoteCommand noteCommand;
- Effect effect;
- } Entry;
- typedef struct
- {
- Entry entries[4];
- } Frame;
- typedef struct
- {
- Effect effects[8];
- } Instrument;
- typedef struct
- {
- Frame frames[32];
- } Order;
- typedef struct
- {
- unsigned char fps;
- unsigned char orderCount;
- unsigned char repeat;
- unsigned char instrumentCount;
- Instrument *instruments;
- Order *orders;
- } Song;
- void printTabs(int tabs)
- {
- int i;
- for(i = 0; i < tabs; i++)
- {
- printf("\t");
- }
- }
- void printEffect(Effect *effect, int tabs)
- {
- int s1, s2;
- s1 = (effect->argument) >> 4;
- s2 = (effect->argument) & 0x0F;
- printTabs(tabs);
- if(effect->enable)
- {
- switch(effect->effect)
- {
- case 0x0:
- if(s1 || s2)
- {
- printf("Arpeggio: Semitones: %i, %i\n", s1, s2);
- }
- else
- {
- printf("Arpeggio Off\n");
- }
- break;
- case 0x1:
- printf("Pitch Slide Up: Rate: %i\n", effect->argument);
- break;
- case 0x2:
- printf("Pitch Slide Down: Rate: %i\n", effect->argument);
- break;
- case 0x3:
- if(effect->argument)
- {
- printf("Portamento: Rate: %i\n", effect->argument);
- }
- else
- {
- printf("Portamento Off");
- }
- break;
- case 0x4:
- printf("Vibrato: Rate: %i, Depth: %i\n", s1, s2);
- break;
- case 0x5:
- printf("Waveform: ");
- switch(effect->argument)
- {
- case 0:
- printf("Pulse\n");
- break;
- case 1:
- printf("Triangle\n");
- break;
- case 2:
- printf("Sawtooth\n");
- break;
- case 3:
- printf("Noise\n");
- break;
- default:
- printf("Unknown\n");
- }
- break;
- case 0x6:
- printf("Duty Cycle: %i\%\n", effect->argument / 256 * 100);
- break;
- case 0x7:
- printf("Tremolo: Rate: %i, Depth: %i\n", s1, s2);
- break;
- case 0x8:
- printf("Pulse Width Modulation: Rate: %i, Depth: %i\n", s1, s2);
- break;
- case 0x9:
- printf("Pitch: %i\n", effect->argument);
- break;
- case 0xA:
- printf("Volume Slide Up: Rate: %i\n", effect->argument);
- break;
- case 0xB:
- printf("Volume Slide Down: Rate: %i\n", effect->argument);
- break;
- case 0xC:
- printf("Volume: %i\n", effect->argument);
- break;
- case 0xD:
- printf("Envelope: Attack: %i, Decay: %i\n", s1, s2);
- break;
- case 0xE:
- printf("Envelope: Sustain: %i\%, Release: %i\n", s1 / 16 * 100, s2);
- break;
- case 0xF:
- printf("Fine Tune: %i\n", effect->argument);
- break;
- default:
- printf("Unknown Effect\n");
- }
- }
- else
- {
- printf("No Effect\n");
- }
- }
- void printInstrumentInfo(Instrument *instrument, int tabs)
- {
- int i;
- for(i = 0; i < sizeof(instrument->effects) / sizeof(Effect); i++)
- {
- printTabs(tabs);
- printf("Effect #%i:\n", i + 1);
- printEffect(&(instrument->effects[i]), tabs + 1);
- }
- }
- void printNote(unsigned char note, unsigned char octave, int tabs)
- {
- printTabs(tabs);
- switch(note)
- {
- case 0x0:
- printf("No Note\n");
- break;
- case 0x1:
- printf("Reset Oscillator Timer\n");
- break;
- case 0x2:
- printf("Enable Oscillator\n");
- break;
- case 0x3:
- printf("Disable Oscillator\n");
- break;
- case 0x4:
- printf("Play Note C%i\n", octave);
- break;
- case 0x5:
- printf("Play Note C#%i\n", octave);
- break;
- case 0x6:
- printf("Play Note D%i\n", octave);
- break;
- case 0x7:
- printf("Play Note D#%i\n", octave);
- break;
- case 0x8:
- printf("Play Note E%i\n", octave);
- break;
- case 0x9:
- printf("Play Note F%i\n", octave);
- break;
- case 0xA:
- printf("Play Note F#%i\n", octave);
- break;
- case 0xB:
- printf("Play Note G%i\n", octave);
- break;
- case 0xC:
- printf("Play Note G#%i\n", octave);
- break;
- case 0xD:
- printf("Play Note A%i\n", octave);
- break;
- case 0xE:
- printf("Play Note A#%i\n", octave);
- break;
- case 0xF:
- printf("Play Note B%i\n", octave);
- break;
- default:
- printf("Unknown Command\n");
- }
- }
- void printVolume(unsigned char volume, int tabs)
- {
- printTabs(tabs);
- if(volume)
- {
- printf("Volume: %i\%\n", volume / 32 * 100);
- }
- else
- {
- printf("Ignore Volume\n");
- }
- }
- void printInstrumentID(unsigned char applyInstrument, unsigned char instrument, int tabs)
- {
- printTabs(tabs);
- if(applyInstrument)
- {
- printf("Apply Instrument #%i\n", instrument + 1);
- }
- else
- {
- printf("Ignore Instrument\n");
- }
- }
- void printNoteCommand(NoteCommand *noteCommand, int tabs)
- {
- printNote(noteCommand->note, noteCommand->octave, tabs);
- printVolume(noteCommand->volume, tabs);
- printInstrumentID(noteCommand->applyInstrument, noteCommand->instrument, tabs);
- }
- void printEntry(Entry *entry, int tabs)
- {
- printNoteCommand(&(entry->noteCommand), tabs);
- printEffect(&(entry->effect), tabs);
- }
- void printFrame(Frame *frame, int tabs)
- {
- int i;
- for(i = 0; i < sizeof(frame->entries) / sizeof(Entry); i++)
- {
- printTabs(tabs);
- printf("Channel #%i:\n", i + 1);
- printEntry(&(frame->entries[i]), tabs + 1);
- }
- }
- void printOrderInfo(Order *order, int tabs)
- {
- int i;
- for(i = 0; i < sizeof(order->frames) / sizeof(Frame); i++)
- {
- printTabs(tabs);
- printf("Frame #%i:\n", i + 1);
- printFrame(&(order->frames[i]), tabs + 1);
- }
- }
- void printSongInfo(Song *song)
- {
- printf("Speed: %i FPS\n", song->fps);
- printf("Length: %i Orders\n", song->orderCount);
- printf("Repeat: Order #%i\n", song->repeat + 1);
- printf("Instrument Count: %i\n", song->instrumentCount);
- printf("Instruments:\n");
- int i;
- for(i = 0; i < song->instrumentCount; i++)
- {
- printf("\tInstrument #%i:\n", i + 1);
- printInstrumentInfo(&(song->instruments[0]), 2);
- }
- printf("Orders:\n");
- for(i = 0; i < song->orderCount; i++)
- {
- printf("\tOrder #%i:\n", i + 1);
- printOrderInfo(&(song->orders[0]), 2);
- }
- }
- void playNote(unsigned char note, unsigned char octave, unsigned char volume, unsigned char osc)
- {
- note &= 0x0F;
- octave &= 0b00000111;
- volume &= 0b00011110; // weird bug causing the last bit to make the octave go really high
- osc &= 0b00000011;
- char highByte = (octave >> 1) | (volume << 2) | (1 << 7);
- char lowByte = (osc << 1) | (note << 3) | (octave << 7);
- fputc(highByte, out);
- fputc(lowByte, out);
- fputc(0xFF, out);
- fclose(out);
- out = fopen(serialPort, "w");
- }
- void playEffect(unsigned char effect, unsigned char argument, unsigned char osc)
- {
- effect &= 0x0F;
- osc &= 0b00000011;
- char highByte = (argument >> 1) | (1 << 7);
- char lowByte = 1 | (osc << 1) | (effect << 3) | (argument << 7);
- fputc(highByte, out);
- fputc(lowByte, out);
- fputc(0xFF, out);
- fclose(out);
- out = fopen(serialPort, "w");
- }
- void sendEffectCommand(Effect *effect, unsigned char osc)
- {
- if(effect->enable)
- {
- playEffect(effect->effect, effect->argument, osc);
- }
- }
- void sendNoteCommand(NoteCommand *noteCommand, unsigned char osc)
- {
- if(noteCommand->note || noteCommand->octave || noteCommand->volume)
- {
- playNote(noteCommand->note, noteCommand->octave, noteCommand->volume, osc);
- //applyInstrument goes here
- }
- }
- void playEntry(Entry *entry, unsigned char osc)
- {
- sendEffectCommand(&entry->effect, osc);
- sendNoteCommand(&entry->noteCommand, osc);
- }
- void playFrame(Frame *frame)
- {
- playEntry(&frame->entries[0], 0);
- playEntry(&frame->entries[1], 1);
- playEntry(&frame->entries[2], 2);
- playEntry(&frame->entries[3], 3);
- }
- NoteCommand newNoteCommand(unsigned char note, unsigned char octave, unsigned char volume, unsigned char applyInstrument, unsigned char instrument)
- {
- NoteCommand command;
- command.note = note;
- command.octave = octave;
- command.volume = volume;
- command.applyInstrument = applyInstrument;
- command.instrument = instrument;
- return command;
- }
- NoteCommand newSimpleNoteCommand(unsigned char note, unsigned char octave, unsigned char volume)
- {
- return newNoteCommand(note, octave, volume, 0, 0);
- }
- NoteCommand newSimplestNoteCommand(unsigned char note, unsigned char octave)
- {
- return newSimpleNoteCommand(note, octave, 0);
- }
- NoteCommand newNullNoteCommand()
- {
- return newSimplestNoteCommand(NOTE_NULL, 0);
- }
- NoteCommand newResetNoteCommand()
- {
- return newSimplestNoteCommand(NOTE_RESET, 0);
- }
- NoteCommand newNoteOnCommand()
- {
- return newSimplestNoteCommand(NOTE_ON, 0);
- }
- NoteCommand newNoteOffCommand()
- {
- return newSimplestNoteCommand(NOTE_OFF, 0);
- }
- Effect newEffect(unsigned char effect, unsigned char argument)
- {
- Effect new_effect;
- new_effect.enable = 1;
- new_effect.effect = effect;
- new_effect.argument = argument;
- return new_effect;
- }
- Effect newNullEffect()
- {
- Effect effect;
- effect.enable = 0;
- effect.effect = 0;
- effect.argument = 0;
- return effect;
- }
- Entry newEntry(NoteCommand command, Effect effect)
- {
- Entry entry;
- entry.noteCommand = command;
- entry.effect = effect;
- return entry;
- }
- Entry newSimpleEntry(NoteCommand command)
- {
- return newEntry(command, newNullEffect());
- }
- Entry newNullEntry()
- {
- return newEntry(newNullNoteCommand(), newNullEffect());
- }
- Frame newFrame(Entry entry1, Entry entry2, Entry entry3, Entry entry4)
- {
- Frame frame;
- frame.entries[0] = entry1;
- frame.entries[1] = entry2;
- frame.entries[2] = entry3;
- frame.entries[3] = entry4;
- return frame;
- }
- Frame newUnisonFrame(Entry entry)
- {
- return newFrame(entry, entry, entry, entry);
- }
- Frame newQuietFrame()
- {
- return newUnisonFrame(newSimpleEntry(newNoteOffCommand()));
- }
- Order *newEmptyOrder()
- {
- Order *order;
- int i;
- for(i = 0; i < sizeof(order->frames) / sizeof(Frame); i++)
- {
- order->frames[i] = newQuietFrame();
- }
- return order;
- }
- WINDOW *orderWindow;
- WINDOW *instrumentWindow;
- WINDOW *effectWindow;
- WINDOW *trackerWindow;
- int w1, w2, h1, h2;
- #define WIN_ORDER 0
- #define WIN_INSTR 1
- #define WIN_EFFECT 2
- #define WIN_TRACKER 3
- int curWindow = WIN_TRACKER;
- #define MODE_EDIT 0
- #define MODE_PLAY 1
- #define MODE_JAM 2
- int curMode = MODE_EDIT;
- #define MAX_ORDERS 64
- #define MAX_INSTRUMENTS 16
- Song song;
- int curOrder = 0;
- int curInstrument = 0;
- int curEffect = 0;
- int orderScroll = 0;
- int instrumentScroll = 0;
- int curOsc = 0;
- int curFrame = 0;
- int curCol = 0;
- void placeCursor()
- {
- move(LINES - 1, COLS - 1);
- }
- void printTitle(WINDOW *window, char *title)
- {
- int h, w;
- getmaxyx(window, h, w);
- wattron(window, A_BOLD);
- mvwprintw(window, 0,( w - strlen(title)) / 2, "%s", title);
- }
- void drawOrderWindow()
- {
- wclear(orderWindow);
- wattrset(orderWindow, A_NORMAL);
- if(curWindow == WIN_ORDER && curMode == MODE_EDIT) wattrset(orderWindow, A_REVERSE);
- box(orderWindow, 0, 0);
- char t[20];
- sprintf(t, "Orders (%i)", song.orderCount);
- printTitle(orderWindow, t);
- wattrset(orderWindow, A_NORMAL);
- int i;
- int a;
- for(i = orderScroll; i < song.orderCount && i < h1 - 2 + orderScroll; i++)
- {
- wattrset(orderWindow, curOrder == i ? A_REVERSE : A_NORMAL);
- for(a = 0; a < w1 - 2; a++)
- {
- mvwprintw(orderWindow, i + 1 - orderScroll, a + 1, " ");
- }
- mvwprintw(orderWindow, i + 1 - orderScroll, 1, "%c 0x%x: ", song.repeat == i ? '*' : '-', i);
- }
- wrefresh(orderWindow);
- placeCursor();
- }
- void drawInstrumentWindow()
- {
- wclear(instrumentWindow);
- wattrset(instrumentWindow, A_NORMAL);
- if(curWindow == WIN_INSTR && curMode == MODE_EDIT) wattrset(instrumentWindow, A_REVERSE);
- box(instrumentWindow, 0, 0);
- char t[20];
- sprintf(t, "Instruments (%i)", song.instrumentCount);
- printTitle(instrumentWindow, t);
- wattrset(instrumentWindow, A_NORMAL);
- int i;
- int a;
- for(i = instrumentScroll; i < song.instrumentCount && i < h1 - 2 + instrumentScroll; i++)
- {
- wattrset(instrumentWindow, curInstrument == i ? A_REVERSE : A_NORMAL);
- for(a = 0; a < w1 - 2; a++)
- {
- mvwprintw(instrumentWindow, i + 1 - instrumentScroll, a + 1, " ");
- }
- mvwprintw(instrumentWindow, i + 1 - instrumentScroll, 1, "0x%x: ", i);
- }
- wrefresh(instrumentWindow);
- placeCursor();
- }
- void drawEffectWindow()
- {
- wclear(effectWindow);
- wattrset(effectWindow, A_NORMAL);
- if(curWindow == WIN_EFFECT && curMode == MODE_EDIT) wattrset(effectWindow, A_REVERSE);
- box(effectWindow, 0, 0);
- printTitle(effectWindow, "Effects");
- wattrset(effectWindow, A_NORMAL);
- Instrument *instrument = &song.instruments[curInstrument];
- int i;
- int a;
- for(i = 0; i < sizeof(instrument->effects) / sizeof(Effect); i++)
- {
- Effect effect = instrument->effects[i];
- wattrset(effectWindow, curEffect == i ? A_REVERSE : A_NORMAL);
- for(a = 0; a < w1 - 2; a++)
- {
- mvwprintw(effectWindow, i + 1, a + 1, " ");
- }
- if(!effect.enable)
- {
- mvwprintw(effectWindow, i + 1, 1, "0x%x: DISABLED", i);
- }
- else
- {
- char eff[20];
- switch(effect.effect)
- {
- case 0x0:
- strcpy(eff, "ARPEGGIO: ");
- break;
- case 0x1:
- strcpy(eff, "PORT UP: ");
- break;
- case 0x2:
- strcpy(eff, "PORT DOWN: ");
- break;
- case 0x3:
- strcpy(eff, "PORTAMENTO:");
- break;
- case 0x4:
- strcpy(eff, "VIBRATO: ");
- break;
- case 0x5:
- strcpy(eff, "WAVEFORM: ");
- break;
- case 0x6:
- strcpy(eff, "DUTY CYCLE:");
- break;
- case 0x7:
- strcpy(eff, "TREMOLO: ");
- break;
- case 0x8:
- strcpy(eff, "PWM: ");
- break;
- case 0x9:
- strcpy(eff, "PITCH: ");
- break;
- case 0xA:
- strcpy(eff, "VOL UP: ");
- break;
- case 0xB:
- strcpy(eff, "VOL DOWN: ");
- break;
- case 0xC:
- strcpy(eff, "SET VOL: ");
- break;
- case 0xD:
- strcpy(eff, "ATCK/DECAY:");
- break;
- case 0xE:
- strcpy(eff, "SUS/RELEAS:");
- break;
- case 0xF:
- strcpy(eff, "FINE TUNE: ");
- break;
- }
- mvwprintw(effectWindow, i + 1, 1, "0x%x: (0x%x) %s 0x%x%x", i, effect.effect, eff, (effect.argument & 0xF0) >> 4, effect.argument & 0xF);
- }
- }
- wrefresh(effectWindow);
- placeCursor();
- }
- void drawTrackerWindow()
- {
- wclear(trackerWindow);
- wattrset(trackerWindow, A_NORMAL);
- if(curWindow == WIN_TRACKER && curMode == MODE_EDIT) wattrset(trackerWindow, A_REVERSE);
- wattron(trackerWindow, A_BOLD);
- box(trackerWindow, 0, 0);
- wattroff(trackerWindow, A_BOLD);
- printTitle(trackerWindow, "Tracker");
- wattrset(trackerWindow, A_NORMAL);
- int i;
- for(i = -2; i <= 32 && i < h2 - 4; i++)
- {
- wattrset(trackerWindow, A_BOLD);
- if(i >= 0 && i < 32)
- {
- if(curFrame == i) wattron(trackerWindow, A_REVERSE);
- mvwprintw(trackerWindow, i + 3, 1, "0x%x%x ", (i & 0xF0) >> 4, i & 0xF);
- }
- else if(i == -2)
- {
- mvwprintw(trackerWindow, i + 3, 1, "(0x%x)", curOrder);
- }
- else if(i == -1 || i == 32)
- {
- wattrset(trackerWindow, A_NORMAL);
- wmove(trackerWindow, i + 3, 1);
- int a;
- for(a = 0; a < 5; a++)
- {
- waddch(trackerWindow, ACS_HLINE);
- }
- }
- wattrset(trackerWindow, A_NORMAL);
- waddch(trackerWindow, ACS_VLINE);
- int a;
- for(a = 0; a < 4; a++)
- {
- if(i == -2)
- {
- wattrset(trackerWindow, A_BOLD);
- if(curOsc == a) wattron(trackerWindow, A_REVERSE);
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "Osc #%i ", a + 1);
- wattrset(trackerWindow, A_NORMAL);
- }
- else if(i == -1 || i == 32)
- {
- wmove(trackerWindow, i + 3, a * 10 + 7);
- int a;
- for(a = 0; a < 9; a++)
- {
- waddch(trackerWindow, ACS_HLINE);
- }
- }
- else
- {
- Entry entry = song.orders[curOrder].frames[i].entries[a];
- wattrset(trackerWindow, curFrame == i && curOsc == a && curCol == 0 ? A_REVERSE : A_NORMAL);
- switch(entry.noteCommand.note)
- {
- case 0x0:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "---");
- break;
- case 0x1:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "RES");
- break;
- case 0x2:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "+++");
- break;
- case 0x3:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "xxx");
- break;
- case 0x4:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "C-%x", entry.noteCommand.octave);
- break;
- case 0x5:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "C#%x", entry.noteCommand.octave);
- break;
- case 0x6:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "D-%x", entry.noteCommand.octave);
- break;
- case 0x7:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "D#%x", entry.noteCommand.octave);
- break;
- case 0x8:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "E-%x", entry.noteCommand.octave);
- break;
- case 0x9:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "F-%x", entry.noteCommand.octave);
- break;
- case 0xA:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "F#%x", entry.noteCommand.octave);
- break;
- case 0xB:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "G-%x", entry.noteCommand.octave);
- break;
- case 0xC:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "G#%x", entry.noteCommand.octave);
- break;
- case 0xD:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "A-%x", entry.noteCommand.octave);
- break;
- case 0xE:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "A#%x", entry.noteCommand.octave);
- break;
- case 0xF:
- mvwprintw(trackerWindow, i + 3, a * 10 + 7, "B-%x", entry.noteCommand.octave);
- break;
- }
- wattrset(trackerWindow, curFrame == i && curOsc == a && curCol == 1 ? A_REVERSE : A_NORMAL);
- if(entry.noteCommand.applyInstrument)
- {
- wprintw(trackerWindow, "%x", entry.noteCommand.instrument);
- }
- else
- {
- wprintw(trackerWindow, "-", entry.noteCommand.instrument);
- }
- wattrset(trackerWindow, curFrame == i && curOsc == a && curCol == 2 ? A_REVERSE : A_NORMAL);
- if(entry.noteCommand.volume)
- {
- wprintw(trackerWindow, "%x%x", entry.noteCommand.volume >> 4, entry.noteCommand.volume & 0xF);
- }
- else
- {
- wprintw(trackerWindow, "--");
- }
- if(entry.effect.enable)
- {
- wattrset(trackerWindow, curFrame == i && curOsc == a && curCol == 3 ? A_REVERSE : A_NORMAL);
- wprintw(trackerWindow, "%x", entry.effect.effect & 0xF);
- wattrset(trackerWindow, curFrame == i && curOsc == a && curCol == 4 ? A_REVERSE : A_NORMAL);
- wprintw(trackerWindow, "%x%x", entry.effect.argument >> 4, entry.effect.argument & 0xF);
- }
- else
- {
- wattrset(trackerWindow, curFrame == i && curOsc == a && curCol == 3 ? A_REVERSE : A_NORMAL);
- wprintw(trackerWindow, "-");
- wattrset(trackerWindow, curFrame == i && curOsc == a && curCol == 4 ? A_REVERSE : A_NORMAL);
- wprintw(trackerWindow, "--");
- }
- }
- wattrset(trackerWindow, A_NORMAL);
- waddch(trackerWindow, ACS_VLINE);
- }
- }
- wrefresh(trackerWindow);
- placeCursor();
- }
- void playOrder(Order *order, int fps)
- {
- int i;
- for(i = 0; i < sizeof(order->frames) / sizeof(Frame); i++)
- {
- curFrame = i;
- drawTrackerWindow();
- placeCursor();
- playFrame(&order->frames[i]);
- usleep(1000000 / fps);
- }
- }
- void drawWindows()
- {
- drawOrderWindow();
- drawInstrumentWindow();
- drawEffectWindow();
- drawTrackerWindow();
- placeCursor();
- refresh();
- }
- void spam(int c, int y)
- {
- move(y, 0);
- int i;
- for(i = 0; i < COLS; i++)
- {
- printw("%c", c);
- }
- }
- void drawStatus()
- {
- char modeString[32];
- switch(curMode)
- {
- case MODE_EDIT:
- strcpy(modeString, "Edit");
- break;
- case MODE_PLAY:
- strcpy(modeString, "Play");
- break;
- case MODE_JAM:
- strcpy(modeString, "Jam");
- break;
- }
- attrset(A_NORMAL);
- spam(' ', LINES - 1);
- mvprintw(LINES - 1, 0, "MODE: %s | SPEED: %i FPS | REPEAT: 0x%x | ORDERS: %i | INSTRUMENTS: %i", modeString, song.fps, song.repeat, song.orderCount, song.instrumentCount);
- placeCursor();
- refresh();
- }
- void initWindows()
- {
- w1 = COLS / 3;
- h1 = 10;
- w2 = w1 * 3;
- h2 = LINES - h1 - 1;
- orderWindow = newwin(h1, w1, 0, 0);
- instrumentWindow = newwin(h1, w1, 0, w1);
- effectWindow = newwin(h1, w1, 0, w1 * 2);
- trackerWindow = newwin(h2, w2, h1, 0);
- clear();
- refresh();
- drawWindows();
- drawStatus();
- placeCursor();
- refresh();
- }
- void editInput(int c)
- {
- Entry *entry = &song.orders[curOrder].frames[curFrame].entries[curOsc];
- switch(c)
- {
- case '\t':
- curWindow = (curWindow + 1) % 4;
- drawWindows();
- break;
- default:
- switch(curWindow)
- {
- case WIN_ORDER:
- switch(c)
- {
- case KEY_DOWN:
- curOrder = (curOrder + 1) % song.orderCount;
- while(curOrder >= orderScroll + h1 - 2)
- {
- orderScroll++;
- }
- while(curOrder < orderScroll)
- {
- orderScroll--;
- }
- drawOrderWindow();
- drawTrackerWindow();
- break;
- case KEY_UP:
- curOrder = (curOrder - 1);
- while(curOrder < 0) curOrder += song.orderCount;
- while(curOrder >= orderScroll + h1 - 2)
- {
- orderScroll++;
- }
- while(curOrder < orderScroll)
- {
- orderScroll--;
- }
- drawOrderWindow();
- drawTrackerWindow();
- break;
- case 'r':
- song.repeat = curOrder;
- drawOrderWindow();
- break;
- case '=':
- song.orderCount++;
- if(song.orderCount > MAX_ORDERS) song.orderCount = MAX_ORDERS;
- drawOrderWindow();
- drawStatus();
- break;
- case '-':
- song.orderCount--;
- if(song.orderCount <= 0) song.orderCount = 1;
- if(curOrder >= song.orderCount) curOrder = song.orderCount - 1;
- while(curOrder >= orderScroll + h1 - 2)
- {
- orderScroll++;
- }
- while(curOrder < orderScroll)
- {
- orderScroll--;
- }
- if(song.repeat >= song.orderCount) song.repeat--;
- drawOrderWindow();
- drawStatus();
- break;
- }
- break;
- case WIN_INSTR:
- switch(c)
- {
- case KEY_DOWN:
- curInstrument = (curInstrument + 1) % song.instrumentCount;
- while(curInstrument >= instrumentScroll + h1 - 2)
- {
- instrumentScroll++;
- }
- while(curInstrument < instrumentScroll)
- {
- instrumentScroll--;
- }
- drawInstrumentWindow();
- drawEffectWindow();
- break;
- case KEY_UP:
- curInstrument = (curInstrument - 1);
- while(curInstrument < 0) curInstrument += song.instrumentCount;
- while(curInstrument >= instrumentScroll + h1 - 2)
- {
- instrumentScroll++;
- }
- while(curInstrument < instrumentScroll)
- {
- instrumentScroll--;
- }
- drawInstrumentWindow();
- drawEffectWindow();
- break;
- case '=':
- song.instrumentCount++;
- if(song.instrumentCount > MAX_INSTRUMENTS) song.instrumentCount = MAX_INSTRUMENTS;
- drawInstrumentWindow();
- drawStatus();
- break;
- case '-':
- song.instrumentCount--;
- if(song.instrumentCount <= 0) song.instrumentCount = 1;
- if(curInstrument >= song.instrumentCount) curInstrument = song.instrumentCount - 1;
- while(curInstrument >= instrumentScroll + h1 - 2)
- {
- orderScroll++;
- }
- while(curInstrument < instrumentScroll)
- {
- instrumentScroll--;
- }
- drawInstrumentWindow();
- drawStatus();
- break;
- }
- break;
- case WIN_EFFECT:
- switch(c)
- {
- case KEY_DOWN:
- curEffect = (curEffect + 1) % 8;
- drawEffectWindow();
- break;
- case KEY_UP:
- curEffect = (curEffect - 1);
- while(curEffect < 0) curEffect += 8;
- drawEffectWindow();
- break;
- case KEY_RIGHT:
- if(song.instruments[curInstrument].effects[curEffect].enable)
- {
- song.instruments[curInstrument].effects[curEffect].effect++;
- if(song.instruments[curInstrument].effects[curEffect].effect > 0xF) song.instruments[curInstrument].effects[curEffect].effect = 0xF;
- }
- else
- {
- song.instruments[curInstrument].effects[curEffect].enable = 1;
- }
- drawEffectWindow();
- break;
- case KEY_LEFT:
- if(song.instruments[curInstrument].effects[curEffect].enable)
- {
- song.instruments[curInstrument].effects[curEffect].effect--;
- if(song.instruments[curInstrument].effects[curEffect].effect > 0xF)
- {
- song.instruments[curInstrument].effects[curEffect].effect = 0;
- song.instruments[curInstrument].effects[curEffect].enable = 0;
- }
- drawEffectWindow();
- }
- break;
- case '=':
- if(song.instruments[curInstrument].effects[curEffect].enable)
- {
- song.instruments[curInstrument].effects[curEffect].argument++;
- drawEffectWindow();
- }
- break;
- case '-':
- if(song.instruments[curInstrument].effects[curEffect].enable)
- {
- song.instruments[curInstrument].effects[curEffect].argument--;
- drawEffectWindow();
- }
- break;
- case ']':
- if(song.instruments[curInstrument].effects[curEffect].enable)
- {
- song.instruments[curInstrument].effects[curEffect].argument += 0x10;
- drawEffectWindow();
- }
- break;
- case '[':
- if(song.instruments[curInstrument].effects[curEffect].enable)
- {
- song.instruments[curInstrument].effects[curEffect].argument -= 0x10;
- drawEffectWindow();
- }
- break;
- }
- break;
- case WIN_TRACKER:
- switch(c)
- {
- case KEY_F(1):
- curOsc = 0;
- drawTrackerWindow();
- break;
- case KEY_F(2):
- curOsc = 1;
- drawTrackerWindow();
- break;
- case KEY_F(3):
- curOsc = 2;
- drawTrackerWindow();
- break;
- case KEY_F(4):
- curOsc = 3;
- drawTrackerWindow();
- break;
- case KEY_RIGHT:
- curCol++;
- if(curCol > 4)
- {
- curCol = 0;
- curOsc = (curOsc + 1) % 4;
- }
- drawTrackerWindow();
- break;
- case KEY_LEFT:
- curCol--;
- if(curCol < 0)
- {
- curCol = 4;
- curOsc--;
- if(curOsc < 0) curOsc = 3;
- }
- drawTrackerWindow();
- break;
- case KEY_DOWN:
- curFrame = (curFrame + 1) % 32;
- drawTrackerWindow();
- break;
- case KEY_UP:
- curFrame--;
- if(curFrame < 0) curFrame = 31;
- drawTrackerWindow();
- break;
- case 'p':
- playEntry(entry, curOsc);
- break;
- default:
- switch(curCol)
- {
- case 0:
- switch(c)
- {
- case KEY_BACKSPACE:
- entry->noteCommand.note = NOTE_NULL;
- break;
- case '`':
- if(entry->noteCommand.note == NOTE_OFF)
- {
- entry->noteCommand.note = NOTE_ON;
- }
- else
- {
- entry->noteCommand.note = NOTE_OFF;
- }
- break;
- case '0':
- entry->noteCommand.octave = 0;
- break;
- case '1':
- entry->noteCommand.octave = 1;
- break;
- case '2':
- entry->noteCommand.octave = 2;
- break;
- case '3':
- entry->noteCommand.octave = 3;
- break;
- case '4':
- entry->noteCommand.octave = 4;
- break;
- case '5':
- entry->noteCommand.octave = 5;
- break;
- case '6':
- entry->noteCommand.octave = 6;
- break;
- case '7':
- entry->noteCommand.octave = 7;
- break;
- case 'a':
- entry->noteCommand.note = NOTE_C;
- break;
- case 'w':
- entry->noteCommand.note = NOTE_Cs;
- break;
- case 's':
- entry->noteCommand.note = NOTE_D;
- break;
- case 'e':
- entry->noteCommand.note = NOTE_Ds;
- break;
- case 'd':
- entry->noteCommand.note = NOTE_E;
- break;
- case 'f':
- entry->noteCommand.note = NOTE_F;
- break;
- case 't':
- entry->noteCommand.note = NOTE_Fs;
- break;
- case 'g':
- entry->noteCommand.note = NOTE_G;
- break;
- case 'y':
- entry->noteCommand.note = NOTE_Gs;
- break;
- case 'h':
- entry->noteCommand.note = NOTE_A;
- break;
- case 'u':
- entry->noteCommand.note = NOTE_As;
- break;
- case 'j':
- entry->noteCommand.note = NOTE_B;
- break;
- case 'k':
- entry->noteCommand.note = NOTE_C;
- break;
- case 'r':
- entry->noteCommand.note = NOTE_RESET;
- break;
- case '=':
- if(entry->noteCommand.note == NOTE_B)
- {
- entry->noteCommand.note = NOTE_C;
- entry->noteCommand.octave = (entry->noteCommand.octave + 1) % 8;
- }
- else if(entry->noteCommand.note > 3)
- {
- entry->noteCommand.note++;
- }
- break;
- case '-':
- if(entry->noteCommand.note == 4)
- {
- entry->noteCommand.note = 15;
- if(entry->noteCommand.octave == 0)
- {
- entry->noteCommand.octave = 7;
- }
- else
- {
- entry->noteCommand.octave--;
- }
- }
- else if(entry->noteCommand.note > 4)
- {
- entry->noteCommand.note--;
- }
- break;
- case ']':
- entry->noteCommand.octave = (entry->noteCommand.octave + 1) % 8;
- break;
- case '[':
- if(entry->noteCommand.octave == 0)
- {
- entry->noteCommand.octave = 7;
- }
- else
- {
- entry->noteCommand.octave--;
- }
- break;
- }
- break;
- case 1:
- switch(c)
- {
- case KEY_BACKSPACE:
- entry->noteCommand.applyInstrument = 0;
- break;
- case '0':
- entry->noteCommand.instrument = 0;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '1':
- entry->noteCommand.instrument = 1;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '2':
- entry->noteCommand.instrument = 2;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '3':
- entry->noteCommand.instrument = 3;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '4':
- entry->noteCommand.instrument = 4;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '5':
- entry->noteCommand.instrument = 5;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '6':
- entry->noteCommand.instrument = 6;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '7':
- entry->noteCommand.instrument = 7;
- entry->noteCommand.applyInstrument = 1;
- break;
- case '=':
- entry->noteCommand.instrument = (entry->noteCommand.instrument + 1) % 8;
- break;
- case '-':
- if(entry->noteCommand.instrument == 0)
- {
- entry->noteCommand.instrument = 7;
- }
- else
- {
- entry->noteCommand.instrument--;
- }
- break;
- }
- break;
- case 2:
- switch(c)
- {
- case KEY_BACKSPACE:
- entry->noteCommand.volume = 0;
- break;
- case '0':
- entry->noteCommand.volume = 0;
- break;
- case '`':
- entry->noteCommand.volume = 31;
- break;
- case '=':
- entry->noteCommand.volume = (entry->noteCommand.volume + 1) % 32;
- break;
- case '-':
- if(entry->noteCommand.volume == 0)
- {
- entry->noteCommand.volume = 31;
- }
- else
- {
- entry->noteCommand.volume--;
- }
- break;
- case ']':
- entry->noteCommand.volume = (entry->noteCommand.volume + 0x10) % 32;
- break;
- case '[':
- if(entry->noteCommand.volume <= 0x10)
- {
- entry->noteCommand.volume = 31;
- }
- else
- {
- entry->noteCommand.volume -= 0x10;
- }
- break;
- }
- break;
- case 3:
- switch(c)
- {
- case KEY_BACKSPACE:
- entry->effect.enable = 0;
- break;
- case '0':
- entry->effect.effect = 0;
- entry->effect.enable = 1;
- break;
- case '1':
- entry->effect.effect = 1;
- entry->effect.enable = 1;
- break;
- case '2':
- entry->effect.effect = 2;
- entry->effect.enable = 1;
- break;
- case '3':
- entry->effect.effect = 3;
- entry->effect.enable = 1;
- break;
- case '4':
- entry->effect.effect = 4;
- entry->effect.enable = 1;
- break;
- case '5':
- entry->effect.effect = 5;
- entry->effect.enable = 1;
- break;
- case '6':
- entry->effect.effect = 6;
- entry->effect.enable = 1;
- break;
- case '7':
- entry->effect.effect = 7;
- entry->effect.enable = 1;
- break;
- case '8':
- entry->effect.effect = 8;
- entry->effect.enable = 1;
- break;
- case '9':
- entry->effect.effect = 9;
- entry->effect.enable = 1;
- break;
- case 'a':
- entry->effect.effect = 0xA;
- entry->effect.enable = 1;
- break;
- case 'b':
- entry->effect.effect = 0xB;
- entry->effect.enable = 1;
- break;
- case 'c':
- entry->effect.effect = 0xC;
- entry->effect.enable = 1;
- break;
- case 'd':
- entry->effect.effect = 0xD;
- entry->effect.enable = 1;
- break;
- case 'e':
- entry->effect.effect = 0xE;
- entry->effect.enable = 1;
- break;
- case 'f':
- entry->effect.effect = 0xF;
- entry->effect.enable = 1;
- break;
- case '=':
- entry->effect.effect = (entry->effect.effect + 1) % 0x10;
- entry->effect.enable = 1;
- break;
- case '-':
- if(entry->effect.effect == 0)
- {
- entry->effect.effect = 0xF;
- }
- else
- {
- entry->effect.effect--;
- }
- entry->effect.enable = 1;
- break;
- }
- break;
- case 4:
- switch(c)
- {
- case KEY_BACKSPACE:
- entry->effect.enable = 0;
- break;
- case '0':
- entry->effect.argument = 0;
- entry->effect.enable = 1;
- break;
- case '=':
- entry->effect.argument++;
- entry->effect.enable = 1;
- break;
- case '-':
- entry->effect.argument--;
- entry->effect.enable = 1;
- break;
- case ']':
- entry->effect.argument += 0x10;
- entry->effect.enable = 1;
- break;
- case '[':
- entry->effect.argument -= 0x10;
- entry->effect.enable = 1;
- break;
- }
- break;
- }
- drawTrackerWindow();
- }
- break;
- }
- }
- }
- void playInput(int c)
- {
- }
- void jamInput(int c)
- {
- }
- void editMode()
- {
- curMode = MODE_EDIT;
- drawWindows();
- drawStatus();
- }
- void playMode()
- {
- curMode = MODE_PLAY;
- drawWindows();
- drawStatus();
- }
- void jamMode()
- {
- curMode = MODE_JAM;
- drawWindows();
- drawStatus();
- }
- void broadcastEffect(Effect effect)
- {
- sendEffectCommand(&effect, 0);
- sendEffectCommand(&effect, 1);
- sendEffectCommand(&effect, 2);
- sendEffectCommand(&effect, 3);
- }
- void resetEffects()
- {
- broadcastEffect(newEffect(0x0, 0x00));
- broadcastEffect(newEffect(0x1, 0x00));
- broadcastEffect(newEffect(0x2, 0x00));
- broadcastEffect(newEffect(0x3, 0x00));
- broadcastEffect(newEffect(0x4, 0x00));
- broadcastEffect(newEffect(0x5, 0x00));
- broadcastEffect(newEffect(0x6, 0x7F));
- broadcastEffect(newEffect(0x7, 0x00));
- broadcastEffect(newEffect(0x8, 0x00));
- broadcastEffect(newEffect(0x9, 0x00));
- broadcastEffect(newEffect(0xA, 0x00));
- broadcastEffect(newEffect(0xB, 0x00));
- broadcastEffect(newEffect(0xC, 0xFF));
- broadcastEffect(newEffect(0xD, 0x00));
- broadcastEffect(newEffect(0xE, 0xF0));
- broadcastEffect(newEffect(0xF, 0x00));
- }
- Frame quietFrame;
- void reset()
- {
- playFrame(&quietFrame);
- resetEffects();
- }
- void playCurrentFrame()
- {
- Frame f = song.orders[curOrder].frames[curFrame];
- playFrame(&f);
- }
- void playCurrentOrder()
- {
- Order o = song.orders[curOrder];
- playOrder(&o, song.fps);
- }
- void inputLoop()
- {
- quietFrame = newQuietFrame();
- int e = 1;
- while(e)
- {
- int c = getch();
- switch(c)
- {
- case KEY_F(5):
- reset();
- break;
- case KEY_F(6):
- playNote(NOTE_C, 3, 0xFF, 0);
- playNote(NOTE_E, 4, 0xFF, 1);
- playNote(NOTE_G, 3, 0xFF, 2);
- playNote(NOTE_Bb, 4, 0xFF, 3);
- break;
- case KEY_F(7):
- playCurrentFrame();
- break;
- case KEY_F(8):
- playCurrentOrder();
- break;
- case 'q':
- e = 0;
- break;
- case KEY_RESIZE:
- initWindows();
- break;
- case '\n':
- if(curMode == MODE_PLAY)
- {
- editMode();
- }
- else
- {
- playMode();
- }
- break;
- case ' ':
- if(curMode == MODE_JAM)
- {
- editMode();
- }
- else
- {
- jamMode();
- }
- break;
- default:
- switch(curMode)
- {
- case MODE_EDIT:
- editInput(c);
- break;
- case MODE_PLAY:
- playInput(c);
- break;
- case MODE_JAM:
- jamInput(c);
- break;
- }
- }
- }
- }
- void gui()
- {
- initscr();
- clear();
- noecho();
- cbreak();
- keypad(stdscr, TRUE);
- nodelay(stdscr, TRUE);
- refresh();
- initWindows();
- inputLoop();
- endwin();
- }
- void initSong()
- {
- song.fps = 8;
- song.repeat = 0;
- song.orderCount = 1;
- song.instrumentCount = 1;
- Order orders[MAX_ORDERS];
- song.orders = orders;
- Instrument instruments[MAX_INSTRUMENTS];
- song.instruments = instruments;
- }
- int main(int argc, char *argv[])
- {
- if(argc > 1)
- {
- strcpy(serialPort, argv[1]);
- }
- else
- {
- printf("Enter full path to serial device: ");
- scanf("%s", serialPort);
- }
- if(out = fopen(serialPort, "w"))
- {
- initSong();
- reset();
- gui();
- reset();
- fclose(out);
- }
- else
- {
- printf("Unable to open file!\n");
- exit(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement