Better implementation of 'Measurement.fromWaveFile, using scipy, which is able to handle wave files with floating point data

This commit is contained in:
Anne de Jong 2020-07-23 16:12:54 +02:00
parent 3e2e8c8953
commit b17178c4a7
4 changed files with 22 additions and 29 deletions

View File

@ -88,7 +88,6 @@ class DAQConfiguration:
en_input = self.input_channel_configs
first_ch_enabled_found = False
ch_disabled_found_after = False
print(en_input)
for ch in en_input:
if ch.channel_enabled:
first_ch_enabled_found = True

View File

@ -11,6 +11,7 @@ import numpy as np
__all__ = ['PinkNoise'] #, 'BrownianNoise', 'BlueNoise']
def PinkNoise(fs, fstart=10, fend=None, N=3):
"""
Creates SOS filter for pink noise. The filter has a flat response below

View File

@ -17,7 +17,6 @@ Common definitions used throughout the code.
__all__ = [
'P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', 'getFreq',
'lasp_shelve', 'this_lasp_shelve', 'W_REF', 'U_REF', 'I_REF', 'dBFS_REF',
'SIUnits'
]
# Reference sound pressure level

View File

@ -39,7 +39,7 @@ from contextlib import contextmanager
import h5py as h5
import numpy as np
from .lasp_config import LASP_NUMPY_FLOAT_TYPE
import wave
from scipy.io import wavfile
import os
import time
@ -537,31 +537,25 @@ class Measurement:
if os.path.exists(newfn) and not force:
raise RuntimeError(f'Measurement file name {newfn} already exists in path, set "force" to true to overwrite')
with wave.open(fn, 'r') as wf:
nchannels = wf.getnchannels()
samplerate = wf.getframerate()
sensitivity = np.ones(nchannels)
sampwidth = wf.getsampwidth()
nframes = wf.getnframes()
if sampwidth == 2:
dtype = np.int16
elif sampwidth == 1:
dtype = np.int8
elif sampwidth == 4:
dtype = np.int32
samplerate, data = wavfile.read(fn)
if data.ndim == 2:
nframes, nchannels = data.shape
else:
nchannels = 1
nframes = len(data)
data = data[:, np.newaxis]
sensitivity = np.ones(nchannels)
with h5.File(newfn, 'w') as hf:
hf.attrs['samplerate'] = samplerate
hf.attrs['nchannels'] = nchannels
hf.attrs['time'] = timestamp
hf.attrs['blocksize'] = 1
hf.attrs['sensitivity'] = sensitivity
data = np.frombuffer(
wf.readframes(nframes), dtype=dtype).reshape(nframes, nchannels)
ad = hf.create_dataset('audio', (1, data.shape[0], data.shape[1]),
dtype=dtype,
maxshape=(1, data.shape[0], data.shape[1]),
compression='gzip')
ad[0] = data
with h5.File(newfn, 'w') as hf:
hf.attrs['samplerate'] = samplerate
hf.attrs['nchannels'] = nchannels
hf.attrs['time'] = timestamp
hf.attrs['blocksize'] = 1
hf.attrs['sensitivity'] = sensitivity
ad = hf.create_dataset('audio', (1, nframes, nchannels),
dtype=data.dtype,
maxshape=(1, nframes, nchannels),
compression='gzip')
ad[0] = data
return Measurement(newfn)
return Measurement(newfn)