The Tooba
 All Data Structures Namespaces Files Functions Variables Macros Pages
key.cpp
Go to the documentation of this file.
1 #include "common.h"
2 #include "key.h"
3 
4 extern uint8_t read_key(uint32_t);
5 
6 static uint8_t next_voice_to_assign = 0;
7 
8 uint32_t Key::check(void) {
9  uint32_t i, j, found;
10  uint32_t Y = read_key(id);
11  if (Y > THRESHOLD) {
12  if (state) {
13  count = 0;
14  } else {
15  if (count < KEYDOWN_COUNT) {
16  count++;
17  if (count == KEYDOWN_COUNT) {
18  // this is a keydown event
19  state = 1;
20  count = 0;
21 
22  // Look for the least recently used voice whose state is zero.
23  for (i = next_voice_to_assign, j = 0, found = 0;
24  j < NUM_VOICES;
25  j++, i = (i + 1) % NUM_VOICES) {
26  if (v[i].adsr.state() == 0) {
27  found = 1;
28  break;
29  }
30  }
31  // If none is found, just grab the least recently used voice.
32  if (!found)
33  i = next_voice_to_assign;
34 
35  next_voice_to_assign = (i + 1) % NUM_VOICES;
36 
37  // does some other key already have this voice? If so, remove
38  // the voice from that other key
39  for (j = 0; j < NUM_KEYS; j++)
40  if (j != id && keyboard[j]->voice == &v[i]) {
41  keyboard[j]->voice = NULL;
42  }
43 
44  voice = &v[i];
46  voice->keydown(1);
47  }
48  }
49  }
50  } else {
51  if (!state) {
52  count = 0;
53  } else {
54  if (count < KEYDOWN_COUNT) {
55  count++;
56  if (count == KEYDOWN_COUNT) {
57  // this is a keyup event
58  state = 0;
59  count = 0;
60  if (voice != NULL) {
61  voice->keydown(0);
62  voice = NULL;
63  }
64  }
65  }
66  }
67  }
68  return Y;
69 }
tuple adsr
Definition: testit.py:74
void setfreq(float f)
Definition: voice.h:152
float pitch
Definition: key.h:29
uint8_t read_key(uint32_t)
Definition: stub.cpp:9
Voice v[NUM_VOICES]
Definition: stub.cpp:6
uint32_t check(void)
Definition: key.cpp:8
uint32_t count
Definition: key.h:24
#define KEYDOWN_COUNT
Definition: common.h:37
uint32_t state
Definition: key.h:20
#define NUM_KEYS
Definition: common.h:30
Voice * voice
Definition: key.h:34
#define NUM_VOICES
Definition: common.h:32
#define NULL
Definition: common.h:24
#define THRESHOLD
Definition: common.h:35
void keydown(uint32_t down)
Definition: voice.h:157
Key * keyboard[NUM_KEYS]
Definition: stub.cpp:7