please write a python script, to generate a stereo...
Created on: December 7, 2025
Answered using GPT-5.1 Thinking by Chat01
Created on: December 7, 2025
Answered using GPT-5.1 Thinking by Chat01
please write a python script, to generate a stereo 24bit 44100hz wav format audio file, with sine wave sweep from 20hz to 20000hz then back to 20hz in 10 seconds, repeat it for 50 times
Here’s a complete Python script that does exactly what you asked:
It only uses the standard library (wave, math, struct).
pythonimport wave import struct import math def generate_sweep( filename="sweep_20_20000_10s_x50_24bit_stereo.wav", duration=10.0, # seconds for one up+down sweep num_repeats=50, # how many times to repeat the sweep samplerate=44100, # Hz f_start=20.0, # Hz f_end=20000.0 # Hz ): n_channels = 2 sampwidth = 3 # 24-bit = 3 bytes samples_per_sweep = int(duration * samplerate) total_samples = samples_per_sweep * num_repeats # 24-bit signed range is -2^23 .. 2^23-1 max_int24 = (1 << 23) - 1 amplitude = int(max_int24 * 0.9) # a little headroom with wave.open(filename, "wb") as wf: wf.setnchannels(n_channels) wf.setsampwidth(sampwidth) wf.setframerate(samplerate) phase = 0.0 two_pi = 2.0 * math.pi half_duration = duration / 2.0 for n in range(total_samples): # Position (in seconds) within the 10-second sweep pos_in_sweep = (n % samples_per_sweep) / float(samplerate) # Linear up then down sweep in frequency if pos_in_sweep < half_duration: # 20 Hz -> 20,000 Hz frac = pos_in_sweep / half_duration freq = f_start + (f_end - f_start) * frac else: # 20,000 Hz -> 20 Hz frac = (pos_in_sweep - half_duration) / half_duration freq = f_end + (f_start - f_end) * frac # Increment phase based on instantaneous frequency phase += two_pi * freq / samplerate # Keep phase bounded to avoid huge floats if phase > 1e6: phase = math.fmod(phase, two_pi) # Generate sine sample sample = math.sin(phase) int_sample = int(max(-max_int24, min(max_int24, sample * amplitude))) # Pack as 32-bit little-endian, then drop to 24-bit (3 bytes) packed_32 = struct.pack("<i", int_sample) sample_24 = packed_32[0:3] # little-endian: least significant 3 bytes # Stereo: same sample in left and right channels frame_bytes = sample_24 + sample_24 # Stream directly to file as raw frames wf.writeframesraw(frame_bytes) # Finalize file header wf.writeframes(b"") if __name__ == "__main__": generate_sweep()
Run it with:
bashpython generate_sweep.py
It will create sweep_20_20000_10s_x50_24bit_stereo.wav in the current directory.
please write a python script, to generate a stereo 24bit 44100hz wav format audio file, with sine wave sweep from 20hz to 20000hz then back to 20hz in 10 seconds in logarithm scale, repeat it for 50 times
Here’s a Python script that generates:
pythonimport wave import struct import math def generate_log_sweep( filename="sweep_log_20_20000_10s_x50_24bit_stereo.wav", duration=10.0, # seconds for one up+down sweep num_repeats=50, # how many times to repeat the sweep samplerate=44100, # Hz f_start=20.0, # Hz f_end=20000.0 # Hz ): n_channels = 2 sampwidth = 3 # 24-bit = 3 bytes samples_per_sweep = int(duration * samplerate) total_samples = samples_per_sweep * num_repeats # 24-bit signed range is -2^23 .. 2^23-1 max_int24 = (1 << 23) - 1 amplitude = int(max_int24 * 0.9) # a little headroom half_duration = duration / 2.0 ratio = f_end / f_start # for logarithmic (geometric) sweep with wave.open(filename, "wb") as wf: wf.setnchannels(n_channels) wf.setsampwidth(sampwidth) wf.setframerate(samplerate) phase = 0.0 two_pi = 2.0 * math.pi for n in range(total_samples): # Position (in seconds) within the 10-second sweep pos_in_sweep = (n % samples_per_sweep) / float(samplerate) # Logarithmic up then down sweep in frequency if pos_in_sweep < half_duration: # Log sweep: 20 Hz -> 20,000 Hz over first half frac = pos_in_sweep / half_duration # 0..1 freq = f_start * (ratio ** frac) else: # Log sweep: 20,000 Hz -> 20 Hz over second half frac = (pos_in_sweep - half_duration) / half_duration # 0..1 freq = f_end * ((1.0 / ratio) ** frac) # Increment phase based on instantaneous frequency phase += two_pi * freq / samplerate # Keep phase bounded to avoid huge floats if phase > 1e6: phase = math.fmod(phase, two_pi) # Generate sine sample sample = math.sin(phase) int_sample = int(max(-max_int24, min(max_int24, sample * amplitude))) # Pack as 32-bit little-endian, then drop to 24-bit (3 bytes) packed_32 = struct.pack("<i", int_sample) sample_24 = packed_32[0:3] # little-endian: least significant 3 bytes # Stereo: same sample for left and right channels frame_bytes = sample_24 + sample_24 wf.writeframesraw(frame_bytes) # Finalize file header wf.writeframes(b"") if __name__ == "__main__": generate_log_sweep()
Save as e.g. generate_log_sweep.py and run:
bashpython generate_log_sweep.py
You’ll get sweep_log_20_20000_10s_x50_24bit_stereo.wav in the current directory.