-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathioReadXwavHeader.m
225 lines (195 loc) · 10.1 KB
/
ioReadXwavHeader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
function hdr = ioReadXwavHeader(Filename, DateRE, ftype)
% hdr = ioReadXWAVHeader(Filename, OptionalArgs)
%
% reads pseudo-wav (XWAV or *.x.wav) file header
% Optional arguments:
% 'ftype', N - Read file of type N. N=1 --> wav, N=2 --> xwav (default)
%
% Do not modify the following line, maintained by CVS
% $Id: ioReadXWAVHeader.m,v 1.11 2008/06/23 16:42:56 mroch Exp $
% make hdr.xhd empty
global PARAMS
hdr.xhd = [];
if nargin < 2
% Use global timestamp if available
if exist('PARAMS', 'var') && isfield(PARAMS, 'fnameTimeRegExp')
DateRE = PARAMS.fnameTimeRegExp;
else
DateRE = [];
end
end
if ftype == 1 % do the following for wav files
hdr = ioReadWavHeader(Filename, DateRE);
elseif ftype == 2 % do the following for xwavs
fid = fopen(Filename,'r');
if fid == -1
error('Unable to open %s', Filename)
end
hdr.filetype = 'xwav';
hdr.fType = 'xwav';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RIFF chunk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hdr.xhd.ChunkID = char(fread(fid,4,'uchar'))'; % "RIFF"
hdr.xhd.ChunkSize = fread(fid,1,'uint32'); % File size - 8 bytes
filesize = getfield(dir(Filename),'bytes');
if hdr.xhd.ChunkSize ~= filesize - 8
warning('Error - incorrect Chunk Size')
% return % comment to work with bad files
end
hdr.xhd.Format = char(fread(fid,4,'uchar'))'; % "WAVE"
if ~strcmp(hdr.xhd.ChunkID,'RIFF') || ~strcmp(hdr.xhd.Format,'WAVE')
error('not wav file - exit')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Format Subchunk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hdr.xhd.fSubchunkID = char(fread(fid,4,'uchar'))'; % "fmt "
hdr.xhd.fSubchunkSize = fread(fid,1,'uint32'); % (Size of Subchunk - 8) = 16 bytes (PCM)
hdr.xhd.AudioFormat = fread(fid,1,'uint16'); % Compression code (PCM = 1)
hdr.xhd.NumChannels = fread(fid,1,'uint16'); % Number of Channels
hdr.xhd.SampleRate = fread(fid,1,'uint32'); % Sampling Rate (samples/second)
hdr.xhd.ByteRate = fread(fid,1,'uint32'); % Byte Rate = SampleRate * NumChannels * BitsPerSample / 8
hdr.xhd.BlockAlign = fread(fid,1,'uint16'); % # of Bytes per Sample Slice = NumChannels * BitsPerSample / 8
hdr.xhd.BitsPerSample = fread(fid,1,'uint16'); % # of Bits per Sample : 8bit = 8, 16bit = 16, etc
if ~strcmp(hdr.xhd.fSubchunkID,'fmt ') || hdr.xhd.fSubchunkSize ~= 16
error('unknown wav format - exit')
end
% should only be needed for special case bad data
% remove after debugged
% if PARAMS.xhd.SampleRate == 100000
% disp_msg('Warning, changing sample rate from 100,000 to 500,000 Hz')
% % PARAMS.xhd.SampleRate = 200000;
% % PARAMS.xhd.ByteRate = 400000;
% PARAMS.xhd.SampleRate = 500000;
% PARAMS.xhd.ByteRate = 1000000;
% end
% copy to another name, and get number of bytes per sample
hdr.nBits = hdr.xhd.BitsPerSample; % # of Bits per Sample : 8bit = 8, 16bit = 16, etc
hdr.samp.byte = floor(hdr.nBits/8); % # of Bytes per Sample
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HARP Subchunk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hdr.xhd.hSubchunkID = char(fread(fid,4,'uchar'))'; % "harp"
if strcmp(hdr.xhd.hSubchunkID,'data')
disp('normal wav file - read data now')
return
elseif ~strcmp(hdr.xhd.hSubchunkID,'harp')
disp('unsupported wav format')
error(['SubchunkID = ',hdr.xhd.hSubchunkID])
return
end
hdr.xhd.hSubchunkSize = fread(fid,1,'uint32'); % (Size of Subchunk - 8) includes write subchunk
hdr.xhd.WavVersionNumber = fread(fid,1,'uchar'); % Version number of the "harp" header (0-255)
hdr.xhd.FirmwareVersionNuumber = char(fread(fid,10,'uchar'))'; % HARP Firmware Vesion
hdr.xhd.InstrumentID = char(fread(fid,4,'uchar'))'; % Instrument ID Number (0-255)
hdr.xhd.SiteName = char(fread(fid,4,'uchar'))'; % Site Name, 4 alpha-numeric characters
hdr.xhd.ExperimentName = char(fread(fid,8,'uchar'))'; % Experiment Name
hdr.xhd.DiskSequenceNumber = fread(fid,1,'uchar'); % Disk Sequence Number (1-16)
hdr.xhd.DiskSerialNumber = char(fread(fid,8,'uchar'))'; % Disk Serial Number
hdr.xhd.NumOfRawFiles = fread(fid,1,'uint16'); % Number of RawFiles in XWAV file
hdr.xhd.Longitude = fread(fid,1,'int32'); % Longitude (+/- 180 degrees) * 100,000
hdr.xhd.Latitude = fread(fid,1,'int32'); % Latitude (+/- 90 degrees) * 100,000
hdr.xhd.Depth = fread(fid,1,'int16'); % Depth, positive == down
hdr.xhd.Reserved = fread(fid,8,'uchar')'; % Padding to extend subchunk to 64 bytes
if hdr.xhd.hSubchunkSize ~= (64 - 8 + hdr.xhd.NumOfRawFiles * 32)
disp('Error - HARP SubchunkSize and NumOfRawFiles discrepancy?')
disp(['hSubchunkSize = ',num2str(hdr.xhd.hSubchunkSize)])
error(['NumOfRawFiles = ',num2str(hdr.xhd.NumOfRawFiles)])
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% write sub-sub chunk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:hdr.xhd.NumOfRawFiles
% Start of Raw file :
hdr.xhd.year(i) = fread(fid,1,'uchar'); % Year
hdr.xhd.month(i) = fread(fid,1,'uchar'); % Month
hdr.xhd.day(i) = fread(fid,1,'uchar'); % Day
hdr.xhd.hour(i) = fread(fid,1,'uchar'); % Hour
hdr.xhd.minute(i) = fread(fid,1,'uchar'); % Minute
hdr.xhd.secs(i) = fread(fid,1,'uchar'); % Seconds
hdr.xhd.ticks(i) = fread(fid,1,'uint16'); % Milliseconds
hdr.xhd.byte_loc(i) = fread(fid,1,'uint32'); % Byte location in xwav file of RawFile start
hdr.xhd.byte_length(i) = fread(fid,1,'uint32'); % Byte length of RawFile in xwav file
hdr.xhd.write_length(i) = fread(fid,1,'uint32'); % # of blocks in RawFile length (default = 60000)
hdr.xhd.sample_rate(i) = fread(fid,1,'uint32'); % sample rate of this RawFile
hdr.xhd.gain(i) = fread(fid,1,'uint8'); % gain (1 = no change)
hdr.xhd.padding = fread(fid,7,'uchar'); % Padding to make it 32 bytes...misc info can be added here
% should only be needed for special case bad data
% remove after debugging
% if hdr.xhd.sample_rate(i) == 100000
% % disp('Warning, changing sample rate from 100,000 to 200,000 Hz')
% % hdr.xhd.sample_rate(i) = 200000;
% hdr.xhd.sample_rate(i) = 500000;
%
% end
% calculate starting time [dnum => datenum in days] for each raw
% write/buffer flush
if hdr.xhd.year(i)<1900 % catch for year 2000 issue if comparing with guided detector dates.
hdr.xhd.year(i) = hdr.xhd.year(i)+2000;
end
hdr.raw.dnumStart(i) = datenum([hdr.xhd.year(i) hdr.xhd.month(i)...
hdr.xhd.day(i) hdr.xhd.hour(i) hdr.xhd.minute(i) ...
hdr.xhd.secs(i)+(hdr.xhd.ticks(i)/1000)]);
hdr.raw.dvecStart(i,:) = [hdr.xhd.year(i) hdr.xhd.month(i)...
hdr.xhd.day(i) hdr.xhd.hour(i) hdr.xhd.minute(i) ...
hdr.xhd.secs(i)+(hdr.xhd.ticks(i)/1000)];
% end of RawFile:
hdr.raw.dnumEnd(i) = hdr.raw.dnumStart(i) ...
+ datenum([0 0 0 0 0 (hdr.xhd.byte_length(i) - 2) ./ hdr.xhd.ByteRate]);
hdr.raw.dvecEnd(i,:) = hdr.raw.dvecStart(i,:) ...
+ [0 0 0 0 0 (hdr.xhd.byte_length(i) - 2) ./ hdr.xhd.ByteRate];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DATA Subchunk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hdr.xhd.dSubchunkID = char(fread(fid,4,'uchar'))'; % "data"
if ~strcmp(hdr.xhd.dSubchunkID,'data')
warning('hummm, should be "data" here?')
error(['SubchunkID = ',hdr.xhd.dSubchunkID])
end
hdr.xhd.dSubchunkSize = fread(fid,1,'uint32'); % (Size of Subchunk - 8) includes write subchunk
% read some data and check
%data = fread(fid,[4,100],'int16');
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% rename a few things
%
hdr.nch = hdr.xhd.NumChannels; % Number of Channels
hdr.fs = hdr.xhd.sample_rate(1); % Real xwav Sampling Rate (samples/second)
% hdr.fs = hdr.xhd.SampleRate; % Sampling Rate(samples/second)
% this is 'wav sample rate,
% could be fake...
% vectors (NumOfWrites)
hdr.xgain = hdr.xhd.gain; % gain (1 = no change)
samp = hdr.xhd.byte_length ./ (hdr.nch * hdr.samp.byte); % # of Samples = Bytes of data / (# of Bytes per Sample Slice = NumChannels * BitsPerSample / 8)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the following assume continuous recording (no schedule)
% total number of samples in file
% hdr.samp.data = sum(sum(samp));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set up some triton timing stuff:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% new stuff to count in samples and simplify timing
%
% smw 050511
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% hdr.start.dvec(1) = hdr.xhd.year(1);
% hdr.start.dvec(2) = hdr.xhd.month(1);
% hdr.start.dvec(3) = hdr.xhd.day(1);
% hdr.start.dvec(4) = hdr.xhd.hour(1);
% hdr.start.dvec(5) = hdr.xhd.minute(1);
% hdr.start.dvec(6) = hdr.xhd.secs(1) + hdr.xhd.ticks(1)/1000;
%
% hdr.start.dnum = datenum(hdr.start.dvec);
hdr.start.dnum = hdr.raw.dnumStart(1);
hdr.start.dvec = hdr.raw.dvecStart(1,:);
%hdr.end.sample = hdr.samp.data; % last sample of file
%hdr.end.dnum = hdr.start.dnum + datenum([0 0 0 0 0 hdr.end.sample/hdr.fs]);
hdr.end.dnum = hdr.raw.dnumEnd(hdr.xhd.NumOfRawFiles);
% hdr.end.dvec = datevec(datenum(hdr.start.dvec + [0 0 0 0 0
% hdr.end.sample/hdr.fs]));
else
fclose(all)
error('Bad ftype')
end