-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: convert qq_official voice messages to wav #6944
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
Closed
1zzxy1
wants to merge
2
commits into
AstrBotDevs:master
from
1zzxy1:fix/qq-official-voice-wav-conversion
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,97 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from astrbot.core.message.components import Record | ||
| from astrbot.core.message.message_event_result import MessageChain | ||
| from astrbot.core.platform.sources.qqofficial.qqofficial_message_event import ( | ||
| QQOfficialMessageEvent, | ||
| ) | ||
|
|
||
|
|
||
| def _wav_header() -> bytes: | ||
| return b"RIFF\x00\x00\x00\x00WAVEfmt " | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_parse_to_qqofficial_converts_non_wav_record_before_silk( | ||
| tmp_path: Path, monkeypatch: pytest.MonkeyPatch | ||
| ): | ||
| source_path = tmp_path / "voice.mp3" | ||
| source_path.write_bytes(b"ID3fake-mp3") | ||
| converted_paths: list[Path] = [] | ||
|
|
||
| async def fake_convert_audio_to_wav(audio_path: str, output_path: str | None = None): | ||
| assert audio_path == str(source_path) | ||
| assert output_path is not None | ||
| converted_path = Path(output_path) | ||
| converted_path.write_bytes(_wav_header()) | ||
| converted_paths.append(converted_path) | ||
| return output_path | ||
|
|
||
| async def fake_wav_to_tencent_silk(wav_path: str, silk_path: str): | ||
| assert converted_paths | ||
| assert wav_path == str(converted_paths[0]) | ||
| Path(silk_path).write_bytes(b"fake-silk") | ||
| return 1200 | ||
|
|
||
| monkeypatch.setattr( | ||
| "astrbot.core.platform.sources.qqofficial.qqofficial_message_event.get_astrbot_temp_path", | ||
| lambda: str(tmp_path), | ||
| ) | ||
| monkeypatch.setattr( | ||
| "astrbot.core.platform.sources.qqofficial.qqofficial_message_event.convert_audio_to_wav", | ||
| fake_convert_audio_to_wav, | ||
| ) | ||
| monkeypatch.setattr( | ||
| "astrbot.core.platform.sources.qqofficial.qqofficial_message_event.wav_to_tencent_silk", | ||
| fake_wav_to_tencent_silk, | ||
| ) | ||
|
|
||
| result = await QQOfficialMessageEvent._parse_to_qqofficial( | ||
| MessageChain([Record.fromFileSystem(str(source_path))]) | ||
| ) | ||
|
|
||
| assert converted_paths | ||
| assert not converted_paths[0].exists() | ||
| assert result[3] is not None | ||
| assert str(result[3]).endswith(".silk") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_parse_to_qqofficial_skips_conversion_for_wav_record( | ||
| tmp_path: Path, monkeypatch: pytest.MonkeyPatch | ||
| ): | ||
| source_path = tmp_path / "voice.wav" | ||
| source_path.write_bytes(_wav_header()) | ||
| conversions: list[tuple[str, str | None]] = [] | ||
|
|
||
| async def fake_convert_audio_to_wav(audio_path: str, output_path: str | None = None): | ||
| conversions.append((audio_path, output_path)) | ||
| return output_path or audio_path | ||
|
|
||
| async def fake_wav_to_tencent_silk(wav_path: str, silk_path: str): | ||
| assert wav_path == str(source_path) | ||
| Path(silk_path).write_bytes(b"fake-silk") | ||
| return 800 | ||
|
|
||
| monkeypatch.setattr( | ||
| "astrbot.core.platform.sources.qqofficial.qqofficial_message_event.get_astrbot_temp_path", | ||
| lambda: str(tmp_path), | ||
| ) | ||
| monkeypatch.setattr( | ||
| "astrbot.core.platform.sources.qqofficial.qqofficial_message_event.convert_audio_to_wav", | ||
| fake_convert_audio_to_wav, | ||
| ) | ||
| monkeypatch.setattr( | ||
| "astrbot.core.platform.sources.qqofficial.qqofficial_message_event.wav_to_tencent_silk", | ||
| fake_wav_to_tencent_silk, | ||
| ) | ||
|
|
||
| result = await QQOfficialMessageEvent._parse_to_qqofficial( | ||
| MessageChain([Record.fromFileSystem(str(source_path))]) | ||
| ) | ||
|
|
||
| assert conversions == [] | ||
| assert result[3] is not None | ||
| assert str(result[3]).endswith(".silk") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_is_wav_audio_filemethod performs synchronous file I/O usingopen(), which can block the asyncio event loop. Sinceaiofilesis already used in the project, it's better to use it here for non-blocking I/O. This method should be converted to anasyncmethod, and the call site updated withawait.