From 1e982bbdbd5928e7d2912820908f77893b2d0849 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 27 Aug 2022 21:33:37 +0300 Subject: [PATCH 1/2] Fix hang when recognize_song feed with less than 8ms sound --- shazamio/api.py | 4 ++++ tests/test_recognize.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/shazamio/api.py b/shazamio/api.py index e21177f..aabcc98 100644 --- a/shazamio/api.py +++ b/shazamio/api.py @@ -267,6 +267,10 @@ async def recognize_song( audio = self.normalize_audio_data(song) signature_generator = self.create_signature_generator(audio) signature = signature_generator.get_next_signature() + + if len(signature_generator.input_pending_processing) < 128: + return {'matches': []} + while not signature: signature = signature_generator.get_next_signature() results = await self.send_recognize_request(signature) diff --git a/tests/test_recognize.py b/tests/test_recognize.py index 4813112..13cb5f9 100644 --- a/tests/test_recognize.py +++ b/tests/test_recognize.py @@ -1,9 +1,12 @@ import pytest_asyncio +from pydub import AudioSegment +from io import BytesIO from shazamio import Shazam from shazamio.utils import get_file_bytes + @pytest_asyncio.fixture(scope="session") async def song_bytes(): yield await get_file_bytes(file="examples/data/dora.ogg") @@ -23,3 +26,17 @@ async def test_recognize_song_bytes(song_bytes: bytes): assert out.get("matches") != [] assert out["track"]["key"] == "549679333" + + +async def test_recognize_song_too_short(): + short_audio_segment = AudioSegment.from_file(file=BytesIO(b'0'*126), + format='pcm', + sample_width=2, + frame_rate=16000, + channels=1) + + shazam = Shazam() + out = await shazam.recognize_song(data=short_audio_segment) + + assert out.get("matches") == [] + assert "track" not in out \ No newline at end of file From 395f7ed19fdea9e11df9f21467a1568e7c1d3495 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 27 Aug 2022 21:44:12 +0300 Subject: [PATCH 2/2] black formatting --- shazamio/api.py | 2 +- tests/test_recognize.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/shazamio/api.py b/shazamio/api.py index aabcc98..fdad6cb 100644 --- a/shazamio/api.py +++ b/shazamio/api.py @@ -269,7 +269,7 @@ async def recognize_song( signature = signature_generator.get_next_signature() if len(signature_generator.input_pending_processing) < 128: - return {'matches': []} + return {"matches": []} while not signature: signature = signature_generator.get_next_signature() diff --git a/tests/test_recognize.py b/tests/test_recognize.py index 13cb5f9..e5a24f5 100644 --- a/tests/test_recognize.py +++ b/tests/test_recognize.py @@ -6,7 +6,6 @@ from shazamio.utils import get_file_bytes - @pytest_asyncio.fixture(scope="session") async def song_bytes(): yield await get_file_bytes(file="examples/data/dora.ogg") @@ -29,14 +28,16 @@ async def test_recognize_song_bytes(song_bytes: bytes): async def test_recognize_song_too_short(): - short_audio_segment = AudioSegment.from_file(file=BytesIO(b'0'*126), - format='pcm', - sample_width=2, - frame_rate=16000, - channels=1) + short_audio_segment = AudioSegment.from_file( + file=BytesIO(b"0" * 126), + format="pcm", + sample_width=2, + frame_rate=16000, + channels=1, + ) shazam = Shazam() out = await shazam.recognize_song(data=short_audio_segment) assert out.get("matches") == [] - assert "track" not in out \ No newline at end of file + assert "track" not in out