Merge branch 'develop' into channel_metadata

This commit is contained in:
Anne de Jong 2020-07-09 09:47:48 +02:00
commit 3e2e8c8953
3 changed files with 54 additions and 3 deletions

View File

@ -71,7 +71,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
link_directories(C:\\mingw\\lib)
link_directories(C:\\mingw\\bin)
link_directories(..\\rtaudio)
link_directories(C:\\Users\\librewin7\\Miniconda3)
link_directories(C:\\Users\\User\\Miniconda3)
else()
set(win32 false)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -std=c11 \

View File

@ -3,7 +3,7 @@
"""
Description: Read data from image stream and record sound at the same time
"""
import cv2 as cv
#import cv2 as cv
from .lasp_atomic import Atomic
from threading import Thread, Condition, Lock
import numpy as np
@ -73,7 +73,7 @@ class AvStream:
rtaudio_inputparams = None
rtaudio_outputparams = None
self.nframes_per_block = 512
self.nframes_per_block = 1024
if self.duplex_mode or avtype == AvType.audio_output:
rtaudio_outputparams = {'deviceid': device.index,

View File

@ -489,6 +489,8 @@ class Measurement:
firstcoltime: If true, the first column is the treated as the
sample time.
"""
if os.path.splitext(mfn)[1] != '.h5':
mfn += '.h5'
if os.path.exists(mfn):
raise ValueError(f'File {mfn} already exist.')
if timestamp is None:
@ -497,6 +499,13 @@ class Measurement:
if data.ndim != 2:
data = data[:, np.newaxis]
try:
len(sensitivity)
except:
raise ValueError('Sensitivity should be given as array-like data type')
sensitivity = np.asarray(sensitivity)
nchannels = data.shape[1]
if nchannels != sensitivity.shape[0]:
raise ValueError(
@ -514,3 +523,45 @@ class Measurement:
compression='gzip')
ad[0] = data
return Measurement(mfn)
@staticmethod
def fromWaveFile(fn, newfn=None, force=False, timestamp=None):
"""Convert a measurement file to a wave file, and return the
measurement handle."""
if timestamp is None:
timestamp = int(time.time())
base_fn = os.path.splitext(fn)[0]
if newfn is None:
newfn = base_fn + '.h5'
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
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
return Measurement(newfn)