Python简单的音频生成器

And*_*_hs 8 python audio raspberry-pi

寻找一些(简单的)Python 音调生成器,用于在带有 USB 声卡的 raspi 上运行的以下脚本。需要动态音调开/关和频率改变。

import serial, time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.1)

def monitor(inp=0):
    if inp != inpold:
        if inp != 0:
            ser.setDTR(1)   # LED on (GPIO?)
                # start tone here, generate tone forever or change tone freq
        else:
            ser.setDTR(0)   # LED off
                # stop tone without clicks
        inpold = inp 
While True:
    time.sleep(0.01)        # min lenght tone pulse 10 milliseconds
    input = ser.getCTS()        # or GPIO input
    monitor(input)
Run Code Online (Sandbox Code Playgroud)

小智 9

所以我找到了几种方法来做到这一点,我将按可行性顺序排列它们(最容易首先应用):-


关于语气的假设:-

  • 波形类型 = 正弦波

  • 频率=440Hz


方式1(离线曲目,没有声音设备/后端麻烦)

1- 使用Audacity软件(或任何类似软件)创建特定的音调并将其导出到文件。

2- 在 Audacity 中,从上面的选项卡中选择“生成”,然后选择“音调”并将 440 放在频率旁边。

3- 在 Audacity 中,从上面的选项卡中选择“文件”,然后选择“导出”并选择导出为您喜欢的任何扩展名,最好是 mp3。'输出.mp3'

4-pip安装播放声音

5-在Python中

import playsound
playsound.playsound('out.mp3')
Run Code Online (Sandbox Code Playgroud)

方式2(灵活,但必须确保后端工作正常)

1-pip安装pygame

2-如果您在 Linux 环境下工作,请确保安装以下库

libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

方式3(正弦波)

如果您需要的只是正弦波,请使用此选项

1-pip安装pysine

2-如果您在Linux环境下工作,请确保安装以下库

portaudio19-dev
Run Code Online (Sandbox Code Playgroud)

但是,如果您在 Windows 环境下工作,请确保使用 pipwin 安装它

pipwin install pysine
Run Code Online (Sandbox Code Playgroud)

3-在Python中

import pysine
pysine.sine(frequency=440.0, duration=1.0) 
Run Code Online (Sandbox Code Playgroud)


And*_*_hs 2

花了很多时间使用 pyaudio 但使用 pygame 非常简单。谢谢 http://shallowsky.com/blog/programming/python-play-chords.html

import pygame, pygame.sndarray
import numpy
import scipy.signal
from time import sleep

sample_rate = 48000
pygame.mixer.pre_init(sample_rate, -16, 1, 1024)
pygame.init()

def square_wave(hz, peak, duty_cycle=.5, n_samples=sample_rate):
    t = numpy.linspace(0, 1, 500 * 440/hz, endpoint=False)
    wave = scipy.signal.square(2 * numpy.pi * 5 * t, duty=duty_cycle)
    wave = numpy.resize(wave, (n_samples,))
    return (peak / 2 * wave.astype(numpy.int16))

def audio_freq(freq = 800):
    global sound
    sample_wave = square_wave(freq, 4096)
    sound = pygame.sndarray.make_sound(sample_wave)
# TEST
audio_freq()
sound.play(-1)
sleep(0.5)
sound.stop()
audio_freq(1000)
#sleep(1)
sound.play(-1)
sleep(2)
sound.stop()
sleep(1)
sound.play(-1)
sleep(0.5)
Run Code Online (Sandbox Code Playgroud)