Skip to content

Commit

Permalink
save to MIDI file
Browse files Browse the repository at this point in the history
  • Loading branch information
HackerFoo committed Dec 11, 2020
1 parent 4305b2f commit 6ffff4b
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/midipush
/.gen/
*~
/midipush.mid
/midipush.state
/record/
2 changes: 1 addition & 1 deletion dtask/tools/generate_task_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def generate_header():

# define id macros
for (task, _) in tasks:
f.write('#define {} 0x{:x}\n'.format(task.upper(), dtask_bit(id)))
f.write('#define {} 0x{:x}ull\n'.format(task.upper(), dtask_bit(id)))
f.write('#define {}_ID {:d}\n'.format(task.upper(), id))
ids[task] = id
id = id + 1
Expand Down
81 changes: 46 additions & 35 deletions midi_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@
#include "midi_tasks.h"
#endif

#define BEATS_PER_PAGE 24
#define PAGES 64
#define BEATS (BEATS_PER_PAGE * PAGES)
#define BANKS 26

#define PAD_RED 5
#define PAD_YELLOW 13
#define PAD_GREEN 21
Expand Down Expand Up @@ -68,15 +63,20 @@ DTASK(tick, long long) {
}
}

DTASK(external_tick, bool) {
return true;
}

unsigned int page_beat(unsigned int b, const set_page_t *p) {
unsigned int current_page = b / BEATS_PER_PAGE;
return ((current_page & p->keep) | p->val) * BEATS_PER_PAGE;
}

DTASK(beat, struct { unsigned int then, now; }) {
DREF(beat)->then = DREF(beat)->now;
if((state->events & TICK) && *DREF(playing)) {
(void)*DREF(tick);
if((state->events & (TICK | EXTERNAL_TICK)) && *DREF(playing)) {
(void)DREF(tick);
(void)DREF(external_tick);
DREF(beat)->now = MOD_INC(DREF(beat)->then, BEATS, 1);
}
if(state->events & SHUTTLE) {
Expand Down Expand Up @@ -602,6 +602,26 @@ DTASK(playback, struct { vec128b played[16]; }) {
bool changed = false;

if(*DREF(playing)) {
// setup channels
if(state->events & PLAYING) {
COUNTDOWN(c, 16) {
write_synth((seg_t) { // bank LSB
.n = 3,
.s = (char [3]) { 0xb0 | c, 32,
DREF_PASS(program)->bank[c] }
});
write_synth((seg_t) { // program
.n = 2,
.s = (char [2]) { 0xc0 | c,
DREF_PASS(program)->program[c] }
});
write_synth((seg_t) { // volume
.n = 3,
.s = (char [3]) { 0xb0 | c, 7,
DREF_PASS(volume)->arr[c] }
});
}
}
map_iterator it = map_iterator_begin(DREF(record)->events, beat);
pair_t *p = map_find_iter(&it);
while(p) {
Expand Down Expand Up @@ -637,6 +657,11 @@ DTASK(playback, struct { vec128b played[16]; }) {
}
}
}
} else if(state->events & PLAYING) {
// clear notes
COUNTDOWN(c, 16) {
all_notes_off(c);
}
}
return changed;
}
Expand Down Expand Up @@ -736,37 +761,10 @@ DTASK_ENABLE(playing) {

DTASK(playing, bool) {
const control_change_t *cc = DREF(control_change);
if(!(state->events & CONTROL_CHANGE)) return true; // allow external triggering
if(cc->control == 85 && cc->value) {
*DREF(playing) = !*DREF(playing);
send_msg(0xb0, 85, *DREF(playing) ? 1 : 2);
if(*DREF(playing)) { // send programs on play
COUNTDOWN(c, 16) {
write_synth((seg_t) { // bank LSB
.n = 3,
.s = (char [3]) { 0xb0 | c, 32,
DREF_PASS(program)->bank[c] }
});
write_synth((seg_t) { // program
.n = 2,
.s = (char [2]) { 0xc0 | c,
DREF_PASS(program)->program[c] }
});
write_synth((seg_t) { // volume
.n = 3,
.s = (char [3]) { 0xb0 | c, 7,
DREF_PASS(volume)->arr[c] }
});
}
} else {
COUNTDOWN(c, 16) {
all_notes_off(c);
write_synth((seg_t) { // volume
.n = 3,
.s = (char [3]) { 0xb0 | c, 7,
DREF_PASS(volume)->arr[c] }
});
}
}
return true;
}
return false;
Expand Down Expand Up @@ -929,6 +927,19 @@ DTASK(poweroff, bool) {
return false;
}

DTASK_ENABLE(save) {
send_msg(0xb0, 53, 1);
}

DTASK(save, bool) {
const control_change_t *cc = DREF(control_change);
if(cc->control == 53 && cc->value) {
*DREF(save) = true;
return true;
}
return false;
}

// Local Variables:
// eval: (add-to-list 'imenu-generic-expression '("Task" "^DTASK(\\([a-zA-Z0-9_]+\\),.*$" 1))
// End:
Loading

0 comments on commit 6ffff4b

Please sign in to comment.