From aa9a30973d31d0bccd73bb9b48bf16ad5797de6e Mon Sep 17 00:00:00 2001 From: Ewald de Wit Date: Mon, 12 Sep 2022 13:47:29 +0200 Subject: [PATCH] Use array.array for recording instread of numpy array, issue #2 --- hifiscan/analyzer.py | 12 +++++++----- hifiscan/audio.py | 7 +++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/hifiscan/analyzer.py b/hifiscan/analyzer.py index 01d4039..debb647 100644 --- a/hifiscan/analyzer.py +++ b/hifiscan/analyzer.py @@ -1,3 +1,4 @@ +import array from functools import lru_cache from typing import NamedTuple, Tuple @@ -66,19 +67,20 @@ class Analyzer: for meth in ['X', 'Y', 'H', 'H2', 'h', 'h_inv', 'spectrum']: setattr(self, meth, lru_cache(getattr(self, meth))) - def findMatch(self, recording: np.ndarray) -> bool: + def findMatch(self, recording: array.array) -> bool: """ Use correlation to find a match of the chirp in the recording. If found, return True and store the system response as ``y``. """ - self.time = recording.size / self.rate - if recording.size >= self.x.size: + sz = len(recording) + self.time = sz / self.rate + if sz >= self.x.size: Y = np.fft.fft(recording) - X = np.fft.fft(np.flip(self.x), n=recording.size) + X = np.fft.fft(np.flip(self.x), n=sz) corr = np.fft.ifft(X * Y).real idx = int(corr.argmax()) - self.x.size + 1 if idx >= 0: - self.y = recording[idx:idx + self.x.size].copy() + self.y = np.array(recording[idx:idx + self.x.size], 'f') return True return False diff --git a/hifiscan/audio.py b/hifiscan/audio.py index 0a169ee..fdd0a6f 100644 --- a/hifiscan/audio.py +++ b/hifiscan/audio.py @@ -66,15 +66,14 @@ class Audio: """Is there sound playing from the play queue?""" return bool(self.playQ) - def record(self) -> AsyncIterator[np.ndarray]: + def record(self) -> AsyncIterator[array.array]: """ Start a recording, yielding the entire recording every time a - new chunk is added. Note: The yielded array holds a memory reference - that is only valid until the next chunk is added. + new chunk is added. The recording is a 32-bit float array. """ arr = array.array('f') return self.recorded.map(arr.extend).map( - lambda _: np.frombuffer(arr, 'f')).aiter(skip_to_last=True) + lambda _: arr).aiter(skip_to_last=True) @dataclass