Python简单的音频发生器

时间:2019-06-14 06:22:57

标签: 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)

4 个答案:

答案 0 :(得分:0)

使用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)

答案 1 :(得分:0)

因此,我找到了几种方法来实现这一点,我将按照可行性的顺序进行排列(最先应用):


有关音调的假设:-

  • 波动类型=正弦波

  • 频率= 440Hz


方法1(离线音轨,无声音设备/后端麻烦)

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

2-在Audacity中,从上面的标签中选择“生成”,然后选择 “ Tone”,然后在频率旁边输入440。

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

4-点安装播放声音

5-在python中

import playsound
playsound.playsound('out.mp3')

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

1-点安装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-点安装pysine

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

portaudio19-dev

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

pipwin install pysine

3-在python中

import pysine
pysine.sine(frequency=440.0, duration=1.0) 

答案 2 :(得分:0)

有一个使用 PySDL2 实时生成音调的示例。 UP 和 DOWN 键用于改变频率。

product_medicines

答案 3 :(得分:0)

试试pysinewave。它允许您开始、停止和平滑地改变音调和音量。
示例:

from pysinewave import SineWave
from time import sleep

sinewave = SineWave(pitch = 12)
sinewave.play()
time.sleep(1)
sinewave.stop()