mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
Merge pull request #6 from Plaud-AI/feat/demo
fix(audio): Fix web client AudioWorklet recording and OpenAI ASR type annotation
This commit is contained in:
commit
e696222040
30 changed files with 8705 additions and 32 deletions
|
|
@ -1,12 +1,14 @@
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from openai.types.audio import transcription
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from typing import Optional, Tuple, List
|
from typing import Optional, Tuple, List
|
||||||
from core.providers.asr.dto.dto import InterfaceType
|
from core.providers.asr.dto.dto import InterfaceType
|
||||||
from core.providers.asr.base import ASRProviderBase
|
from core.providers.asr.base import ASRProviderBase
|
||||||
|
|
||||||
import requests
|
from openai import OpenAI
|
||||||
|
from openai.types.audio import Transcription
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
@ -14,11 +16,16 @@ class ASRProvider(ASRProviderBase):
|
||||||
def __init__(self, config: dict, delete_audio_file: bool):
|
def __init__(self, config: dict, delete_audio_file: bool):
|
||||||
self.interface_type = InterfaceType.NON_STREAM
|
self.interface_type = InterfaceType.NON_STREAM
|
||||||
self.api_key = config.get("api_key")
|
self.api_key = config.get("api_key")
|
||||||
self.api_url = config.get("base_url")
|
self.base_url = config.get("base_url")
|
||||||
self.model = config.get("model_name")
|
self.model = config.get("model_name")
|
||||||
self.output_dir = config.get("output_dir")
|
self.output_dir = config.get("output_dir")
|
||||||
self.delete_audio_file = delete_audio_file
|
self.delete_audio_file = delete_audio_file
|
||||||
|
|
||||||
|
self._client = OpenAI(
|
||||||
|
api_key=self.api_key,
|
||||||
|
base_url=self.base_url
|
||||||
|
)
|
||||||
|
|
||||||
os.makedirs(self.output_dir, exist_ok=True)
|
os.makedirs(self.output_dir, exist_ok=True)
|
||||||
|
|
||||||
async def speech_to_text(self, opus_data: List[bytes], session_id: str, audio_format="opus") -> Tuple[Optional[str], Optional[str]]:
|
async def speech_to_text(self, opus_data: List[bytes], session_id: str, audio_format="opus") -> Tuple[Optional[str], Optional[str]]:
|
||||||
|
|
@ -32,44 +39,25 @@ class ASRProvider(ASRProviderBase):
|
||||||
file_path = self.save_audio_to_file(pcm_data, session_id)
|
file_path = self.save_audio_to_file(pcm_data, session_id)
|
||||||
|
|
||||||
logger.bind(tag=TAG).debug(
|
logger.bind(tag=TAG).debug(
|
||||||
f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}"
|
f"Audio file save latency: {time.time() - start_time:.3f}s | Path: {file_path}"
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.bind(tag=TAG).info(f"file path: {file_path}")
|
with open(file_path, "rb") as audio_file: # with open to ensure file is closed
|
||||||
headers = {
|
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
|
||||||
}
|
|
||||||
|
|
||||||
# 使用data参数传递模型名称
|
|
||||||
data = {
|
|
||||||
"model": self.model
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
with open(file_path, "rb") as audio_file: # 使用with语句确保文件关闭
|
|
||||||
files = {
|
|
||||||
"file": audio_file
|
|
||||||
}
|
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
response = requests.post(
|
transcription: str = self._client.audio.transcriptions.create(
|
||||||
self.api_url,
|
model=self.model,
|
||||||
files=files,
|
file=audio_file,
|
||||||
data=data,
|
response_format="text"
|
||||||
headers=headers
|
|
||||||
)
|
)
|
||||||
logger.bind(tag=TAG).debug(
|
logger.bind(tag=TAG).debug(
|
||||||
f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {response.text}"
|
f"Audio transcription latency: {time.time() - start_time:.3f}s | Result: {transcription}"
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code == 200:
|
return transcription, file_path
|
||||||
text = response.json().get("text", "")
|
|
||||||
return text, file_path
|
|
||||||
else:
|
|
||||||
raise Exception(f"API请求失败: {response.status_code} - {response.text}")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"语音识别失败: {e}")
|
logger.bind(tag=TAG).error(f"Audio transcription failed: {e}")
|
||||||
return "", None
|
return "", None
|
||||||
finally:
|
finally:
|
||||||
# 文件清理逻辑
|
# 文件清理逻辑
|
||||||
|
|
|
||||||
126
main/xiaozhi-server/core/providers/asr/test/QUICKSTART.txt
Normal file
126
main/xiaozhi-server/core/providers/asr/test/QUICKSTART.txt
Normal file
|
|
@ -0,0 +1,126 @@
|
||||||
|
ASR Test Quick Start Guide
|
||||||
|
==========================
|
||||||
|
|
||||||
|
1. Set API Keys (one-time setup)
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
Add to ~/.zshrc for permanent access:
|
||||||
|
|
||||||
|
# Groq
|
||||||
|
echo 'export GROQ_API_KEY="gsk_your_key_here"' >> ~/.zshrc
|
||||||
|
|
||||||
|
# OpenAI
|
||||||
|
echo 'export OPENAI_API_KEY="sk-proj-your_key_here"' >> ~/.zshrc
|
||||||
|
|
||||||
|
source ~/.zshrc
|
||||||
|
|
||||||
|
|
||||||
|
2. Install Dependencies
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
# Install required packages
|
||||||
|
pip install openai pyaudio
|
||||||
|
|
||||||
|
|
||||||
|
3. Run Tests
|
||||||
|
------------
|
||||||
|
|
||||||
|
cd /Users/harold/Projects/live-agent/main/xiaozhi-server/
|
||||||
|
|
||||||
|
# Test Groq ASR (with recording)
|
||||||
|
python3 -m core.providers.asr.test.test_groq
|
||||||
|
|
||||||
|
# Test Groq ASR (with existing audio file)
|
||||||
|
python3 -m core.providers.asr.test.test_groq /path/to/audio.wav
|
||||||
|
|
||||||
|
# Test OpenAI ASR (with recording)
|
||||||
|
python3 -m core.providers.asr.test.test_openai
|
||||||
|
|
||||||
|
# Test OpenAI ASR (with existing audio file)
|
||||||
|
python3 -m core.providers.asr.test.test_openai /path/to/audio.wav
|
||||||
|
|
||||||
|
# Test audio recorder only
|
||||||
|
python3 -m core.providers.asr.test.audio_recorder
|
||||||
|
|
||||||
|
|
||||||
|
4. Available Models
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Groq Models:
|
||||||
|
- whisper-large-v3 (default, best accuracy)
|
||||||
|
- whisper-large-v3-turbo (faster)
|
||||||
|
|
||||||
|
OpenAI Models:
|
||||||
|
- whisper-1 (official OpenAI Whisper)
|
||||||
|
|
||||||
|
|
||||||
|
5. Recording Instructions
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
When running the test:
|
||||||
|
1. Press Enter to START recording
|
||||||
|
2. Speak into your microphone
|
||||||
|
3. Press Enter to STOP recording
|
||||||
|
4. Wait for transcription result
|
||||||
|
|
||||||
|
|
||||||
|
6. Example Output
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
🤖 OpenAI ASR Test
|
||||||
|
API: https://api.openai.com/v1
|
||||||
|
Model: whisper-1
|
||||||
|
Audio: core/providers/asr/test/tmp/recording_20251111_143022.wav
|
||||||
|
Size: 256.3 KB
|
||||||
|
|
||||||
|
⏳ Transcribing audio...
|
||||||
|
============================================================
|
||||||
|
✅ Transcription Result:
|
||||||
|
============================================================
|
||||||
|
|
||||||
|
Hello, this is a test of OpenAI Whisper speech recognition.
|
||||||
|
|
||||||
|
============================================================
|
||||||
|
⏱️ Latency: 1523 ms (1.52s)
|
||||||
|
============================================================
|
||||||
|
|
||||||
|
✅ Test completed successfully!
|
||||||
|
|
||||||
|
|
||||||
|
7. Check Output
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Generated audio files are in:
|
||||||
|
core/providers/asr/test/tmp/
|
||||||
|
- recording_YYYYMMDD_HHMMSS.wav
|
||||||
|
|
||||||
|
|
||||||
|
8. Troubleshooting
|
||||||
|
------------------
|
||||||
|
|
||||||
|
If API key error:
|
||||||
|
echo $GROQ_API_KEY # Check Groq key
|
||||||
|
echo $OPENAI_API_KEY # Check OpenAI key
|
||||||
|
source ~/.zshrc # Reload config
|
||||||
|
|
||||||
|
If pyaudio not installed:
|
||||||
|
pip install pyaudio
|
||||||
|
|
||||||
|
# macOS (if pip install fails):
|
||||||
|
brew install portaudio
|
||||||
|
pip install pyaudio
|
||||||
|
|
||||||
|
If OpenAI SDK not installed:
|
||||||
|
pip install openai
|
||||||
|
|
||||||
|
If module not found:
|
||||||
|
cd /Users/harold/Projects/live-agent/main/xiaozhi-server/
|
||||||
|
# Make sure you're in the right directory!
|
||||||
|
|
||||||
|
Get API keys:
|
||||||
|
Groq: https://console.groq.com/keys
|
||||||
|
OpenAI: https://platform.openai.com/api-keys
|
||||||
|
|
||||||
|
|
||||||
|
That's it! 🎉
|
||||||
|
|
||||||
2
main/xiaozhi-server/core/providers/asr/test/__init__.py
Normal file
2
main/xiaozhi-server/core/providers/asr/test/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
# ASR Test Module
|
||||||
|
|
||||||
186
main/xiaozhi-server/core/providers/asr/test/audio_recorder.py
Normal file
186
main/xiaozhi-server/core/providers/asr/test/audio_recorder.py
Normal file
|
|
@ -0,0 +1,186 @@
|
||||||
|
"""
|
||||||
|
Audio Recorder Utility for ASR Testing
|
||||||
|
|
||||||
|
Simple command-line based audio recorder that supports cross-platform recording.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import wave
|
||||||
|
import time
|
||||||
|
import platform
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pyaudio
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: pyaudio not installed")
|
||||||
|
print(" Install with: pip install pyaudio")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
class AudioRecorder:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
sample_rate: int = 16000,
|
||||||
|
channels: int = 1,
|
||||||
|
chunk_size: int = 1024,
|
||||||
|
format: int = pyaudio.paInt16
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Initialize audio recorder
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sample_rate: Sampling rate in Hz (default: 16000)
|
||||||
|
channels: Number of audio channels (default: 1 for mono)
|
||||||
|
chunk_size: Audio buffer size
|
||||||
|
format: Audio format (default: 16-bit PCM)
|
||||||
|
"""
|
||||||
|
self.sample_rate = sample_rate
|
||||||
|
self.channels = channels
|
||||||
|
self.chunk_size = chunk_size
|
||||||
|
self.format = format
|
||||||
|
self.audio = pyaudio.PyAudio()
|
||||||
|
|
||||||
|
def record_audio(self, output_path: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
Record audio with interactive command-line control
|
||||||
|
|
||||||
|
Args:
|
||||||
|
output_path: Path to save the WAV file (optional)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Path to the saved audio file
|
||||||
|
"""
|
||||||
|
if output_path is None:
|
||||||
|
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
||||||
|
output_path = os.path.join(
|
||||||
|
os.path.dirname(__file__),
|
||||||
|
"tmp",
|
||||||
|
f"recording_{timestamp}.wav"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ensure output directory exists
|
||||||
|
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||||
|
|
||||||
|
print("\n🎤 Audio Recorder")
|
||||||
|
print(f" Sample Rate: {self.sample_rate} Hz")
|
||||||
|
print(f" Channels: {self.channels} (Mono)")
|
||||||
|
print(f" Format: 16-bit PCM")
|
||||||
|
print(f"\n📁 Output: {output_path}\n")
|
||||||
|
|
||||||
|
# Wait for user to start recording
|
||||||
|
input("▶️ Press Enter to start recording...")
|
||||||
|
|
||||||
|
print("\n🔴 Recording... Press Enter to stop")
|
||||||
|
|
||||||
|
# Open audio stream
|
||||||
|
stream = self.audio.open(
|
||||||
|
format=self.format,
|
||||||
|
channels=self.channels,
|
||||||
|
rate=self.sample_rate,
|
||||||
|
input=True,
|
||||||
|
frames_per_buffer=self.chunk_size
|
||||||
|
)
|
||||||
|
|
||||||
|
frames = []
|
||||||
|
recording = True
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
# Start recording in a separate thread to allow keyboard interrupt
|
||||||
|
import threading
|
||||||
|
|
||||||
|
def record_frames():
|
||||||
|
while recording:
|
||||||
|
try:
|
||||||
|
data = stream.read(self.chunk_size, exception_on_overflow=False)
|
||||||
|
frames.append(data)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Warning: {e}")
|
||||||
|
break
|
||||||
|
|
||||||
|
record_thread = threading.Thread(target=record_frames, daemon=True)
|
||||||
|
record_thread.start()
|
||||||
|
|
||||||
|
# Wait for user to stop recording
|
||||||
|
try:
|
||||||
|
input()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n")
|
||||||
|
|
||||||
|
recording = False
|
||||||
|
record_thread.join(timeout=1.0)
|
||||||
|
|
||||||
|
duration = time.time() - start_time
|
||||||
|
|
||||||
|
# Stop and close stream
|
||||||
|
stream.stop_stream()
|
||||||
|
stream.close()
|
||||||
|
|
||||||
|
print(f"⏹️ Recording stopped ({duration:.1f}s)")
|
||||||
|
|
||||||
|
# Save to WAV file
|
||||||
|
with wave.open(output_path, 'wb') as wf:
|
||||||
|
wf.setnchannels(self.channels)
|
||||||
|
wf.setsampwidth(self.audio.get_sample_size(self.format))
|
||||||
|
wf.setframerate(self.sample_rate)
|
||||||
|
wf.writeframes(b''.join(frames))
|
||||||
|
|
||||||
|
file_size = os.path.getsize(output_path) / 1024 # KB
|
||||||
|
print(f"✅ Audio saved: {file_size:.1f} KB\n")
|
||||||
|
|
||||||
|
return output_path
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
"""Clean up PyAudio resources"""
|
||||||
|
self.audio.terminate()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
self.cleanup()
|
||||||
|
|
||||||
|
|
||||||
|
def record_audio_file(output_path: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
Convenient function to record audio file
|
||||||
|
|
||||||
|
Args:
|
||||||
|
output_path: Path to save the WAV file (optional)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Path to the saved audio file
|
||||||
|
"""
|
||||||
|
with AudioRecorder() as recorder:
|
||||||
|
return recorder.record_audio(output_path)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
"""
|
||||||
|
Test the audio recorder
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
python3 -m core.providers.asr.test.audio_recorder
|
||||||
|
python3 -m core.providers.asr.test.audio_recorder output.wav
|
||||||
|
"""
|
||||||
|
output_file = sys.argv[1] if len(sys.argv) > 1 else None
|
||||||
|
|
||||||
|
try:
|
||||||
|
audio_path = record_audio_file(output_file)
|
||||||
|
print(f"🎉 Recording completed: {audio_path}")
|
||||||
|
|
||||||
|
# Play audio on macOS
|
||||||
|
if platform.system() == "Darwin":
|
||||||
|
print("\n🔊 Playing back audio...")
|
||||||
|
os.system(f"afplay '{audio_path}'")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Recording cancelled")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
143
main/xiaozhi-server/core/providers/asr/test/test_groq.py
Normal file
143
main/xiaozhi-server/core/providers/asr/test/test_groq.py
Normal file
|
|
@ -0,0 +1,143 @@
|
||||||
|
"""
|
||||||
|
Groq ASR (Whisper) Test
|
||||||
|
|
||||||
|
This script tests Groq's speech-to-text API using the OpenAI SDK.
|
||||||
|
Groq is compatible with OpenAI's API format.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
# Record and transcribe
|
||||||
|
python3 -m core.providers.asr.test.test_groq
|
||||||
|
|
||||||
|
# Transcribe existing audio file
|
||||||
|
python3 -m core.providers.asr.test.test_groq path/to/audio.wav
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
GROQ_API_KEY - Your Groq API key (required)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
export GROQ_API_KEY="gsk_your_key_here"
|
||||||
|
python3 -m core.providers.asr.test.test_groq
|
||||||
|
python3 -m core.providers.asr.test.test_groq /path/to/test.wav
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import platform
|
||||||
|
|
||||||
|
# Check if OpenAI SDK is installed
|
||||||
|
try:
|
||||||
|
from openai import OpenAI
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: openai package not installed")
|
||||||
|
print(" Install with: pip install openai")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Import audio recorder
|
||||||
|
try:
|
||||||
|
from core.providers.asr.test.audio_recorder import record_audio_file
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: Could not import audio_recorder")
|
||||||
|
print(" Make sure you're running from xiaozhi-server directory")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_groq_asr(audio_file_path: str):
|
||||||
|
"""
|
||||||
|
Test Groq ASR with the given audio file
|
||||||
|
|
||||||
|
Args:
|
||||||
|
audio_file_path: Path to the audio WAV file
|
||||||
|
api_key: Groq API key
|
||||||
|
model: Groq Whisper model name (default: whisper-large-v3)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (transcribed_text, latency_ms)
|
||||||
|
"""
|
||||||
|
print(f"🤖 Groq ASR Test")
|
||||||
|
print(f" API: https://api.groq.com/openai/v1")
|
||||||
|
model = "whisper-large-v3"
|
||||||
|
api_key = os.environ.get("GROQ_API_KEY")
|
||||||
|
print(f" Model: {model}")
|
||||||
|
print(f" Audio: {audio_file_path}")
|
||||||
|
|
||||||
|
# Get file size
|
||||||
|
file_size = os.path.getsize(audio_file_path) / 1024 # KB
|
||||||
|
print(f" Size: {file_size:.1f} KB\n")
|
||||||
|
|
||||||
|
# Initialize OpenAI client with Groq endpoint
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=api_key,
|
||||||
|
base_url="https://api.groq.com/openai/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("⏳ Transcribing audio...")
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
# Open audio file and send to Groq
|
||||||
|
with open(audio_file_path, "rb") as audio_file:
|
||||||
|
transcription = client.audio.transcriptions.create(
|
||||||
|
model=model,
|
||||||
|
file=audio_file,
|
||||||
|
response_format="text"
|
||||||
|
)
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
latency_ms = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
# Get transcription text
|
||||||
|
if isinstance(transcription, str):
|
||||||
|
text = transcription
|
||||||
|
else:
|
||||||
|
text = transcription.text if hasattr(transcription, 'text') else str(transcription)
|
||||||
|
|
||||||
|
return text, latency_ms
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Transcription failed: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Check if audio file path is provided as argument
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
audio_path = sys.argv[1]
|
||||||
|
if not os.path.exists(audio_path):
|
||||||
|
print(f"❌ Error: Audio file not found: {audio_path}")
|
||||||
|
sys.exit(1)
|
||||||
|
print(f"📂 Using provided audio file: {audio_path}\n")
|
||||||
|
else:
|
||||||
|
# Record audio
|
||||||
|
audio_path = record_audio_file()
|
||||||
|
|
||||||
|
# Test Groq ASR
|
||||||
|
text, latency = test_groq_asr(audio_path)
|
||||||
|
|
||||||
|
# Print results
|
||||||
|
print("=" * 60)
|
||||||
|
print("✅ Transcription Result:")
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"\n{text}\n")
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"⏱️ Latency: {latency:.0f} ms ({latency/1000:.2f}s)")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
# Play audio on macOS if it was just recorded
|
||||||
|
if len(sys.argv) == 1 and platform.system() == "Darwin":
|
||||||
|
print("\n🔊 Playing back audio...")
|
||||||
|
os.system(f"afplay '{audio_path}'")
|
||||||
|
|
||||||
|
print("\n✅ Test completed successfully!")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Test interrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
144
main/xiaozhi-server/core/providers/asr/test/test_openai.py
Normal file
144
main/xiaozhi-server/core/providers/asr/test/test_openai.py
Normal file
|
|
@ -0,0 +1,144 @@
|
||||||
|
"""
|
||||||
|
OpenAI ASR (Whisper) Test
|
||||||
|
|
||||||
|
This script tests OpenAI's official Whisper speech-to-text API.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
# Record and transcribe
|
||||||
|
python3 -m core.providers.asr.test.test_openai
|
||||||
|
|
||||||
|
# Transcribe existing audio file
|
||||||
|
python3 -m core.providers.asr.test.test_openai path/to/audio.wav
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
OPENAI_API_KEY - Your OpenAI API key (required)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
export OPENAI_API_KEY="sk-proj-your_key_here"
|
||||||
|
python3 -m core.providers.asr.test.test_openai
|
||||||
|
python3 -m core.providers.asr.test.test_openai /path/to/test.wav
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import platform
|
||||||
|
|
||||||
|
# Check if OpenAI SDK is installed
|
||||||
|
try:
|
||||||
|
from openai import OpenAI
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: openai package not installed")
|
||||||
|
print(" Install with: pip install openai")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Import audio recorder
|
||||||
|
try:
|
||||||
|
from core.providers.asr.test.audio_recorder import record_audio_file
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: Could not import audio_recorder")
|
||||||
|
print(" Make sure you're running from xiaozhi-server directory")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_openai_asr(audio_file_path: str):
|
||||||
|
"""
|
||||||
|
Test OpenAI Whisper ASR with the given audio file
|
||||||
|
|
||||||
|
Args:
|
||||||
|
audio_file_path: Path to the audio WAV file
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (transcribed_text, latency_ms)
|
||||||
|
"""
|
||||||
|
api_key = os.environ.get("OPENAI_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
print("❌ Error: OPENAI_API_KEY not set")
|
||||||
|
print(" export OPENAI_API_KEY='sk-proj-your_key_here'")
|
||||||
|
print("\n💡 Get your API key from: https://platform.openai.com/api-keys")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# model = "whisper-1" # transcription performance is low
|
||||||
|
model = "gpt-4o-transcribe"
|
||||||
|
|
||||||
|
print(f"🤖 OpenAI ASR Test")
|
||||||
|
print(f" API: https://api.openai.com/v1")
|
||||||
|
print(f" Model: {model}")
|
||||||
|
print(f" Audio: {audio_file_path}")
|
||||||
|
|
||||||
|
# Get file size
|
||||||
|
file_size = os.path.getsize(audio_file_path) / 1024 # KB
|
||||||
|
print(f" Size: {file_size:.1f} KB\n")
|
||||||
|
|
||||||
|
# Initialize OpenAI client
|
||||||
|
client = OpenAI(api_key=api_key)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("⏳ Transcribing audio...")
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
# Open audio file and send to OpenAI
|
||||||
|
with open(audio_file_path, "rb") as audio_file:
|
||||||
|
transcription = client.audio.transcriptions.create(
|
||||||
|
model=model,
|
||||||
|
file=audio_file,
|
||||||
|
response_format="text"
|
||||||
|
)
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
latency_ms = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
# Get transcription text
|
||||||
|
if isinstance(transcription, str):
|
||||||
|
text = transcription
|
||||||
|
else:
|
||||||
|
text = transcription.text if hasattr(transcription, 'text') else str(transcription)
|
||||||
|
|
||||||
|
return text, latency_ms
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Transcription failed: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
# Check if audio file path is provided as argument
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
audio_path = sys.argv[1]
|
||||||
|
if not os.path.exists(audio_path):
|
||||||
|
print(f"❌ Error: Audio file not found: {audio_path}")
|
||||||
|
sys.exit(1)
|
||||||
|
print(f"📂 Using provided audio file: {audio_path}\n")
|
||||||
|
else:
|
||||||
|
# Record audio
|
||||||
|
audio_path = record_audio_file()
|
||||||
|
|
||||||
|
# Test OpenAI ASR
|
||||||
|
text, latency = test_openai_asr(audio_path)
|
||||||
|
|
||||||
|
# Print results
|
||||||
|
print("=" * 60)
|
||||||
|
print("✅ Transcription Result:")
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"\n{text}\n")
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"⏱️ Latency: {latency:.0f} ms ({latency/1000:.2f}s)")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
# Play audio on macOS if it was just recorded
|
||||||
|
if len(sys.argv) == 1 and platform.system() == "Darwin":
|
||||||
|
print("\n🔊 Playing back audio...")
|
||||||
|
os.system(f"afplay '{audio_path}'")
|
||||||
|
|
||||||
|
print("\n✅ Test completed successfully!")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Test interrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
147
main/xiaozhi-server/core/providers/llm/test/BENCHMARK.txt
Normal file
147
main/xiaozhi-server/core/providers/llm/test/BENCHMARK.txt
Normal file
|
|
@ -0,0 +1,147 @@
|
||||||
|
LLM Model Benchmark Guide
|
||||||
|
=========================
|
||||||
|
|
||||||
|
This benchmark script tests multiple LLM models from different providers
|
||||||
|
and generates a detailed performance comparison report.
|
||||||
|
|
||||||
|
1. Prerequisites
|
||||||
|
----------------
|
||||||
|
|
||||||
|
# Set API keys
|
||||||
|
export GROQ_API_KEY="gsk_your_key_here"
|
||||||
|
export OPENROUTER_API_KEY="sk-or-v1-your_key_here"
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
pip install openai
|
||||||
|
|
||||||
|
|
||||||
|
2. Quick Start
|
||||||
|
--------------
|
||||||
|
|
||||||
|
cd /Users/harold/Projects/live-agent/main/xiaozhi-server/
|
||||||
|
|
||||||
|
# Run the benchmark
|
||||||
|
python3 -m core.providers.llm.test.benchmark
|
||||||
|
|
||||||
|
# Output file: core/providers/llm/test/benchmark_report.md
|
||||||
|
|
||||||
|
|
||||||
|
3. What Gets Tested
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Groq Models:
|
||||||
|
- llama-3.3-70b-versatile
|
||||||
|
- openai/gpt-oss-120b
|
||||||
|
- groq/compound
|
||||||
|
|
||||||
|
OpenRouter Models:
|
||||||
|
- openai/gpt-4o
|
||||||
|
- anthropic/claude-sonnet-4.5
|
||||||
|
- openai/gpt-5
|
||||||
|
- google/gemini-2.5
|
||||||
|
|
||||||
|
|
||||||
|
4. Metrics Collected
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
For each model:
|
||||||
|
✅ TTFT (Time to First Token) - How fast the model starts responding
|
||||||
|
✅ Total Latency - Complete response time
|
||||||
|
✅ Response Length - Character count
|
||||||
|
✅ Token Chunks - Number of streaming chunks
|
||||||
|
✅ Avg Token Time - Average generation speed per token
|
||||||
|
|
||||||
|
|
||||||
|
5. Customizing the Test
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
Edit benchmark.py to customize:
|
||||||
|
|
||||||
|
# Change test prompt
|
||||||
|
TEST_PROMPT = "Your custom question here"
|
||||||
|
|
||||||
|
# Add/remove models
|
||||||
|
GROQ_MODELS = [
|
||||||
|
"llama-3.3-70b-versatile",
|
||||||
|
"your-custom-model",
|
||||||
|
]
|
||||||
|
|
||||||
|
OPENROUTER_MODELS = [
|
||||||
|
"openai/gpt-4o",
|
||||||
|
"your-custom-model",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
6. Output Report Structure
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
The generated markdown report includes:
|
||||||
|
|
||||||
|
📊 Performance Metrics Summary
|
||||||
|
- Ranked table by TTFT (fastest to slowest)
|
||||||
|
- Failed model information
|
||||||
|
|
||||||
|
📈 Detailed Performance Metrics
|
||||||
|
- Per-provider breakdown
|
||||||
|
- All performance metrics
|
||||||
|
|
||||||
|
📝 Response Samples
|
||||||
|
- Preview of each model's response
|
||||||
|
|
||||||
|
🏆 Performance Analysis
|
||||||
|
- Fastest/slowest models
|
||||||
|
- Provider comparison
|
||||||
|
- Speed differences
|
||||||
|
|
||||||
|
|
||||||
|
7. Example Output
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
See benchmark_report_example.md for a sample output.
|
||||||
|
|
||||||
|
Key insights from example:
|
||||||
|
- Groq models: 234-289ms TTFT
|
||||||
|
- OpenRouter models: 412-612ms TTFT
|
||||||
|
- Groq is ~50% faster on average
|
||||||
|
|
||||||
|
|
||||||
|
8. Tips
|
||||||
|
-------
|
||||||
|
|
||||||
|
✅ Run during off-peak hours for consistent results
|
||||||
|
✅ Multiple runs recommended for averaging
|
||||||
|
✅ Script includes 1-second delay between tests (rate limiting)
|
||||||
|
✅ Failed models are tracked but don't stop the benchmark
|
||||||
|
✅ Results saved automatically to benchmark_report.md
|
||||||
|
|
||||||
|
|
||||||
|
9. Troubleshooting
|
||||||
|
------------------
|
||||||
|
|
||||||
|
If models fail:
|
||||||
|
- Check API key validity
|
||||||
|
- Verify model names are correct
|
||||||
|
- Check rate limits
|
||||||
|
- Review error messages in report
|
||||||
|
|
||||||
|
If script hangs:
|
||||||
|
- Press Ctrl+C to stop
|
||||||
|
- Partial results may be incomplete
|
||||||
|
- Re-run for complete benchmark
|
||||||
|
|
||||||
|
|
||||||
|
10. Advanced Usage
|
||||||
|
------------------
|
||||||
|
|
||||||
|
# Test specific providers only
|
||||||
|
unset OPENROUTER_API_KEY # Skip OpenRouter models
|
||||||
|
|
||||||
|
# Run multiple times for averaging
|
||||||
|
for i in {1..3}; do
|
||||||
|
python3 -m core.providers.llm.test.benchmark
|
||||||
|
mv benchmark_report.md benchmark_report_$i.md
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
That's it! 🚀
|
||||||
|
|
||||||
182
main/xiaozhi-server/core/providers/llm/test/QUICKSTART.txt
Normal file
182
main/xiaozhi-server/core/providers/llm/test/QUICKSTART.txt
Normal file
|
|
@ -0,0 +1,182 @@
|
||||||
|
LLM Test Quick Start Guide
|
||||||
|
==========================
|
||||||
|
|
||||||
|
1. Set API Keys (one-time setup)
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
Add to ~/.zshrc for permanent access:
|
||||||
|
|
||||||
|
# Groq
|
||||||
|
echo 'export GROQ_API_KEY="gsk_your_key_here"' >> ~/.zshrc
|
||||||
|
echo 'export GROQ_MODEL="openai/gpt-oss-120b"' >> ~/.zshrc # optional
|
||||||
|
|
||||||
|
# OpenRouter
|
||||||
|
echo 'export OPENROUTER_API_KEY="sk-or-v1-your_key_here"' >> ~/.zshrc
|
||||||
|
echo 'export OPENROUTER_MODEL="openai/gpt-4o"' >> ~/.zshrc # optional
|
||||||
|
|
||||||
|
# OpenAI
|
||||||
|
echo 'export OPENAI_API_KEY="sk-proj-your_key_here"' >> ~/.zshrc
|
||||||
|
echo 'export OPENAI_MODEL="gpt-4o-mini"' >> ~/.zshrc # optional
|
||||||
|
|
||||||
|
# Google Gemini
|
||||||
|
echo 'export GOOGLE_API_KEY="your_api_key_here"' >> ~/.zshrc
|
||||||
|
echo 'export GOOGLE_MODEL="gemini-2.0-flash-exp"' >> ~/.zshrc # optional
|
||||||
|
|
||||||
|
source ~/.zshrc
|
||||||
|
|
||||||
|
|
||||||
|
2. Install Dependencies
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
# Install required packages
|
||||||
|
pip install openai google-generativeai
|
||||||
|
|
||||||
|
|
||||||
|
3. Run Tests
|
||||||
|
------------
|
||||||
|
|
||||||
|
cd /Users/harold/Projects/live-agent/main/xiaozhi-server/
|
||||||
|
|
||||||
|
# Test with default prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_groq
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter
|
||||||
|
python3 -m core.providers.llm.test.test_openai
|
||||||
|
python3 -m core.providers.llm.test.test_google
|
||||||
|
|
||||||
|
# Test with custom prompt
|
||||||
|
python3 -m core.providers.llm.test.test_groq "What is machine learning?"
|
||||||
|
python3 -m core.providers.llm.test.test_openai "解释一下什么是人工智能"
|
||||||
|
|
||||||
|
# Test with custom model (specify model via --model flag)
|
||||||
|
python3 -m core.providers.llm.test.test_groq --model llama-3.1-8b-instant
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter --model anthropic/claude-3.5-sonnet
|
||||||
|
python3 -m core.providers.llm.test.test_openai --model gpt-4o
|
||||||
|
python3 -m core.providers.llm.test.test_google --model gemini-1.5-pro
|
||||||
|
|
||||||
|
# Test with custom prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_groq "Tell me a joke" --model mixtral-8x7b-32768
|
||||||
|
python3 -m core.providers.llm.test.test_openai "讲个笑话" --model gpt-4o-mini
|
||||||
|
|
||||||
|
|
||||||
|
4. Available Models
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Groq Models:
|
||||||
|
- openai/gpt-oss-120b (default, fast)
|
||||||
|
- llama-3.3-70b-versatile (best balance)
|
||||||
|
- llama-3.1-8b-instant (fastest)
|
||||||
|
- mixtral-8x7b-32768 (large context)
|
||||||
|
|
||||||
|
OpenRouter Models:
|
||||||
|
- openai/gpt-4o (default, best quality)
|
||||||
|
- openai/gpt-4o-mini (faster, cheaper)
|
||||||
|
- anthropic/claude-3.5-sonnet
|
||||||
|
- google/gemini-pro-1.5
|
||||||
|
- meta-llama/llama-3.3-70b-instruct
|
||||||
|
|
||||||
|
OpenAI Models:
|
||||||
|
- gpt-4o-mini (default, fast & cheap)
|
||||||
|
- gpt-4o (best quality)
|
||||||
|
- gpt-4-turbo
|
||||||
|
- gpt-3.5-turbo
|
||||||
|
|
||||||
|
Google Gemini Models:
|
||||||
|
- gemini-2.0-flash-exp (default, fastest, experimental)
|
||||||
|
- gemini-1.5-pro (best quality)
|
||||||
|
- gemini-1.5-flash (fast & balanced)
|
||||||
|
- gemini-1.0-pro (stable)
|
||||||
|
|
||||||
|
Set models:
|
||||||
|
export GROQ_MODEL="llama-3.1-8b-instant"
|
||||||
|
export OPENROUTER_MODEL="openai/gpt-4o-mini"
|
||||||
|
export OPENAI_MODEL="gpt-4o"
|
||||||
|
export GOOGLE_MODEL="gemini-1.5-flash"
|
||||||
|
|
||||||
|
|
||||||
|
5. Output Metrics
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Test measures streaming performance:
|
||||||
|
- ⚡ TTFT (Time to First Token): How fast the model starts responding
|
||||||
|
- ⏱️ Total Latency: Total time to complete the response
|
||||||
|
- 📝 Response Length: Character count
|
||||||
|
- 🔢 Token Chunks: Number of chunks received
|
||||||
|
- 🚀 Avg Time per Token: Average generation speed
|
||||||
|
|
||||||
|
|
||||||
|
6. Example Output
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
🤖 Groq LLM Test (Streaming)
|
||||||
|
API: https://api.groq.com/openai/v1
|
||||||
|
Model: llama-3.3-70b-versatile
|
||||||
|
Prompt: Hello! Please introduce yourself briefly in 2-3...
|
||||||
|
|
||||||
|
⏳ Generating response...
|
||||||
|
|
||||||
|
============================================================
|
||||||
|
Hello! I'm an AI assistant powered by Groq. I'm designed
|
||||||
|
to be helpful, informative, and conversational. How can I
|
||||||
|
assist you today?
|
||||||
|
============================================================
|
||||||
|
|
||||||
|
📊 Performance Metrics:
|
||||||
|
⚡ TTFT (Time to First Token): 234 ms
|
||||||
|
⏱️ Total Latency: 1567 ms (1.57s)
|
||||||
|
📝 Response Length: 123 characters
|
||||||
|
🔢 Token Chunks: 45
|
||||||
|
🚀 Avg Time per Token: 29.6 ms
|
||||||
|
|
||||||
|
✅ Test completed successfully!
|
||||||
|
|
||||||
|
|
||||||
|
7. Troubleshooting
|
||||||
|
------------------
|
||||||
|
|
||||||
|
If API key error:
|
||||||
|
echo $GROQ_API_KEY # Check Groq key
|
||||||
|
echo $OPENROUTER_API_KEY # Check OpenRouter key
|
||||||
|
echo $OPENAI_API_KEY # Check OpenAI key
|
||||||
|
echo $GOOGLE_API_KEY # Check Google key
|
||||||
|
source ~/.zshrc # Reload config
|
||||||
|
|
||||||
|
If packages not installed:
|
||||||
|
pip install openai google-generativeai
|
||||||
|
|
||||||
|
If module not found:
|
||||||
|
cd /Users/harold/Projects/live-agent/main/xiaozhi-server/
|
||||||
|
# Make sure you're in the right directory!
|
||||||
|
|
||||||
|
Get API keys:
|
||||||
|
Groq: https://console.groq.com/keys
|
||||||
|
OpenRouter: https://openrouter.ai/keys
|
||||||
|
OpenAI: https://platform.openai.com/api-keys
|
||||||
|
Google: https://aistudio.google.com/apikey
|
||||||
|
|
||||||
|
|
||||||
|
8. Benchmark Multiple Models
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
Run benchmark to test multiple models and generate performance report:
|
||||||
|
|
||||||
|
cd /Users/harold/Projects/live-agent/main/xiaozhi-server/
|
||||||
|
|
||||||
|
# Run benchmark (requires both GROQ_API_KEY and OPENROUTER_API_KEY)
|
||||||
|
python3 -m core.providers.llm.test.benchmark
|
||||||
|
|
||||||
|
# Output: benchmark_report.md with detailed performance comparison
|
||||||
|
|
||||||
|
|
||||||
|
9. Tips
|
||||||
|
-------
|
||||||
|
|
||||||
|
- TTFT is crucial for chat applications (aim for <500ms)
|
||||||
|
- Groq is known for extremely fast inference speed
|
||||||
|
- Lower TTFT = better user experience
|
||||||
|
- Token chunks count may vary based on response length
|
||||||
|
- Adjust temperature (0-2) in code for creativity control
|
||||||
|
- Use benchmark script to compare multiple models at once
|
||||||
|
|
||||||
|
|
||||||
|
That's it! 🎉
|
||||||
|
|
||||||
2
main/xiaozhi-server/core/providers/llm/test/__init__.py
Normal file
2
main/xiaozhi-server/core/providers/llm/test/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
# LLM Test Module
|
||||||
|
|
||||||
379
main/xiaozhi-server/core/providers/llm/test/benchmark.py
Normal file
379
main/xiaozhi-server/core/providers/llm/test/benchmark.py
Normal file
|
|
@ -0,0 +1,379 @@
|
||||||
|
"""
|
||||||
|
LLM Model Benchmark Script
|
||||||
|
|
||||||
|
This script tests multiple LLM models from different providers with the same prompt
|
||||||
|
and generates a markdown report comparing their performance metrics.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
python3 -m core.providers.llm.test.benchmark
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
GROQ_API_KEY - Your Groq API key (required for Groq models)
|
||||||
|
OPENROUTER_API_KEY - Your OpenRouter API key (required for OpenRouter models)
|
||||||
|
OPENAI_API_KEY - Your OpenAI API key (required for OpenAI models)
|
||||||
|
|
||||||
|
Output:
|
||||||
|
benchmark_report.md - Performance comparison report
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
# Check if OpenAI SDK is installed
|
||||||
|
try:
|
||||||
|
from openai import OpenAI
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: openai package not installed")
|
||||||
|
print(" Install with: pip install openai")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
# Test configuration
|
||||||
|
TEST_PROMPT = "What is artificial intelligence? Please explain in 2-3 sentences."
|
||||||
|
|
||||||
|
GROQ_MODELS = [
|
||||||
|
"llama-3.3-70b-versatile",
|
||||||
|
"openai/gpt-oss-120b",
|
||||||
|
"groq/compound",
|
||||||
|
"openai/gpt-oss-20b"
|
||||||
|
]
|
||||||
|
|
||||||
|
OPENROUTER_MODELS = [
|
||||||
|
"openai/gpt-4o",
|
||||||
|
"anthropic/claude-sonnet-4.5",
|
||||||
|
"openai/gpt-oss-120b",
|
||||||
|
"openai/gpt-oss-20b"
|
||||||
|
]
|
||||||
|
|
||||||
|
OPENAI_MODELS = [
|
||||||
|
"gpt-4o",
|
||||||
|
"gpt-4o-mini",
|
||||||
|
"gpt-4-turbo",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_model(provider: str, model: str, prompt: str, api_key: str, base_url: str):
|
||||||
|
"""
|
||||||
|
Test a single model and return performance metrics
|
||||||
|
|
||||||
|
Args:
|
||||||
|
provider: Provider name (e.g., "Groq", "OpenRouter")
|
||||||
|
model: Model name
|
||||||
|
prompt: Test prompt
|
||||||
|
api_key: API key
|
||||||
|
base_url: API base URL
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict with metrics or None if failed
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
print(f" Testing {provider}/{model}... ", end="", flush=True)
|
||||||
|
|
||||||
|
client = OpenAI(api_key=api_key, base_url=base_url)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
ttft = None
|
||||||
|
full_response = ""
|
||||||
|
token_count = 0
|
||||||
|
|
||||||
|
# Create streaming chat completion
|
||||||
|
stream = client.chat.completions.create(
|
||||||
|
model=model,
|
||||||
|
messages=[{"role": "user", "content": prompt}],
|
||||||
|
stream=True,
|
||||||
|
max_tokens=1024,
|
||||||
|
temperature=0.7
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process stream
|
||||||
|
for chunk in stream:
|
||||||
|
if chunk.choices[0].delta.content:
|
||||||
|
content = chunk.choices[0].delta.content
|
||||||
|
|
||||||
|
if ttft is None:
|
||||||
|
ttft = (time.time() - start_time) * 1000
|
||||||
|
|
||||||
|
full_response += content
|
||||||
|
token_count += 1
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
total_latency = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
print(f"✅ TTFT: {ttft:.0f}ms")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"provider": provider,
|
||||||
|
"model": model,
|
||||||
|
"ttft": ttft,
|
||||||
|
"total_latency": total_latency,
|
||||||
|
"response_length": len(full_response),
|
||||||
|
"token_count": token_count,
|
||||||
|
"response": full_response[:100] + "..." if len(full_response) > 100 else full_response,
|
||||||
|
"success": True
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Failed: {str(e)[:50]}")
|
||||||
|
return {
|
||||||
|
"provider": provider,
|
||||||
|
"model": model,
|
||||||
|
"error": str(e),
|
||||||
|
"success": False
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def generate_markdown_report(results: list, prompt: str):
|
||||||
|
"""
|
||||||
|
Generate a markdown report from test results
|
||||||
|
|
||||||
|
Args:
|
||||||
|
results: List of test results
|
||||||
|
prompt: The test prompt used
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: Markdown formatted report
|
||||||
|
"""
|
||||||
|
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
md = f"""# LLM Model Benchmark Report
|
||||||
|
|
||||||
|
**Generated:** {timestamp}
|
||||||
|
|
||||||
|
## Test Configuration
|
||||||
|
|
||||||
|
**Test Prompt:**
|
||||||
|
```
|
||||||
|
{prompt}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Tested Models:**
|
||||||
|
- Groq: {len([r for r in results if r['provider'] == 'Groq'])} models
|
||||||
|
- OpenRouter: {len([r for r in results if r['provider'] == 'OpenRouter'])} models
|
||||||
|
- OpenAI: {len([r for r in results if r['provider'] == 'OpenAI'])} models
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Performance Metrics Summary
|
||||||
|
|
||||||
|
### Time to First Token (TTFT) Comparison
|
||||||
|
|
||||||
|
Lower is better. This metric indicates how quickly the model starts responding.
|
||||||
|
|
||||||
|
| Rank | Provider | Model | TTFT (ms) | Status |
|
||||||
|
|------|----------|-------|-----------|--------|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Sort by TTFT
|
||||||
|
successful_results = [r for r in results if r['success']]
|
||||||
|
sorted_by_ttft = sorted(successful_results, key=lambda x: x['ttft'])
|
||||||
|
|
||||||
|
for i, result in enumerate(sorted_by_ttft, 1):
|
||||||
|
ttft = f"{result['ttft']:.0f}" if result['ttft'] else "N/A"
|
||||||
|
status = "✅" if result['success'] else "❌"
|
||||||
|
md += f"| {i} | {result['provider']} | `{result['model']}` | **{ttft}** | {status} |\n"
|
||||||
|
|
||||||
|
# Failed models
|
||||||
|
failed_results = [r for r in results if not r['success']]
|
||||||
|
if failed_results:
|
||||||
|
md += "\n### Failed Models\n\n"
|
||||||
|
md += "| Provider | Model | Error |\n"
|
||||||
|
md += "|----------|-------|-------|\n"
|
||||||
|
for result in failed_results:
|
||||||
|
error = result.get('error', 'Unknown error')[:50]
|
||||||
|
md += f"| {result['provider']} | `{result['model']}` | {error} |\n"
|
||||||
|
|
||||||
|
md += "\n---\n\n"
|
||||||
|
|
||||||
|
# Detailed metrics
|
||||||
|
md += "## Detailed Performance Metrics\n\n"
|
||||||
|
|
||||||
|
for provider in ["Groq", "OpenRouter", "OpenAI"]:
|
||||||
|
provider_results = [r for r in successful_results if r['provider'] == provider]
|
||||||
|
if not provider_results:
|
||||||
|
continue
|
||||||
|
|
||||||
|
md += f"### {provider} Models\n\n"
|
||||||
|
md += "| Model | TTFT (ms) | Total Latency (ms) | Response Length | Token Chunks | Avg Token Time (ms) |\n"
|
||||||
|
md += "|-------|-----------|-------------------|-----------------|--------------|--------------------|\n"
|
||||||
|
|
||||||
|
for result in sorted(provider_results, key=lambda x: x['ttft']):
|
||||||
|
ttft = f"{result['ttft']:.0f}"
|
||||||
|
total = f"{result['total_latency']:.0f}"
|
||||||
|
length = result['response_length']
|
||||||
|
tokens = result['token_count']
|
||||||
|
avg_token = f"{(result['total_latency'] - result['ttft']) / tokens:.1f}" if tokens > 0 else "N/A"
|
||||||
|
|
||||||
|
md += f"| `{result['model']}` | {ttft} | {total} | {length} | {tokens} | {avg_token} |\n"
|
||||||
|
|
||||||
|
md += "\n"
|
||||||
|
|
||||||
|
# Response samples
|
||||||
|
md += "## Response Samples\n\n"
|
||||||
|
for result in successful_results:
|
||||||
|
md += f"### {result['provider']} - {result['model']}\n\n"
|
||||||
|
md += f"**TTFT:** {result['ttft']:.0f}ms\n\n"
|
||||||
|
md += f"**Response Preview:**\n```\n{result['response']}\n```\n\n"
|
||||||
|
|
||||||
|
# Performance analysis
|
||||||
|
md += "---\n\n## Performance Analysis\n\n"
|
||||||
|
|
||||||
|
if successful_results:
|
||||||
|
fastest = min(successful_results, key=lambda x: x['ttft'])
|
||||||
|
slowest = max(successful_results, key=lambda x: x['ttft'])
|
||||||
|
avg_ttft = sum(r['ttft'] for r in successful_results) / len(successful_results)
|
||||||
|
|
||||||
|
md += f"**Fastest Model:** `{fastest['model']}` ({fastest['provider']}) - {fastest['ttft']:.0f}ms TTFT\n\n"
|
||||||
|
md += f"**Slowest Model:** `{slowest['model']}` ({slowest['provider']}) - {slowest['ttft']:.0f}ms TTFT\n\n"
|
||||||
|
md += f"**Average TTFT:** {avg_ttft:.0f}ms\n\n"
|
||||||
|
|
||||||
|
# Provider comparison
|
||||||
|
groq_results = [r for r in successful_results if r['provider'] == 'Groq']
|
||||||
|
openrouter_results = [r for r in successful_results if r['provider'] == 'OpenRouter']
|
||||||
|
openai_results = [r for r in successful_results if r['provider'] == 'OpenAI']
|
||||||
|
|
||||||
|
if groq_results or openrouter_results or openai_results:
|
||||||
|
md += "### Provider Comparison\n\n"
|
||||||
|
|
||||||
|
if groq_results:
|
||||||
|
groq_avg = sum(r['ttft'] for r in groq_results) / len(groq_results)
|
||||||
|
md += f"- **Groq Average TTFT:** {groq_avg:.0f}ms\n"
|
||||||
|
|
||||||
|
if openrouter_results:
|
||||||
|
openrouter_avg = sum(r['ttft'] for r in openrouter_results) / len(openrouter_results)
|
||||||
|
md += f"- **OpenRouter Average TTFT:** {openrouter_avg:.0f}ms\n"
|
||||||
|
|
||||||
|
if openai_results:
|
||||||
|
openai_avg = sum(r['ttft'] for r in openai_results) / len(openai_results)
|
||||||
|
md += f"- **OpenAI Average TTFT:** {openai_avg:.0f}ms\n"
|
||||||
|
|
||||||
|
md += "\n"
|
||||||
|
|
||||||
|
md += "---\n\n"
|
||||||
|
md += "*Report generated by LLM Benchmark Script*\n"
|
||||||
|
|
||||||
|
return md
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main benchmark execution"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("LLM Model Benchmark")
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"\nTest Prompt: {TEST_PROMPT}\n")
|
||||||
|
|
||||||
|
# Check API keys
|
||||||
|
groq_api_key = os.environ.get("GROQ_API_KEY")
|
||||||
|
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
|
||||||
|
openai_api_key = os.environ.get("OPENAI_API_KEY")
|
||||||
|
|
||||||
|
if not groq_api_key:
|
||||||
|
print("⚠️ Warning: GROQ_API_KEY not set, skipping Groq models")
|
||||||
|
if not openrouter_api_key:
|
||||||
|
print("⚠️ Warning: OPENROUTER_API_KEY not set, skipping OpenRouter models")
|
||||||
|
if not openai_api_key:
|
||||||
|
print("⚠️ Warning: OPENAI_API_KEY not set, skipping OpenAI models")
|
||||||
|
|
||||||
|
if not groq_api_key and not openrouter_api_key and not openai_api_key:
|
||||||
|
print("\n❌ Error: No API keys configured")
|
||||||
|
print(" export GROQ_API_KEY='your_key'")
|
||||||
|
print(" export OPENROUTER_API_KEY='your_key'")
|
||||||
|
print(" export OPENAI_API_KEY='your_key'")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print()
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# Test Groq models
|
||||||
|
if groq_api_key:
|
||||||
|
print("📊 Testing Groq Models:")
|
||||||
|
for model in GROQ_MODELS:
|
||||||
|
result = test_model(
|
||||||
|
provider="Groq",
|
||||||
|
model=model,
|
||||||
|
prompt=TEST_PROMPT,
|
||||||
|
api_key=groq_api_key,
|
||||||
|
base_url="https://api.groq.com/openai/v1"
|
||||||
|
)
|
||||||
|
results.append(result)
|
||||||
|
time.sleep(1) # Rate limiting
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Test OpenRouter models
|
||||||
|
if openrouter_api_key:
|
||||||
|
print("📊 Testing OpenRouter Models:")
|
||||||
|
for model in OPENROUTER_MODELS:
|
||||||
|
result = test_model(
|
||||||
|
provider="OpenRouter",
|
||||||
|
model=model,
|
||||||
|
prompt=TEST_PROMPT,
|
||||||
|
api_key=openrouter_api_key,
|
||||||
|
base_url="https://openrouter.ai/api/v1"
|
||||||
|
)
|
||||||
|
results.append(result)
|
||||||
|
time.sleep(1) # Rate limiting
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Test OpenAI models
|
||||||
|
if openai_api_key:
|
||||||
|
print("📊 Testing OpenAI Models:")
|
||||||
|
for model in OPENAI_MODELS:
|
||||||
|
result = test_model(
|
||||||
|
provider="OpenAI",
|
||||||
|
model=model,
|
||||||
|
prompt=TEST_PROMPT,
|
||||||
|
api_key=openai_api_key,
|
||||||
|
base_url="https://api.openai.com/v1"
|
||||||
|
)
|
||||||
|
results.append(result)
|
||||||
|
time.sleep(1) # Rate limiting
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Generate report
|
||||||
|
print("=" * 60)
|
||||||
|
print("Generating Report...")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
markdown_report = generate_markdown_report(results, TEST_PROMPT)
|
||||||
|
|
||||||
|
# Save to file
|
||||||
|
output_file = os.path.join(
|
||||||
|
os.path.dirname(__file__),
|
||||||
|
"benchmark_report.md"
|
||||||
|
)
|
||||||
|
|
||||||
|
with open(output_file, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(markdown_report)
|
||||||
|
|
||||||
|
print(f"\n✅ Report saved to: {output_file}")
|
||||||
|
|
||||||
|
# Print summary
|
||||||
|
successful = len([r for r in results if r['success']])
|
||||||
|
failed = len([r for r in results if not r['success']])
|
||||||
|
|
||||||
|
print(f"\n📊 Summary:")
|
||||||
|
print(f" Total Models Tested: {len(results)}")
|
||||||
|
print(f" Successful: {successful}")
|
||||||
|
print(f" Failed: {failed}")
|
||||||
|
|
||||||
|
if successful > 0:
|
||||||
|
fastest = min([r for r in results if r['success']], key=lambda x: x['ttft'])
|
||||||
|
print(f"\n🏆 Fastest Model: {fastest['model']} ({fastest['provider']})")
|
||||||
|
print(f" TTFT: {fastest['ttft']:.0f}ms")
|
||||||
|
|
||||||
|
print(f"\n✅ Benchmark completed!")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Benchmark interrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
188
main/xiaozhi-server/core/providers/llm/test/benchmark_report.md
Normal file
188
main/xiaozhi-server/core/providers/llm/test/benchmark_report.md
Normal file
|
|
@ -0,0 +1,188 @@
|
||||||
|
# LLM Model Benchmark Report
|
||||||
|
|
||||||
|
**Generated:** 2025-11-11 18:37:21
|
||||||
|
|
||||||
|
## Test Configuration
|
||||||
|
|
||||||
|
**Test Prompt:**
|
||||||
|
```
|
||||||
|
What is artificial intelligence? Please explain in 2-3 sentences.
|
||||||
|
```
|
||||||
|
|
||||||
|
**Tested Models:**
|
||||||
|
- Groq: 4 models
|
||||||
|
- OpenRouter: 4 models
|
||||||
|
- OpenAI: 3 models
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Performance Metrics Summary
|
||||||
|
|
||||||
|
### Time to First Token (TTFT) Comparison
|
||||||
|
|
||||||
|
Lower is better. This metric indicates how quickly the model starts responding.
|
||||||
|
|
||||||
|
| Rank | Provider | Model | TTFT (ms) | Status |
|
||||||
|
|------|----------|-------|-----------|--------|
|
||||||
|
| 1 | Groq | `openai/gpt-oss-20b` | **453** | ✅ |
|
||||||
|
| 2 | Groq | `openai/gpt-oss-120b` | **455** | ✅ |
|
||||||
|
| 3 | Groq | `groq/compound` | **593** | ✅ |
|
||||||
|
| 4 | OpenRouter | `openai/gpt-oss-20b` | **1016** | ✅ |
|
||||||
|
| 5 | Groq | `llama-3.3-70b-versatile` | **1019** | ✅ |
|
||||||
|
| 6 | OpenRouter | `openai/gpt-4o` | **1429** | ✅ |
|
||||||
|
| 7 | OpenRouter | `anthropic/claude-sonnet-4.5` | **1513** | ✅ |
|
||||||
|
| 8 | OpenAI | `gpt-4o` | **1703** | ✅ |
|
||||||
|
| 9 | OpenAI | `gpt-4-turbo` | **1882** | ✅ |
|
||||||
|
| 10 | OpenAI | `gpt-4o-mini` | **1983** | ✅ |
|
||||||
|
| 11 | OpenRouter | `openai/gpt-oss-120b` | **2135** | ✅ |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Detailed Performance Metrics
|
||||||
|
|
||||||
|
### Groq Models
|
||||||
|
|
||||||
|
| Model | TTFT (ms) | Total Latency (ms) | Response Length | Token Chunks | Avg Token Time (ms) |
|
||||||
|
|-------|-----------|-------------------|-----------------|--------------|--------------------|
|
||||||
|
| `openai/gpt-oss-20b` | 453 | 530 | 416 | 76 | 1.0 |
|
||||||
|
| `openai/gpt-oss-120b` | 455 | 595 | 364 | 64 | 2.2 |
|
||||||
|
| `groq/compound` | 593 | 736 | 384 | 69 | 2.1 |
|
||||||
|
| `llama-3.3-70b-versatile` | 1019 | 1178 | 477 | 79 | 2.0 |
|
||||||
|
|
||||||
|
### OpenRouter Models
|
||||||
|
|
||||||
|
| Model | TTFT (ms) | Total Latency (ms) | Response Length | Token Chunks | Avg Token Time (ms) |
|
||||||
|
|-------|-----------|-------------------|-----------------|--------------|--------------------|
|
||||||
|
| `openai/gpt-oss-20b` | 1016 | 1666 | 539 | 87 | 7.5 |
|
||||||
|
| `openai/gpt-4o` | 1429 | 2238 | 362 | 60 | 13.5 |
|
||||||
|
| `anthropic/claude-sonnet-4.5` | 1513 | 2748 | 468 | 18 | 68.6 |
|
||||||
|
| `openai/gpt-oss-120b` | 2135 | 2800 | 547 | 94 | 7.1 |
|
||||||
|
|
||||||
|
### OpenAI Models
|
||||||
|
|
||||||
|
| Model | TTFT (ms) | Total Latency (ms) | Response Length | Token Chunks | Avg Token Time (ms) |
|
||||||
|
|-------|-----------|-------------------|-----------------|--------------|--------------------|
|
||||||
|
| `gpt-4o` | 1703 | 2367 | 393 | 62 | 10.7 |
|
||||||
|
| `gpt-4-turbo` | 1882 | 5315 | 520 | 88 | 39.0 |
|
||||||
|
| `gpt-4o-mini` | 1983 | 3797 | 400 | 64 | 28.3 |
|
||||||
|
|
||||||
|
## Response Samples
|
||||||
|
|
||||||
|
### Groq - llama-3.3-70b-versatile
|
||||||
|
|
||||||
|
**TTFT:** 1019ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) refers to the development of computer systems that can perform tasks th...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Groq - openai/gpt-oss-120b
|
||||||
|
|
||||||
|
**TTFT:** 455ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) is a branch of computer science that creates systems capable of perform...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Groq - groq/compound
|
||||||
|
|
||||||
|
**TTFT:** 593ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) is the field of computer science that creates systems capable of perfor...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Groq - openai/gpt-oss-20b
|
||||||
|
|
||||||
|
**TTFT:** 453ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) is the field of computer science that focuses on creating systems capab...
|
||||||
|
```
|
||||||
|
|
||||||
|
### OpenRouter - openai/gpt-4o
|
||||||
|
|
||||||
|
**TTFT:** 1429ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) is a branch of computer science that focuses on creating systems capabl...
|
||||||
|
```
|
||||||
|
|
||||||
|
### OpenRouter - anthropic/claude-sonnet-4.5
|
||||||
|
|
||||||
|
**TTFT:** 1513ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) is technology that enables computers and machines to simulate human int...
|
||||||
|
```
|
||||||
|
|
||||||
|
### OpenRouter - openai/gpt-oss-120b
|
||||||
|
|
||||||
|
**TTFT:** 2135ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) is the field of computer science that designs systems capable of perfor...
|
||||||
|
```
|
||||||
|
|
||||||
|
### OpenRouter - openai/gpt-oss-20b
|
||||||
|
|
||||||
|
**TTFT:** 1016ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) is a branch of computer science that creates systems capable of perform...
|
||||||
|
```
|
||||||
|
|
||||||
|
### OpenAI - gpt-4o
|
||||||
|
|
||||||
|
**TTFT:** 1703ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) refers to the development of computer systems and software that can per...
|
||||||
|
```
|
||||||
|
|
||||||
|
### OpenAI - gpt-4o-mini
|
||||||
|
|
||||||
|
**TTFT:** 1983ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) refers to the simulation of human intelligence processes by computer sy...
|
||||||
|
```
|
||||||
|
|
||||||
|
### OpenAI - gpt-4-turbo
|
||||||
|
|
||||||
|
**TTFT:** 1882ms
|
||||||
|
|
||||||
|
**Response Preview:**
|
||||||
|
```
|
||||||
|
Artificial intelligence (AI) refers to the simulation of human intelligence in machines that are pro...
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Performance Analysis
|
||||||
|
|
||||||
|
**Fastest Model:** `openai/gpt-oss-20b` (Groq) - 453ms TTFT
|
||||||
|
|
||||||
|
**Slowest Model:** `openai/gpt-oss-120b` (OpenRouter) - 2135ms TTFT
|
||||||
|
|
||||||
|
**Average TTFT:** 1289ms
|
||||||
|
|
||||||
|
### Provider Comparison
|
||||||
|
|
||||||
|
- **Groq Average TTFT:** 630ms
|
||||||
|
- **OpenRouter Average TTFT:** 1523ms
|
||||||
|
- **OpenAI Average TTFT:** 1856ms
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*Report generated by LLM Benchmark Script*
|
||||||
162
main/xiaozhi-server/core/providers/llm/test/test_google.py
Normal file
162
main/xiaozhi-server/core/providers/llm/test/test_google.py
Normal file
|
|
@ -0,0 +1,162 @@
|
||||||
|
"""
|
||||||
|
Google Gemini LLM Test (Streaming with TTFT Measurement)
|
||||||
|
|
||||||
|
This script tests Google's Gemini API using streaming mode.
|
||||||
|
Focuses on measuring Time to First Token (TTFT) latency.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
# Default prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_google
|
||||||
|
|
||||||
|
# Custom prompt
|
||||||
|
python3 -m core.providers.llm.test.test_google "What is the capital of France?"
|
||||||
|
|
||||||
|
# Custom model
|
||||||
|
python3 -m core.providers.llm.test.test_google --model gemini-1.5-pro
|
||||||
|
|
||||||
|
# Custom prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_google "Tell me a joke" --model gemini-1.5-flash
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
GOOGLE_API_KEY - Your Google AI API key (required)
|
||||||
|
GOOGLE_MODEL - Default model name (optional, default: gemini-2.0-flash-exp)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
export GOOGLE_API_KEY="your_api_key_here"
|
||||||
|
python3 -m core.providers.llm.test.test_google
|
||||||
|
python3 -m core.providers.llm.test.test_google "解释一下什么是人工智能"
|
||||||
|
python3 -m core.providers.llm.test.test_google --model gemini-1.0-pro
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Check if Google Generative AI SDK is installed
|
||||||
|
try:
|
||||||
|
import google.generativeai as genai
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: google-generativeai package not installed")
|
||||||
|
print(" Install with: pip install google-generativeai")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_google_llm_streaming(prompt: str, model_name: str = None):
|
||||||
|
"""
|
||||||
|
Test Google Gemini LLM with streaming mode and measure TTFT
|
||||||
|
|
||||||
|
Args:
|
||||||
|
prompt: User prompt text
|
||||||
|
model_name: Model name (optional, uses env or default)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (response_text, ttft_ms, total_latency_ms, token_count)
|
||||||
|
"""
|
||||||
|
api_key = os.environ.get("GOOGLE_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
print("❌ Error: GOOGLE_API_KEY not set")
|
||||||
|
print(" export GOOGLE_API_KEY='your_api_key_here'")
|
||||||
|
print("\n💡 Get your API key from: https://aistudio.google.com/apikey")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if model_name is None:
|
||||||
|
model_name = os.environ.get("GOOGLE_MODEL", "gemini-2.0-flash-exp")
|
||||||
|
|
||||||
|
print(f"🤖 Google Gemini LLM Test (Streaming)")
|
||||||
|
print(f" API: Google AI Studio")
|
||||||
|
print(f" Model: {model_name}")
|
||||||
|
print(f" Prompt: {prompt[:50]}{'...' if len(prompt) > 50 else ''}\n")
|
||||||
|
|
||||||
|
# Configure Google AI
|
||||||
|
genai.configure(api_key=api_key)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("⏳ Generating response...\n")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
# Initialize model
|
||||||
|
model = genai.GenerativeModel(model_name)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
ttft = None
|
||||||
|
full_response = ""
|
||||||
|
token_count = 0
|
||||||
|
|
||||||
|
# Generate content with streaming
|
||||||
|
response = model.generate_content(
|
||||||
|
prompt,
|
||||||
|
stream=True,
|
||||||
|
generation_config=genai.types.GenerationConfig(
|
||||||
|
max_output_tokens=1024,
|
||||||
|
temperature=0.7,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process stream
|
||||||
|
for chunk in response:
|
||||||
|
if chunk.text:
|
||||||
|
content = chunk.text
|
||||||
|
|
||||||
|
# Record time to first token
|
||||||
|
if ttft is None:
|
||||||
|
ttft = (time.time() - start_time) * 1000
|
||||||
|
|
||||||
|
full_response += content
|
||||||
|
token_count += 1
|
||||||
|
print(content, end="", flush=True)
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
total_latency = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
|
||||||
|
return full_response, ttft, total_latency, token_count
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Parse command line arguments
|
||||||
|
prompt = "Hello! Please introduce yourself briefly in 2-3 sentences."
|
||||||
|
model = None
|
||||||
|
|
||||||
|
args = sys.argv[1:]
|
||||||
|
i = 0
|
||||||
|
while i < len(args):
|
||||||
|
if args[i] == "--model" and i + 1 < len(args):
|
||||||
|
model = args[i + 1]
|
||||||
|
i += 2
|
||||||
|
elif not args[i].startswith("--"):
|
||||||
|
prompt = args[i]
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Run streaming test
|
||||||
|
response, ttft, total_latency, token_count = test_google_llm_streaming(prompt, model)
|
||||||
|
|
||||||
|
# Print metrics
|
||||||
|
print(f"\n📊 Performance Metrics:")
|
||||||
|
print(f" ⚡ TTFT (Time to First Token): {ttft:.0f} ms")
|
||||||
|
print(f" ⏱️ Total Latency: {total_latency:.0f} ms ({total_latency/1000:.2f}s)")
|
||||||
|
print(f" 📝 Response Length: {len(response)} characters")
|
||||||
|
print(f" 🔢 Token Chunks: {token_count}")
|
||||||
|
|
||||||
|
if token_count > 0 and total_latency > ttft:
|
||||||
|
avg_token_time = (total_latency - ttft) / token_count
|
||||||
|
print(f" 🚀 Avg Time per Token: {avg_token_time:.1f} ms")
|
||||||
|
|
||||||
|
print("\n✅ Test completed successfully!")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Test interrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
163
main/xiaozhi-server/core/providers/llm/test/test_groq.py
Normal file
163
main/xiaozhi-server/core/providers/llm/test/test_groq.py
Normal file
|
|
@ -0,0 +1,163 @@
|
||||||
|
"""
|
||||||
|
Groq LLM Test (Streaming with TTFT Measurement)
|
||||||
|
|
||||||
|
This script tests Groq's chat completion API using streaming mode.
|
||||||
|
Focuses on measuring Time to First Token (TTFT) latency.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
# Default prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_groq
|
||||||
|
|
||||||
|
# Custom prompt
|
||||||
|
python3 -m core.providers.llm.test.test_groq "What is the capital of France?"
|
||||||
|
|
||||||
|
# Custom model
|
||||||
|
python3 -m core.providers.llm.test.test_groq --model llama-3.3-70b-versatile
|
||||||
|
|
||||||
|
# Custom prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_groq "Tell me a joke" --model llama-3.1-8b-instant
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
GROQ_API_KEY - Your Groq API key (required)
|
||||||
|
GROQ_MODEL - Default model name (optional, default: openai/gpt-oss-120b)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
export GROQ_API_KEY="gsk_your_key_here"
|
||||||
|
python3 -m core.providers.llm.test.test_groq
|
||||||
|
python3 -m core.providers.llm.test.test_groq "解释一下什么是人工智能"
|
||||||
|
python3 -m core.providers.llm.test.test_groq --model mixtral-8x7b-32768
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Check if OpenAI SDK is installed
|
||||||
|
try:
|
||||||
|
from openai import OpenAI
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: openai package not installed")
|
||||||
|
print(" Install with: pip install openai")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_groq_llm_streaming(prompt: str, model: str = None):
|
||||||
|
"""
|
||||||
|
Test Groq LLM with streaming mode and measure TTFT
|
||||||
|
|
||||||
|
Args:
|
||||||
|
prompt: User prompt text
|
||||||
|
model: Model name (optional, uses env or default)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (response_text, ttft_ms, total_latency_ms, token_count)
|
||||||
|
"""
|
||||||
|
api_key = os.environ.get("GROQ_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
print("❌ Error: GROQ_API_KEY not set")
|
||||||
|
print(" export GROQ_API_KEY='gsk_your_key_here'")
|
||||||
|
print("\n💡 Get your API key from: https://console.groq.com/keys")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if model is None:
|
||||||
|
model = os.environ.get("GROQ_MODEL", "openai/gpt-oss-120b")
|
||||||
|
|
||||||
|
print(f"🤖 Groq LLM Test (Streaming)")
|
||||||
|
print(f" API: https://api.groq.com/openai/v1")
|
||||||
|
print(f" Model: {model}")
|
||||||
|
print(f" Prompt: {prompt[:50]}{'...' if len(prompt) > 50 else ''}\n")
|
||||||
|
|
||||||
|
# Initialize OpenAI client with Groq endpoint
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=api_key,
|
||||||
|
base_url="https://api.groq.com/openai/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("⏳ Generating response...\n")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
ttft = None
|
||||||
|
full_response = ""
|
||||||
|
token_count = 0
|
||||||
|
|
||||||
|
# Create streaming chat completion
|
||||||
|
stream = client.chat.completions.create(
|
||||||
|
model=model,
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
],
|
||||||
|
stream=True,
|
||||||
|
max_tokens=1024,
|
||||||
|
temperature=0.7
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process stream
|
||||||
|
for chunk in stream:
|
||||||
|
if chunk.choices[0].delta.content:
|
||||||
|
content = chunk.choices[0].delta.content
|
||||||
|
|
||||||
|
# Record time to first token
|
||||||
|
if ttft is None:
|
||||||
|
ttft = (time.time() - start_time) * 1000
|
||||||
|
|
||||||
|
full_response += content
|
||||||
|
token_count += 1
|
||||||
|
print(content, end="", flush=True)
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
total_latency = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
|
||||||
|
return full_response, ttft, total_latency, token_count
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Parse command line arguments
|
||||||
|
prompt = "Hello! Please introduce yourself briefly in 2-3 sentences."
|
||||||
|
model = None
|
||||||
|
|
||||||
|
args = sys.argv[1:]
|
||||||
|
i = 0
|
||||||
|
while i < len(args):
|
||||||
|
if args[i] == "--model" and i + 1 < len(args):
|
||||||
|
model = args[i + 1]
|
||||||
|
i += 2
|
||||||
|
elif not args[i].startswith("--"):
|
||||||
|
prompt = args[i]
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Run streaming test
|
||||||
|
response, ttft, total_latency, token_count = test_groq_llm_streaming(prompt, model)
|
||||||
|
|
||||||
|
# Print metrics
|
||||||
|
print(f"\n📊 Performance Metrics:")
|
||||||
|
print(f" ⚡ TTFT (Time to First Token): {ttft:.0f} ms")
|
||||||
|
print(f" ⏱️ Total Latency: {total_latency:.0f} ms ({total_latency/1000:.2f}s)")
|
||||||
|
print(f" 📝 Response Length: {len(response)} characters")
|
||||||
|
print(f" 🔢 Token Chunks: {token_count}")
|
||||||
|
|
||||||
|
if token_count > 0 and total_latency > ttft:
|
||||||
|
avg_token_time = (total_latency - ttft) / token_count
|
||||||
|
print(f" 🚀 Avg Time per Token: {avg_token_time:.1f} ms")
|
||||||
|
|
||||||
|
print("\n✅ Test completed successfully!")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Test interrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
160
main/xiaozhi-server/core/providers/llm/test/test_openai.py
Normal file
160
main/xiaozhi-server/core/providers/llm/test/test_openai.py
Normal file
|
|
@ -0,0 +1,160 @@
|
||||||
|
"""
|
||||||
|
OpenAI LLM Test (Streaming with TTFT Measurement)
|
||||||
|
|
||||||
|
This script tests OpenAI's chat completion API using streaming mode.
|
||||||
|
Focuses on measuring Time to First Token (TTFT) latency.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
# Default prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_openai
|
||||||
|
|
||||||
|
# Custom prompt
|
||||||
|
python3 -m core.providers.llm.test.test_openai "What is the capital of France?"
|
||||||
|
|
||||||
|
# Custom model
|
||||||
|
python3 -m core.providers.llm.test.test_openai --model gpt-4o
|
||||||
|
|
||||||
|
# Custom prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_openai "Tell me a joke" --model gpt-4-turbo
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
OPENAI_API_KEY - Your OpenAI API key (required)
|
||||||
|
OPENAI_MODEL - Default model name (optional, default: gpt-4o-mini)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
export OPENAI_API_KEY="sk-proj-your_key_here"
|
||||||
|
python3 -m core.providers.llm.test.test_openai
|
||||||
|
python3 -m core.providers.llm.test.test_openai "解释一下什么是人工智能"
|
||||||
|
python3 -m core.providers.llm.test.test_openai --model gpt-3.5-turbo
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Check if OpenAI SDK is installed
|
||||||
|
try:
|
||||||
|
from openai import OpenAI
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: openai package not installed")
|
||||||
|
print(" Install with: pip install openai")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_openai_llm_streaming(prompt: str, model: str = None):
|
||||||
|
"""
|
||||||
|
Test OpenAI LLM with streaming mode and measure TTFT
|
||||||
|
|
||||||
|
Args:
|
||||||
|
prompt: User prompt text
|
||||||
|
model: Model name (optional, uses env or default)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (response_text, ttft_ms, total_latency_ms, token_count)
|
||||||
|
"""
|
||||||
|
api_key = os.environ.get("OPENAI_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
print("❌ Error: OPENAI_API_KEY not set")
|
||||||
|
print(" export OPENAI_API_KEY='sk-proj-your_key_here'")
|
||||||
|
print("\n💡 Get your API key from: https://platform.openai.com/api-keys")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if model is None:
|
||||||
|
model = os.environ.get("OPENAI_MODEL", "gpt-4o-mini")
|
||||||
|
|
||||||
|
print(f"🤖 OpenAI LLM Test (Streaming)")
|
||||||
|
print(f" API: https://api.openai.com/v1")
|
||||||
|
print(f" Model: {model}")
|
||||||
|
print(f" Prompt: {prompt[:50]}{'...' if len(prompt) > 50 else ''}\n")
|
||||||
|
|
||||||
|
# Initialize OpenAI client
|
||||||
|
client = OpenAI(api_key=api_key)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("⏳ Generating response...\n")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
ttft = None
|
||||||
|
full_response = ""
|
||||||
|
token_count = 0
|
||||||
|
|
||||||
|
# Create streaming chat completion
|
||||||
|
stream = client.chat.completions.create(
|
||||||
|
model=model,
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
],
|
||||||
|
stream=True,
|
||||||
|
max_tokens=1024,
|
||||||
|
temperature=0.7
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process stream
|
||||||
|
for chunk in stream:
|
||||||
|
if chunk.choices[0].delta.content:
|
||||||
|
content = chunk.choices[0].delta.content
|
||||||
|
|
||||||
|
# Record time to first token
|
||||||
|
if ttft is None:
|
||||||
|
ttft = (time.time() - start_time) * 1000
|
||||||
|
|
||||||
|
full_response += content
|
||||||
|
token_count += 1
|
||||||
|
print(content, end="", flush=True)
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
total_latency = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
|
||||||
|
return full_response, ttft, total_latency, token_count
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Parse command line arguments
|
||||||
|
prompt = "Hello! Please introduce yourself briefly in 2-3 sentences."
|
||||||
|
model = None
|
||||||
|
|
||||||
|
args = sys.argv[1:]
|
||||||
|
i = 0
|
||||||
|
while i < len(args):
|
||||||
|
if args[i] == "--model" and i + 1 < len(args):
|
||||||
|
model = args[i + 1]
|
||||||
|
i += 2
|
||||||
|
elif not args[i].startswith("--"):
|
||||||
|
prompt = args[i]
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Run streaming test
|
||||||
|
response, ttft, total_latency, token_count = test_openai_llm_streaming(prompt, model)
|
||||||
|
|
||||||
|
# Print metrics
|
||||||
|
print(f"\n📊 Performance Metrics:")
|
||||||
|
print(f" ⚡ TTFT (Time to First Token): {ttft:.0f} ms")
|
||||||
|
print(f" ⏱️ Total Latency: {total_latency:.0f} ms ({total_latency/1000:.2f}s)")
|
||||||
|
print(f" 📝 Response Length: {len(response)} characters")
|
||||||
|
print(f" 🔢 Token Chunks: {token_count}")
|
||||||
|
|
||||||
|
if token_count > 0 and total_latency > ttft:
|
||||||
|
avg_token_time = (total_latency - ttft) / token_count
|
||||||
|
print(f" 🚀 Avg Time per Token: {avg_token_time:.1f} ms")
|
||||||
|
|
||||||
|
print("\n✅ Test completed successfully!")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Test interrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
168
main/xiaozhi-server/core/providers/llm/test/test_openrouter.py
Normal file
168
main/xiaozhi-server/core/providers/llm/test/test_openrouter.py
Normal file
|
|
@ -0,0 +1,168 @@
|
||||||
|
"""
|
||||||
|
OpenRouter LLM Test (Streaming with TTFT Measurement)
|
||||||
|
|
||||||
|
This script tests OpenRouter's chat completion API using streaming mode.
|
||||||
|
Focuses on measuring Time to First Token (TTFT) latency.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
# Default prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter
|
||||||
|
|
||||||
|
# Custom prompt
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter "What is the capital of France?"
|
||||||
|
|
||||||
|
# Custom model
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter --model openai/gpt-4o-mini
|
||||||
|
|
||||||
|
# Custom prompt and model
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter "Tell me a joke" --model anthropic/claude-3.5-sonnet
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
OPENROUTER_API_KEY - Your OpenRouter API key (required)
|
||||||
|
OPENROUTER_MODEL - Default model name (optional, default: openai/gpt-4o)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
export OPENROUTER_API_KEY="sk-or-v1-your_key_here"
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter "解释一下什么是人工智能"
|
||||||
|
python3 -m core.providers.llm.test.test_openrouter --model google/gemini-pro-1.5
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Check if OpenAI SDK is installed
|
||||||
|
try:
|
||||||
|
from openai import OpenAI
|
||||||
|
except ImportError:
|
||||||
|
print("❌ Error: openai package not installed")
|
||||||
|
print(" Install with: pip install openai")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_openrouter_llm_streaming(prompt: str, model: str = None):
|
||||||
|
"""
|
||||||
|
Test OpenRouter LLM with streaming mode and measure TTFT
|
||||||
|
|
||||||
|
Args:
|
||||||
|
prompt: User prompt text
|
||||||
|
model: Model name (optional, uses env or default)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (response_text, ttft_ms, total_latency_ms, token_count)
|
||||||
|
"""
|
||||||
|
api_key = os.environ.get("OPENROUTER_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
print("❌ Error: OPENROUTER_API_KEY not set")
|
||||||
|
print(" export OPENROUTER_API_KEY='sk-or-v1-your_key_here'")
|
||||||
|
print("\n💡 Get your API key from: https://openrouter.ai/keys")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if model is None:
|
||||||
|
model = os.environ.get("OPENROUTER_MODEL", "openai/gpt-4o")
|
||||||
|
|
||||||
|
print(f"🤖 OpenRouter LLM Test (Streaming)")
|
||||||
|
print(f" API: https://openrouter.ai/api/v1")
|
||||||
|
print(f" Model: {model}")
|
||||||
|
print(f" Prompt: {prompt[:50]}{'...' if len(prompt) > 50 else ''}\n")
|
||||||
|
|
||||||
|
# Initialize OpenAI client with OpenRouter endpoint
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=api_key,
|
||||||
|
base_url="https://openrouter.ai/api/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("⏳ Generating response...\n")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
ttft = None
|
||||||
|
full_response = ""
|
||||||
|
token_count = 0
|
||||||
|
|
||||||
|
# Create streaming chat completion
|
||||||
|
# OpenRouter supports additional headers for better tracking
|
||||||
|
stream = client.chat.completions.create(
|
||||||
|
model=model,
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
],
|
||||||
|
stream=True,
|
||||||
|
max_tokens=1024,
|
||||||
|
temperature=0.7,
|
||||||
|
extra_headers={
|
||||||
|
"HTTP-Referer": "https://github.com/ai-bot-pro/live-agent",
|
||||||
|
"X-Title": "Live Agent Test"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process stream
|
||||||
|
for chunk in stream:
|
||||||
|
if chunk.choices[0].delta.content:
|
||||||
|
content = chunk.choices[0].delta.content
|
||||||
|
|
||||||
|
# Record time to first token
|
||||||
|
if ttft is None:
|
||||||
|
ttft = (time.time() - start_time) * 1000
|
||||||
|
|
||||||
|
full_response += content
|
||||||
|
token_count += 1
|
||||||
|
print(content, end="", flush=True)
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
total_latency = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
|
||||||
|
return full_response, ttft, total_latency, token_count
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Parse command line arguments
|
||||||
|
prompt = "Hello! Please introduce yourself briefly in 2-3 sentences."
|
||||||
|
model = None
|
||||||
|
|
||||||
|
args = sys.argv[1:]
|
||||||
|
i = 0
|
||||||
|
while i < len(args):
|
||||||
|
if args[i] == "--model" and i + 1 < len(args):
|
||||||
|
model = args[i + 1]
|
||||||
|
i += 2
|
||||||
|
elif not args[i].startswith("--"):
|
||||||
|
prompt = args[i]
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Run streaming test
|
||||||
|
response, ttft, total_latency, token_count = test_openrouter_llm_streaming(prompt, model)
|
||||||
|
|
||||||
|
# Print metrics
|
||||||
|
print(f"\n📊 Performance Metrics:")
|
||||||
|
print(f" ⚡ TTFT (Time to First Token): {ttft:.0f} ms")
|
||||||
|
print(f" ⏱️ Total Latency: {total_latency:.0f} ms ({total_latency/1000:.2f}s)")
|
||||||
|
print(f" 📝 Response Length: {len(response)} characters")
|
||||||
|
print(f" 🔢 Token Chunks: {token_count}")
|
||||||
|
|
||||||
|
if token_count > 0 and total_latency > ttft:
|
||||||
|
avg_token_time = (total_latency - ttft) / token_count
|
||||||
|
print(f" 🚀 Avg Time per Token: {avg_token_time:.1f} ms")
|
||||||
|
|
||||||
|
print("\n✅ Test completed successfully!")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Test interrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
@ -78,6 +78,7 @@ class VADProvider(VADProviderBase):
|
||||||
if conn.client_have_voice and not client_have_voice:
|
if conn.client_have_voice and not client_have_voice:
|
||||||
stop_duration = time.time() * 1000 - conn.last_activity_time
|
stop_duration = time.time() * 1000 - conn.last_activity_time
|
||||||
if stop_duration >= self.silence_threshold_ms:
|
if stop_duration >= self.silence_threshold_ms:
|
||||||
|
if conn.client_listen_mode != "manual":
|
||||||
conn.client_voice_stop = True
|
conn.client_voice_stop = True
|
||||||
if client_have_voice:
|
if client_have_voice:
|
||||||
conn.client_have_voice = True
|
conn.client_have_voice = True
|
||||||
|
|
|
||||||
1254
main/xiaozhi-server/test/abbreviated_version/app.js
Normal file
1254
main/xiaozhi-server/test/abbreviated_version/app.js
Normal file
File diff suppressed because it is too large
Load diff
503
main/xiaozhi-server/test/abbreviated_version/test.html
Normal file
503
main/xiaozhi-server/test/abbreviated_version/test.html
Normal file
|
|
@ -0,0 +1,503 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>小智语音服务测试</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
padding: 8px 15px;
|
||||||
|
margin-right: 10px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
background-color: #4285f4;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: #3367d6;
|
||||||
|
}
|
||||||
|
button:disabled {
|
||||||
|
background-color: #cccccc;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
#status {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
#scriptStatus {
|
||||||
|
position: fixed;
|
||||||
|
top: 10px;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 5px;
|
||||||
|
display: block;
|
||||||
|
z-index: 100;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
#scriptStatus.info {
|
||||||
|
background-color: #e8f0fe;
|
||||||
|
color: #4285f4;
|
||||||
|
border-left: 4px solid #4285f4;
|
||||||
|
}
|
||||||
|
#scriptStatus.success {
|
||||||
|
background-color: #e6f4ea;
|
||||||
|
color: #0f9d58;
|
||||||
|
border-left: 4px solid #0f9d58;
|
||||||
|
}
|
||||||
|
#scriptStatus.error {
|
||||||
|
background-color: #fce8e6;
|
||||||
|
color: #db4437;
|
||||||
|
border-left: 4px solid #db4437;
|
||||||
|
}
|
||||||
|
#debugInfo {
|
||||||
|
margin-top: 20px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 10px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 12px;
|
||||||
|
max-height: 200px;
|
||||||
|
overflow-y: auto;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#showDebug {
|
||||||
|
margin-top: 10px;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
color: #444;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
#audioMeter {
|
||||||
|
margin-top: 10px;
|
||||||
|
height: 20px;
|
||||||
|
background-color: #eee;
|
||||||
|
border-radius: 4px;
|
||||||
|
overflow: hidden;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#audioLevel {
|
||||||
|
height: 100%;
|
||||||
|
width: 0%;
|
||||||
|
background-color: #4285f4;
|
||||||
|
transition: width 0.1s;
|
||||||
|
}
|
||||||
|
.conversation {
|
||||||
|
max-height: 300px;
|
||||||
|
overflow-y: auto;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 10px;
|
||||||
|
background-color: white;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
.message {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 8px;
|
||||||
|
max-width: 80%;
|
||||||
|
}
|
||||||
|
.user {
|
||||||
|
background-color: #e2f2ff;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: 10px;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
.server {
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
margin-right: auto;
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
#serverUrl {
|
||||||
|
flex-grow: 1;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
width: 60%;
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
#messageInput {
|
||||||
|
flex-grow: 1;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
width: 70%;
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
.section {
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>小智语音服务测试</h2>
|
||||||
|
|
||||||
|
<div id="scriptStatus" class="info">正在加载Opus库...</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h3>WebSocket连接 <span id="connectionStatus">未连接</span></h3>
|
||||||
|
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
||||||
|
<input type="text" id="serverUrl" value="ws://127.0.0.1:8000/xiaozhi/v1/" placeholder="WebSocket服务器地址">
|
||||||
|
<button id="connectButton">连接</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h3>录音测试</h3>
|
||||||
|
<button id="initAudio" style="background-color: #34a853;">初始化音频</button>
|
||||||
|
<button id="testMic" style="background-color: #fbbc05;">测试麦克风</button>
|
||||||
|
<p></p>
|
||||||
|
<button id="start" disabled>开始录音</button>
|
||||||
|
<button id="stop" disabled>停止录音</button>
|
||||||
|
<button id="play" disabled>播放录音</button>
|
||||||
|
<p>录音状态: <span id="status">待机,正在初始化...</span></p>
|
||||||
|
<div id="audioMeter">
|
||||||
|
<div id="audioLevel"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h3>文本消息</h3>
|
||||||
|
<div style="display: flex; align-items: center;">
|
||||||
|
<input type="text" id="messageInput" placeholder="输入消息..." disabled>
|
||||||
|
<button id="sendTextButton" disabled>发送</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h3>会话记录</h3>
|
||||||
|
<div id="conversation" class="conversation"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button id="showDebug">显示/隐藏调试信息</button>
|
||||||
|
<div id="debugInfo"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// 定义全局变量以跟踪库加载状态
|
||||||
|
window.opusLoaded = false;
|
||||||
|
window.startButton = document.getElementById("start");
|
||||||
|
window.stopButton = document.getElementById("stop");
|
||||||
|
window.playButton = document.getElementById("play");
|
||||||
|
window.statusLabel = document.getElementById("status");
|
||||||
|
window.debugInfo = document.getElementById("debugInfo");
|
||||||
|
window.audioContextReady = false;
|
||||||
|
window.testMicActive = false;
|
||||||
|
|
||||||
|
// 显示/隐藏调试信息
|
||||||
|
document.getElementById("showDebug").addEventListener("click", function() {
|
||||||
|
if (debugInfo.style.display === "none" || !debugInfo.style.display) {
|
||||||
|
debugInfo.style.display = "block";
|
||||||
|
this.textContent = "隐藏调试信息";
|
||||||
|
} else {
|
||||||
|
debugInfo.style.display = "none";
|
||||||
|
this.textContent = "显示调试信息";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加初始化音频按钮事件
|
||||||
|
document.getElementById("initAudio").addEventListener("click", function() {
|
||||||
|
initializeAudioSystem();
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加测试麦克风按钮事件
|
||||||
|
document.getElementById("testMic").addEventListener("click", function() {
|
||||||
|
if (window.testMicActive) {
|
||||||
|
stopMicTest();
|
||||||
|
} else {
|
||||||
|
startMicTest();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 初始化音频系统
|
||||||
|
function initializeAudioSystem() {
|
||||||
|
try {
|
||||||
|
log("初始化音频系统...");
|
||||||
|
// 创建临时AudioContext来触发用户授权
|
||||||
|
const tempContext = new (window.AudioContext || window.webkitAudioContext)({
|
||||||
|
sampleRate: 16000,
|
||||||
|
latencyHint: 'interactive'
|
||||||
|
});
|
||||||
|
|
||||||
|
// 创建振荡器并播放短促的声音
|
||||||
|
const oscillator = tempContext.createOscillator();
|
||||||
|
const gain = tempContext.createGain();
|
||||||
|
gain.gain.value = 0.1; // 很小的音量
|
||||||
|
oscillator.connect(gain);
|
||||||
|
gain.connect(tempContext.destination);
|
||||||
|
oscillator.frequency.value = 440; // A4
|
||||||
|
oscillator.start();
|
||||||
|
|
||||||
|
// 0.2秒后停止
|
||||||
|
setTimeout(() => {
|
||||||
|
oscillator.stop();
|
||||||
|
// 关闭上下文
|
||||||
|
tempContext.close().then(() => {
|
||||||
|
log("音频系统初始化成功", "success");
|
||||||
|
updateScriptStatus("音频系统已激活", "success");
|
||||||
|
document.getElementById("initAudio").disabled = true;
|
||||||
|
document.getElementById("initAudio").textContent = "音频已初始化";
|
||||||
|
window.audioContextReady = true;
|
||||||
|
|
||||||
|
// 如果Opus已加载,启用开始录音按钮
|
||||||
|
if (window.opusLoaded) {
|
||||||
|
startButton.disabled = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, 200);
|
||||||
|
} catch (err) {
|
||||||
|
log("初始化音频系统失败: " + err.message, "error");
|
||||||
|
updateScriptStatus("初始化音频失败: " + err.message, "error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试麦克风
|
||||||
|
function startMicTest() {
|
||||||
|
const audioMeter = document.getElementById("audioMeter");
|
||||||
|
const audioLevel = document.getElementById("audioLevel");
|
||||||
|
const testMicBtn = document.getElementById("testMic");
|
||||||
|
|
||||||
|
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||||
|
log("浏览器不支持麦克风访问", "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log("开始麦克风测试...");
|
||||||
|
audioMeter.style.display = "block";
|
||||||
|
testMicBtn.textContent = "停止测试";
|
||||||
|
testMicBtn.style.backgroundColor = "#ea4335";
|
||||||
|
window.testMicActive = true;
|
||||||
|
|
||||||
|
// 创建音频上下文
|
||||||
|
const testContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
|
window.testContext = testContext;
|
||||||
|
|
||||||
|
// 获取麦克风权限
|
||||||
|
navigator.mediaDevices.getUserMedia({
|
||||||
|
audio: {
|
||||||
|
echoCancellation: true,
|
||||||
|
noiseSuppression: true,
|
||||||
|
autoGainControl: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(stream => {
|
||||||
|
log("已获取麦克风访问权限", "success");
|
||||||
|
|
||||||
|
// 保存流以便稍后关闭
|
||||||
|
window.testStream = stream;
|
||||||
|
|
||||||
|
// 创建音频分析器
|
||||||
|
const source = testContext.createMediaStreamSource(stream);
|
||||||
|
const analyser = testContext.createAnalyser();
|
||||||
|
analyser.fftSize = 256;
|
||||||
|
source.connect(analyser);
|
||||||
|
|
||||||
|
// 创建音量显示更新函数
|
||||||
|
const bufferLength = analyser.frequencyBinCount;
|
||||||
|
const dataArray = new Uint8Array(bufferLength);
|
||||||
|
|
||||||
|
function updateMeter() {
|
||||||
|
if (!window.testMicActive) return;
|
||||||
|
|
||||||
|
analyser.getByteFrequencyData(dataArray);
|
||||||
|
|
||||||
|
// 计算音量级别 (0-100)
|
||||||
|
let sum = 0;
|
||||||
|
for (let i = 0; i < bufferLength; i++) {
|
||||||
|
sum += dataArray[i];
|
||||||
|
}
|
||||||
|
const average = sum / bufferLength;
|
||||||
|
const level = Math.min(100, Math.max(0, average * 2));
|
||||||
|
|
||||||
|
// 更新音量计
|
||||||
|
audioLevel.style.width = level + "%";
|
||||||
|
|
||||||
|
// 如果有声音,记录日志
|
||||||
|
if (level > 10) {
|
||||||
|
log(`检测到声音: ${level.toFixed(1)}%`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 循环更新
|
||||||
|
window.testMicAnimationFrame = requestAnimationFrame(updateMeter);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始更新
|
||||||
|
updateMeter();
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
log("麦克风测试失败: " + err.message, "error");
|
||||||
|
window.testMicActive = false;
|
||||||
|
testMicBtn.textContent = "测试麦克风";
|
||||||
|
testMicBtn.style.backgroundColor = "#fbbc05";
|
||||||
|
audioMeter.style.display = "none";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止麦克风测试
|
||||||
|
function stopMicTest() {
|
||||||
|
const audioMeter = document.getElementById("audioMeter");
|
||||||
|
const testMicBtn = document.getElementById("testMic");
|
||||||
|
|
||||||
|
log("停止麦克风测试");
|
||||||
|
window.testMicActive = false;
|
||||||
|
testMicBtn.textContent = "测试麦克风";
|
||||||
|
testMicBtn.style.backgroundColor = "#fbbc05";
|
||||||
|
|
||||||
|
// 停止分析器动画
|
||||||
|
if (window.testMicAnimationFrame) {
|
||||||
|
cancelAnimationFrame(window.testMicAnimationFrame);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止麦克风流
|
||||||
|
if (window.testStream) {
|
||||||
|
window.testStream.getTracks().forEach(track => track.stop());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭测试上下文
|
||||||
|
if (window.testContext) {
|
||||||
|
window.testContext.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 隐藏音量计
|
||||||
|
audioMeter.style.display = "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加调试日志
|
||||||
|
function log(message, type = "info") {
|
||||||
|
console.log(message);
|
||||||
|
const time = new Date().toLocaleTimeString();
|
||||||
|
const entry = document.createElement("div");
|
||||||
|
entry.textContent = `[${time}] ${message}`;
|
||||||
|
|
||||||
|
if (type === "error") {
|
||||||
|
entry.style.color = "#db4437";
|
||||||
|
} else if (type === "success") {
|
||||||
|
entry.style.color = "#0f9d58";
|
||||||
|
}
|
||||||
|
|
||||||
|
debugInfo.appendChild(entry);
|
||||||
|
debugInfo.scrollTop = debugInfo.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新脚本状态显示
|
||||||
|
function updateScriptStatus(message, type) {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) {
|
||||||
|
statusElement.textContent = message;
|
||||||
|
statusElement.className = type;
|
||||||
|
statusElement.style.display = 'block';
|
||||||
|
}
|
||||||
|
log(message, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查Opus库是否已加载
|
||||||
|
function checkOpusLoaded() {
|
||||||
|
try {
|
||||||
|
// 检查Module是否存在(本地库导出的全局变量)
|
||||||
|
if (typeof Module === 'undefined') {
|
||||||
|
log("Module对象不存在", "error");
|
||||||
|
throw new Error('Opus库未加载,Module对象不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录Module对象结构以便调试
|
||||||
|
log("Module对象结构: " + Object.keys(Module).join(", "));
|
||||||
|
|
||||||
|
// 尝试使用全局Module函数
|
||||||
|
if (typeof Module._opus_decoder_get_size === 'function') {
|
||||||
|
window.ModuleInstance = Module;
|
||||||
|
log('Opus库加载成功(使用全局Module)', "success");
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 启用开始录音按钮
|
||||||
|
startButton.disabled = false;
|
||||||
|
statusLabel.textContent = "待机";
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
setTimeout(() => {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
}, 3000);
|
||||||
|
|
||||||
|
window.opusLoaded = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试使用Module.instance
|
||||||
|
if (typeof Module.instance !== 'undefined' && typeof Module.instance._opus_decoder_get_size === 'function') {
|
||||||
|
window.ModuleInstance = Module.instance;
|
||||||
|
log('Opus库加载成功(使用Module.instance)', "success");
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 启用开始录音按钮
|
||||||
|
startButton.disabled = false;
|
||||||
|
statusLabel.textContent = "待机";
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
setTimeout(() => {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
}, 3000);
|
||||||
|
|
||||||
|
window.opusLoaded = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否有其他导出方式
|
||||||
|
log("Module上可用方法: " + Object.getOwnPropertyNames(Module).filter(prop => typeof Module[prop] === 'function').join(", "));
|
||||||
|
|
||||||
|
if (typeof Module.onRuntimeInitialized === 'function' || typeof Module.onRuntimeInitialized === 'undefined') {
|
||||||
|
log("Module.onRuntimeInitialized 尚未执行,等待模块初始化完成...");
|
||||||
|
// Module可能未完成初始化,注册回调
|
||||||
|
Module.onRuntimeInitialized = function() {
|
||||||
|
log("Opus库运行时初始化完成,重新检查");
|
||||||
|
checkOpusLoaded();
|
||||||
|
};
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Opus解码函数未找到,可能Module结构不正确');
|
||||||
|
} catch (err) {
|
||||||
|
log(`Opus库加载失败: ${err.message}`, "error");
|
||||||
|
updateScriptStatus(`Opus库加载失败: ${err.message}`, 'error');
|
||||||
|
statusLabel.textContent = "错误:Opus库加载失败";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 页面加载完成后检查浏览器能力和Opus库
|
||||||
|
window.addEventListener('load', function() {
|
||||||
|
log("页面加载完成,开始初始化...");
|
||||||
|
updateScriptStatus('正在初始化录音环境...', 'info');
|
||||||
|
|
||||||
|
// 延迟一小段时间再检查,确保库有时间加载
|
||||||
|
setTimeout(function checkAndRetry() {
|
||||||
|
if (!checkOpusLoaded()) {
|
||||||
|
// 如果加载失败,5秒后重试
|
||||||
|
log("Opus库加载失败,5秒后重试...");
|
||||||
|
updateScriptStatus('Opus库加载失败,正在重试...', 'error');
|
||||||
|
setTimeout(checkAndRetry, 5000);
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 防止按钮误操作
|
||||||
|
document.getElementById("stop").disabled = true;
|
||||||
|
document.getElementById("play").disabled = true;
|
||||||
|
</script>
|
||||||
|
<script src="./../libopus.js"></script>
|
||||||
|
<script src="app.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
72
main/xiaozhi-server/test/default-mcp-tools.json
Normal file
72
main/xiaozhi-server/test/default-mcp-tools.json
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "self.get_device_status",
|
||||||
|
"description": "Provides the real-time information of the device, including the current status of the audio speaker, screen, battery, network, etc.\nUse this tool for: \n1. Answering questions about current condition (e.g. what is the current volume of the audio speaker?)\n2. As the first step to control the device (e.g. turn up / down the volume of the audio speaker, etc.)",
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {}
|
||||||
|
},
|
||||||
|
"mockResponse": {
|
||||||
|
"audio_speaker": {
|
||||||
|
"volume": 50,
|
||||||
|
"muted": false
|
||||||
|
},
|
||||||
|
"screen": {
|
||||||
|
"brightness": 80,
|
||||||
|
"theme": "light"
|
||||||
|
},
|
||||||
|
"battery": {
|
||||||
|
"level": 85,
|
||||||
|
"charging": false
|
||||||
|
},
|
||||||
|
"network": {
|
||||||
|
"connected": true,
|
||||||
|
"type": "wifi"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "self.audio_speaker.set_volume",
|
||||||
|
"description": "Set the volume of the audio speaker. If the current volume is unknown, you must call `self.get_device_status` tool first and then call this tool.",
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"volume": {
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 100
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"volume"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"mockResponse": {
|
||||||
|
"success": true,
|
||||||
|
"volume": "${volume}",
|
||||||
|
"message": "音量已设置为 ${volume}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "self.screen.set_brightness",
|
||||||
|
"description": "Set the brightness of the screen.",
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"brightness": {
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 100
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"brightness"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"mockResponse": {
|
||||||
|
"success": true,
|
||||||
|
"brightness": "${brightness}",
|
||||||
|
"message": "亮度已设置为 ${brightness}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
149
main/xiaozhi-server/test/js/StreamingContext.js
Normal file
149
main/xiaozhi-server/test/js/StreamingContext.js
Normal file
|
|
@ -0,0 +1,149 @@
|
||||||
|
import BlockingQueue from './utils/BlockingQueue.js';
|
||||||
|
import { log } from './utils/logger.js';
|
||||||
|
|
||||||
|
// 音频流播放上下文类
|
||||||
|
export class StreamingContext {
|
||||||
|
constructor(opusDecoder, audioContext, sampleRate, channels, minAudioDuration) {
|
||||||
|
this.opusDecoder = opusDecoder;
|
||||||
|
this.audioContext = audioContext;
|
||||||
|
|
||||||
|
// 音频参数
|
||||||
|
this.sampleRate = sampleRate;
|
||||||
|
this.channels = channels;
|
||||||
|
this.minAudioDuration = minAudioDuration;
|
||||||
|
|
||||||
|
// 初始化队列和状态
|
||||||
|
this.queue = []; // 已解码的PCM队列。正在播放
|
||||||
|
this.activeQueue = new BlockingQueue(); // 已解码的PCM队列。准备播放
|
||||||
|
this.pendingAudioBufferQueue = []; // 待处理的缓存队列
|
||||||
|
this.audioBufferQueue = new BlockingQueue(); // 缓存队列
|
||||||
|
this.playing = false; // 是否正在播放
|
||||||
|
this.endOfStream = false; // 是否收到结束信号
|
||||||
|
this.source = null; // 当前音频源
|
||||||
|
this.totalSamples = 0; // 累积的总样本数
|
||||||
|
this.lastPlayTime = 0; // 上次播放的时间戳
|
||||||
|
}
|
||||||
|
|
||||||
|
// 缓存音频数组
|
||||||
|
pushAudioBuffer(item) {
|
||||||
|
this.audioBufferQueue.enqueue(...item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取需要处理缓存队列,单线程:在audioBufferQueue一直更新的状态下不会出现安全问题
|
||||||
|
async getPendingAudioBufferQueue() {
|
||||||
|
// 原子交换 + 清空
|
||||||
|
[this.pendingAudioBufferQueue, this.audioBufferQueue] = [await this.audioBufferQueue.dequeue(), new BlockingQueue()];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取正在播放已解码的PCM队列,单线程:在activeQueue一直更新的状态下不会出现安全问题
|
||||||
|
async getQueue(minSamples) {
|
||||||
|
let TepArray = [];
|
||||||
|
const num = minSamples - this.queue.length > 0 ? minSamples - this.queue.length : 1;
|
||||||
|
// 原子交换 + 清空
|
||||||
|
[TepArray, this.activeQueue] = [await this.activeQueue.dequeue(num), new BlockingQueue()];
|
||||||
|
this.queue.push(...TepArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将Int16音频数据转换为Float32音频数据
|
||||||
|
convertInt16ToFloat32(int16Data) {
|
||||||
|
const float32Data = new Float32Array(int16Data.length);
|
||||||
|
for (let i = 0; i < int16Data.length; i++) {
|
||||||
|
// 将[-32768,32767]范围转换为[-1,1]
|
||||||
|
float32Data[i] = int16Data[i] / (int16Data[i] < 0 ? 0x8000 : 0x7FFF);
|
||||||
|
}
|
||||||
|
return float32Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将Opus数据解码为PCM
|
||||||
|
async decodeOpusFrames() {
|
||||||
|
if (!this.opusDecoder) {
|
||||||
|
log('Opus解码器未初始化,无法解码', 'error');
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
log('Opus解码器启动', 'info');
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
let decodedSamples = [];
|
||||||
|
for (const frame of this.pendingAudioBufferQueue) {
|
||||||
|
try {
|
||||||
|
// 使用Opus解码器解码
|
||||||
|
const frameData = this.opusDecoder.decode(frame);
|
||||||
|
if (frameData && frameData.length > 0) {
|
||||||
|
// 转换为Float32
|
||||||
|
const floatData = this.convertInt16ToFloat32(frameData);
|
||||||
|
// 使用循环替代展开运算符
|
||||||
|
for (let i = 0; i < floatData.length; i++) {
|
||||||
|
decodedSamples.push(floatData[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
log("Opus解码失败: " + error.message, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (decodedSamples.length > 0) {
|
||||||
|
// 使用循环替代展开运算符
|
||||||
|
for (let i = 0; i < decodedSamples.length; i++) {
|
||||||
|
this.activeQueue.enqueue(decodedSamples[i]);
|
||||||
|
}
|
||||||
|
this.totalSamples += decodedSamples.length;
|
||||||
|
} else {
|
||||||
|
log('没有成功解码的样本', 'warning');
|
||||||
|
}
|
||||||
|
await this.getPendingAudioBufferQueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始播放音频
|
||||||
|
async startPlaying() {
|
||||||
|
while (true) {
|
||||||
|
// 如果累积了至少0.3秒的音频,开始播放
|
||||||
|
const minSamples = this.sampleRate * this.minAudioDuration * 3;
|
||||||
|
if (!this.playing && this.queue.length < minSamples) {
|
||||||
|
await this.getQueue(minSamples);
|
||||||
|
}
|
||||||
|
this.playing = true;
|
||||||
|
while (this.playing && this.queue.length) {
|
||||||
|
// 创建新的音频缓冲区
|
||||||
|
const minPlaySamples = Math.min(this.queue.length, this.sampleRate);
|
||||||
|
const currentSamples = this.queue.splice(0, minPlaySamples);
|
||||||
|
|
||||||
|
const audioBuffer = this.audioContext.createBuffer(this.channels, currentSamples.length, this.sampleRate);
|
||||||
|
audioBuffer.copyToChannel(new Float32Array(currentSamples), 0);
|
||||||
|
|
||||||
|
// 创建音频源
|
||||||
|
this.source = this.audioContext.createBufferSource();
|
||||||
|
this.source.buffer = audioBuffer;
|
||||||
|
|
||||||
|
// 创建增益节点用于平滑过渡
|
||||||
|
const gainNode = this.audioContext.createGain();
|
||||||
|
|
||||||
|
// 应用淡入淡出效果避免爆音
|
||||||
|
const fadeDuration = 0.02; // 20毫秒
|
||||||
|
gainNode.gain.setValueAtTime(0, this.audioContext.currentTime);
|
||||||
|
gainNode.gain.linearRampToValueAtTime(1, this.audioContext.currentTime + fadeDuration);
|
||||||
|
|
||||||
|
const duration = audioBuffer.duration;
|
||||||
|
if (duration > fadeDuration * 2) {
|
||||||
|
gainNode.gain.setValueAtTime(1, this.audioContext.currentTime + duration - fadeDuration);
|
||||||
|
gainNode.gain.linearRampToValueAtTime(0, this.audioContext.currentTime + duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接节点并开始播放
|
||||||
|
this.source.connect(gainNode);
|
||||||
|
gainNode.connect(this.audioContext.destination);
|
||||||
|
|
||||||
|
this.lastPlayTime = this.audioContext.currentTime;
|
||||||
|
log(`开始播放 ${currentSamples.length} 个样本,约 ${(currentSamples.length / this.sampleRate).toFixed(2)} 秒`, 'info');
|
||||||
|
this.source.start();
|
||||||
|
}
|
||||||
|
await this.getQueue(minSamples);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建streamingContext实例的工厂函数
|
||||||
|
export function createStreamingContext(opusDecoder, audioContext, sampleRate, channels, minAudioDuration) {
|
||||||
|
return new StreamingContext(opusDecoder, audioContext, sampleRate, channels, minAudioDuration);
|
||||||
|
}
|
||||||
49
main/xiaozhi-server/test/js/document.js
Normal file
49
main/xiaozhi-server/test/js/document.js
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
// DOM元素
|
||||||
|
const connectButton = document.getElementById('connectButton');
|
||||||
|
const serverUrlInput = document.getElementById('serverUrl');
|
||||||
|
const connectionStatus = document.getElementById('connectionStatus');
|
||||||
|
const messageInput = document.getElementById('messageInput');
|
||||||
|
const sendTextButton = document.getElementById('sendTextButton');
|
||||||
|
const recordButton = document.getElementById('recordButton');
|
||||||
|
const stopButton = document.getElementById('stopButton');
|
||||||
|
// 会话记录
|
||||||
|
const conversationDiv = document.getElementById('conversation');
|
||||||
|
const logContainer = document.getElementById('logContainer');
|
||||||
|
let visualizerCanvas = document.getElementById('audioVisualizer');
|
||||||
|
|
||||||
|
// ota 是否连接成功,修改成对应的样式
|
||||||
|
export function otaStatusStyle (flan) {
|
||||||
|
if(flan){
|
||||||
|
document.getElementById('otaStatus').textContent = 'ota已连接';
|
||||||
|
document.getElementById('otaStatus').style.color = 'green';
|
||||||
|
}else{
|
||||||
|
document.getElementById('otaStatus').textContent = 'ota未连接';
|
||||||
|
document.getElementById('otaStatus').style.color = 'red';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ota 是否连接成功,修改成对应的样式
|
||||||
|
export function getLogContainer (flan) {
|
||||||
|
return logContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新Opus库状态显示
|
||||||
|
export function updateScriptStatus(message, type) {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) {
|
||||||
|
statusElement.textContent = message;
|
||||||
|
statusElement.className = `script-status ${type}`;
|
||||||
|
statusElement.style.display = 'block';
|
||||||
|
statusElement.style.width = 'auto';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加消息到会话记录
|
||||||
|
export function addMessage(text, isUser = false) {
|
||||||
|
const messageDiv = document.createElement('div');
|
||||||
|
messageDiv.className = `message ${isUser ? 'user' : 'server'}`;
|
||||||
|
messageDiv.textContent = text;
|
||||||
|
conversationDiv.appendChild(messageDiv);
|
||||||
|
conversationDiv.scrollTop = conversationDiv.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
186
main/xiaozhi-server/test/js/opus.js
Normal file
186
main/xiaozhi-server/test/js/opus.js
Normal file
|
|
@ -0,0 +1,186 @@
|
||||||
|
import { log } from './utils/logger.js';
|
||||||
|
import { updateScriptStatus } from './document.js'
|
||||||
|
|
||||||
|
|
||||||
|
// 检查Opus库是否已加载
|
||||||
|
export function checkOpusLoaded() {
|
||||||
|
try {
|
||||||
|
// 检查Module是否存在(本地库导出的全局变量)
|
||||||
|
if (typeof Module === 'undefined') {
|
||||||
|
throw new Error('Opus库未加载,Module对象不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试先使用Module.instance(libopus.js最后一行导出方式)
|
||||||
|
if (typeof Module.instance !== 'undefined' && typeof Module.instance._opus_decoder_get_size === 'function') {
|
||||||
|
// 使用Module.instance对象替换全局Module对象
|
||||||
|
window.ModuleInstance = Module.instance;
|
||||||
|
log('Opus库加载成功(使用Module.instance)', 'success');
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有Module.instance,检查全局Module函数
|
||||||
|
if (typeof Module._opus_decoder_get_size === 'function') {
|
||||||
|
window.ModuleInstance = Module;
|
||||||
|
log('Opus库加载成功(使用全局Module)', 'success');
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Opus解码函数未找到,可能Module结构不正确');
|
||||||
|
} catch (err) {
|
||||||
|
log(`Opus库加载失败,请检查libopus.js文件是否存在且正确: ${err.message}`, 'error');
|
||||||
|
updateScriptStatus('Opus库加载失败,请检查libopus.js文件是否存在且正确', 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 创建一个Opus编码器
|
||||||
|
let opusEncoder = null;
|
||||||
|
export function initOpusEncoder() {
|
||||||
|
try {
|
||||||
|
if (opusEncoder) {
|
||||||
|
return opusEncoder; // 已经初始化过
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!window.ModuleInstance) {
|
||||||
|
log('无法创建Opus编码器:ModuleInstance不可用', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化一个Opus编码器
|
||||||
|
const mod = window.ModuleInstance;
|
||||||
|
const sampleRate = 16000; // 16kHz采样率
|
||||||
|
const channels = 1; // 单声道
|
||||||
|
const application = 2048; // OPUS_APPLICATION_VOIP = 2048
|
||||||
|
|
||||||
|
// 创建编码器
|
||||||
|
opusEncoder = {
|
||||||
|
channels: channels,
|
||||||
|
sampleRate: sampleRate,
|
||||||
|
frameSize: 960, // 60ms @ 16kHz = 60 * 16 = 960 samples
|
||||||
|
maxPacketSize: 4000, // 最大包大小
|
||||||
|
module: mod,
|
||||||
|
|
||||||
|
// 初始化编码器
|
||||||
|
init: function () {
|
||||||
|
try {
|
||||||
|
// 获取编码器大小
|
||||||
|
const encoderSize = mod._opus_encoder_get_size(this.channels);
|
||||||
|
log(`Opus编码器大小: ${encoderSize}字节`, 'info');
|
||||||
|
|
||||||
|
// 分配内存
|
||||||
|
this.encoderPtr = mod._malloc(encoderSize);
|
||||||
|
if (!this.encoderPtr) {
|
||||||
|
throw new Error("无法分配编码器内存");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化编码器
|
||||||
|
const err = mod._opus_encoder_init(
|
||||||
|
this.encoderPtr,
|
||||||
|
this.sampleRate,
|
||||||
|
this.channels,
|
||||||
|
application
|
||||||
|
);
|
||||||
|
|
||||||
|
if (err < 0) {
|
||||||
|
throw new Error(`Opus编码器初始化失败: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置位率 (16kbps)
|
||||||
|
mod._opus_encoder_ctl(this.encoderPtr, 4002, 16000); // OPUS_SET_BITRATE
|
||||||
|
|
||||||
|
// 设置复杂度 (0-10, 越高质量越好但CPU使用越多)
|
||||||
|
mod._opus_encoder_ctl(this.encoderPtr, 4010, 5); // OPUS_SET_COMPLEXITY
|
||||||
|
|
||||||
|
// 设置使用DTX (不传输静音帧)
|
||||||
|
mod._opus_encoder_ctl(this.encoderPtr, 4016, 1); // OPUS_SET_DTX
|
||||||
|
|
||||||
|
log("Opus编码器初始化成功", 'success');
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
if (this.encoderPtr) {
|
||||||
|
mod._free(this.encoderPtr);
|
||||||
|
this.encoderPtr = null;
|
||||||
|
}
|
||||||
|
log(`Opus编码器初始化失败: ${error.message}`, 'error');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 编码PCM数据为Opus
|
||||||
|
encode: function (pcmData) {
|
||||||
|
if (!this.encoderPtr) {
|
||||||
|
if (!this.init()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const mod = this.module;
|
||||||
|
|
||||||
|
// 为PCM数据分配内存
|
||||||
|
const pcmPtr = mod._malloc(pcmData.length * 2); // 2字节/int16
|
||||||
|
|
||||||
|
// 将PCM数据复制到HEAP
|
||||||
|
for (let i = 0; i < pcmData.length; i++) {
|
||||||
|
mod.HEAP16[(pcmPtr >> 1) + i] = pcmData[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 为输出分配内存
|
||||||
|
const outPtr = mod._malloc(this.maxPacketSize);
|
||||||
|
|
||||||
|
// 进行编码
|
||||||
|
const encodedLen = mod._opus_encode(
|
||||||
|
this.encoderPtr,
|
||||||
|
pcmPtr,
|
||||||
|
this.frameSize,
|
||||||
|
outPtr,
|
||||||
|
this.maxPacketSize
|
||||||
|
);
|
||||||
|
|
||||||
|
if (encodedLen < 0) {
|
||||||
|
throw new Error(`Opus编码失败: ${encodedLen}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制编码后的数据
|
||||||
|
const opusData = new Uint8Array(encodedLen);
|
||||||
|
for (let i = 0; i < encodedLen; i++) {
|
||||||
|
opusData[i] = mod.HEAPU8[outPtr + i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放内存
|
||||||
|
mod._free(pcmPtr);
|
||||||
|
mod._free(outPtr);
|
||||||
|
|
||||||
|
return opusData;
|
||||||
|
} catch (error) {
|
||||||
|
log(`Opus编码出错: ${error.message}`, 'error');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 销毁编码器
|
||||||
|
destroy: function () {
|
||||||
|
if (this.encoderPtr) {
|
||||||
|
this.module._free(this.encoderPtr);
|
||||||
|
this.encoderPtr = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
opusEncoder.init();
|
||||||
|
return opusEncoder;
|
||||||
|
} catch (error) {
|
||||||
|
log(`创建Opus编码器失败: ${error.message}`, 'error');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
98
main/xiaozhi-server/test/js/utils/BlockingQueue.js
Normal file
98
main/xiaozhi-server/test/js/utils/BlockingQueue.js
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
export default class BlockingQueue {
|
||||||
|
#items = [];
|
||||||
|
#waiters = []; // {resolve, reject, min, timer, onTimeout}
|
||||||
|
|
||||||
|
/* 空队列一次性闸门 */
|
||||||
|
#emptyPromise = null;
|
||||||
|
#emptyResolve = null;
|
||||||
|
|
||||||
|
/* 生产者:把数据塞进去 */
|
||||||
|
enqueue(item, ...restItems) {
|
||||||
|
if (restItems.length === 0) {
|
||||||
|
this.#items.push(item);
|
||||||
|
}
|
||||||
|
// 如果有额外参数,批量处理所有项
|
||||||
|
else {
|
||||||
|
const items = [item, ...restItems].filter(i => i);
|
||||||
|
if (items.length === 0) return;
|
||||||
|
this.#items.push(...items);
|
||||||
|
}
|
||||||
|
// 若有空队列闸门,一次性放行所有等待者
|
||||||
|
if (this.#emptyResolve) {
|
||||||
|
this.#emptyResolve();
|
||||||
|
this.#emptyResolve = null;
|
||||||
|
this.#emptyPromise = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 唤醒所有正在等的 waiter
|
||||||
|
this.#wakeWaiters();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 消费者:min 条或 timeout ms 先到谁 */
|
||||||
|
async dequeue(min = 1, timeout = Infinity, onTimeout = null) {
|
||||||
|
// 1. 若空,等第一次数据到达(所有调用共享同一个 promise)
|
||||||
|
if (this.#items.length === 0) {
|
||||||
|
await this.#waitForFirstItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 立即满足
|
||||||
|
if (this.#items.length >= min) {
|
||||||
|
return this.#flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 需要等待
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let timer = null;
|
||||||
|
const waiter = { resolve, reject, min, onTimeout, timer };
|
||||||
|
|
||||||
|
// 超时逻辑
|
||||||
|
if (Number.isFinite(timeout)) {
|
||||||
|
waiter.timer = setTimeout(() => {
|
||||||
|
this.#removeWaiter(waiter);
|
||||||
|
if (onTimeout) onTimeout(this.#items.length);
|
||||||
|
resolve(this.#flush());
|
||||||
|
}, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#waiters.push(waiter);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 空队列闸门生成器 */
|
||||||
|
#waitForFirstItem() {
|
||||||
|
if (!this.#emptyPromise) {
|
||||||
|
this.#emptyPromise = new Promise(r => (this.#emptyResolve = r));
|
||||||
|
}
|
||||||
|
return this.#emptyPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 内部:每次数据变动后,检查哪些 waiter 已满足 */
|
||||||
|
#wakeWaiters() {
|
||||||
|
for (let i = this.#waiters.length - 1; i >= 0; i--) {
|
||||||
|
const w = this.#waiters[i];
|
||||||
|
if (this.#items.length >= w.min) {
|
||||||
|
this.#removeWaiter(w);
|
||||||
|
w.resolve(this.#flush());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#removeWaiter(waiter) {
|
||||||
|
const idx = this.#waiters.indexOf(waiter);
|
||||||
|
if (idx !== -1) {
|
||||||
|
this.#waiters.splice(idx, 1);
|
||||||
|
if (waiter.timer) clearTimeout(waiter.timer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#flush() {
|
||||||
|
const snapshot = [...this.#items];
|
||||||
|
this.#items.length = 0;
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 当前缓存长度(不含等待者) */
|
||||||
|
get length() {
|
||||||
|
return this.#items.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
37
main/xiaozhi-server/test/js/utils/logger.js
Normal file
37
main/xiaozhi-server/test/js/utils/logger.js
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import { getLogContainer } from '../document.js'
|
||||||
|
|
||||||
|
const logContainer = getLogContainer();
|
||||||
|
// 日志记录函数
|
||||||
|
export function log(message, type = 'info') {
|
||||||
|
// 将消息按换行符分割成多行
|
||||||
|
const lines = message.split('\n');
|
||||||
|
const now = new Date();
|
||||||
|
// const timestamp = `[${now.toLocaleTimeString()}] `;
|
||||||
|
const timestamp = `[${now.toLocaleTimeString()}.${now.getMilliseconds().toString().padStart(3, '0')}] `;
|
||||||
|
// 为每一行创建日志条目
|
||||||
|
lines.forEach((line, index) => {
|
||||||
|
const logEntry = document.createElement('div');
|
||||||
|
logEntry.className = `log-entry log-${type}`;
|
||||||
|
// 如果是第一条日志,显示时间戳
|
||||||
|
const prefix = index === 0 ? timestamp : ' '.repeat(timestamp.length);
|
||||||
|
logEntry.textContent = `${prefix}${line}`;
|
||||||
|
// logEntry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
|
||||||
|
// logEntry.style 保留起始的空格
|
||||||
|
logEntry.style.whiteSpace = 'pre';
|
||||||
|
if (type === 'error') {
|
||||||
|
logEntry.style.color = 'red';
|
||||||
|
} else if (type === 'debug') {
|
||||||
|
logEntry.style.color = 'gray';
|
||||||
|
return;
|
||||||
|
} else if (type === 'warning') {
|
||||||
|
logEntry.style.color = 'orange';
|
||||||
|
} else if (type === 'success') {
|
||||||
|
logEntry.style.color = 'green';
|
||||||
|
} else {
|
||||||
|
logEntry.style.color = 'black';
|
||||||
|
}
|
||||||
|
logContainer.appendChild(logEntry);
|
||||||
|
});
|
||||||
|
|
||||||
|
logContainer.scrollTop = logContainer.scrollHeight;
|
||||||
|
}
|
||||||
124
main/xiaozhi-server/test/js/xiaoZhiConnect.js
Normal file
124
main/xiaozhi-server/test/js/xiaoZhiConnect.js
Normal file
|
|
@ -0,0 +1,124 @@
|
||||||
|
import { otaStatusStyle } from './document.js';
|
||||||
|
import { log } from './utils/logger.js';
|
||||||
|
|
||||||
|
// WebSocket 连接
|
||||||
|
export async function webSocketConnect(otaUrl, config) {
|
||||||
|
|
||||||
|
if (!validateConfig(config)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送OTA请求并获取返回的websocket信息
|
||||||
|
const otaResult = await sendOTA(otaUrl, config);
|
||||||
|
if (!otaResult) {
|
||||||
|
log('无法从OTA服务器获取信息', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从OTA响应中提取websocket信息
|
||||||
|
const { websocket } = otaResult;
|
||||||
|
if (!websocket || !websocket.url) {
|
||||||
|
log('OTA响应中缺少websocket信息', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用OTA返回的websocket URL
|
||||||
|
let connUrl = new URL(websocket.url);
|
||||||
|
|
||||||
|
// 添加token参数(从OTA响应中获取)
|
||||||
|
if (websocket.token) {
|
||||||
|
if (websocket.token.startsWith("Bearer ")) {
|
||||||
|
connUrl.searchParams.append('authorization', websocket.token);
|
||||||
|
} else {
|
||||||
|
connUrl.searchParams.append('authorization', 'Bearer ' + websocket.token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加认证参数(保持原有逻辑)
|
||||||
|
connUrl.searchParams.append('device-id', config.deviceId);
|
||||||
|
connUrl.searchParams.append('client-id', config.clientId);
|
||||||
|
|
||||||
|
const wsurl = connUrl.toString()
|
||||||
|
|
||||||
|
log(`正在连接: ${wsurl}`, 'info');
|
||||||
|
|
||||||
|
if (wsurl) {
|
||||||
|
document.getElementById('serverUrl').value = wsurl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new WebSocket(connUrl.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证配置
|
||||||
|
function validateConfig(config) {
|
||||||
|
if (!config.deviceMac) {
|
||||||
|
log('设备MAC地址不能为空', 'error');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!config.clientId) {
|
||||||
|
log('客户端ID不能为空', 'error');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断wsUrl路径是否存在错误
|
||||||
|
function validateWsUrl(wsUrl) {
|
||||||
|
if (wsUrl === '') return false;
|
||||||
|
// 检查URL格式
|
||||||
|
if (!wsUrl.startsWith('ws://') && !wsUrl.startsWith('wss://')) {
|
||||||
|
log('URL格式错误,必须以ws://或wss://开头', 'error');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// OTA发送请求,验证状态,并返回响应数据
|
||||||
|
async function sendOTA(otaUrl, config) {
|
||||||
|
try {
|
||||||
|
const res = await fetch(otaUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Device-Id': config.deviceId,
|
||||||
|
'Client-Id': config.clientId
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
version: 0,
|
||||||
|
uuid: '',
|
||||||
|
application: {
|
||||||
|
name: 'xiaozhi-web-test',
|
||||||
|
version: '1.0.0',
|
||||||
|
compile_time: '2025-04-16 10:00:00',
|
||||||
|
idf_version: '4.4.3',
|
||||||
|
elf_sha256: '1234567890abcdef1234567890abcdef1234567890abcdef'
|
||||||
|
},
|
||||||
|
ota: { label: 'xiaozhi-web-test' },
|
||||||
|
board: {
|
||||||
|
type: 'xiaozhi-web-test',
|
||||||
|
ssid: 'xiaozhi-web-test',
|
||||||
|
rssi: 0,
|
||||||
|
channel: 0,
|
||||||
|
ip: '192.168.1.1',
|
||||||
|
mac: config.deviceMac
|
||||||
|
},
|
||||||
|
flash_size: 0,
|
||||||
|
minimum_free_heap_size: 0,
|
||||||
|
mac_address: config.deviceMac,
|
||||||
|
chip_model_name: '',
|
||||||
|
chip_info: { model: 0, cores: 0, revision: 0, features: 0 },
|
||||||
|
partition_table: [{ label: '', type: 0, subtype: 0, address: 0, size: 0 }]
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
|
||||||
|
|
||||||
|
const result = await res.json();
|
||||||
|
otaStatusStyle(true)
|
||||||
|
return result; // 返回完整的响应数据
|
||||||
|
} catch (err) {
|
||||||
|
otaStatusStyle(false)
|
||||||
|
return null; // 失败返回null
|
||||||
|
}
|
||||||
|
}
|
||||||
266
main/xiaozhi-server/test/libopus.js
Normal file
266
main/xiaozhi-server/test/libopus.js
Normal file
File diff suppressed because one or more lines are too long
670
main/xiaozhi-server/test/opus_test/app.js
Normal file
670
main/xiaozhi-server/test/opus_test/app.js
Normal file
|
|
@ -0,0 +1,670 @@
|
||||||
|
const SAMPLE_RATE = 16000;
|
||||||
|
const CHANNELS = 1;
|
||||||
|
const FRAME_SIZE = 960; // 对应于60ms帧大小 (16000Hz * 0.06s = 960 samples)
|
||||||
|
const OPUS_APPLICATION = 2049; // OPUS_APPLICATION_AUDIO
|
||||||
|
const BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
|
let audioContext = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: SAMPLE_RATE });
|
||||||
|
let mediaStream, mediaSource, audioProcessor;
|
||||||
|
let recordedPcmData = []; // 存储原始PCM数据
|
||||||
|
let recordedOpusData = []; // 存储Opus编码后的数据
|
||||||
|
let opusEncoder, opusDecoder;
|
||||||
|
let isRecording = false;
|
||||||
|
|
||||||
|
const startButton = document.getElementById("start");
|
||||||
|
const stopButton = document.getElementById("stop");
|
||||||
|
const playButton = document.getElementById("play");
|
||||||
|
const statusLabel = document.getElementById("status");
|
||||||
|
|
||||||
|
startButton.addEventListener("click", startRecording);
|
||||||
|
stopButton.addEventListener("click", stopRecording);
|
||||||
|
playButton.addEventListener("click", playRecording);
|
||||||
|
|
||||||
|
// 初始化Opus编码器与解码器
|
||||||
|
async function initOpus() {
|
||||||
|
if (typeof window.ModuleInstance === 'undefined') {
|
||||||
|
if (typeof Module !== 'undefined') {
|
||||||
|
// 尝试使用全局Module
|
||||||
|
window.ModuleInstance = Module;
|
||||||
|
console.log('使用全局Module作为ModuleInstance');
|
||||||
|
} else {
|
||||||
|
console.error("Opus库未加载,ModuleInstance和Module对象都不存在");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const mod = window.ModuleInstance;
|
||||||
|
|
||||||
|
// 创建编码器
|
||||||
|
opusEncoder = {
|
||||||
|
channels: CHANNELS,
|
||||||
|
sampleRate: SAMPLE_RATE,
|
||||||
|
frameSize: FRAME_SIZE,
|
||||||
|
maxPacketSize: 4000,
|
||||||
|
module: mod,
|
||||||
|
|
||||||
|
// 初始化编码器
|
||||||
|
init: function() {
|
||||||
|
// 获取编码器大小
|
||||||
|
const encoderSize = mod._opus_encoder_get_size(this.channels);
|
||||||
|
console.log(`Opus编码器大小: ${encoderSize}字节`);
|
||||||
|
|
||||||
|
// 分配内存
|
||||||
|
this.encoderPtr = mod._malloc(encoderSize);
|
||||||
|
if (!this.encoderPtr) {
|
||||||
|
throw new Error("无法分配编码器内存");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化编码器
|
||||||
|
const err = mod._opus_encoder_init(
|
||||||
|
this.encoderPtr,
|
||||||
|
this.sampleRate,
|
||||||
|
this.channels,
|
||||||
|
OPUS_APPLICATION
|
||||||
|
);
|
||||||
|
|
||||||
|
if (err < 0) {
|
||||||
|
throw new Error(`Opus编码器初始化失败: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
// 编码方法
|
||||||
|
encode: function(pcmData) {
|
||||||
|
const mod = this.module;
|
||||||
|
|
||||||
|
// 为PCM数据分配内存
|
||||||
|
const pcmPtr = mod._malloc(pcmData.length * 2); // Int16 = 2字节
|
||||||
|
|
||||||
|
// 将数据复制到WASM内存
|
||||||
|
for (let i = 0; i < pcmData.length; i++) {
|
||||||
|
mod.HEAP16[(pcmPtr >> 1) + i] = pcmData[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 为Opus编码数据分配内存
|
||||||
|
const maxEncodedSize = this.maxPacketSize;
|
||||||
|
const encodedPtr = mod._malloc(maxEncodedSize);
|
||||||
|
|
||||||
|
// 编码
|
||||||
|
const encodedBytes = mod._opus_encode(
|
||||||
|
this.encoderPtr,
|
||||||
|
pcmPtr,
|
||||||
|
this.frameSize,
|
||||||
|
encodedPtr,
|
||||||
|
maxEncodedSize
|
||||||
|
);
|
||||||
|
|
||||||
|
if (encodedBytes < 0) {
|
||||||
|
mod._free(pcmPtr);
|
||||||
|
mod._free(encodedPtr);
|
||||||
|
throw new Error(`Opus编码失败: ${encodedBytes}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制编码后的数据
|
||||||
|
const encodedData = new Uint8Array(encodedBytes);
|
||||||
|
for (let i = 0; i < encodedBytes; i++) {
|
||||||
|
encodedData[i] = mod.HEAPU8[encodedPtr + i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放内存
|
||||||
|
mod._free(pcmPtr);
|
||||||
|
mod._free(encodedPtr);
|
||||||
|
|
||||||
|
return encodedData;
|
||||||
|
},
|
||||||
|
|
||||||
|
// 销毁方法
|
||||||
|
destroy: function() {
|
||||||
|
if (this.encoderPtr) {
|
||||||
|
this.module._free(this.encoderPtr);
|
||||||
|
this.encoderPtr = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 创建解码器
|
||||||
|
opusDecoder = {
|
||||||
|
channels: CHANNELS,
|
||||||
|
rate: SAMPLE_RATE,
|
||||||
|
frameSize: FRAME_SIZE,
|
||||||
|
module: mod,
|
||||||
|
|
||||||
|
// 初始化解码器
|
||||||
|
init: function() {
|
||||||
|
// 获取解码器大小
|
||||||
|
const decoderSize = mod._opus_decoder_get_size(this.channels);
|
||||||
|
console.log(`Opus解码器大小: ${decoderSize}字节`);
|
||||||
|
|
||||||
|
// 分配内存
|
||||||
|
this.decoderPtr = mod._malloc(decoderSize);
|
||||||
|
if (!this.decoderPtr) {
|
||||||
|
throw new Error("无法分配解码器内存");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化解码器
|
||||||
|
const err = mod._opus_decoder_init(
|
||||||
|
this.decoderPtr,
|
||||||
|
this.rate,
|
||||||
|
this.channels
|
||||||
|
);
|
||||||
|
|
||||||
|
if (err < 0) {
|
||||||
|
throw new Error(`Opus解码器初始化失败: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
// 解码方法
|
||||||
|
decode: function(opusData) {
|
||||||
|
const mod = this.module;
|
||||||
|
|
||||||
|
// 为Opus数据分配内存
|
||||||
|
const opusPtr = mod._malloc(opusData.length);
|
||||||
|
mod.HEAPU8.set(opusData, opusPtr);
|
||||||
|
|
||||||
|
// 为PCM输出分配内存
|
||||||
|
const pcmPtr = mod._malloc(this.frameSize * 2); // Int16 = 2字节
|
||||||
|
|
||||||
|
// 解码
|
||||||
|
const decodedSamples = mod._opus_decode(
|
||||||
|
this.decoderPtr,
|
||||||
|
opusPtr,
|
||||||
|
opusData.length,
|
||||||
|
pcmPtr,
|
||||||
|
this.frameSize,
|
||||||
|
0 // 不使用FEC
|
||||||
|
);
|
||||||
|
|
||||||
|
if (decodedSamples < 0) {
|
||||||
|
mod._free(opusPtr);
|
||||||
|
mod._free(pcmPtr);
|
||||||
|
throw new Error(`Opus解码失败: ${decodedSamples}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制解码后的数据
|
||||||
|
const decodedData = new Int16Array(decodedSamples);
|
||||||
|
for (let i = 0; i < decodedSamples; i++) {
|
||||||
|
decodedData[i] = mod.HEAP16[(pcmPtr >> 1) + i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放内存
|
||||||
|
mod._free(opusPtr);
|
||||||
|
mod._free(pcmPtr);
|
||||||
|
|
||||||
|
return decodedData;
|
||||||
|
},
|
||||||
|
|
||||||
|
// 销毁方法
|
||||||
|
destroy: function() {
|
||||||
|
if (this.decoderPtr) {
|
||||||
|
this.module._free(this.decoderPtr);
|
||||||
|
this.decoderPtr = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 初始化编码器和解码器
|
||||||
|
if (opusEncoder.init() && opusDecoder.init()) {
|
||||||
|
console.log("Opus 编码器和解码器初始化成功。");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
console.error("Opus 初始化失败");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Opus 初始化失败:", error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将Float32音频数据转换为Int16音频数据
|
||||||
|
function convertFloat32ToInt16(float32Data) {
|
||||||
|
const int16Data = new Int16Array(float32Data.length);
|
||||||
|
for (let i = 0; i < float32Data.length; i++) {
|
||||||
|
// 将[-1,1]范围转换为[-32768,32767]
|
||||||
|
const s = Math.max(-1, Math.min(1, float32Data[i]));
|
||||||
|
int16Data[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
|
||||||
|
}
|
||||||
|
return int16Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将Int16音频数据转换为Float32音频数据
|
||||||
|
function convertInt16ToFloat32(int16Data) {
|
||||||
|
const float32Data = new Float32Array(int16Data.length);
|
||||||
|
for (let i = 0; i < int16Data.length; i++) {
|
||||||
|
// 将[-32768,32767]范围转换为[-1,1]
|
||||||
|
float32Data[i] = int16Data[i] / (int16Data[i] < 0 ? 0x8000 : 0x7FFF);
|
||||||
|
}
|
||||||
|
return float32Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function startRecording() {
|
||||||
|
if (isRecording) return;
|
||||||
|
|
||||||
|
// 确保有权限并且AudioContext是活跃的
|
||||||
|
if (audioContext.state === 'suspended') {
|
||||||
|
audioContext.resume().then(() => {
|
||||||
|
console.log("AudioContext已恢复");
|
||||||
|
continueStartRecording();
|
||||||
|
}).catch(err => {
|
||||||
|
console.error("恢复AudioContext失败:", err);
|
||||||
|
statusLabel.textContent = "无法激活音频上下文,请再次点击";
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
continueStartRecording();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 实际开始录音的逻辑
|
||||||
|
function continueStartRecording() {
|
||||||
|
// 重置录音数据
|
||||||
|
recordedPcmData = [];
|
||||||
|
recordedOpusData = [];
|
||||||
|
window.audioDataBuffer = new Int16Array(0); // 重置缓冲区
|
||||||
|
|
||||||
|
// 初始化Opus
|
||||||
|
initOpus().then(success => {
|
||||||
|
if (!success) {
|
||||||
|
statusLabel.textContent = "Opus初始化失败";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("开始录音,参数:", {
|
||||||
|
sampleRate: SAMPLE_RATE,
|
||||||
|
channels: CHANNELS,
|
||||||
|
frameSize: FRAME_SIZE,
|
||||||
|
bufferSize: BUFFER_SIZE
|
||||||
|
});
|
||||||
|
|
||||||
|
// 请求麦克风权限
|
||||||
|
navigator.mediaDevices.getUserMedia({
|
||||||
|
audio: {
|
||||||
|
sampleRate: SAMPLE_RATE,
|
||||||
|
channelCount: CHANNELS,
|
||||||
|
echoCancellation: true,
|
||||||
|
noiseSuppression: true,
|
||||||
|
autoGainControl: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(stream => {
|
||||||
|
console.log("获取到麦克风流,实际参数:", stream.getAudioTracks()[0].getSettings());
|
||||||
|
|
||||||
|
// 检查流是否有效
|
||||||
|
if (!stream || !stream.getAudioTracks().length || !stream.getAudioTracks()[0].enabled) {
|
||||||
|
throw new Error("获取到的音频流无效");
|
||||||
|
}
|
||||||
|
|
||||||
|
mediaStream = stream;
|
||||||
|
mediaSource = audioContext.createMediaStreamSource(stream);
|
||||||
|
|
||||||
|
// 创建ScriptProcessor(虽然已弃用,但兼容性好)
|
||||||
|
// 在降级到ScriptProcessor之前尝试使用AudioWorklet
|
||||||
|
createAudioProcessor().then(processor => {
|
||||||
|
if (processor) {
|
||||||
|
console.log("使用AudioWorklet处理音频");
|
||||||
|
audioProcessor = processor;
|
||||||
|
// 连接音频处理链
|
||||||
|
mediaSource.connect(audioProcessor);
|
||||||
|
audioProcessor.connect(audioContext.destination);
|
||||||
|
} else {
|
||||||
|
console.log("回退到ScriptProcessor");
|
||||||
|
// 创建ScriptProcessor节点
|
||||||
|
audioProcessor = audioContext.createScriptProcessor(BUFFER_SIZE, CHANNELS, CHANNELS);
|
||||||
|
|
||||||
|
// 处理音频数据
|
||||||
|
audioProcessor.onaudioprocess = processAudioData;
|
||||||
|
|
||||||
|
// 连接音频处理链
|
||||||
|
mediaSource.connect(audioProcessor);
|
||||||
|
audioProcessor.connect(audioContext.destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新UI
|
||||||
|
isRecording = true;
|
||||||
|
statusLabel.textContent = "录音中...";
|
||||||
|
startButton.disabled = true;
|
||||||
|
stopButton.disabled = false;
|
||||||
|
playButton.disabled = true;
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("创建音频处理器失败:", error);
|
||||||
|
statusLabel.textContent = "创建音频处理器失败";
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("获取麦克风失败:", error);
|
||||||
|
statusLabel.textContent = "获取麦克风失败: " + error.message;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建AudioWorklet处理器
|
||||||
|
async function createAudioProcessor() {
|
||||||
|
try {
|
||||||
|
// 尝试使用更现代的AudioWorklet API
|
||||||
|
if ('AudioWorklet' in window && 'AudioWorkletNode' in window) {
|
||||||
|
// 定义AudioWorklet处理器代码
|
||||||
|
const workletCode = `
|
||||||
|
class OpusRecorderProcessor extends AudioWorkletProcessor {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.buffers = [];
|
||||||
|
this.frameSize = ${FRAME_SIZE};
|
||||||
|
this.buffer = new Float32Array(this.frameSize);
|
||||||
|
this.bufferIndex = 0;
|
||||||
|
this.isRecording = false;
|
||||||
|
|
||||||
|
this.port.onmessage = (event) => {
|
||||||
|
if (event.data.command === 'start') {
|
||||||
|
this.isRecording = true;
|
||||||
|
} else if (event.data.command === 'stop') {
|
||||||
|
this.isRecording = false;
|
||||||
|
// 发送最后的缓冲区
|
||||||
|
if (this.bufferIndex > 0) {
|
||||||
|
const finalBuffer = this.buffer.slice(0, this.bufferIndex);
|
||||||
|
this.port.postMessage({ buffer: finalBuffer });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
process(inputs, outputs) {
|
||||||
|
if (!this.isRecording) return true;
|
||||||
|
|
||||||
|
// 获取输入数据
|
||||||
|
const input = inputs[0][0]; // mono channel
|
||||||
|
if (!input || input.length === 0) return true;
|
||||||
|
|
||||||
|
// 将输入数据添加到缓冲区
|
||||||
|
for (let i = 0; i < input.length; i++) {
|
||||||
|
this.buffer[this.bufferIndex++] = input[i];
|
||||||
|
|
||||||
|
// 当缓冲区填满时,发送给主线程
|
||||||
|
if (this.bufferIndex >= this.frameSize) {
|
||||||
|
this.port.postMessage({ buffer: this.buffer.slice() });
|
||||||
|
this.bufferIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
registerProcessor('opus-recorder-processor', OpusRecorderProcessor);
|
||||||
|
`;
|
||||||
|
|
||||||
|
// 创建Blob URL
|
||||||
|
const blob = new Blob([workletCode], { type: 'application/javascript' });
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
// 加载AudioWorklet模块
|
||||||
|
await audioContext.audioWorklet.addModule(url);
|
||||||
|
|
||||||
|
// 创建AudioWorkletNode
|
||||||
|
const workletNode = new AudioWorkletNode(audioContext, 'opus-recorder-processor');
|
||||||
|
|
||||||
|
// 处理从AudioWorklet接收的消息
|
||||||
|
workletNode.port.onmessage = (event) => {
|
||||||
|
if (event.data.buffer) {
|
||||||
|
// 使用与ScriptProcessor相同的处理逻辑
|
||||||
|
processAudioData({
|
||||||
|
inputBuffer: {
|
||||||
|
getChannelData: () => event.data.buffer
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 启动录音
|
||||||
|
workletNode.port.postMessage({ command: 'start' });
|
||||||
|
|
||||||
|
// 保存停止函数
|
||||||
|
workletNode.stopRecording = () => {
|
||||||
|
workletNode.port.postMessage({ command: 'stop' });
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("AudioWorklet 音频处理器创建成功");
|
||||||
|
return workletNode;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("创建AudioWorklet失败,将使用ScriptProcessor:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果AudioWorklet不可用或失败,返回null以便回退到ScriptProcessor
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理音频数据
|
||||||
|
function processAudioData(e) {
|
||||||
|
// 获取输入缓冲区
|
||||||
|
const inputBuffer = e.inputBuffer;
|
||||||
|
|
||||||
|
// 获取第一个通道的Float32数据
|
||||||
|
const inputData = inputBuffer.getChannelData(0);
|
||||||
|
|
||||||
|
// 添加调试信息
|
||||||
|
const nonZeroCount = Array.from(inputData).filter(x => Math.abs(x) > 0.001).length;
|
||||||
|
console.log(`接收到音频数据: ${inputData.length} 个样本, 非零样本数: ${nonZeroCount}`);
|
||||||
|
|
||||||
|
// 如果全是0,可能是麦克风没有正确获取声音
|
||||||
|
if (nonZeroCount < 5) {
|
||||||
|
console.warn("警告: 检测到大量静音样本,请检查麦克风是否正常工作");
|
||||||
|
// 继续处理,以防有些样本确实是静音
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存储PCM数据用于调试
|
||||||
|
recordedPcmData.push(new Float32Array(inputData));
|
||||||
|
|
||||||
|
// 转换为Int16数据供Opus编码
|
||||||
|
const int16Data = convertFloat32ToInt16(inputData);
|
||||||
|
|
||||||
|
// 如果收集到的数据不是FRAME_SIZE的整数倍,需要进行处理
|
||||||
|
// 创建静态缓冲区来存储不足一帧的数据
|
||||||
|
if (!window.audioDataBuffer) {
|
||||||
|
window.audioDataBuffer = new Int16Array(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合并之前缓存的数据和新数据
|
||||||
|
const combinedData = new Int16Array(window.audioDataBuffer.length + int16Data.length);
|
||||||
|
combinedData.set(window.audioDataBuffer);
|
||||||
|
combinedData.set(int16Data, window.audioDataBuffer.length);
|
||||||
|
|
||||||
|
// 处理完整帧
|
||||||
|
const frameCount = Math.floor(combinedData.length / FRAME_SIZE);
|
||||||
|
console.log(`可编码的完整帧数: ${frameCount}, 缓冲区总大小: ${combinedData.length}`);
|
||||||
|
|
||||||
|
for (let i = 0; i < frameCount; i++) {
|
||||||
|
const frameData = combinedData.subarray(i * FRAME_SIZE, (i + 1) * FRAME_SIZE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log(`编码第 ${i+1}/${frameCount} 帧, 帧大小: ${frameData.length}`);
|
||||||
|
const encodedData = opusEncoder.encode(frameData);
|
||||||
|
if (encodedData) {
|
||||||
|
console.log(`编码成功: ${encodedData.length} 字节`);
|
||||||
|
recordedOpusData.push(encodedData);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Opus编码帧 ${i+1} 失败:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存剩余不足一帧的数据
|
||||||
|
const remainingSamples = combinedData.length % FRAME_SIZE;
|
||||||
|
if (remainingSamples > 0) {
|
||||||
|
window.audioDataBuffer = combinedData.subarray(frameCount * FRAME_SIZE);
|
||||||
|
console.log(`保留 ${remainingSamples} 个样本到下一次处理`);
|
||||||
|
} else {
|
||||||
|
window.audioDataBuffer = new Int16Array(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopRecording() {
|
||||||
|
if (!isRecording) return;
|
||||||
|
|
||||||
|
// 处理剩余的缓冲数据
|
||||||
|
if (window.audioDataBuffer && window.audioDataBuffer.length > 0) {
|
||||||
|
console.log(`停止录音,处理剩余的 ${window.audioDataBuffer.length} 个样本`);
|
||||||
|
// 如果剩余数据不足一帧,可以通过补零的方式凑成一帧
|
||||||
|
if (window.audioDataBuffer.length < FRAME_SIZE) {
|
||||||
|
const paddedFrame = new Int16Array(FRAME_SIZE);
|
||||||
|
paddedFrame.set(window.audioDataBuffer);
|
||||||
|
// 剩余部分填充为0
|
||||||
|
for (let i = window.audioDataBuffer.length; i < FRAME_SIZE; i++) {
|
||||||
|
paddedFrame[i] = 0;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
console.log(`编码最后一帧(补零): ${paddedFrame.length} 样本`);
|
||||||
|
const encodedData = opusEncoder.encode(paddedFrame);
|
||||||
|
if (encodedData) {
|
||||||
|
recordedOpusData.push(encodedData);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("最后一帧Opus编码失败:", error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果数据超过一帧,按正常流程处理
|
||||||
|
processAudioData({
|
||||||
|
inputBuffer: {
|
||||||
|
getChannelData: () => convertInt16ToFloat32(window.audioDataBuffer)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
window.audioDataBuffer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果使用的是AudioWorklet,调用其特定的停止方法
|
||||||
|
if (audioProcessor && typeof audioProcessor.stopRecording === 'function') {
|
||||||
|
audioProcessor.stopRecording();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止麦克风
|
||||||
|
if (mediaStream) {
|
||||||
|
mediaStream.getTracks().forEach(track => track.stop());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 断开音频处理链
|
||||||
|
if (audioProcessor) {
|
||||||
|
try {
|
||||||
|
audioProcessor.disconnect();
|
||||||
|
if (mediaSource) mediaSource.disconnect();
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("断开音频处理链时出错:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新UI
|
||||||
|
isRecording = false;
|
||||||
|
statusLabel.textContent = "已停止录音,收集了 " + recordedOpusData.length + " 帧Opus数据";
|
||||||
|
startButton.disabled = false;
|
||||||
|
stopButton.disabled = true;
|
||||||
|
playButton.disabled = recordedOpusData.length === 0;
|
||||||
|
|
||||||
|
console.log("录制完成:",
|
||||||
|
"PCM帧数:", recordedPcmData.length,
|
||||||
|
"Opus帧数:", recordedOpusData.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
function playRecording() {
|
||||||
|
if (!recordedOpusData.length) {
|
||||||
|
statusLabel.textContent = "没有可播放的录音";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将所有Opus数据解码为PCM
|
||||||
|
let allDecodedData = [];
|
||||||
|
|
||||||
|
for (const opusData of recordedOpusData) {
|
||||||
|
try {
|
||||||
|
// 解码为Int16数据
|
||||||
|
const decodedData = opusDecoder.decode(opusData);
|
||||||
|
|
||||||
|
if (decodedData && decodedData.length > 0) {
|
||||||
|
// 将Int16数据转换为Float32
|
||||||
|
const float32Data = convertInt16ToFloat32(decodedData);
|
||||||
|
|
||||||
|
// 添加到总解码数据中
|
||||||
|
allDecodedData.push(...float32Data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Opus解码失败:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有解码出数据,返回
|
||||||
|
if (allDecodedData.length === 0) {
|
||||||
|
statusLabel.textContent = "解码失败,无法播放";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建音频缓冲区
|
||||||
|
const audioBuffer = audioContext.createBuffer(CHANNELS, allDecodedData.length, SAMPLE_RATE);
|
||||||
|
audioBuffer.copyToChannel(new Float32Array(allDecodedData), 0);
|
||||||
|
|
||||||
|
// 创建音频源并播放
|
||||||
|
const source = audioContext.createBufferSource();
|
||||||
|
source.buffer = audioBuffer;
|
||||||
|
source.connect(audioContext.destination);
|
||||||
|
source.start();
|
||||||
|
|
||||||
|
// 更新UI
|
||||||
|
statusLabel.textContent = "正在播放...";
|
||||||
|
playButton.disabled = true;
|
||||||
|
|
||||||
|
// 播放结束后恢复UI
|
||||||
|
source.onended = () => {
|
||||||
|
statusLabel.textContent = "播放完毕";
|
||||||
|
playButton.disabled = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模拟服务端返回的Opus数据进行解码播放
|
||||||
|
function playOpusFromServer(opusData) {
|
||||||
|
// 这个函数展示如何处理服务端返回的opus数据
|
||||||
|
// opusData应该是一个包含opus帧的数组
|
||||||
|
|
||||||
|
if (!opusDecoder) {
|
||||||
|
initOpus().then(success => {
|
||||||
|
if (success) {
|
||||||
|
decodeAndPlayOpusData(opusData);
|
||||||
|
} else {
|
||||||
|
statusLabel.textContent = "Opus解码器初始化失败";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
decodeAndPlayOpusData(opusData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeAndPlayOpusData(opusData) {
|
||||||
|
let allDecodedData = [];
|
||||||
|
|
||||||
|
for (const frame of opusData) {
|
||||||
|
try {
|
||||||
|
const decodedData = opusDecoder.decode(frame);
|
||||||
|
if (decodedData && decodedData.length > 0) {
|
||||||
|
const float32Data = convertInt16ToFloat32(decodedData);
|
||||||
|
allDecodedData.push(...float32Data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("服务端Opus数据解码失败:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allDecodedData.length === 0) {
|
||||||
|
statusLabel.textContent = "服务端数据解码失败";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const audioBuffer = audioContext.createBuffer(CHANNELS, allDecodedData.length, SAMPLE_RATE);
|
||||||
|
audioBuffer.copyToChannel(new Float32Array(allDecodedData), 0);
|
||||||
|
|
||||||
|
const source = audioContext.createBufferSource();
|
||||||
|
source.buffer = audioBuffer;
|
||||||
|
source.connect(audioContext.destination);
|
||||||
|
source.start();
|
||||||
|
|
||||||
|
statusLabel.textContent = "正在播放服务端数据...";
|
||||||
|
source.onended = () => statusLabel.textContent = "服务端数据播放完毕";
|
||||||
|
}
|
||||||
464
main/xiaozhi-server/test/opus_test/test.html
Normal file
464
main/xiaozhi-server/test/opus_test/test.html
Normal file
|
|
@ -0,0 +1,464 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Opus 编解码测试</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
padding: 8px 15px;
|
||||||
|
margin-right: 10px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
background-color: #4285f4;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: #3367d6;
|
||||||
|
}
|
||||||
|
button:disabled {
|
||||||
|
background-color: #cccccc;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
#status {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
#scriptStatus {
|
||||||
|
position: fixed;
|
||||||
|
top: 10px;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 5px;
|
||||||
|
display: block;
|
||||||
|
z-index: 100;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
#scriptStatus.info {
|
||||||
|
background-color: #e8f0fe;
|
||||||
|
color: #4285f4;
|
||||||
|
border-left: 4px solid #4285f4;
|
||||||
|
}
|
||||||
|
#scriptStatus.success {
|
||||||
|
background-color: #e6f4ea;
|
||||||
|
color: #0f9d58;
|
||||||
|
border-left: 4px solid #0f9d58;
|
||||||
|
}
|
||||||
|
#scriptStatus.error {
|
||||||
|
background-color: #fce8e6;
|
||||||
|
color: #db4437;
|
||||||
|
border-left: 4px solid #db4437;
|
||||||
|
}
|
||||||
|
#debugInfo {
|
||||||
|
margin-top: 20px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 10px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 12px;
|
||||||
|
max-height: 200px;
|
||||||
|
overflow-y: auto;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#showDebug {
|
||||||
|
margin-top: 10px;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
color: #444;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>Opus 编解码录音播放测试</h2>
|
||||||
|
|
||||||
|
<div id="scriptStatus" class="info">正在加载Opus库...</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<button id="initAudio" style="background-color: #34a853;">初始化音频</button>
|
||||||
|
<button id="testMic" style="background-color: #fbbc05;">测试麦克风</button>
|
||||||
|
<p></p>
|
||||||
|
<button id="start" disabled>开始录音</button>
|
||||||
|
<button id="stop" disabled>停止录音</button>
|
||||||
|
<button id="play" disabled>播放录音</button>
|
||||||
|
<p>录音状态: <span id="status">待机,正在初始化...</span></p>
|
||||||
|
<button id="showDebug">显示/隐藏调试信息</button>
|
||||||
|
<div id="debugInfo"></div>
|
||||||
|
<div id="audioMeter" style="margin-top: 10px; height: 20px; background-color: #eee; border-radius: 4px; overflow: hidden; display: none;">
|
||||||
|
<div id="audioLevel" style="height: 100%; width: 0%; background-color: #4285f4; transition: width 0.1s;"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Opus解码库 - 这个库会设置一个全局Module变量 -->
|
||||||
|
<script>
|
||||||
|
// 定义全局变量以跟踪库加载状态
|
||||||
|
window.opusLoaded = false;
|
||||||
|
window.startButton = document.getElementById("start");
|
||||||
|
window.stopButton = document.getElementById("stop");
|
||||||
|
window.playButton = document.getElementById("play");
|
||||||
|
window.statusLabel = document.getElementById("status");
|
||||||
|
window.debugInfo = document.getElementById("debugInfo");
|
||||||
|
window.audioContextReady = false;
|
||||||
|
window.testMicActive = false;
|
||||||
|
|
||||||
|
// 显示/隐藏调试信息
|
||||||
|
document.getElementById("showDebug").addEventListener("click", function() {
|
||||||
|
if (debugInfo.style.display === "none" || !debugInfo.style.display) {
|
||||||
|
debugInfo.style.display = "block";
|
||||||
|
this.textContent = "隐藏调试信息";
|
||||||
|
} else {
|
||||||
|
debugInfo.style.display = "none";
|
||||||
|
this.textContent = "显示调试信息";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加初始化音频按钮事件
|
||||||
|
document.getElementById("initAudio").addEventListener("click", function() {
|
||||||
|
initializeAudioSystem();
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加测试麦克风按钮事件
|
||||||
|
document.getElementById("testMic").addEventListener("click", function() {
|
||||||
|
if (window.testMicActive) {
|
||||||
|
stopMicTest();
|
||||||
|
} else {
|
||||||
|
startMicTest();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 初始化音频系统
|
||||||
|
function initializeAudioSystem() {
|
||||||
|
try {
|
||||||
|
log("初始化音频系统...");
|
||||||
|
// 创建临时AudioContext来触发用户授权
|
||||||
|
const tempContext = new (window.AudioContext || window.webkitAudioContext)({
|
||||||
|
sampleRate: 16000,
|
||||||
|
latencyHint: 'interactive'
|
||||||
|
});
|
||||||
|
|
||||||
|
// 创建振荡器并播放短促的声音
|
||||||
|
const oscillator = tempContext.createOscillator();
|
||||||
|
const gain = tempContext.createGain();
|
||||||
|
gain.gain.value = 0.1; // 很小的音量
|
||||||
|
oscillator.connect(gain);
|
||||||
|
gain.connect(tempContext.destination);
|
||||||
|
oscillator.frequency.value = 440; // A4
|
||||||
|
oscillator.start();
|
||||||
|
|
||||||
|
// 0.2秒后停止
|
||||||
|
setTimeout(() => {
|
||||||
|
oscillator.stop();
|
||||||
|
// 关闭上下文
|
||||||
|
tempContext.close().then(() => {
|
||||||
|
log("音频系统初始化成功", "success");
|
||||||
|
updateScriptStatus("音频系统已激活", "success");
|
||||||
|
document.getElementById("initAudio").disabled = true;
|
||||||
|
document.getElementById("initAudio").textContent = "音频已初始化";
|
||||||
|
window.audioContextReady = true;
|
||||||
|
|
||||||
|
// 如果Opus已加载,启用开始录音按钮
|
||||||
|
if (window.opusLoaded) {
|
||||||
|
startButton.disabled = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, 200);
|
||||||
|
} catch (err) {
|
||||||
|
log("初始化音频系统失败: " + err.message, "error");
|
||||||
|
updateScriptStatus("初始化音频失败: " + err.message, "error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试麦克风
|
||||||
|
function startMicTest() {
|
||||||
|
const audioMeter = document.getElementById("audioMeter");
|
||||||
|
const audioLevel = document.getElementById("audioLevel");
|
||||||
|
const testMicBtn = document.getElementById("testMic");
|
||||||
|
|
||||||
|
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||||
|
log("浏览器不支持麦克风访问", "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log("开始麦克风测试...");
|
||||||
|
audioMeter.style.display = "block";
|
||||||
|
testMicBtn.textContent = "停止测试";
|
||||||
|
testMicBtn.style.backgroundColor = "#ea4335";
|
||||||
|
window.testMicActive = true;
|
||||||
|
|
||||||
|
// 创建音频上下文
|
||||||
|
const testContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
|
window.testContext = testContext;
|
||||||
|
|
||||||
|
// 获取麦克风权限
|
||||||
|
navigator.mediaDevices.getUserMedia({
|
||||||
|
audio: {
|
||||||
|
echoCancellation: true,
|
||||||
|
noiseSuppression: true,
|
||||||
|
autoGainControl: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(stream => {
|
||||||
|
log("已获取麦克风访问权限", "success");
|
||||||
|
|
||||||
|
// 保存流以便稍后关闭
|
||||||
|
window.testStream = stream;
|
||||||
|
|
||||||
|
// 创建音频分析器
|
||||||
|
const source = testContext.createMediaStreamSource(stream);
|
||||||
|
const analyser = testContext.createAnalyser();
|
||||||
|
analyser.fftSize = 256;
|
||||||
|
source.connect(analyser);
|
||||||
|
|
||||||
|
// 创建音量显示更新函数
|
||||||
|
const bufferLength = analyser.frequencyBinCount;
|
||||||
|
const dataArray = new Uint8Array(bufferLength);
|
||||||
|
|
||||||
|
function updateMeter() {
|
||||||
|
if (!window.testMicActive) return;
|
||||||
|
|
||||||
|
analyser.getByteFrequencyData(dataArray);
|
||||||
|
|
||||||
|
// 计算音量级别 (0-100)
|
||||||
|
let sum = 0;
|
||||||
|
for (let i = 0; i < bufferLength; i++) {
|
||||||
|
sum += dataArray[i];
|
||||||
|
}
|
||||||
|
const average = sum / bufferLength;
|
||||||
|
const level = Math.min(100, Math.max(0, average * 2));
|
||||||
|
|
||||||
|
// 更新音量计
|
||||||
|
audioLevel.style.width = level + "%";
|
||||||
|
|
||||||
|
// 如果有声音,记录日志
|
||||||
|
if (level > 10) {
|
||||||
|
log(`检测到声音: ${level.toFixed(1)}%`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 循环更新
|
||||||
|
window.testMicAnimationFrame = requestAnimationFrame(updateMeter);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始更新
|
||||||
|
updateMeter();
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
log("麦克风测试失败: " + err.message, "error");
|
||||||
|
window.testMicActive = false;
|
||||||
|
testMicBtn.textContent = "测试麦克风";
|
||||||
|
testMicBtn.style.backgroundColor = "#fbbc05";
|
||||||
|
audioMeter.style.display = "none";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止麦克风测试
|
||||||
|
function stopMicTest() {
|
||||||
|
const audioMeter = document.getElementById("audioMeter");
|
||||||
|
const testMicBtn = document.getElementById("testMic");
|
||||||
|
|
||||||
|
log("停止麦克风测试");
|
||||||
|
window.testMicActive = false;
|
||||||
|
testMicBtn.textContent = "测试麦克风";
|
||||||
|
testMicBtn.style.backgroundColor = "#fbbc05";
|
||||||
|
|
||||||
|
// 停止分析器动画
|
||||||
|
if (window.testMicAnimationFrame) {
|
||||||
|
cancelAnimationFrame(window.testMicAnimationFrame);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止麦克风流
|
||||||
|
if (window.testStream) {
|
||||||
|
window.testStream.getTracks().forEach(track => track.stop());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭测试上下文
|
||||||
|
if (window.testContext) {
|
||||||
|
window.testContext.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 隐藏音量计
|
||||||
|
audioMeter.style.display = "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化AudioContext
|
||||||
|
function initAudioContext() {
|
||||||
|
try {
|
||||||
|
window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
||||||
|
if (!window.AudioContext) {
|
||||||
|
throw new Error("浏览器不支持AudioContext");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 我们在app.js中会创建AudioContext,这里只检查兼容性
|
||||||
|
log("AudioContext API 可用");
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
log("AudioContext初始化失败: " + err.message, "error");
|
||||||
|
updateScriptStatus("AudioContext初始化失败,录音功能不可用", "error");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查麦克风权限
|
||||||
|
function checkMicrophonePermission() {
|
||||||
|
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||||
|
log("浏览器不支持getUserMedia API", "error");
|
||||||
|
updateScriptStatus("浏览器不支持录音功能", "error");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
log("getUserMedia API 可用");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加调试日志
|
||||||
|
function log(message, type = "info") {
|
||||||
|
console.log(message);
|
||||||
|
const time = new Date().toLocaleTimeString();
|
||||||
|
const entry = document.createElement("div");
|
||||||
|
entry.textContent = `[${time}] ${message}`;
|
||||||
|
|
||||||
|
if (type === "error") {
|
||||||
|
entry.style.color = "#db4437";
|
||||||
|
} else if (type === "success") {
|
||||||
|
entry.style.color = "#0f9d58";
|
||||||
|
}
|
||||||
|
|
||||||
|
debugInfo.appendChild(entry);
|
||||||
|
debugInfo.scrollTop = debugInfo.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新脚本状态显示
|
||||||
|
function updateScriptStatus(message, type) {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) {
|
||||||
|
statusElement.textContent = message;
|
||||||
|
statusElement.className = type;
|
||||||
|
statusElement.style.display = 'block';
|
||||||
|
}
|
||||||
|
log(message, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查Opus库是否已加载
|
||||||
|
function checkOpusLoaded() {
|
||||||
|
try {
|
||||||
|
// 检查Module是否存在(本地库导出的全局变量)
|
||||||
|
if (typeof Module === 'undefined') {
|
||||||
|
log("Module对象不存在", "error");
|
||||||
|
throw new Error('Opus库未加载,Module对象不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录Module对象结构以便调试
|
||||||
|
log("Module对象结构: " + Object.keys(Module).join(", "));
|
||||||
|
|
||||||
|
// 尝试使用全局Module函数
|
||||||
|
if (typeof Module._opus_decoder_get_size === 'function') {
|
||||||
|
window.ModuleInstance = Module;
|
||||||
|
log('Opus库加载成功(使用全局Module)', "success");
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 启用开始录音按钮
|
||||||
|
startButton.disabled = false;
|
||||||
|
statusLabel.textContent = "待机";
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
setTimeout(() => {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
}, 3000);
|
||||||
|
|
||||||
|
window.opusLoaded = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试使用Module.instance
|
||||||
|
if (typeof Module.instance !== 'undefined' && typeof Module.instance._opus_decoder_get_size === 'function') {
|
||||||
|
window.ModuleInstance = Module.instance;
|
||||||
|
log('Opus库加载成功(使用Module.instance)', "success");
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 启用开始录音按钮
|
||||||
|
startButton.disabled = false;
|
||||||
|
statusLabel.textContent = "待机";
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
setTimeout(() => {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
}, 3000);
|
||||||
|
|
||||||
|
window.opusLoaded = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否有其他导出方式
|
||||||
|
log("Module上可用方法: " + Object.getOwnPropertyNames(Module).filter(prop => typeof Module[prop] === 'function').join(", "));
|
||||||
|
|
||||||
|
if (typeof Module.onRuntimeInitialized === 'function' || typeof Module.onRuntimeInitialized === 'undefined') {
|
||||||
|
log("Module.onRuntimeInitialized 尚未执行,等待模块初始化完成...");
|
||||||
|
// Module可能未完成初始化,注册回调
|
||||||
|
Module.onRuntimeInitialized = function() {
|
||||||
|
log("Opus库运行时初始化完成,重新检查");
|
||||||
|
checkOpusLoaded();
|
||||||
|
};
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Opus解码函数未找到,可能Module结构不正确');
|
||||||
|
} catch (err) {
|
||||||
|
log(`Opus库加载失败: ${err.message}`, "error");
|
||||||
|
updateScriptStatus(`Opus库加载失败: ${err.message}`, 'error');
|
||||||
|
statusLabel.textContent = "错误:Opus库加载失败";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 页面加载完成后检查浏览器能力和Opus库
|
||||||
|
window.addEventListener('load', function() {
|
||||||
|
log("页面加载完成,开始初始化...");
|
||||||
|
updateScriptStatus('正在初始化录音环境...', 'info');
|
||||||
|
|
||||||
|
// 检查浏览器能力
|
||||||
|
const audioContextSupported = initAudioContext();
|
||||||
|
const microphoneSupported = checkMicrophonePermission();
|
||||||
|
|
||||||
|
if (!audioContextSupported || !microphoneSupported) {
|
||||||
|
log("浏览器不支持必要API,无法进行录音", "error");
|
||||||
|
updateScriptStatus('浏览器不支持录音功能', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log("检查环境完成,开始加载Opus库");
|
||||||
|
updateScriptStatus('正在加载Opus库...', 'info');
|
||||||
|
|
||||||
|
// 延迟一小段时间再检查,确保库有时间加载
|
||||||
|
setTimeout(function checkAndRetry() {
|
||||||
|
if (!checkOpusLoaded()) {
|
||||||
|
// 如果加载失败,5秒后重试
|
||||||
|
log("Opus库加载失败,5秒后重试...");
|
||||||
|
updateScriptStatus('Opus库加载失败,正在重试...', 'error');
|
||||||
|
setTimeout(checkAndRetry, 5000);
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 防止按钮误操作
|
||||||
|
document.getElementById("stop").disabled = true;
|
||||||
|
document.getElementById("play").disabled = true;
|
||||||
|
</script>
|
||||||
|
<script src="./../libopus.js"></script>
|
||||||
|
<script src="app.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
425
main/xiaozhi-server/test/test_page.css
Normal file
425
main/xiaozhi-server/test/test_page.css
Normal file
|
|
@ -0,0 +1,425 @@
|
||||||
|
body {
|
||||||
|
font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
|
||||||
|
margin: 0;
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
max-width: 1000px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background-color: white;
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||||||
|
padding: 10px 20px 10px 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
color: #333;
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 30px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section {
|
||||||
|
margin-bottom: 5px;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 10px;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section h2 {
|
||||||
|
margin-top: 0;
|
||||||
|
color: #444;
|
||||||
|
font-size: 18px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
padding: 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section h2 .toggle-button {
|
||||||
|
margin-left: auto;
|
||||||
|
padding: 4px 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
height: 28px;
|
||||||
|
line-height: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.device-info {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 20px;
|
||||||
|
margin-left: 20px;
|
||||||
|
padding: 0 15px;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
border-radius: 4px;
|
||||||
|
height: 28px;
|
||||||
|
line-height: 28px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.device-info span {
|
||||||
|
color: #666;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.device-info strong {
|
||||||
|
color: #333;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-panel {
|
||||||
|
display: none;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
margin-top: 5px;
|
||||||
|
padding: 5px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-panel.expanded {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.control-panel {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 10px;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
padding: 8px 15px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
background-color: #4285f4;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
background-color: #3367d6;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:disabled {
|
||||||
|
background-color: #cccccc;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
#serverUrl,
|
||||||
|
#otaUrl {
|
||||||
|
flex-grow: 1;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-input {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#messageInput {
|
||||||
|
flex-grow: 1;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#nfcCardId {
|
||||||
|
flex-grow: 1;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.conversation {
|
||||||
|
max-height: 300px;
|
||||||
|
overflow-y: auto;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 10px;
|
||||||
|
background-color: white;
|
||||||
|
flex: 1;
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 8px;
|
||||||
|
max-width: 80%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user {
|
||||||
|
background-color: #e2f2ff;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: 10px;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.server {
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
margin-right: auto;
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status {
|
||||||
|
color: #666;
|
||||||
|
font-style: italic;
|
||||||
|
font-size: 14px;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.audio-controls {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 10px;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.audio-visualizer {
|
||||||
|
height: 60px;
|
||||||
|
width: 100%;
|
||||||
|
margin-top: 10px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
background-color: #fafafa;
|
||||||
|
}
|
||||||
|
|
||||||
|
.record-button {
|
||||||
|
background-color: #db4437;
|
||||||
|
}
|
||||||
|
|
||||||
|
.record-button:hover {
|
||||||
|
background-color: #c53929;
|
||||||
|
}
|
||||||
|
|
||||||
|
.record-button.recording {
|
||||||
|
animation: pulse 1.5s infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes pulse {
|
||||||
|
0% {
|
||||||
|
background-color: #db4437;
|
||||||
|
}
|
||||||
|
|
||||||
|
50% {
|
||||||
|
background-color: #ff6659;
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
background-color: #db4437;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#logContainer {
|
||||||
|
margin-top: 0;
|
||||||
|
padding: 10px;
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
border-radius: 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
height: 300px;
|
||||||
|
overflow-y: auto;
|
||||||
|
flex: 1;
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-entry {
|
||||||
|
margin: 5px 0;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-info {
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-error {
|
||||||
|
color: #db4437;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-success {
|
||||||
|
color: #0f9d58;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes fadeIn {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translate(-50%, -60%);
|
||||||
|
}
|
||||||
|
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translate(-50%, -50%);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.script-status {
|
||||||
|
display: inline-block;
|
||||||
|
width: 10px;
|
||||||
|
height: 10px;
|
||||||
|
border-radius: 50%;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.script-loaded {
|
||||||
|
background-color: #0f9d58;
|
||||||
|
}
|
||||||
|
|
||||||
|
.script-loading {
|
||||||
|
background-color: #f4b400;
|
||||||
|
}
|
||||||
|
|
||||||
|
.script-error {
|
||||||
|
background-color: #db4437;
|
||||||
|
}
|
||||||
|
|
||||||
|
.script-list {
|
||||||
|
margin: 10px 0;
|
||||||
|
padding: 10px;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
border-radius: 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 11px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#scriptStatus.success {
|
||||||
|
background-color: #e6f4ea;
|
||||||
|
color: #0f9d58;
|
||||||
|
border-left: 4px solid #0f9d58;
|
||||||
|
}
|
||||||
|
|
||||||
|
#scriptStatus.error {
|
||||||
|
background-color: #fce8e6;
|
||||||
|
color: #db4437;
|
||||||
|
border-left: 4px solid #db4437;
|
||||||
|
}
|
||||||
|
|
||||||
|
#scriptStatus.warning {
|
||||||
|
background-color: #fef7e0;
|
||||||
|
color: #f4b400;
|
||||||
|
border-left: 4px solid #f4b400;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 标签页样式 */
|
||||||
|
.tabs {
|
||||||
|
display: flex;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
border-bottom: 2px solid #e0e0e0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab {
|
||||||
|
padding: 10px 20px;
|
||||||
|
cursor: pointer;
|
||||||
|
border: none;
|
||||||
|
background: none;
|
||||||
|
font-size: 16px;
|
||||||
|
color: #666;
|
||||||
|
position: relative;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab:hover {
|
||||||
|
color: #4285f4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab.active {
|
||||||
|
color: #4285f4;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab.active::after {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
bottom: -2px;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 2px;
|
||||||
|
background-color: #4285f4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-content {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-content.active {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.flex-container {
|
||||||
|
display: flex;
|
||||||
|
gap: 20px;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-item label {
|
||||||
|
width: 100px;
|
||||||
|
text-align: right;
|
||||||
|
margin-right: 10px;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-item input {
|
||||||
|
flex-grow: 1;
|
||||||
|
padding: 6px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.control-panel {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 10px;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connection-controls {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
align-items: center;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connection-controls input {
|
||||||
|
flex: 1;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
min-width: 200px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connection-controls button {
|
||||||
|
white-space: nowrap;
|
||||||
|
padding: 8px 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connection-status {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 20px;
|
||||||
|
margin-left: 20px;
|
||||||
|
padding: 0 15px;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
border-radius: 4px;
|
||||||
|
height: 28px;
|
||||||
|
line-height: 28px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connection-status span {
|
||||||
|
color: #666;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connection-status .status {
|
||||||
|
color: #333;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
2235
main/xiaozhi-server/test/test_page.html
Normal file
2235
main/xiaozhi-server/test/test_page.html
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue