Вот несколько методов генерации синусоидального сигнала с помощью Python и PyAudio:
Метод 1: использование NumPy и PyAudio
import numpy as np
import pyaudio
# Set parameters
frequency = 440.0 # Frequency of the sine wave in Hz
duration = 3.0 # Duration in seconds
volume = 0.5 # Volume (0.0 to 1.0)
# Generate samples
sample_rate = 44100 # Number of samples per second (standard for audio)
t = np.linspace(0, duration, int(sample_rate * duration), False)
samples = volume * np.sin(2 * np.pi * frequency * t)
# Play the audio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=sample_rate,
output=True)
stream.write(samples.tobytes())
stream.stop_stream()
stream.close()
p.terminate()
Метод 2: использование Wave и PyAudio
import math
import wave
import pyaudio
# Set parameters
frequency = 440.0 # Frequency of the sine wave in Hz
duration = 3.0 # Duration in seconds
volume = 0.5 # Volume (0.0 to 1.0)
# Generate samples
sample_rate = 44100 # Number of samples per second (standard for audio)
num_frames = int(sample_rate * duration)
num_channels = 1
sampwidth = 2
comptype = "NONE"
compname = "not compressed"
# Create a new wave file
wf = wave.open("sine_wave.wav", "w")
wf.setnchannels(num_channels)
wf.setsampwidth(sampwidth)
wf.setframerate(sample_rate)
for i in range(num_frames):
amplitude = volume * math.sin(2 * math.pi * frequency * i / sample_rate)
value = int(amplitude * 32767) # Convert to 16-bit PCM
data = value.to_bytes(2, byteorder="little", signed=True)
wf.writeframesraw(data)
# Close the wave file
wf.close()
# Play the audio
p = pyaudio.PyAudio()
wf = wave.open("sine_wave.wav", "rb")
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
stream.write(wf.readframes(num_frames))
stream.stop_stream()
stream.close()
wf.close()
p.terminate()
Метод 3: использование SciPy и PyAudio
import numpy as np
import pyaudio
from scipy import signal
# Set parameters
frequency = 440.0 # Frequency of the sine wave in Hz
duration = 3.0 # Duration in seconds
volume = 0.5 # Volume (0.0 to 1.0)
# Generate samples
sample_rate = 44100 # Number of samples per second (standard for audio)
t = np.linspace(0, duration, int(sample_rate * duration), False)
samples = volume * signal.sawtooth(2 * np.pi * frequency * t)
# Play the audio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=sample_rate,
output=True)
stream.write(samples.tobytes())
stream.stop_stream()
stream.close()
p.terminate()
Метод 4: использование Math и PyAudio
import math
import pyaudio
# Set parameters
frequency = 440.0 # Frequency of the sine wave in Hz
duration = 3.0 # Duration in seconds
volume = 0.5 # Volume (0.0 to 1.0)
# Generate samples
sample_rate = 44100 # Number of samples per second (standard for audio)
num_frames = int(sample_rate * duration)
samples = []
for i in range(num_frames):
value = volume * math.sin(2 * math.pi * frequency * i / sample_rate)
samples.append(value)
# Play the audio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=sample_rate,
output=True)
stream.write(b''.join(struct.pack('f', sample) for sample in samples))
stream.stop_stream()
stream.close()
p.terminate()