diff --git a/shazamio/algorithm.py b/shazamio/algorithm.py index e7ac8a0..7f84adf 100644 --- a/shazamio/algorithm.py +++ b/shazamio/algorithm.py @@ -100,13 +100,6 @@ def get_next_signature(self) -> Optional[DecodedMessage]: ) self.samples_processed += 128 - returned_signature = self.next_signature - - self.next_signature = DecodedMessage() - self.next_signature.sample_rate_hz = 16000 - self.next_signature.number_samples = 0 - self.next_signature.frequency_band_to_sound_peaks = {} - self.ring_buffer_of_samples: RingBuffer[int] = RingBuffer(buffer_size=2048, default_value=0) self.fft_outputs: RingBuffer[List[float]] = RingBuffer( buffer_size=256, default_value=[0.0 * 1025] @@ -115,7 +108,7 @@ def get_next_signature(self) -> Optional[DecodedMessage]: buffer_size=256, default_value=[0] * 1025 ) - return returned_signature + return self.next_signature def process_input(self, s16le_mono_samples: List[int]): self.next_signature.number_samples += len(s16le_mono_samples) diff --git a/shazamio/api.py b/shazamio/api.py index e7e04a2..62040b3 100644 --- a/shazamio/api.py +++ b/shazamio/api.py @@ -373,13 +373,10 @@ async def recognize_song( signature_generator = self.create_signature_generator(audio) signature = signature_generator.get_next_signature() - if len(signature_generator.input_pending_processing) < 128: + if signature is None: return {"matches": []} - while not signature: - signature = signature_generator.get_next_signature() - results = await self.send_recognize_request(signature) - return results + return await self.send_recognize_request(signature) async def send_recognize_request(self, sig: DecodedMessage) -> Dict[str, Any]: data = Converter.data_search( diff --git a/tests/test_get_next_signature.py b/tests/test_get_next_signature.py new file mode 100644 index 0000000..b22df38 --- /dev/null +++ b/tests/test_get_next_signature.py @@ -0,0 +1,26 @@ +from shazamio.algorithm import SignatureGenerator +from shazamio.signature import DecodedMessage +import random +import pytest + + +@pytest.fixture +def signature_instance(): + instance = SignatureGenerator() + instance.input_pending_processing = [ + random.randint(-32768, 32767) for _ in range(128 * 2) + ] + return instance + + +def test_no_samples_to_process(signature_instance): + signature_instance.samples_processed = len( + signature_instance.input_pending_processing + ) + result = signature_instance.get_next_signature() + assert result is None + + +def test_successful_signature_extraction(signature_instance): + result = signature_instance.get_next_signature() + assert isinstance(result, DecodedMessage)