I'm sad because I thought I had it figured out. I am hoping someone can point out what I'm doing wrong.
this code has 5 mics listening and cycling through five 30 value 1-D arrays until an amplitude threshold is crossed, then it fills a 130 row x 5 column, two dimensional array starting at the 30th row index. Then, when it reaches the 129th row index the algorithm starts filling the first 30 rows in the 2-D array from the stored 1-D arrays.
the whole purpose being so that I can send 130 data points per mic, with historical values prior to the sound triggering event (I'm doing this to keep the sound event in the middle of the data set.)
here's the code:
It writes a 0 value if the first 16 bit TDM value wasn't read.
the code works but it's buggy, for example, for some reason the first 30 rows are usually always solid zeros but varies (I thought I was very careful about the array indices and the for loop executions, but I must have messed up somewhere.) and it prints a lot of zero values throughout (a sign that my error checking method to show when the first (MSB) half of each mics 24 bit value wasn't available to read when it checked)
I currently only have 3 mics connected as can be seen in my sample serial printout:
and this is what that looks like:
I have a couple of suspicions of what is wrong, I think I need to handle the reading of the queue in a better way(any sugestions?), and I think I may have screwed up the transferring of the historical values to the final array being sent.
I know you guys have been generous with help already, but I could really use some more help to figure this out.
this code has 5 mics listening and cycling through five 30 value 1-D arrays until an amplitude threshold is crossed, then it fills a 130 row x 5 column, two dimensional array starting at the 30th row index. Then, when it reaches the 129th row index the algorithm starts filling the first 30 rows in the 2-D array from the stored 1-D arrays.
the whole purpose being so that I can send 130 data points per mic, with historical values prior to the sound triggering event (I'm doing this to keep the sound event in the middle of the data set.)
here's the code:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
// GUItool: begin automatically generated code
AudioInputTDM tdm1; //xy=172,228
AudioRecordQueue queue9; //xy=428,202
AudioRecordQueue queue7; //xy=430,159
AudioRecordQueue queue5; //xy=434,117
AudioRecordQueue queue1; //xy=439,34
AudioRecordQueue queue3; //xy=439,78
AudioRecordQueue queue6; //xy=554,118
AudioRecordQueue queue8; //xy=554,159
AudioRecordQueue queue10; //xy=554,201
AudioRecordQueue queue2; //xy=559,35
AudioRecordQueue queue4; //xy=559,77
AudioConnection patchCord1(tdm1, 0, queue1, 0);
AudioConnection patchCord2(tdm1, 1, queue2, 0);
AudioConnection patchCord3(tdm1, 2, queue3, 0);
AudioConnection patchCord4(tdm1, 3, queue4, 0);
AudioConnection patchCord5(tdm1, 4, queue5, 0);
AudioConnection patchCord6(tdm1, 5, queue6, 0);
AudioConnection patchCord7(tdm1, 6, queue7, 0);
AudioConnection patchCord8(tdm1, 7, queue8, 0);
AudioConnection patchCord9(tdm1, 8, queue9, 0);
AudioConnection patchCord10(tdm1, 9, queue10, 0);
// GUItool: end automatically generated code
int16_t* spot1 ; //I don't know what I'm doing - Greg
int16_t* spot2 ;
int16_t* spot3 ;
int16_t* spot4 ;
int16_t* spot5 ;
int16_t* spot6 ;
int16_t* spot7 ;
int16_t* spot8 ;
int16_t* spot9 ;
int16_t* spot10 ;
int sig1;
int sig2;
int sig3;
int sig4;
int sig5;
int rows=0;
bool sp1 = false;
bool sp3 = false;
bool sp5 = false;
bool sp7 = false;
bool sp9 = false;
bool newl;
bool tripped = false;
bool filling = false;
long thresh=50000;
long lastThresh;
long mic1[100];
long mic2[100];
long mic3[100];
long mic4[100];
long mic5[100];
long sendMics[130][5];
byte ndex=0;
byte sendex=30;
byte row=0;
byte indexWrap;
byte indexTailLength;
void setup()
{
// << nothing before the first delay will be printed to the serial
Serial.begin(9600);
AudioMemory(256);
queue1.begin();//I thought I should enable
queue2.begin();
queue3.begin();
queue4.begin();
queue5.begin();
queue6.begin();
queue7.begin();
queue8.begin();
queue9.begin();
queue10.begin();
}
void loop()
{
if(queue1.available() > 0)
{
spot1=queue1.readBuffer();
queue1.freeBuffer();
sp1 = true;
}
if(queue2.available() > 0)
{
spot2=queue2.readBuffer();
queue2.freeBuffer();
if (sp1)
{
sig1 = spot1[0]*256 + spot2[0]/256;
if (filling)
{
sendMics[sendex][0]=sig1;
}
else
{
mic1[ndex]=sig1;
}
sp1=false;
}
else
{
sig1 = 0;
if (filling)
{
sendMics[sendex][0]=sig1;
}
else
{
mic1[ndex]=sig1;
}
}
}
if(queue3.available() > 0)
{
spot3=queue3.readBuffer();
queue3.freeBuffer();
sp3 = true;
}
if(queue4.available() > 0)
{
spot4=queue4.readBuffer();
queue4.freeBuffer();
if (sp3)
{
sig2 = spot3[0]*256 + spot4[0]/256;
if (filling)
{
sendMics[sendex][1]=sig2;
}
else
{
mic2[ndex]=sig2;
}
sp3=false;
}
else
{
sig2 = 0;
if (filling)
{
sendMics[sendex][1]=sig2;
}
else
{
mic2[ndex]=sig2;
}
}
}
if(queue5.available() > 0)
{
spot5=queue5.readBuffer();
queue5.freeBuffer();
sp5 = true;
}
if(queue6.available() > 0)
{
spot6=queue6.readBuffer();
queue6.freeBuffer();
if (sp5)
{
sig3 = spot5[0]*256 + spot6[0]/256;
if (filling)
{
sendMics[sendex][2]=sig3;
}
else
{
mic3[ndex]=sig3;
}
sp5=false;
}
else
{
sig3 = 0;
if (filling)
{
sendMics[sendex][2]=sig3;
}
else
{
mic3[ndex]=sig3;
}
}
}
if(queue7.available() > 0)
{
spot7=queue7.readBuffer();
queue7.freeBuffer();
sp7 = true;
}
if(queue8.available() > 0)
{
spot8=queue8.readBuffer();
queue8.freeBuffer();
if (sp7)
{
sig5 = spot7[0]*256 + spot8[0]/256;
if (filling)
{
sendMics[sendex][3]=sig4;
}
else
{
mic4[ndex]=sig4;
}
sp7=false;
}
else
{
sig4 = 0;
if (filling)
{
sendMics[sendex][3]=sig4;
}
else
{
mic4[ndex]=sig4;
}
}
}
if(queue9.available() > 0)
{
spot9=queue9.readBuffer();
queue9.freeBuffer();
sp9 = true;
}
if(queue10.available() > 0)
{
spot10=queue10.readBuffer();
queue10.freeBuffer();
if (sp9)
{
sig5 = spot9[0]*256 + spot10[0]/256;
if (filling)
{
sendMics[sendex][4]=sig5;
sendex++;
}
else
{
mic5[ndex]=sig5;
ndex++;
}
sp9=false;
}
else
{
sig5 = 0;
if (filling)
{
sendMics[sendex][4]=sig5;
sendex++;
}
else
{
mic5[ndex]=sig5;
ndex++;
}
}
}
if(ndex>=30)
{
ndex=0;
}
if (((sig1 >= thresh || sig1 <= -thresh) || (sig2 >= thresh || sig2 <= -thresh) || (sig3 >= thresh || sig3 <= -thresh) || (sig4 >= thresh || sig4 <= -thresh) || (sig5 >= thresh || sig5 <= -thresh)) && !filling)
{
tripped = true;
}
else
{
if (lastThresh>thresh)
{
//Serial.println(thresh);
lastThresh=thresh;
}
}
if (tripped)
{
indexWrap = ndex; //indexWrap is the indice of the oldest value in the "history arrays" (mic1,mic2,...)
indexTailLength = 29-ndex; //the remaining number of indices(if any) in the history arrays.
filling = true;
tripped = false;
}
if (filling)
{
if(sendex > 129)
{
for (row=0; row <= indexTailLength; row++)
{
sendMics[row][0]=mic1[row+indexWrap];
sendMics[row][1]=mic2[row+indexWrap];
sendMics[row][2]=mic3[row+indexWrap];
sendMics[row][3]=mic4[row+indexWrap];
sendMics[row][4]=mic5[row+indexWrap];
}
for (rows = 0; rows <= indexWrap; rows++)
{
sendMics[row+indexTailLength+1][0]=mic1[row];
sendMics[row+indexTailLength+1][1]=mic2[row];
sendMics[row+indexTailLength+1][2]=mic3[row];
sendMics[row+indexTailLength+1][3]=mic4[row];
sendMics[row+indexTailLength+1][4]=mic5[row];
}
filling=false;
sendex=30;
Serial.printf("indexWrap = %d",indexWrap);
Serial.println();
for (row = 0; row < 130; row++)
{
Serial.print(row);
Serial.print(" ");
for(int columns=0; columns<5; columns++)
{
Serial.print(sendMics[row][columns]);
Serial.print(" ");
delay(1);
}
Serial.println();
}
}
}
}
the code works but it's buggy, for example, for some reason the first 30 rows are usually always solid zeros but varies (I thought I was very careful about the array indices and the for loop executions, but I must have messed up somewhere.) and it prints a lot of zero values throughout (a sign that my error checking method to show when the first (MSB) half of each mics 24 bit value wasn't available to read when it checked)
I currently only have 3 mics connected as can be seen in my sample serial printout:
Code:
indexWrap = 8
0 -34082 -63356 -49581 0 0
1 2230 1212 2675 0 0
2 1057 1330 462 0 0
3 1024 2051 1138 0 0
4 1974 710 1145 0 0
5 -88 1680 638 0 0
6 -2026 -3696 -1596 0 0
7 -712 -2100 -1746 0 0
8 1472 -1816 -2310 0 0
9 892 91 1310 0 0
10 -598 80 845 0 0
11 -1288 656 -387 0 0
12 565 -312 0 0 0
13 13440 3142 4902 0 0
14 1828 746 -106 0 0
15 4074 14628 12720 0 0
16 -2832 -1517 -2174 0 0
17 13022 9090 10765 0 0
18 -17978 -16561 -9950 0 0
19 10093 2500 7862 0 0
20 11608 2704 14318 0 0
21 -200 -796 -35120 0 0
22 0 0 0 0 0
23 0 0 0 0 0
24 -572 880 -467 0 0
25 0 0 0 0 0
26 0 0 0 0 0
27 0 0 0 0 0
28 0 0 0 0 0
29 0 0 0 0 0
30 43420 46518 35222 0 0
31 21598 28446 -3318 0 0
32 -11994 -24322 -7829 0 0
33 55166 30588 42670 0 0
34 -24528 -18577 -41796 0 0
35 6312 4643 -2168 0 0
36 55228 69576 72960 0 0
37 -119346 -125024 -121546 0 0
38 100840 92015 92483 0 0
39 -30705 -11664 -22622 0 0
40 4550 -1022 -382 0 0
41 80306 74720 87645 0 0
42 -69331 -71316 -71984 0 0
43 32375 34348 38808 0 0
44 1828 746 -106 0 0
45 -59593 -85144 -70809 0 0
46 28488 22768 41332 0 0
47 -10981 -10600 -18984 0 0
48 -1764 -4062 -4362 0 0
49 40024 73757 39422 0 0
50 0 -47704 -33858 0 0
51 -2274 2666 14752 0 0
52 -5444 4762 -3269 0 0
53 -13906 -26828 -9174 0 0
54 -2406 -1208 724 0 0
55 0 -896 2132 0 0
56 -128 787 4030 0 0
57 -17108 -18230 -12654 0 0
58 13924 11150 8991 0 0
59 -152963 0 11109 0 0
60 -22796 -28044 -16508 0 0
61 37782 38802 39256 0 0
62 -39577 -35190 -34443 0 0
63 -84862 -78146 -81607 0 0
64 0 53862 41406 0 0
65 758 -804 -56205 0 0
66 44820 40368 43502 0 0
67 922 716 1082 0 0
68 -44768 -47732 -41760 0 0
69 58002 65256 62279 0 0
70 -32808 -36787 -40480 0 0
71 2654 2609 2814 0 0
72 -6410 -11098 -4670 0 0
73 -43183 -56990 -57162 0 0
74 8248 11520 14500 0 0
75 36450 44829 38552 0 0
76 -15220 -15742 -14806 0 0
77 9672 14657 14164 0 0
78 18718 23504 24096 0 0
79 -30036 -37970 -34124 0 0
80 -1727 -2228 0 0 0
81 15655 12188 14403 0 0
82 -20182 -24468 -18862 0 0
83 17165 22922 14048 0 0
84 -17774 -19147 -13708 0 0
85 -2279 -9674 0 0 0
86 11402 21557 12778 0 0
87 -13547 -19629 -16992 0 0
88 3724 6884 5944 0 0
89 -376 617 -672 0 0
90 0 -5249 -3840 0 0
91 4688 6312 2948 0 0
92 -7352 -4829 -6750 0 0
93 -389 -4434 -971 0 0
94 2200 2879 3542 0 0
95 0 -1796 -2174 0 0
96 -3104 -2744 -2908 0 0
97 4943 5374 4254 0 0
98 -1197 -694 -1592 0 0
99 -2762 2081 1291 0 0
100 -953 1184 -438 0 0
101 540 -2368 166 0 0
102 254 -1169 -188 0 0
103 1268 1514 1790 0 0
104 0 -1560 633 0 0
105 -1588 -1646 -2032 0 0
106 -3828 -2576 -2463 0 0
107 -147 -2 -431 0 0
108 -544 0 2355 0 0
109 -3082 -3920 -2098 0 0
110 496 844 17 0 0
111 -768 932 799 0 0
112 -1177 -1298 -653 0 0
113 -878 0 972 0 0
114 -556 1486 -46 0 0
115 176 -876 -1267 0 0
116 2306 3324 2377 0 0
117 3314 0 1004 0 0
118 910 190 968 0 0
119 868 -526 440 0 0
120 -64 2702 1039 0 0
121 0 0 0 0 0
122 4264 3430 1954 0 0
123 1033 1097 -42 0 0
124 -202 -501 -512 0 0
125 285 -434 1110 0 0
126 -522 0 0 0 0
127 390 -618 36 0 0
128 462 799 230 0 0
129 -412 -1920 -1880 0 0
I have a couple of suspicions of what is wrong, I think I need to handle the reading of the queue in a better way(any sugestions?), and I think I may have screwed up the transferring of the historical values to the final array being sent.
I know you guys have been generous with help already, but I could really use some more help to figure this out.