-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_model.cpp
145 lines (119 loc) · 2.79 KB
/
model_model.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* model_model.cpp
*
* Created on: Apr 2, 2018
* Author: MisterCavespider
*/
#include "model_model.h"
Model::Model(uint8_t size) {
this->size = size;
count = 0;
modules = new Module*[size];
streams = NULL;
connections = NULL;
connectionCount = 0;
baked = NULL;
sgtl5000 = AudioControlSGTL5000();
}
int8_t Model::usesInput(Module *m)
{
uint8_t i,c=0;
for(i = 0; i < m->getInputCount(); i++)
{
if(m->getInputs()[i] > 0) c++;
}
return c;
}
int8_t Model::usesOutput(Module *m)
{
uint8_t i,c=0;
for(i = 0; i < m->getOutputCount() * m->getParalsCount(); i++)
{
if(m->getOutputs()[i] > 0) c++;
}
return c;
}
int8_t Model::hasInput(Module *t, Module *m)
{
for(uint8_t i = 0; i < t->getInputCount(); i++)
{
if(t->getInputs()[i] == m->getId()) return i;
}
return -1;
}
void Model::updateForValue(Module *m, uint8_t i)
{
m->updateForValue(&streams[m->getId()-1], i);
}
void Model::deleteAllAudio()
{
for(int i = 0; i < size; i++)
{
if(modules[i] != NULL) delete modules[i];
}
delete[] modules;
for(int i = 0; i < 2*size; i++)
{
if(connections[i] != NULL) delete connections[i];
}
delete[] connections;
connectionCount = 0;
}
result_t Model::bakeAudioFor(uint8_t id)
{
if(baked[id-1]) return result_t::SUCCESS;
uint8_t i;
Module *m = modules[id-1];
if(m == NULL) return result_t::NULL_POINTER;
m->spStream(&streams[id-1]);
baked[id-1] = true;
for(i = 0; i < m->getInputCount(); i++)
{
uint8_t tIdx = m->getInputs()[i];
if(tIdx == 0) break;
if(!baked[tIdx-1])
{
bakeAudioFor(tIdx);
}
}
for(i = 0; i < m->getOutputCount() * m->getParalsCount(); i++)
{
// FIXME: this is really janky
uint8_t tIdx = m->getOutputs()[i];
if(tIdx >= 4 || tIdx == 0) break;
Module *t = modules[tIdx-1];
uint8_t in = hasInput(t, m);
if(in >= 0)
{
AudioStream *fUse, *tUse;
int fPort, tPort;
bakeAudioFor(t->getId());
m->spConnOut(&streams[id-1], &fUse, &fPort, i);
t->spConnIn(&streams[t->getId()-1], &tUse, &tPort, in);
connections[connectionCount] = new AudioConnection(*fUse, fPort, *tUse, tPort);
connectionCount++;
}
}
return SUCCESS;
}
result_t Model::bakeAudioFrom(uint8_t id)
{
if(modules[id-1]->getInputCount() > 0) return result_t::INVALID_ARGUMENT;
if(streams != NULL || connections != NULL) deleteAllAudio();
if(baked != NULL) delete[] baked;
streams = new AudioStream*[size]; // TODO: will not work for modules with multiple streams
connections = new AudioConnection*[2*size]; // Assumes an average of 2 connections per module
baked = new bool[size];
for(int i = 0; i < size; i++){baked[i] = false;}
result_t r = bakeAudioFor(id);
if(r < 0)
{
return r;
}
sgtl5000.enable();
sgtl5000.inputSelect(AUDIO_INPUT_LINEIN);
sgtl5000.volume(0.5);
sgtl5000.adcHighPassFilterDisable();
return SUCCESS;
}
Model::~Model() {}