-
Notifications
You must be signed in to change notification settings - Fork 9
[INFERENCE][BUG] Fix last audio chunk sent twice during inference #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b474f71
Remove unnecessary reprocessing of the last audio chunk
sarapapi dde4837
Add tests
sarapapi 4e0f8fb
Fix Lint
sarapapi eef73a2
Apply suggestions from code review
sarapapi 1ec8986
Add check empty buffer with exact multiple
sarapapi b72059c
Correct casting in tests
sarapapi 69a0418
Correct casting in tests
sarapapi b1788d9
Remove logging patch
sarapapi f8a027a
Remove patch import from unittest
sarapapi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Copyright 2026 FBK | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock | ||
| import numpy as np | ||
|
|
||
| from simulstream.inference import process_audio | ||
| from simulstream.server.message_processor import MessageProcessor | ||
| from simulstream.server.speech_processors import SAMPLE_RATE | ||
| from simulstream.server.speech_processors.incremental_output import IncrementalOutput | ||
|
|
||
|
|
||
| def make_speech_processor(chunk_size_seconds=1.0): | ||
| """Creates a mock SpeechProcessor with the minimal interface needed.""" | ||
| mock_output = IncrementalOutput( | ||
| new_tokens=[], deleted_tokens=0, new_string="", deleted_string="") | ||
| processor = MagicMock( | ||
| spec=["speech_chunk_size", "process_chunk", "end_of_stream", "clear", "tokens_to_string"]) | ||
| processor.speech_chunk_size = chunk_size_seconds | ||
| processor.process_chunk.return_value = mock_output | ||
| processor.end_of_stream.return_value = mock_output | ||
| processor.tokens_to_string.return_value = "" | ||
| return processor | ||
|
|
||
|
|
||
| def make_message_processor(chunk_size_seconds=1.0): | ||
| speech_processor = make_speech_processor(chunk_size_seconds) | ||
| return MessageProcessor(client_id=0, speech_processor=speech_processor) | ||
|
|
||
|
|
||
| class TestProcessAudio(unittest.TestCase): | ||
|
|
||
| def test_exact_multiple(self): | ||
| chunk_size = 1.0 | ||
| message_processor = make_message_processor(chunk_size) | ||
| # 2 Full chunks, no reminder | ||
| data = np.zeros(SAMPLE_RATE * 2, dtype=np.int16) | ||
|
|
||
| process_audio(message_processor, SAMPLE_RATE, data) | ||
|
|
||
| self.assertEqual(message_processor.speech_processor.process_chunk.call_count, 2) | ||
| self.assertEqual(message_processor.client_buffer, b'') | ||
|
|
||
| def test_remainder_chunk_not_sent_twice(self): | ||
| chunk_size = 1.0 | ||
| message_processor = make_message_processor(chunk_size) | ||
| # 2 Full chunks + a remainder of 0.5s | ||
| data = np.zeros(int(SAMPLE_RATE * 2.5), dtype=np.int16) | ||
|
|
||
| process_audio(message_processor, SAMPLE_RATE, data) | ||
|
|
||
| # Process_chunk processes full chunks only; remainder stays buffered for end_of_stream | ||
| self.assertEqual(message_processor.speech_processor.process_chunk.call_count, 2) | ||
| # Each sample is int16 (2 bytes), so the buffer size in bytes is samples * 2 | ||
| self.assertEqual(len(message_processor.client_buffer), int(SAMPLE_RATE * 0.5) * 2) | ||
|
|
||
| def test_single_chunk(self): | ||
| chunk_size = 1.0 | ||
| message_processor = make_message_processor(chunk_size) | ||
| # Data smaller than one chunk (process_chunk not called, data stays buffered) | ||
| data = np.zeros(SAMPLE_RATE // 2, dtype=np.int16) # 0.5s | ||
|
|
||
| process_audio(message_processor, SAMPLE_RATE, data) | ||
|
|
||
| message_processor.speech_processor.process_chunk.assert_not_called() | ||
| # Each sample is int16 (2 bytes), so the buffer size in bytes is samples * 2 | ||
| self.assertEqual(len(message_processor.client_buffer), int(SAMPLE_RATE * 0.5) * 2) | ||
|
|
||
| def test_empty_data(self): | ||
| message_processor = make_message_processor() | ||
| # Empty array (process_chunk never called, buffer remains empty) | ||
| data = np.array([], dtype=np.int16) | ||
|
|
||
| process_audio(message_processor, SAMPLE_RATE, data) | ||
|
|
||
| message_processor.speech_processor.process_chunk.assert_not_called() | ||
| self.assertEqual(message_processor.client_buffer, b'') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is empty? rather than equal to empty one?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? There is no |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.