-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoteset.cpp
More file actions
112 lines (99 loc) · 1.99 KB
/
Copy pathnoteset.cpp
File metadata and controls
112 lines (99 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdlib.h>
#include <stdio.h>
#include <Arduino.h>
#include "Sharp.h"
#include "noteset.h"
noteset::noteset(int n):size(n), count(0), heldcount(0)
{
notes = new noteinfo[n];
}
noteset::~noteset()
{
if (notes)
delete notes;
}
void noteset::addnote(noteinfo note)
{
if (count == size)
return; // we're full
int i = findnote(note.pitch);
if (i >= 0) // note is already there - remove then update
removenote(note.pitch);
notes[count] = note;
count++;
if (!note.latched) // if not latched it was a genuine held
heldcount++;
}
int noteset::findnote(unsigned char pitch)
{
if (pitch == 0 || pitch > 127)
return -1;
int i = 0;
while (i < count)
{
if (notes[i].pitch == pitch)
return i;
else
i++;
}
return -1;
}
void noteset::removenote(unsigned char pitch)
{
int i = findnote(pitch);
if (i < 0)
return; // nothing to remove
noteinfo removednote = notes[i];
for (int j = i; j < size - 1; ++j)
{
if (notes[j].pitch == 0)
break;
notes[j] = notes[j + 1]; // shuffle them back
}
count--;
if (!removednote.latched) // if not latched it was a genuine held
heldcount--;
}
void noteset::clearnote(noteinfo *note)
{
note->pitch = 0;
note->bypass = 0;
note->channel = 0;
note->latched = 0;
}
void noteset::clear()
{
count = heldcount = 0;
for (int i = 0; i < count; ++i)
clearnote(¬es[i]);
}
noteinfo *noteset::getnote(int i)
{
if (i < 0 || i >= count)
return NULL;
return ¬es[i];
}
noteset *noteset::copy()
{
noteset *result = new noteset(size);
for (int i = 0; i < count; ++i)
{
result->addnote(*getnote(i));
}
return result;
}
noteinfo *noteset::getrandomnote()
{
int i = (int)(1.0f * count * rand() / RAND_MAX);
return ¬es[i];
}
int compare(const void* pa, const void* pb)
{
noteinfo a = *((noteinfo *)pa);
noteinfo b = *((noteinfo *)pb);
return (a.pitch > b.pitch) - (a.pitch < b.pitch);
}
void noteset::sort()
{
qsort(notes, count, sizeof(noteinfo), compare);
}