Skip to content

Commit

Permalink
Merge pull request #31 from Dronablo/fix_hang_on_small_input
Browse files Browse the repository at this point in the history
Fix hang when recognize_song feed with less than 8ms sound
  • Loading branch information
dotX12 authored Aug 29, 2022
2 parents 8bfd75b + 395f7ed commit 3f3c7b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions shazamio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_recognize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest_asyncio
from pydub import AudioSegment
from io import BytesIO

from shazamio import Shazam
from shazamio.utils import get_file_bytes
Expand All @@ -23,3 +25,19 @@ 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

0 comments on commit 3f3c7b9

Please sign in to comment.