所以我找到了几种方法来做到这一点,我将按可行性顺序排列它们(最容易先申请):-
关于语气的假设:-
方式1(离线曲目,没有声音设备/后端麻烦)
1- 使用Audacity软件(或任何类似的软件)来创建
特定的音调并将其导出到文件。
2- 从 Audacity 中,从上面的选项卡中选择“生成”,然后选择
“音调”并在频率旁边输入 440。
3- 在 Audacity 中,从上面的选项卡中选择“文件”,然后选择“导出”并选择
导出为您喜欢的任何扩展名,最好是 mp3。 '输出.mp3'
4-pip安装播放声音
5-在Python中
import playsound
playsound.playsound('out.mp3')
方式2(灵活,但必须确保后端工作正常)
1-pip安装pygame
2-如果您在 Linux 环境下工作,请确保安装以下库
libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev
3-在Python中
import numpy
import pygame
sampleRate = 44100
freq = 440
pygame.mixer.init(44100,-16,2,512)
# sampling frequency, size, channels, buffer
# Sampling frequency
# Analog audio is recorded by sampling it 44,100 times per second,
# and then these samples are used to reconstruct the audio signal
# when playing it back.
# size
# The size argument represents how many bits are used for each
# audio sample. If the value is negative then signed sample
# values will be used.
# channels
# 1 = mono, 2 = stereo
# buffer
# The buffer argument controls the number of internal samples
# used in the sound mixer. It can be lowered to reduce latency,
# but sound dropout may occur. It can be raised to larger values
# to ensure playback never skips, but it will impose latency on sound playback.
arr = numpy.array([4096 * numpy.sin(2.0 * numpy.pi * freq * x / sampleRate) for x in range(0, sampleRate)]).astype(numpy.int16)
arr2 = numpy.c_[arr,arr]
sound = pygame.sndarray.make_sound(arr2)
sound.play(-1)
pygame.time.delay(1000)
sound.stop()
方式3(正弦波)
如果您需要的只是正弦波,请使用此选项
1-pip安装pysine
2-如果您在Linux环境下工作,请确保安装以下库
portaudio19-dev
但是,如果您在 Windows 环境下工作,请确保使用 pipwin 安装它
pipwin install pysine
3-在Python中
import pysine
pysine.sine(frequency=440.0, duration=1.0)