Debian Codegraph @

biosig4c++/t210/sopen_acqbiopac.c

raw symbols SCIP
1 /*
2 sandbox is used for development and under constraction work
3 The functions here are either under construction or experimental.
4 The functions will be either fixed, then they are moved to another place;
5 or the functions are discarded. Do not rely on the interface in this function
6
7 Copyright (C) 2025 Alois Schloegl <[email protected]>
8 This file is part of the "BioSig for C/C++" repository
9 (biosig4c++) at http://biosig.sf.net/
10
11 BioSig is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 3
14 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*****
26 This implementation is based on the following references:
27 [1] http://www.biopac.com/Manuals/app_pdf/app156.pdf
28 [2] https://github.com/uwmadison-chm/bioread/raw/2e9a312342210c394b9bd3a6dbc220e1e9e89851/notes/acqknowledge_file_structure.pdf
29 *****/
30
31 #define _GNU_SOURCE
32 #include <assert.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <iconv.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "../biosig.h"
40
41 #define min(a,b) (((a) < (b)) ? (a) : (b))
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 void sopen_acqbiopac_read(HDRTYPE* hdr) {
49
50
51 size_t SectionTable[37];
52 memset(SectionTable,0,sizeof(SectionTable));
53 SectionTable[0] = 0;
54
55 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s (HeadLen=%d)\n", __FILE__, __LINE__, __func__, hdr->HeadLen);
56
57 void *ptr = NULL;
58 // revert (ab-) use of hdr->FILE.POS in gdffiletype
59 size_t count = hdr->HeadLen; // bytes of data actually read intp hdr->AS.Header
60 uint32_t compressed=0;
61 hdr->FILE.POS = 0;
62 if ( hdr->FILE.LittleEndian ) {
63 hdr->HeadLen = leu32p(hdr->AS.Header+6);
64 hdr->NS = lei16p(hdr->AS.Header+10);
65 hdr->SampleRate = 1000.0/lef64p(hdr->AS.Header+16);
66 compressed = leu32p(hdr->AS.Header+822);
67 }
68 else {
69 hdr->HeadLen = beu32p(hdr->AS.Header+6);
70 hdr->NS = bei16p(hdr->AS.Header+10);
71 hdr->SampleRate = 1000.0/bef64p(hdr->AS.Header+16);
72 compressed = beu32p(hdr->AS.Header+822);
73 }
74 SectionTable[1] = hdr->HeadLen;
75
76 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s (HeadLen=%d) compressed=0x%x \n", __FILE__, __LINE__, __func__, hdr->HeadLen,compressed);
77
78 if (hdr->VERSION>=124.0) {
79 // Block Seq 2: Unknown
80 ptr = hdr->AS.Header + hdr->HeadLen;
81 uint32_t u32=( hdr->FILE.LittleEndian ? leu32p(ptr) : beu32p(ptr) );
82 if (VERBOSE_LEVEL>8) fprintf(stdout,"Warning: %s (line %i) %s(...) v%f HeadLen=%d,0x%x) Block Seq=2,Type=unknown does not have 40 but %d bytes\n", \
83 __FILE__, __LINE__, __func__, hdr->VERSION, hdr->HeadLen, hdr->HeadLen, u32);
84 if ( u32 != 40 ) {
85 biosigERROR(hdr, B4C_UNSPECIFIC_ERROR, "SOPEN(ACQ-READ): unsupported file format version - needs fixing of code.");
86 return;
87 }
88 hdr->HeadLen+=40;
89 }
90 SectionTable[2] = hdr->HeadLen;
91
92 /* defined in http://biopac.com/AppNotes/app156FileFormat/FileFormat.htm */
93
94 // Block Seq 3: channel header
95 // define channel specific header information
96 hdr->NRec = 1;
97 hdr->SPR = 1;
98 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
99
100 if (hdr->HeadLen+4 > count) {
101 hdr->AS.Header = (uint8_t*) realloc(hdr->AS.Header, hdr->HeadLen+4);
102 count += ifread(hdr->AS.Header+count, 1, hdr->HeadLen+4-count, hdr);
103 }
104 ptr = hdr->AS.Header+hdr->HeadLen;
105 uint32_t lChanHeaderLen = (hdr->FILE.LittleEndian ? leu32p(ptr) : beu32p(ptr));
106 hdr->HeadLen += lChanHeaderLen * hdr->NS;
107 SectionTable[3] = hdr->HeadLen;
108
109 // Block Seq 4: "foreign data section"
110 ptr = hdr->AS.Header+hdr->HeadLen;
111 if (hdr->VERSION > 60.0)
112 hdr->HeadLen += hdr->FILE.LittleEndian ? leu32p(ptr) : beu32p(ptr);
113 else if (hdr->VERSION > 35.0)
114 hdr->HeadLen += hdr->FILE.LittleEndian ? leu16p(ptr) : beu16p(ptr);
115 SectionTable[4] = hdr->HeadLen;
116
117 // Block Seq 5: channel type header
118 hdr->HeadLen += 4*hdr->NS;
119 SectionTable[5] = hdr->HeadLen;
120 if (hdr->HeadLen+4 > count) {
121 hdr->AS.Header = (uint8_t*) realloc(hdr->AS.Header, hdr->HeadLen+4);
122 count += ifread(hdr->AS.Header+count, 1, hdr->HeadLen+4-count, hdr);
123 }
124
125 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s(...): Version=%g HeadLen=%d compressed=%d\n", __FILE__, __LINE__, __func__, hdr->VERSION, hdr->HeadLen, compressed);
126
127 // Block Seq 6: channel data
128 hdr->AS.bpb = 0;
129
130 uint32_t minspr = 0xffffffff;
131 int nVarSampleDividerOffset = -1;
132 if (hdr->VERSION>=38)
133 nVarSampleDividerOffset=250;
134 else if (hdr->VERSION>=60)
135 nVarSampleDividerOffset=152;
136
137 uint8_t *Header2 = hdr->AS.Header+SectionTable[2];
138 uint8_t *Header3 = hdr->AS.Header+SectionTable[4];
139 for (int k = 0; k < hdr->NS; k++) {
140 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
141
142 hc->LeadIdCode = 0;
143 hc->Transducer[0] = '\0';
144 hc->XYZ[0] = 0.0;
145 hc->XYZ[1] = 0.0;
146 hc->XYZ[2] = 0.0;
147 hc->TOffset = 0.0;
148 hc->HighPass = 0.0/0.0;
149 hc->LowPass = 0.0/0.0;
150 hc->Notch = 0.0/0.0;
151
152 //CHAN = leu16p(Header2+4);
153
154 int len=min(MAX_LENGTH_LABEL,40);
155 strncpy(hc->Label,(char*)Header2+6,len);
156 hc->Label[len]=0;
157
158 char tmp[21];
159 strncpy(tmp,(char*)Header2+68,20); tmp[20]=0;
160 /* ACQ uses none-standard way of encoding physical units
161 Convert to ISO/IEEE 11073-10101 */
162 if (!strcmp(tmp,"Volts"))
163 hc->PhysDimCode = 4256;
164 else if (!strcmp(tmp,"Seconds"))
165 hc->PhysDimCode = 2176;
166 else if (!strcmp(tmp,"deg C"))
167 hc->PhysDimCode = 6048;
168 else if (!strcmp(tmp,"microsiemen"))
169 hc->PhysDimCode = 8307;
170 else
171 hc->PhysDimCode = PhysDimCode(tmp);
172
173 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s..) [%s]<%s>\n", __FILE__, __LINE__, __func__,hc->Label,tmp);
174
175 hc->OnOff = 1;
176 hc->SPR = 1;
177 typeof(hc->SPR) spr = 1;
178 size_t tmp64;
179 uint32_t next, BufLength;
180 uint32_t u32;
181 uint16_t nVarSampleDivider=1;
182 if ( hdr->FILE.LittleEndian ) {
183 next = leu32p(Header2);
184 BufLength = leu32p(Header2+88);
185 hc->Cal = lef64p(Header2+92);
186 hc->Off = lef64p(Header2+100);
187 if (hdr->VERSION>=38) nVarSampleDivider = leu16p(Header2 + nVarSampleDividerOffset); // used here as Divider
188 u32 = leu32p(Header3); // bug in the documentation [1,2]
189 }
190 else {
191 next = beu32p(Header2);
192 BufLength = beu32p(Header2+88);
193 hc->Cal = bef64p(Header2+92);
194 hc->Off = bef64p(Header2+100);
195 if (hdr->VERSION>=38) nVarSampleDivider = beu16p(Header2 + nVarSampleDividerOffset); // used here as Divider
196 u32 = beu32p(Header3); // bug in the documentation [1,2]
197 }
198 if (k==0) hdr->NRec = BufLength;
199 if (hdr->NRec != BufLength)
200 biosigERROR(hdr, B4C_UNSPECIFIC_ERROR, "SOPEN(ACQ-READ): channels with different sampling rates are not supported\n");
201
202 if (hdr->NRec < BufLength)
203 hdr->NRec = BufLength;
204 if (minspr > BufLength)
205 minspr = BufLength;
206
207 hc->bi = hdr->AS.bpb;
208 // u32 is
209 switch (u32>>16) {
210 case 0: /* undocumented, maybe a bug on BigEndian platform, because u32=0x00000008, 0x00000000 have been observed only on BE
211 on LE we see the documented values u32=0x00010008, 0x00020002
212 */
213 case 1:
214 hc->GDFTYP = 17; // double
215 hc->DigMax = 1e9;
216 hc->DigMin = -1e9;
217 break;
218 case 2:
219 hc->GDFTYP = 3; // int
220 hc->DigMax = 32767;
221 hc->DigMin = -32678;
222 break;
223 default:
224 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s: #%d 0x%08x: type=%i v%g E%d c%d\n", \
225 __FILE__, __LINE__, __func__, (int)k, u32, u32>>16, hdr->VERSION, hdr->FILE.LittleEndian, compressed);
226
227 biosigERROR(hdr, B4C_UNSPECIFIC_ERROR, "SOPEN(ACQ-READ): invalid channel type.");
228 };
229
230 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s: #%d 0x%08x: v%g E%d c%d NRec=%d SPR=%d spr=%d/%d/%d\n", \
231 __FILE__, __LINE__, __func__, (int)k, u32, hdr->VERSION, hdr->FILE.LittleEndian, compressed, hdr->NRec, \
232 hdr->SPR, hc->SPR,minspr,nVarSampleDivider);
233
234 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
235 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
236 hdr->AS.bpb += (GDFTYP_BITS[hc->GDFTYP]*hc->SPR)>>3;
237
238 Header2 += lChanHeaderLen;
239 Header3 += 4;
240 }
241
242 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s: v%g E%d c%d NRec=%d SPR=%d minspr=%d\n", __FILE__, __LINE__, __func__, \
243 hdr->VERSION, hdr->FILE.LittleEndian, compressed, hdr->NRec, hdr->SPR,minspr);
244
245 hdr->HeadLen += hdr->AS.bpb;
246 SectionTable[6] = hdr->HeadLen;
247
248 /// Block Seq 7: Markers header section
249 Header2 = hdr->AS.Header+hdr->HeadLen;
250 uint32_t markersByteLength = hdr->FILE.LittleEndian ? leu32p(Header2) : beu32p(Header2);
251
252 if ( (hdr->HeadLen+4) > count) {
253 hdr->AS.Header = (uint8_t*) realloc(hdr->AS.Header, hdr->HeadLen+4);
254 count += ifread(hdr->AS.Header+count, 1, hdr->HeadLen+4-count, hdr);
255 }
256 Header2 = hdr->AS.Header+hdr->HeadLen+4;
257 hdr->EVENT.N = hdr->FILE.LittleEndian ? leu32p(Header2) : beu32p(Header2);
258
259 SectionTable[7] = hdr->HeadLen+8;
260 hdr->HeadLen += markersByteLength;
261 SectionTable[8] = hdr->HeadLen;
262
263 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s: HeadLen=%i NumberOfEvents=%d v%g markerLen=%d\n", __FILE__, __LINE__, __func__, hdr->HeadLen, hdr->EVENT.N, hdr->VERSION, markersByteLength);
264
265 #if 0
266 /// Block Seq 8: Marker Item section
267 Header2 = hdr->AS.Header+SectionTable[7];
268 if (35.0 <= hdr->VERSION && hdr->VERSION <= 45.0) {
269 hdr->EVENT.TYP = (uint16_t*)calloc(hdr->EVENT.N,2);
270 hdr->EVENT.POS = (uint32_t*)calloc(hdr->EVENT.N,4);
271 // Version 3 marker structure
272 for (int k=0; k<hdr->EVENT.N; k++) {
273 hdr->EVENT.POS[k] = hdr->FILE.LittleEndian ? leu32p(Header2) : leu32p(Header2);
274 hdr->EVENT.TYP[k] = 1;
275 char *markerText = Header2+12;
276 Header2 += 12 + (hdr->FILE.LittleEndian ? leu32p(Header2+10) : leu32p(Header2+10));
277 }
278 }
279 else if (60.0 < hdr->VERSION && hdr->VERSION < 133.0) {
280 hdr->EVENT.TYP = (uint16_t*)calloc(hdr->EVENT.N,2);
281 hdr->EVENT.POS = (uint32_t*)calloc(hdr->EVENT.N,4);
282 hdr->EVENT.CHN = (uint16_t*)calloc(hdr->EVENT.N,2);
283 hdr->EVENT.DUR = (uint32_t*)calloc(hdr->EVENT.N,4);
284 int next = 14 + (hdr->VERSION>=124.0) * 8 + (hdr->VERSION>=128.0) * 8 ;
285 // Version 4 marker structure
286 for (int k=0; k<hdr->EVENT.N; k++) {
287 char *markerText = Header2 + next + 2;
288 uint16_t markerType;
289 hdr->EVENT.TYP[k] = 1;
290 hdr->EVENT.DUR[k] = 0;
291 if (hdr->FILE.LittleEndian) {
292 hdr->EVENT.POS[k] = leu32p(Header2);
293 hdr->EVENT.CHN[k] = leu16p(Header2+8);
294 markerType = leu16p(Header2+10);
295 Header2 += next + 2 + leu32p(Header2+next);
296 }
297 else {
298 hdr->EVENT.POS[k] = beu32p(Header2);
299 hdr->EVENT.CHN[k] = beu16p(Header2+8);
300 markerType = beu16p(Header2+10);
301 Header2 += next + 2 + beu32p(Header2+next);
302 }
303 }
304 }
305 else {
306 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s(..) reading of (%d) Markers not supported for ACQ v%g: \n", \
307 __FILE__, __LINE__, __func__, hdr->EVENT.N, hdr->VERSION );
308
309 hdr->EVENT.N = 0;
310 }
311 #endif
312
313 /* TODO: implement ACQ/BIOPAC format support
314 define all fields in hdr->....
315 currently only the first hdr->HeadLen bytes are stored in
316 hdr->AS.Header, all other fields still need to be defined.
317 */
318
319 // hdr->AS.rawdata = hdr->AS.Header+SectionTable[5];
320 hdr->HeadLen = SectionTable[5];
321 ifseek(hdr, hdr->HeadLen, SEEK_SET);
322
323 }
324
325 #ifdef __cplusplus
326 }
327 #endif
328