c5234d5799
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Generate test WAV files with a 1kHz sine wave tone at different sample rates.
|
|
"""
|
|
|
|
import wave
|
|
import struct
|
|
import math
|
|
import os
|
|
|
|
def generate_tone_wav(filename, sample_rate, duration_sec=2.0, frequency=1000.0, amplitude=0.5):
|
|
"""
|
|
Generate a WAV file with a single sine wave tone.
|
|
|
|
Args:
|
|
filename: Output WAV file path
|
|
sample_rate: Sample rate in Hz (e.g., 16000, 22050, 44100)
|
|
duration_sec: Duration in seconds
|
|
frequency: Tone frequency in Hz
|
|
amplitude: Amplitude 0.0-1.0
|
|
"""
|
|
num_samples = int(sample_rate * duration_sec)
|
|
|
|
# Generate samples
|
|
samples = []
|
|
for i in range(num_samples):
|
|
t = i / sample_rate
|
|
sample = amplitude * math.sin(2 * math.pi * frequency * t)
|
|
# Convert to 16-bit PCM
|
|
sample_int = int(sample * 32767)
|
|
samples.append(sample_int)
|
|
|
|
# Write WAV file
|
|
with wave.open(filename, 'w') as wav_file:
|
|
wav_file.setnchannels(1) # Mono
|
|
wav_file.setsampwidth(2) # 16-bit (2 bytes)
|
|
wav_file.setframerate(sample_rate)
|
|
|
|
for sample in samples:
|
|
wav_file.writeframes(struct.pack('<h', sample)) # Little-endian 16-bit
|
|
|
|
print(f"Created: {filename}")
|
|
print(f" Sample Rate: {sample_rate} Hz")
|
|
print(f" Duration: {duration_sec} sec")
|
|
print(f" Frequency: {frequency} Hz")
|
|
print(f" Samples: {num_samples}")
|
|
print()
|
|
|
|
def main():
|
|
# Get the directory where this script is located
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
# Test frequencies and sample rates
|
|
tone_frequency = 1000.0 # 1 kHz tone
|
|
|
|
sample_rates = [
|
|
(16000, "16k"),
|
|
(22050, "22k"),
|
|
(44100, "44k"),
|
|
]
|
|
|
|
print(f"Generating {tone_frequency} Hz test tones at different sample rates...\n")
|
|
|
|
for sample_rate, suffix in sample_rates:
|
|
filename = os.path.join(script_dir, f"test_tone_{suffix}_{int(tone_frequency)}hz.wav")
|
|
generate_tone_wav(filename, sample_rate, duration_sec=2.0, frequency=tone_frequency)
|
|
|
|
print("Done! Copy these WAV files to your spectrogram viewer to test.")
|
|
print("\nExpected display:")
|
|
print(" - 16k file: Tone should appear at 1kHz (1/8 of max 8kHz)")
|
|
print(" - 22k file: Tone should appear at 1kHz (~1/11 of max 11kHz)")
|
|
print(" - 44k file: Tone should appear at 1kHz (~1/22 of max 22kHz)")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|