Some GIL releases, and bugfix in AvStream

This commit is contained in:
Anne de Jong 2019-12-28 12:01:56 +01:00
parent dcb861a6ef
commit 28122d5c15
2 changed files with 37 additions and 29 deletions

View File

@ -65,7 +65,7 @@ class AvStream:
rtaudio_inputparams = None rtaudio_inputparams = None
rtaudio_outputparams = None rtaudio_outputparams = None
if daqconfig.duplex_mode or avtype == AvType.audio_output: if self.duplex_mode or avtype == AvType.audio_output:
rtaudio_outputparams = {'deviceid': device.index, rtaudio_outputparams = {'deviceid': device.index,
# TODO: Add option to specify the number of output channels to use # TODO: Add option to specify the number of output channels to use
'nchannels': 1, #device.outputchannels, 'nchannels': 1, #device.outputchannels,
@ -73,7 +73,7 @@ class AvStream:
self.sampleformat = daqconfig.en_output_sample_format self.sampleformat = daqconfig.en_output_sample_format
self.samplerate = int(daqconfig.en_output_rate) self.samplerate = int(daqconfig.en_output_rate)
if avtype == AvType.audio_input: if avtype == AvType.audio_input or self.duplex_mode:
for i, channelconfig in enumerate(channelconfigs): for i, channelconfig in enumerate(channelconfigs):
if channelconfig.channel_enabled: if channelconfig.channel_enabled:
self.nchannels = i+1 self.nchannels = i+1
@ -139,6 +139,7 @@ class AvStream:
outputcallbacks = self._callbacks[AvType.audio_output] outputcallbacks = self._callbacks[AvType.audio_output]
if cbtype == AvType.audio_output and len(outputcallbacks) > 0: if cbtype == AvType.audio_output and len(outputcallbacks) > 0:
raise RuntimeError('Only one audio output callback can be allowed') raise RuntimeError('Only one audio output callback can be allowed')
if cb not in self._callbacks[cbtype]: if cb not in self._callbacks[cbtype]:
self._callbacks[cbtype].append(cb) self._callbacks[cbtype].append(cb)
@ -203,11 +204,13 @@ class AvStream:
cb(indata, outdata, self._aframectr()) cb(indata, outdata, self._aframectr())
except Exception as e: except Exception as e:
print(e) print(e)
return 1
for cb in self._callbacks[AvType.audio_output]: for cb in self._callbacks[AvType.audio_output]:
try: try:
cb(indata, outdata, self._aframectr()) cb(indata, outdata, self._aframectr())
except Exception as e: except Exception as e:
print(e) print(e)
return 1
return 0 if self._running else 1 return 0 if self._running else 1
def stop(self): def stop(self):

View File

@ -28,7 +28,7 @@ cdef extern from "lasp_tracer.h":
void clearScreen() void clearScreen()
cdef extern from "lasp_mat.h": cdef extern from "lasp_mat.h" nogil:
ctypedef struct dmat: ctypedef struct dmat:
us n_cols us n_cols
us n_rows us n_rows
@ -127,11 +127,11 @@ cdef class Fft:
timedata.shape[1], timedata.shape[1],
&timedata[0,0], &timedata[0,0],
False) False)
with nogil:
Fft_fft(self._fft,&t,&r)
Fft_fft(self._fft,&t,&r) dmat_free(&t)
cmat_free(&r)
dmat_free(&t)
cmat_free(&r)
return result return result
@ -159,10 +159,11 @@ cdef class Fft:
&timedata_view[0,0], &timedata_view[0,0],
False) False)
Fft_ifft(self._fft,&f,&t) with nogil:
Fft_ifft(self._fft,&f,&t)
dmat_free(&t) dmat_free(&t)
cmat_free(&f) cmat_free(&f)
return timedata return timedata
@ -189,7 +190,7 @@ cdef extern from "lasp_ps.h":
void PowerSpectra_compute(const c_PowerSpectra* ps, void PowerSpectra_compute(const c_PowerSpectra* ps,
const dmat * timedata, const dmat * timedata,
cmat * result) cmat * result) nogil
void PowerSpectra_free(c_PowerSpectra*) void PowerSpectra_free(c_PowerSpectra*)
@ -236,8 +237,8 @@ cdef class PowerSpectra:
False) False)
with nogil:
PowerSpectra_compute(self._ps,&td,&result_mat) PowerSpectra_compute(self._ps,&td,&result_mat)
dmat_free(&td) dmat_free(&td)
cmat_free(&result_mat) cmat_free(&result_mat)
@ -259,7 +260,7 @@ cdef extern from "lasp_aps.h":
const vd* weighting) const vd* weighting)
cmat* AvPowerSpectra_addTimeData(const c_AvPowerSpectra* ps, cmat* AvPowerSpectra_addTimeData(const c_AvPowerSpectra* ps,
const dmat * timedata) const dmat * timedata) nogil
void AvPowerSpectra_free(c_AvPowerSpectra*) void AvPowerSpectra_free(c_AvPowerSpectra*)
@ -319,16 +320,6 @@ cdef class AvPowerSpectra:
&timedata[0,0], &timedata[0,0],
False) False)
result_ptr = AvPowerSpectra_addTimeData(self.aps,
&td)
# The array here is created in such a way that the strides
# increase with increasing dimension. This is required for
# interoperability with the C-code, that stores all
# cross-spectra in a 2D matrix, where the first axis is the
# frequency axis, and the second axis corresponds to a certain
# cross-spectrum, as C_ij(f) = result[freq,i+j*nchannels]
result = np.empty((self.nfft//2+1,nchannels,nchannels), result = np.empty((self.nfft//2+1,nchannels,nchannels),
dtype = NUMPY_COMPLEX_TYPE, dtype = NUMPY_COMPLEX_TYPE,
order='F') order='F')
@ -339,11 +330,22 @@ cdef class AvPowerSpectra:
nchannels*nchannels, nchannels*nchannels,
&result_view[0,0,0], &result_view[0,0,0],
False) False)
# Copy result with nogil:
cmat_copy(&res,result_ptr) result_ptr = AvPowerSpectra_addTimeData(self.aps,
&td)
cmat_free(&res) # The array here is created in such a way that the strides
dmat_free(&td) # increase with increasing dimension. This is required for
# interoperability with the C-code, that stores all
# cross-spectra in a 2D matrix, where the first axis is the
# frequency axis, and the second axis corresponds to a certain
# cross-spectrum, as C_ij(f) = result[freq,i+j*nchannels]
# Copy result
cmat_copy(&res,result_ptr)
cmat_free(&res)
dmat_free(&td)
return result return result
@ -377,7 +379,10 @@ cdef class FilterBank:
cdef dmat input_vd = dmat_foreign_data(input_.shape[0],1, cdef dmat input_vd = dmat_foreign_data(input_.shape[0],1,
&input_[0, 0],False) &input_[0, 0],False)
cdef dmat output = FilterBank_filter(self.fb,&input_vd)
cdef dmat output
with nogil:
output = FilterBank_filter(self.fb,&input_vd)
# Steal the pointer from output # Steal the pointer from output
result = dmat_to_ndarray(&output,True) result = dmat_to_ndarray(&output,True)