Skip to content

Commit

Permalink
Merge pull request #70 from sfendourakis/master
Browse files Browse the repository at this point in the history
minor refactoring and signature test
  • Loading branch information
dotX12 authored Dec 10, 2023
2 parents 2e3f261 + 4d74416 commit 4a7462c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
9 changes: 1 addition & 8 deletions shazamio/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions shazamio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_get_next_signature.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4a7462c

Please sign in to comment.