118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
音频转换测试脚本
|
||
用于测试高性能音频格式转换
|
||
"""
|
||
|
||
import time
|
||
import struct
|
||
from audio_converter import AudioConverter
|
||
|
||
def test_conversion_performance():
|
||
"""测试转换性能"""
|
||
print("=== 音频转换性能测试 ===")
|
||
|
||
converter = AudioConverter()
|
||
|
||
# 生成测试数据(1秒的24kHz Float32音频)
|
||
sample_rate = 24000
|
||
duration = 1.0 # 1秒
|
||
num_samples = int(sample_rate * duration)
|
||
|
||
# 生成正弦波测试数据
|
||
test_data = bytearray()
|
||
for i in range(num_samples):
|
||
# 生成440Hz正弦波
|
||
value = 0.5 * (i / sample_rate * 440 * 2 * 3.14159)
|
||
sample = (value).astype('float32') if hasattr(value, 'astype') else float(value)
|
||
test_data.extend(struct.pack('f', sample))
|
||
|
||
test_data = bytes(test_data)
|
||
print(f"生成了 {len(test_data)} 字节的测试数据")
|
||
|
||
# 测试转换性能
|
||
start_time = time.time()
|
||
converted_data = converter.float32_to_int16_fast(test_data)
|
||
end_time = time.time()
|
||
|
||
conversion_time = end_time - start_time
|
||
data_ratio = len(converted_data) / len(test_data)
|
||
|
||
print(f"转换结果:")
|
||
print(f" 原始数据: {len(test_data)} 字节")
|
||
print(f" 转换后: {len(converted_data)} 字节")
|
||
print(f" 数据比例: {data_ratio:.2f}")
|
||
print(f" 转换时间: {conversion_time:.4f} 秒")
|
||
print(f" 转换速度: {len(test_data) / conversion_time / 1024:.1f} KB/s")
|
||
|
||
# 验证转换质量
|
||
print("\n=== 转换质量验证 ===")
|
||
|
||
# 检查一些样本值
|
||
original_samples = struct.unpack('10f', test_data[:40])
|
||
converted_samples = struct.unpack('10h', converted_data[:20])
|
||
|
||
print("前10个样本的转换结果:")
|
||
for i, (orig, conv) in enumerate(zip(original_samples, converted_samples)):
|
||
expected = int(orig * 32767)
|
||
print(f" 样本{i}: {orig:.6f} -> {conv} (期望: {expected})")
|
||
|
||
# 检查是否有明显错误
|
||
errors = 0
|
||
for orig, conv in zip(original_samples, converted_samples):
|
||
expected = int(orig * 32767)
|
||
if abs(conv - expected) > 1: # 允许1的误差
|
||
errors += 1
|
||
|
||
if errors == 0:
|
||
print("✓ 转换质量验证通过")
|
||
else:
|
||
print(f"✗ 转换质量验证失败,{errors}个样本有误差")
|
||
|
||
def test_numpy_vs_python():
|
||
"""测试numpy和纯Python实现的性能差异"""
|
||
print("\n=== NumPy vs Python 性能对比 ===")
|
||
|
||
converter = AudioConverter()
|
||
|
||
# 生成较大的测试数据
|
||
sample_rate = 24000
|
||
duration = 2.0 # 2秒
|
||
num_samples = int(sample_rate * duration)
|
||
|
||
# 生成测试数据
|
||
import random
|
||
test_data = bytearray()
|
||
for _ in range(num_samples):
|
||
sample = random.uniform(-1.0, 1.0)
|
||
test_data.extend(struct.pack('f', sample))
|
||
|
||
test_data = bytes(test_data)
|
||
|
||
# 测试NumPy版本
|
||
if converter._numpy_available:
|
||
print("测试NumPy版本...")
|
||
start_time = time.time()
|
||
for _ in range(10): # 重复10次
|
||
converter.float32_to_int16_fast(test_data)
|
||
numpy_time = time.time() - start_time
|
||
print(f"NumPy版本: {numpy_time:.4f} 秒 (10次)")
|
||
else:
|
||
print("NumPy不可用")
|
||
numpy_time = None
|
||
|
||
# 测试纯Python版本
|
||
print("测试纯Python版本...")
|
||
start_time = time.time()
|
||
for _ in range(10): # 重复10次
|
||
AudioConverter.float32_to_int16(test_data)
|
||
python_time = time.time() - start_time
|
||
print(f"纯Python版本: {python_time:.4f} 秒 (10次)")
|
||
|
||
if numpy_time:
|
||
speedup = python_time / numpy_time
|
||
print(f"NumPy加速比: {speedup:.2f}x")
|
||
|
||
if __name__ == "__main__":
|
||
test_conversion_performance()
|
||
test_numpy_vs_python() |