Debian Codegraph @

biosig4c++/igor/tn003/WriteWave.c

raw symbols SCIP
1 // This file contains an example of writing 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 "IgorBin.h"
9 #include "CrossPlatformFileIO.h"
10
11 /* Checksum(data,oldcksum,numbytes)
12
13 Returns shortwise simpleminded checksum over the data.
14 ASSUMES data starts on an even boundary.
15 */
16 static int
17 Checksum(short *data, int oldcksum, int numbytes)
18 {
19 numbytes >>= 1; // 2 bytes to a short -- ignore trailing odd byte.
20 while(numbytes-- > 0)
21 oldcksum += *data++;
22 return oldcksum&0xffff;
23 }
24
25 /* NumBytesPerPoint(int type)
26
27 Given a numeric wave type, returns the number of data bytes per point.
28 */
29 static int
30 NumBytesPerPoint(int type)
31 {
32 int numBytesPerPoint;
33
34 // Consider the number type, not including the complex bit or the unsigned bit.
35 switch(type & ~(NT_CMPLX | NT_UNSIGNED)) {
36 case NT_I8:
37 numBytesPerPoint = 1; // char
38 break;
39 case NT_I16:
40 numBytesPerPoint = 2; // short
41 break;
42 case NT_I32:
43 numBytesPerPoint = 4; // long
44 break;
45 case NT_FP32:
46 numBytesPerPoint = 4; // float
47 break;
48 case NT_FP64:
49 numBytesPerPoint = 8; // double
50 break;
51 default:
52 return 0;
53 break;
54 }
55
56 if (type & NT_CMPLX)
57 numBytesPerPoint *= 2; // Complex wave - twice as many points.
58
59 return numBytesPerPoint;
60 }
61
62 /* WriteVersion2NumericWave(fr, whp, data, waveNote, noteSize)
63
64 Writes an Igor version 2 binary wave with the properties specified in
65 whp, the data specified by data, and the wave note specified by waveNote
66 and noteSize.
67
68 Returns 0 or an error code.
69 */
70 static int
71 WriteVersion2NumericWave(CP_FILE_REF fr, WaveHeader2* whp, const void* data, const char* waveNote, long noteSize)
72 {
73 unsigned long numBytesToWrite;
74 unsigned long numBytesWritten;
75 unsigned long waveDataSize;
76 int numBytesPerPoint;
77 short cksum;
78 BinHeader2 bh;
79 char padding[16];
80 int err;
81
82 numBytesPerPoint = NumBytesPerPoint(whp->type);
83 if (numBytesPerPoint <= 0) {
84 printf("Invalid wave type (0x%x).\n", whp->type);
85 return -1;
86 }
87 waveDataSize = whp->npnts * numBytesPerPoint;
88
89 // Prepare the BinHeader structure.
90 memset(&bh,0,sizeof(struct BinHeader2));
91 bh.version = 2;
92 bh.wfmSize = offsetof(WaveHeader2, wData) + waveDataSize + 16; // Includes 16 bytes padding.
93 bh.noteSize = noteSize;
94
95 /* The checksum is over the BinHeader2 structure and the WaveHeader2 structure.
96 The wData field of the WaveHeader2 structure is assumed to contain the same
97 data as the first 16 bytes of the actual wave data. This is necessary
98 to get the correct checksum.
99 */
100 cksum = Checksum((short *)&bh, 0, sizeof(struct BinHeader2));
101 cksum = Checksum((short *)whp, cksum, sizeof(struct WaveHeader2));
102 bh.checksum = -cksum;
103
104 do {
105 // Write the BinHeader.
106 numBytesToWrite = sizeof(struct BinHeader2);
107 if (err = CPWriteFile(fr, numBytesToWrite, &bh, &numBytesWritten))
108 break;
109
110 // Write the WaveHeader, up to but not including the wData field.
111 numBytesToWrite = offsetof(WaveHeader2, wData);
112 if (err = CPWriteFile(fr, numBytesToWrite, whp, &numBytesWritten))
113 break;
114
115 // Write the wave data.
116 numBytesToWrite = waveDataSize;
117 if (err = CPWriteFile(fr, numBytesToWrite, data, &numBytesWritten))
118 break;
119
120 // Write the 16 byte padding.
121 memset(padding, 0, 16); // Write padding at the end of the wave data.
122 numBytesToWrite = 16;
123 if (err = CPWriteFile(fr, numBytesToWrite, padding, &numBytesWritten))
124 break;
125
126 // Now write optional data, in the correct order.
127
128 // Write the wave note.
129 numBytesToWrite = noteSize;
130 if (numBytesToWrite > 0) {
131 if (err = CPWriteFile(fr, numBytesToWrite, waveNote, &numBytesWritten))
132 break;
133 }
134
135 } while(0);
136
137 return err;
138 }
139
140 #define WAVEPOINTS 100
141
142 int DoWriteTest(const char* filePath);
143 int
144 DoWriteTest(const char* filePath)
145 {
146 WaveHeader2 wh;
147 unsigned long now;
148 float data[WAVEPOINTS];
149 char waveNote[256];
150 CP_FILE_REF fr;
151 long p;
152 int err;
153
154 #ifdef WIN32
155 now = 0; // It would be possible to write a Windows equivalent for the Macintosh GetDateTime function but it is not easy.
156 #else
157 GetDateTime(&now);
158 #endif
159
160 memset(&wh, 0, sizeof(wh));
161 wh.type = NT_FP32; // Single precision floating point.
162 strcpy(wh.bname, "TestWave");
163 strcpy(wh.dataUnits, "v");
164 strcpy(wh.xUnits, "s");
165 wh.npnts = WAVEPOINTS;
166 wh.hsA = 1e-6; // 1 usec per point.
167 wh.hsB = 0.0e0; // Starting from zero.
168 wh.modDate = now;
169
170 // Fill in data.
171 for (p = 0; p < WAVEPOINTS; p++)
172 data[p] = (float)p;
173
174 // Copy the first 16 bytes of data to the wData field of the WaveHeader2 structure.
175 // This is necessary for the checksum done by WriteVersion2NumericWave.
176 memcpy(wh.wData, data, 16);
177
178 // Add a wave note.
179 strcpy(waveNote, "This is a test, this is only a test.");
180
181 do {
182 // Create a file.
183 if (err = CPCreateFile(filePath, 1, 'IGR0', 'IGBW')) {
184 printf("Error %d creating the file.\n", err);
185 break;
186 }
187
188 // Open the file.
189 if (err = CPOpenFile(filePath, 1, &fr)) {
190 printf("Error %d opening the file.\n", err);
191 break;
192 }
193
194 // Write the data.
195 if (err = WriteVersion2NumericWave(fr, &wh, data, waveNote, strlen(waveNote)))
196 printf("Error %d writing the file.\n", err);
197
198 CPCloseFile(fr);
199 } while(0);
200
201 if (err == 0)
202 printf("Successfully wrote the file %s.\n", filePath);
203
204 return err;
205 }