Skip to content

Commit

Permalink
Added #define for max polyphony. Minor code changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBlueStone committed Aug 26, 2023
1 parent 15c5891 commit 99a283b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
54 changes: 24 additions & 30 deletions src/inst.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>
#define _WIN32_WINNT 0x0500 // for VK_OEM_PERIOD ?????
#define WIN32_LEAN_AND_MEAN
Expand Down Expand Up @@ -39,32 +40,14 @@ static struct window_template inst_list_template = {
};

static unsigned char valid_insts[MAX_INSTRUMENTS];
static int cnote[16];
static char sustained[16] = { 0 };
static int cnote[INST_MAX_POLYPHONY];
static char sustained[INST_MAX_POLYPHONY] = { 0 };
static char sustain = FALSE;
static struct history {
struct history *prev;
struct history *next;
} channel_order[16] = {
// TODO: Construct this on tab init so the polyphony can change from one location.
{ &channel_order[15], &channel_order[1] },
{ &channel_order[0] , &channel_order[2] },
{ &channel_order[1], &channel_order[3] },
{ &channel_order[2], &channel_order[4] },
{ &channel_order[3], &channel_order[5] },
{ &channel_order[4], &channel_order[6] },
{ &channel_order[5], &channel_order[7] },
{ &channel_order[6], &channel_order[8] },
{ &channel_order[7], &channel_order[9] },
{ &channel_order[8], &channel_order[10] },
{ &channel_order[9], &channel_order[11] },
{ &channel_order[10], &channel_order[12] },
{ &channel_order[11], &channel_order[13] },
{ &channel_order[12], &channel_order[14] },
{ &channel_order[13], &channel_order[15] },
{ &channel_order[14], &channel_order[0] },
};
struct history* oldest_chan = channel_order;
} channel_order[INST_MAX_POLYPHONY] = { 0 };
static struct history* oldest_chan = channel_order;

int note_from_key(int key, BOOL shift) {
if (key == VK_OEM_PERIOD) return 0x48; // continue
Expand Down Expand Up @@ -98,11 +81,13 @@ static void set_latest_channel(int ch) {
if (&channel_order[ch] == oldest_chan) {
oldest_chan = oldest_chan->next;
} else {
// Verify channel_order items are defined. (They should also form a complete loop.)
assert(channel_order[ch].prev && channel_order[ch].next);

// Remove this item from the linked list.
if (channel_order[ch].prev && channel_order[ch].next) {
channel_order[ch].prev->next = channel_order[ch].next;
channel_order[ch].next->prev = channel_order[ch].prev;
}
channel_order[ch].prev->next = channel_order[ch].next;
channel_order[ch].next->prev = channel_order[ch].prev;

// Move it to the end of the linked list
channel_order[ch].next = oldest_chan ? oldest_chan : (oldest_chan = &channel_order[ch]);
channel_order[ch].prev = oldest_chan->prev ? oldest_chan->prev : (oldest_chan->prev = &channel_order[ch]);
Expand Down Expand Up @@ -132,21 +117,22 @@ static void sustain_on() {

static void sustain_off() {
sustain = FALSE;
for (int ch = 0; ch < 16; ch++) {
for (int ch = 0; ch < INST_MAX_POLYPHONY; ch++) {
if (sustained[ch]) {
channel_off(ch);
}
}
}

static void note_off(int note) {
for (int ch = 0; ch < 16; ch++)
for (int ch = 0; ch < INST_MAX_POLYPHONY; ch++) {
if (cnote[ch] == note) {
if (sustain)
sustained[ch] = TRUE;
else
channel_off(ch);
}
}
draw_square(note, GetStockObject(WHITE_BRUSH));
}

Expand Down Expand Up @@ -264,7 +250,13 @@ LRESULT CALLBACK InstrumentsWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
switch (uMsg) {
case WM_CREATE: {
prev_chmask = chmask;
chmask = 0xFFFF;

#if INST_MAX_POLYPHONY > 31
#error INST_MAX_POLYPHONY must be less than 32 to prevent left-shift overflowing.
#else
chmask = (1u << INST_MAX_POLYPHONY) - 1;
#endif

WPARAM fixed = (WPARAM)fixed_font();
char buf[40];

Expand Down Expand Up @@ -311,8 +303,10 @@ LRESULT CALLBACK InstrumentsWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
start_playing();
timer_speed = 0;
memset(&state.chan, 0, sizeof state.chan);
for (int ch = 0; ch < 16; ch++) {
for (int ch = 0; ch < INST_MAX_POLYPHONY; ch++) {
state.chan[ch].samp_pos = -1;
channel_order[ch].next = &channel_order[(ch + 1) % INST_MAX_POLYPHONY];
channel_order[ch].prev = &channel_order[(ch - 1 + INST_MAX_POLYPHONY) % INST_MAX_POLYPHONY];
}

// Restore the previous instrument selection
Expand Down
3 changes: 2 additions & 1 deletion src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef int BOOL;
#define TRUE 1
typedef void *HWND;
#endif
#define INST_MAX_POLYPHONY 16

// structure used for track or subroutine
// "size" does not include the ending [00] byte
Expand Down Expand Up @@ -98,7 +99,7 @@ struct song_state {
short sustain_level;
short sustain_rate;
short gain_rate;
} chan[16];
} chan[INST_MAX_POLYPHONY];
signed char transpose;
struct slider volume;
struct slider tempo;
Expand Down

0 comments on commit 99a283b

Please sign in to comment.