Debian Codegraph @

biosig4c++/igor/tn003/ReadWave.c

raw symbols SCIP
1 // This file contains an example of reading a wave file.
2
3 #include <ctype.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stddef.h>
8 #include "CrossPlatformFileIO.h"
9 #include "IgorBin.h"
10
11 static void
12 ReorderBytes(void *p, int bytesPerPoint, long numValues) // Reverses byte order.
13 {
14 unsigned char ch, *p1, *p2, *pEnd;
15
16 pEnd = (unsigned char *)p + numValues*bytesPerPoint;
17 while (p < (void *)pEnd) {
18 p1 = p;
19 p2 = (unsigned char *)p + bytesPerPoint-1;
20 while (p1 < p2) {
21 ch = *p1;
22 *p1++ = *p2;
23 *p2-- = ch;
24 }
25 p = (unsigned char *)p + bytesPerPoint;
26 }
27 }
28
29 static void
30 ReorderShort(void* sp)
31 {
32 ReorderBytes(sp, 2, 1);
33 }
34
35 static void
36 ReorderLong(void* lp)
37 {
38 ReorderBytes(lp, 4, 1);
39 }
40
41 static void
42 ReorderDouble(void* dp)
43 {
44 ReorderBytes(dp, 8, 1);
45 }
46
47 static void
48 ReorderBinHeader1(BinHeader1* p)
49 {
50 ReorderShort(&p->version);
51 ReorderLong(&p->wfmSize);
52 ReorderShort(&p->checksum);
53 }
54
55 static void
56 ReorderBinHeader2(BinHeader2* p)
57 {
58 ReorderShort(&p->version);
59 ReorderLong(&p->wfmSize);
60 ReorderLong(&p->noteSize);
61 ReorderLong(&p->pictSize);
62 ReorderShort(&p->checksum);
63 }
64
65 static void
66 ReorderBinHeader3(BinHeader3* p)
67 {
68 ReorderShort(&p->version);
69 ReorderLong(&p->wfmSize);
70 ReorderLong(&p->noteSize);
71 ReorderLong(&p->formulaSize);
72 ReorderLong(&p->pictSize);
73 ReorderShort(&p->checksum);
74 }
75
76 static void
77 ReorderBinHeader5(BinHeader5* p)
78 {
79 ReorderShort(&p->version);
80 ReorderShort(&p->checksum);
81 ReorderLong(&p->wfmSize);
82 ReorderLong(&p->formulaSize);
83 ReorderLong(&p->noteSize);
84 ReorderLong(&p->dataEUnitsSize);
85 ReorderBytes(&p->dimEUnitsSize, 4, 4);
86 ReorderBytes(&p->dimLabelsSize, 4, 4);
87 ReorderLong(&p->sIndicesSize);
88 ReorderLong(&p->optionsSize1);
89 ReorderLong(&p->optionsSize2);
90 }
91
92 static void
93 ReorderWaveHeader2(WaveHeader2* p)
94 {
95 ReorderShort(&p->type);
96 ReorderLong(&p->next);
97 // char bname does not need to be reordered.
98 ReorderShort(&p->whVersion);
99 ReorderShort(&p->srcFldr);
100 ReorderLong(&p->fileName);
101 // char dataUnits does not need to be reordered.
102 // char xUnits does not need to be reordered.
103 ReorderLong(&p->npnts);
104 ReorderShort(&p->aModified);
105 ReorderDouble(&p->hsA);
106 ReorderDouble(&p->hsB);
107 ReorderShort(&p->wModified);
108 ReorderShort(&p->swModified);
109 ReorderShort(&p->fsValid);
110 ReorderDouble(&p->topFullScale);
111 ReorderDouble(&p->botFullScale);
112 // char useBits does not need to be reordered.
113 // char kindBits does not need to be reordered.
114 ReorderLong(&p->formula);
115 ReorderLong(&p->depID);
116 ReorderLong(&p->creationDate);
117 // char wUnused does not need to be reordered.
118 ReorderLong(&p->modDate);
119 ReorderLong(&p->waveNoteH);
120 // The wData field marks the start of the wave data which will be reordered separately.
121 }
122
123 static void
124 ReorderWaveHeader5(WaveHeader5* p)
125 {
126 ReorderLong(&p->next);
127 ReorderLong(&p->creationDate);
128 ReorderLong(&p->modDate);
129 ReorderLong(&p->npnts);
130 ReorderShort(&p->type);
131 ReorderShort(&p->dLock);
132 // char whpad1 does not need to be reordered.
133 ReorderShort(&p->whVersion);
134 // char bname does not need to be reordered.
135 ReorderLong(&p->whpad2);
136 ReorderLong(&p->dFolder);
137 ReorderBytes(&p->nDim, 4, 4);
138 ReorderBytes(&p->sfA, 8, 4);
139 ReorderBytes(&p->sfB, 8, 4);
140 // char dataUnits does not need to be reordered.
141 // char dimUnits does not need to be reordered.
142 ReorderShort(&p->fsValid);
143 ReorderShort(&p->whpad3);
144 ReorderDouble(&p->topFullScale);
145 ReorderDouble(&p->botFullScale);
146 ReorderLong(&p->dataEUnits);
147 ReorderBytes(&p->dimEUnits, 4, 4);
148 ReorderBytes(&p->dimLabels, 4, 4);
149 ReorderLong(&p->waveNoteH);
150 ReorderBytes(&p->whUnused, 4, 16);
151 ReorderShort(&p->aModified);
152 ReorderShort(&p->wModified);
153 ReorderShort(&p->swModified);
154 // char useBits does not need to be reordered.
155 // char kindBits does not need to be reordered.
156 ReorderLong(&p->formula);
157 ReorderLong(&p->depID);
158 ReorderShort(&p->whpad4);
159 ReorderShort(&p->srcFldr);
160 ReorderLong(&p->fileName);
161 ReorderLong(&p->sIndices);
162 // The wData field marks the start of the wave data which will be reordered separately.
163 }
164
165 static int
166 Checksum(short *data, int needToReorderBytes, int oldcksum, int numbytes)
167 {
168 unsigned short s;
169
170 numbytes >>= 1; // 2 bytes to a short -- ignore trailing odd byte.
171 while(numbytes-- > 0) {
172 s = *data++;
173 if (needToReorderBytes)
174 ReorderShort(&s);
175 oldcksum += s;
176 }
177 return oldcksum&0xffff;
178 }
179
180 /* NumBytesPerPoint(int type)
181
182 Given a numeric wave type, returns the number of data bytes per point.
183 */
184 static int
185 NumBytesPerPoint(int type)
186 {
187 int numBytesPerPoint;
188
189 // Consider the number type, not including the complex bit or the unsigned bit.
190 switch(type & ~(NT_CMPLX | NT_UNSIGNED)) {
191 case NT_I8:
192 numBytesPerPoint = 1; // char
193 break;
194 case NT_I16:
195 numBytesPerPoint = 2; // short
196 break;
197 case NT_I32:
198 numBytesPerPoint = 4; // long
199 break;
200 case NT_FP32:
201 numBytesPerPoint = 4; // float
202 break;
203 case NT_FP64:
204 numBytesPerPoint = 8; // double
205 break;
206 default:
207 return 0;
208 break;
209 }
210
211 if (type & NT_CMPLX)
212 numBytesPerPoint *= 2; // Complex wave - twice as many points.
213
214 return numBytesPerPoint;
215 }
216
217 /* LoadNumericWaveData(fr, type, npnts, waveDataSize, needToReorderBytes, pp)
218
219 fr is a file reference.
220 type is the Igor number type.
221 npnts is the total number of elements in all dimensions.
222 waveDataSize is the number of data bytes stored in the file.
223 needToReorderBytes if the byte ordering of the file is not the byte ordering of the current platform.
224 pp is a pointer to a pointer.
225
226 If an error occurs, LoadWaveData returns a non-zero error code and sets
227 *pp to NULL.
228
229 If no error occurs, LoadWaveData returns 0 and sets *pp to a pointer allocated
230 via malloc. This pointer must be freed by the calling routine.
231 */
232 static int
233 LoadNumericWaveData(CP_FILE_REF fr, int type, long npnts, unsigned long waveDataSize, int needToReorderBytes, void**pp)
234 {
235 int numBytesPerPoint;
236 unsigned long numBytesToRead, numBytesToAllocate;
237 unsigned long numBytesRead;
238 void* p;
239 int err;
240
241 *pp = NULL; // Assume that we can not allocate memory.
242
243 numBytesPerPoint = NumBytesPerPoint(type);
244 if (numBytesPerPoint <= 0) {
245 printf("Invalid wave type (0x%x).\n", type);
246 return -1;
247 }
248 numBytesToRead = npnts * numBytesPerPoint;
249
250 numBytesToAllocate = numBytesToRead;
251 if (numBytesToAllocate == 0)
252 numBytesToAllocate = 8; // This is just because malloc refuses to allocate a zero byte block.
253 p = malloc(numBytesToAllocate); // Allocate memory to store the wave data.
254 if (p == NULL) {
255 printf("Unable to allocate %ld bytes to store data.\n", numBytesToAllocate);
256 return -1;
257 }
258 if (numBytesToRead > 0) {
259 if (waveDataSize < numBytesToRead) {
260 /* If here, this should be a wave governed by a dependency formula
261 for which no wave data was written to the file. Since we can't
262 execute the dependency formula we have no way to recreate the wave's
263 data. Therefore, we return 0 for all points in the wave.
264 */
265 memset(p, 0, numBytesToRead);
266 }
267 else {
268 if (err = CPReadFile(fr, numBytesToRead, p, &numBytesRead)) {
269 free(p);
270 printf("Error %d occurred while reading the wave data.\n", err);
271 return err;
272 }
273 if (needToReorderBytes) {
274 if (type != 0) // Text wave data does not need to be reordered.
275 ReorderBytes(p, numBytesPerPoint, numBytesToRead/numBytesPerPoint);
276 }
277 }
278 }
279
280 *pp = p; // Return the pointer to the calling routine.
281 return 0;
282 }
283
284 /* ReadWave(fr, typePtr, npntsPtr, waveDataPtrPtr)
285
286 Reads the wave file and prints some information about it.
287
288 Returns to the calling routine the wave's type, number of points, and the
289 wave data. The calling routine must free *waveDataPtrPtr if it is
290 not null.
291
292 Returns 0 or an error code.
293
294 This routine is written such that it could be used to read waves
295 from an Igor packed experiment file as well as from a standalone
296 Igor binary wave file. In order to achieve this, we must not assume
297 that the wave is at the start of the file. We do assume that, on entry
298 to this routine, the file position is at the start of the wave.
299 */
300 static int
301 ReadWave(CP_FILE_REF fr, int* typePtr, long* npntsPtr, void** waveDataPtrPtr)
302 {
303 unsigned long startFilePos;
304 short version;
305 short check;
306 int binHeaderSize, waveHeaderSize, checkSumSize;
307 unsigned long waveDataSize;
308 unsigned long numBytesRead;
309 int needToReorderBytes;
310 char buffer[512];
311 unsigned long modDate;
312 long wfmSize;
313 long npnts;
314 int type;
315 char name[64];
316 int err;
317
318 *waveDataPtrPtr = NULL;
319 *typePtr = 0;
320 *npntsPtr = 0;
321
322 if (err = CPGetFilePosition(fr, &startFilePos))
323 return err;
324
325 // Read the file version field.
326 if (err = CPReadFile(fr, 2, &version, &numBytesRead)) {
327 printf("Error %d occurred while reading the file version.\n", err);
328 return err;
329 }
330
331 /* Reorder version field bytes if necessary.
332 If the low order byte of the version field of the BinHeader structure
333 is zero then the file is from a platform that uses different byte-ordering
334 and therefore all data will need to be reordered.
335 */
336 needToReorderBytes = (version & 0xFF) == 0;
337 if (needToReorderBytes)
338 ReorderShort(&version);
339
340 // Check the version.
341 switch(version) {
342 case 1:
343 printf("This is a version 1 file.\n");
344 binHeaderSize = sizeof(BinHeader1);
345 waveHeaderSize = sizeof(WaveHeader2);
346 checkSumSize = binHeaderSize + waveHeaderSize;
347 break;
348 case 2:
349 printf("This is a version 2 file.\n");
350 binHeaderSize = sizeof(BinHeader2);
351 waveHeaderSize = sizeof(WaveHeader2);
352 checkSumSize = binHeaderSize + waveHeaderSize;
353 break;
354 case 3:
355 printf("This is a version 3 file.\n");
356 binHeaderSize = sizeof(BinHeader3);
357 waveHeaderSize = sizeof(WaveHeader2);
358 checkSumSize = binHeaderSize + waveHeaderSize;
359 break;
360 case 5:
361 printf("This is a version 5 file.\n");
362 binHeaderSize = sizeof(BinHeader5);
363 waveHeaderSize = sizeof(WaveHeader5);
364 checkSumSize = binHeaderSize + waveHeaderSize - 4; // Version 5 checksum does not include the wData field.
365 break;
366 default:
367 printf("This does not appear to be a valid Igor binary wave file. The version field = %d.\n", version);
368 return -1;
369 break;
370 }
371
372 // Load the BinHeader and the WaveHeader into memory.
373 CPSetFilePosition(fr, startFilePos, -1);
374 if (err = CPReadFile(fr, binHeaderSize+waveHeaderSize, buffer, &numBytesRead)) {
375 printf("Error %d occurred while reading the file headers.\n", err);
376 return err;
377 }
378
379 // Check the checksum.
380 check = Checksum((short*)buffer, needToReorderBytes, 0, checkSumSize);
381 if (check != 0) {
382 printf("Error in checksum - should be 0, is %d.\n", check);
383 printf("This does not appear to be a valid Igor binary wave file.\n");
384 return -1;
385 }
386
387 // Do byte reordering if the file is from another platform.
388 if (needToReorderBytes) {
389 switch(version) {
390 case 1:
391 ReorderBinHeader1((BinHeader1*)buffer);
392 break;
393 case 2:
394 ReorderBinHeader2((BinHeader2*)buffer);
395 break;
396 case 3:
397 ReorderBinHeader3((BinHeader3*)buffer);
398 break;
399 case 5:
400 ReorderBinHeader5((BinHeader5*)buffer);
401 break;
402 }
403 switch(version) {
404 case 1: // Version 1 and 2 files use WaveHeader2.
405 case 2:
406 case 3:
407 ReorderWaveHeader2((WaveHeader2*)(buffer+binHeaderSize));
408 break;
409 case 5:
410 ReorderWaveHeader5((WaveHeader5*)(buffer+binHeaderSize));
411 break;
412 }
413 }
414
415 // Read some of the BinHeader fields.
416 switch(version) {
417 case 1:
418 {
419 BinHeader1* b1;
420 b1 = (BinHeader1*)buffer;
421 wfmSize = b1->wfmSize;
422 }
423 break;
424
425 case 2:
426 {
427 BinHeader2* b2;
428 b2 = (BinHeader2*)buffer;
429 wfmSize = b2->wfmSize;
430 }
431 break;
432
433 case 3:
434 {
435 BinHeader3* b3;
436 b3 = (BinHeader3*)buffer;
437 wfmSize = b3->wfmSize;
438 }
439 break;
440
441 case 5:
442 {
443 BinHeader5* b5;
444 b5 = (BinHeader5*)buffer;
445 wfmSize = b5->wfmSize;
446 }
447 break;
448 }
449
450 // Read some of the WaveHeader fields.
451 switch(version) {
452 case 1:
453 case 2:
454 case 3:
455 {
456 WaveHeader2* w2;
457 w2 = (WaveHeader2*)(buffer+binHeaderSize);
458 modDate = w2->modDate;
459 npnts = w2->npnts;
460 type = w2->type;
461 strcpy(name, w2->bname);
462 }
463 break;
464
465 case 5:
466 {
467 WaveHeader5* w5;
468 w5 = (WaveHeader5*)(buffer+binHeaderSize);
469 modDate = w5->modDate;
470 npnts = w5->npnts;
471 type = w5->type;
472 strcpy(name, w5->bname);
473 }
474 break;
475 }
476 printf("Wave name=%s, npnts=%ld, type=0x%x, wfmSize=%ld.\n", name, npnts, type, wfmSize);
477
478 // Return information to the calling routine.
479 *typePtr = type;
480 *npntsPtr = npnts;
481
482 // Determine the number of bytes of wave data in the file.
483 switch(version) {
484 case 1:
485 case 2:
486 case 3:
487 waveDataSize = wfmSize - offsetof(WaveHeader2, wData) - 16;
488 break;
489 case 5:
490 waveDataSize = wfmSize - offsetof(WaveHeader5, wData);
491 break;
492 }
493
494 // Position the file pointer to the start of the wData field.
495 switch(version) {
496 case 1:
497 case 2:
498 case 3:
499 CPSetFilePosition(fr, startFilePos+binHeaderSize+waveHeaderSize-16, -1); // 16 = size of wData field in WaveHeader2 structure.
500 break;
501 case 5:
502 CPSetFilePosition(fr, startFilePos+binHeaderSize+waveHeaderSize-4, -1); // 4 = size of wData field in WaveHeader2 structure.
503 break;
504 }
505
506 if (type == 0) {
507 // For simplicity, we don't load text wave data in this example program.
508 printf("This is a text wave.\n");
509 return 0;
510 }
511
512 // Load the data and allocates memory to store it.
513 if (err = LoadNumericWaveData(fr, type, npnts, waveDataSize, needToReorderBytes, waveDataPtrPtr))
514 return err;
515
516 return 0;
517 }
518
519
520 int DoReadTest(const char* filePath);
521 int
522 DoReadTest(const char* filePath)
523 {
524 CP_FILE_REF fr;
525 int type;
526 long npnts;
527 void* waveDataPtr;
528 int err;
529
530 if (err = CPOpenFile(filePath, 0, &fr)) {
531 printf("Error %d occurred while opening the file.\n", err);
532 return err;
533 }
534
535 err = ReadWave(fr, &type, &npnts, &waveDataPtr);
536
537 // Here you would do something with the data.
538
539 CPCloseFile(fr);
540
541 if (waveDataPtr != NULL)
542 free(waveDataPtr);
543
544 printf("End of read test.\n");
545
546 return err;
547 }