Debian Codegraph @

biosig4c++/igor/ptn003/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 #include "PackedFileDemo.h"
11
12 static void
13 ReorderBytes(void *p, int bytesPerPoint, long numValues) // Reverses byte order.
14 {
15 unsigned char ch, *p1, *p2, *pEnd;
16
17 pEnd = (unsigned char *)p + numValues*bytesPerPoint;
18 while (p < (void *)pEnd) {
19 p1 = p;
20 p2 = (unsigned char *)p + bytesPerPoint-1;
21 while (p1 < p2) {
22 ch = *p1;
23 *p1++ = *p2;
24 *p2-- = ch;
25 }
26 p = (unsigned char *)p + bytesPerPoint;
27 }
28 }
29
30 static void
31 ReorderShort(void* sp)
32 {
33 ReorderBytes(sp, 2, 1);
34 }
35
36 static void
37 ReorderLong(void* lp)
38 {
39 ReorderBytes(lp, 4, 1);
40 }
41
42 static void
43 ReorderDouble(void* dp)
44 {
45 ReorderBytes(dp, 8, 1);
46 }
47
48 static void
49 ReorderBinHeader1(BinHeader1* p)
50 {
51 ReorderShort(&p->version);
52 ReorderLong(&p->wfmSize);
53 ReorderShort(&p->checksum);
54 }
55
56 static void
57 ReorderBinHeader2(BinHeader2* p)
58 {
59 ReorderShort(&p->version);
60 ReorderLong(&p->wfmSize);
61 ReorderLong(&p->noteSize);
62 ReorderLong(&p->pictSize);
63 ReorderShort(&p->checksum);
64 }
65
66 static void
67 ReorderBinHeader3(BinHeader3* p)
68 {
69 ReorderShort(&p->version);
70 ReorderLong(&p->wfmSize);
71 ReorderLong(&p->noteSize);
72 ReorderLong(&p->formulaSize);
73 ReorderLong(&p->pictSize);
74 ReorderShort(&p->checksum);
75 }
76
77 static void
78 ReorderBinHeader5(BinHeader5* p)
79 {
80 ReorderShort(&p->version);
81 ReorderShort(&p->checksum);
82 ReorderLong(&p->wfmSize);
83 ReorderLong(&p->formulaSize);
84 ReorderLong(&p->noteSize);
85 ReorderLong(&p->dataEUnitsSize);
86 ReorderBytes(&p->dimEUnitsSize, 4, 4);
87 ReorderBytes(&p->dimLabelsSize, 4, 4);
88 ReorderLong(&p->sIndicesSize);
89 ReorderLong(&p->optionsSize1);
90 ReorderLong(&p->optionsSize2);
91 }
92
93 static void
94 ReorderWaveHeader2(WaveHeader2* p)
95 {
96 ReorderShort(&p->type);
97 ReorderLong(&p->next);
98 // char bname does not need to be reordered.
99 ReorderShort(&p->whVersion);
100 ReorderShort(&p->srcFldr);
101 ReorderLong(&p->fileName);
102 // char dataUnits does not need to be reordered.
103 // char xUnits does not need to be reordered.
104 ReorderLong(&p->npnts);
105 ReorderShort(&p->aModified);
106 ReorderDouble(&p->hsA);
107 ReorderDouble(&p->hsB);
108 ReorderShort(&p->wModified);
109 ReorderShort(&p->swModified);
110 ReorderShort(&p->fsValid);
111 ReorderDouble(&p->topFullScale);
112 ReorderDouble(&p->botFullScale);
113 // char useBits does not need to be reordered.
114 // char kindBits does not need to be reordered.
115 ReorderLong(&p->formula);
116 ReorderLong(&p->depID);
117 ReorderLong(&p->creationDate);
118 // char wUnused does not need to be reordered.
119 ReorderLong(&p->modDate);
120 ReorderLong(&p->waveNoteH);
121 // The wData field marks the start of the wave data which will be reordered separately.
122 }
123
124 static void
125 ReorderWaveHeader5(WaveHeader5* p)
126 {
127 ReorderLong(&p->next);
128 ReorderLong(&p->creationDate);
129 ReorderLong(&p->modDate);
130 ReorderLong(&p->npnts);
131 ReorderShort(&p->type);
132 ReorderShort(&p->dLock);
133 // char whpad1 does not need to be reordered.
134 ReorderShort(&p->whVersion);
135 // char bname does not need to be reordered.
136 ReorderLong(&p->whpad2);
137 ReorderLong(&p->dFolder);
138 ReorderBytes(&p->nDim, 4, 4);
139 ReorderBytes(&p->sfA, 8, 4);
140 ReorderBytes(&p->sfB, 8, 4);
141 // char dataUnits does not need to be reordered.
142 // char dimUnits does not need to be reordered.
143 ReorderShort(&p->fsValid);
144 ReorderShort(&p->whpad3);
145 ReorderDouble(&p->topFullScale);
146 ReorderDouble(&p->botFullScale);
147 ReorderLong(&p->dataEUnits);
148 ReorderBytes(&p->dimEUnits, 4, 4);
149 ReorderBytes(&p->dimLabels, 4, 4);
150 ReorderLong(&p->waveNoteH);
151 ReorderBytes(&p->whUnused, 4, 16);
152 ReorderShort(&p->aModified);
153 ReorderShort(&p->wModified);
154 ReorderShort(&p->swModified);
155 // char useBits does not need to be reordered.
156 // char kindBits does not need to be reordered.
157 ReorderLong(&p->formula);
158 ReorderLong(&p->depID);
159 ReorderShort(&p->whpad4);
160 ReorderShort(&p->srcFldr);
161 ReorderLong(&p->fileName);
162 ReorderLong(&p->sIndices);
163 // The wData field marks the start of the wave data which will be reordered separately.
164 }
165
166 static int
167 Checksum(short *data, int needToReorderBytes, int oldcksum, int numbytes)
168 {
169 unsigned short s;
170
171 numbytes >>= 1; // 2 bytes to a short -- ignore trailing odd byte.
172 while(numbytes-- > 0) {
173 s = *data++;
174 if (needToReorderBytes)
175 ReorderShort(&s);
176 oldcksum += s;
177 }
178 return oldcksum&0xffff;
179 }
180
181 /* NumBytesPerPoint(int type)
182
183 Given a numeric wave type, returns the number of data bytes per point.
184 */
185 static int
186 NumBytesPerPoint(int type)
187 {
188 int numBytesPerPoint;
189
190 // Consider the number type, not including the complex bit or the unsigned bit.
191 switch(type & ~(NT_CMPLX | NT_UNSIGNED)) {
192 case NT_I8:
193 numBytesPerPoint = 1; // char
194 break;
195 case NT_I16:
196 numBytesPerPoint = 2; // short
197 break;
198 case NT_I32:
199 numBytesPerPoint = 4; // long
200 break;
201 case NT_FP32:
202 numBytesPerPoint = 4; // float
203 break;
204 case NT_FP64:
205 numBytesPerPoint = 8; // double
206 break;
207 default:
208 return 0;
209 break;
210 }
211
212 if (type & NT_CMPLX)
213 numBytesPerPoint *= 2; // Complex wave - twice as many points.
214
215 return numBytesPerPoint;
216 }
217
218 /* LoadNumericWaveData(fr, type, npnts, waveDataSize, needToReorderBytes, pp)
219
220 fr is a file reference.
221 type is the Igor number type.
222 npnts is the total number of elements in all dimensions.
223 waveDataSize is the number of data bytes stored in the file.
224 needToReorderBytes if the byte ordering of the file is not the byte ordering of the current platform.
225 pp is a pointer to a pointer.
226
227 If an error occurs, LoadNumericWaveData returns a non-zero error code and sets
228 *pp to NULL.
229
230 If no error occurs, LoadNumericWaveData returns 0 and sets *pp to a pointer allocated
231 via malloc. This pointer must be freed by the calling routine.
232
233 For simplicity, this example does not handle text waves.
234 */
235 static int
236 LoadNumericWaveData(CP_FILE_REF fr, int type, long npnts, unsigned long waveDataSize, int needToReorderBytes, void**pp)
237 {
238 int numBytesPerPoint;
239 unsigned long numBytesToRead, numBytesToAllocate;
240 unsigned long numBytesRead;
241 void* p;
242 int err;
243
244 *pp = NULL; // Assume that we can not allocate memory.
245
246 numBytesPerPoint = NumBytesPerPoint(type);
247 if (numBytesPerPoint <= 0) {
248 printf("Invalid wave type (0x%x).\n", type);
249 return -1;
250 }
251 numBytesToRead = npnts * numBytesPerPoint;
252 if (type & NT_CMPLX)
253 numBytesToRead *= 2; // Complex wave - twice as many points.
254
255 numBytesToAllocate = numBytesToRead;
256 if (numBytesToAllocate == 0)
257 numBytesToAllocate = 8; // This is just because malloc refuses to allocate a zero byte block.
258 p = malloc(numBytesToAllocate); // Allocate memory to store the wave data.
259 if (p == NULL) {
260 printf("Unable to allocate %ld bytes to store data.\n", numBytesToAllocate);
261 return -1;
262 }
263 if (numBytesToRead > 0) {
264 if (waveDataSize < numBytesToRead) {
265 /* If here, this should be a wave governed by a dependency formula
266 for which no wave data was written to the file. Since we can't
267 execute the dependency formula we have no way to recreate the wave's
268 data. Therefore, we return 0 for all points in the wave.
269 */
270 memset(p, 0, numBytesToRead);
271 }
272 else {
273 if (err = CPReadFile(fr, numBytesToRead, p, &numBytesRead)) {
274 free(p);
275 printf("Error %d occurred while reading the wave data.\n", err);
276 return err;
277 }
278 if (needToReorderBytes) {
279 if (type != 0) // Text wave data does not need to be reordered.
280 ReorderBytes(p, numBytesPerPoint, numBytesToRead/numBytesPerPoint);
281 }
282 }
283 }
284
285 *pp = p; // Return the pointer to the calling routine.
286 return 0;
287 }
288
289 /* ReadWave(fr, typePtr, npntsPtr, waveDataPtrPtr)
290
291 Reads the wave file and prints some information about it.
292
293 Returns to the calling routine the wave's type, number of points, and the
294 wave data. The calling routine must free *waveDataPtrPtr if it is
295 not null.
296
297 Returns 0 or an error code.
298 */
299 int
300 ReadWave(CP_FILE_REF fr, int* typePtr, long* npntsPtr, void** waveDataPtrPtr)
301 {
302 unsigned long startFilePos;
303 short version;
304 short check;
305 int binHeaderSize, waveHeaderSize, checkSumSize;
306 unsigned long waveDataSize;
307 unsigned long numBytesRead;
308 int needToReorderBytes;
309 char buffer[512];
310 WaveHeader2* w2;
311 WaveHeader5* w5;
312 unsigned long modDate;
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 wave version field.
326 if (err = CPReadFile(fr, 2, &version, &numBytesRead)) {
327 printf("Error %d occurred while reading the wave 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 wave.\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 wave.\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 wave.\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 wave.\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. 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 wave 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.\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 WaveHeader fields.
416 switch(version) {
417 case 1:
418 case 2:
419 case 3:
420 w2 = (WaveHeader2*)(buffer+binHeaderSize);
421 modDate = w2->modDate;
422 npnts = w2->npnts;
423 type = w2->type;
424 strcpy(name, w2->bname);
425 break;
426
427 case 5:
428 w5 = (WaveHeader5*)(buffer+binHeaderSize);
429 modDate = w5->modDate;
430 npnts = w5->npnts;
431 type = w5->type;
432 strcpy(name, w5->bname);
433 break;
434 }
435 printf(" Wave name=%s, npnts=%d, type=0x%x.\n", name, npnts, type);
436
437 // Return information to the calling routine.
438 *typePtr = type;
439 *npntsPtr = npnts;
440
441 // Determine the number of bytes of wave data in the file.
442 switch(version) {
443 case 1:
444 waveDataSize = ((BinHeader1*)buffer)->wfmSize - sizeof(WaveHeader2);
445 break;
446 case 2:
447 waveDataSize = ((BinHeader2*)buffer)->wfmSize - sizeof(WaveHeader2);
448 break;
449 case 3:
450 waveDataSize = ((BinHeader3*)buffer)->wfmSize - sizeof(WaveHeader2);
451 break;
452 case 5:
453 waveDataSize = ((BinHeader5*)buffer)->wfmSize - (sizeof(WaveHeader5) - 4);
454 break;
455 }
456
457 // Position the file pointer to the start of the wData field.
458 switch(version) {
459 case 1:
460 case 2:
461 case 3:
462 CPSetFilePosition(fr, startFilePos+binHeaderSize+waveHeaderSize-16, -1); // 16 = size of wData field in WaveHeader2 structure.
463 break;
464 case 5:
465 CPSetFilePosition(fr, startFilePos+binHeaderSize+waveHeaderSize-4, -1); // 4 = size of wData field in WaveHeader2 structure.
466 break;
467 }
468
469 if (type == 0) {
470 // For simplicity, we don't load text wave data in this example program.
471 printf(" This is a text wave.\n");
472 return 0;
473 }
474
475 // Load the data and allocates memory to store it.
476 if (err = LoadNumericWaveData(fr, type, npnts, waveDataSize, needToReorderBytes, waveDataPtrPtr))
477 return err;
478
479 return 0;
480 }