Skip to content

Commit

Permalink
fix .wav file header sample rate rounding
Browse files Browse the repository at this point in the history
This is the cause of the TDoA problems.
  • Loading branch information
jks-prv committed Jan 12, 2024
1 parent 89614f9 commit bc18908
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions kiwirecorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ def by_dBm(e):
return e['dBm']

def _write_wav_header(fp, filesize, samplerate, num_channels, is_kiwi_wav):
samplerate = int(samplerate+0.5);
fp.write(struct.pack('<4sI4s', b'RIFF', filesize - 8, b'WAVE'))
bits_per_sample = 16
byte_rate = samplerate * num_channels * bits_per_sample // 8
block_align = num_channels * bits_per_sample // 8
fp.write(struct.pack('<4sIHHIIHH', b'fmt ', 16, 1, num_channels, int(samplerate+0.5), byte_rate, block_align, bits_per_sample))
fp.write(struct.pack('<4sIHHIIHH', b'fmt ', 16, 1, num_channels, samplerate, byte_rate, block_align, bits_per_sample))
if not is_kiwi_wav:
fp.write(struct.pack('<4sI', b'data', filesize - 12 - 8 - 16 - 8))

Expand Down Expand Up @@ -372,7 +373,7 @@ def _update_wav_header(self):

# fp.tell() sometimes returns zero. _write_wav_header writes filesize - 8
if filesize >= 8:
_write_wav_header(fp, filesize, int(self._output_sample_rate), self._num_channels, self._options.is_kiwi_wav)
_write_wav_header(fp, filesize, self._output_sample_rate, self._num_channels, self._options.is_kiwi_wav)

def _write_samples(self, samples, *args):
"""Output to a file on the disk."""
Expand All @@ -384,7 +385,7 @@ def _write_samples(self, samples, *args):
self._start_time = time.time()
# Write a static WAV header
with open(self._get_output_filename(), 'wb') as fp:
_write_wav_header(fp, 100, int(self._output_sample_rate), self._num_channels, self._options.is_kiwi_wav)
_write_wav_header(fp, 100, self._output_sample_rate, self._num_channels, self._options.is_kiwi_wav)
if self._options.is_kiwi_tdoa:
# NB for TDoA support: MUST be a print (i.e. not a logging.info)
print("file=%d %s" % (self._options.idx, self._get_output_filename()))
Expand Down

0 comments on commit bc18908

Please sign in to comment.