树莓派 theremin 制作

1 软件安装

更新系统 指令

sudo apt update 

sudo apt upgrade -y 

安装 Python 依赖文件

sudo pip3 install python-osc

如果 安装不成功 则 将pip3 --> pip

2 模拟泰勒明琴的过程

需要:

  • 超声波距离感应器(ultrasonic distance sensor)


    hc-sr04
  • Python
    当前流行的人工智能语言


    python
  • sonic-pi
    树莓派的一款音乐制作程序

sonic-pi

3背景:

泰勒明琴是一款特殊的乐器,通过不接触形式的演奏方式来产生音乐,但是泰勒明琴的制作过程十分复杂,内部结构多样,价格昂贵。我们的项目是通过使用Python代码与超声波传感器在树莓派系统上利用sonic-pi模拟泰勒明琴的演奏过程。


图片

4 .接线图表

名称 接线
GND GND
Trig Pin4
Echo Pin17
VCC VCC
图纸

5 Python代码

使用 Python代码连接超声波传感器

#导入超声波传感器的库
from gpiozero import DistanceSensor
from time import sleep

sensor = DistanceSensor(echo=17, trigger=4)

while True:
    print(sensor.distance)
    sleep(1)


result

6.sonic-pi的连接

安装 sonic-pi

sudo apt-get install sonic-pi

打开


打开
live_loop :listen do
    use_real_time
    note = sync "/osc/play_this"
    play note[0]
end

PYTHON连接 sonic-pi代码:

from gpiozero import DistanceSensor
from time import sleep

from pythonosc import osc_message_builder
from pythonosc import udp_client

sensor = DistanceSensor(echo=17, trigger=4)
sender = udp_client.SimpleUDPClient('127.0.0.1', 4559)

while True:
    pitch = round(sensor.distance * 100 + 30)
    sender.send_message('/play_this', pitch)
    sleep(0.1)

你可能感兴趣的:(树莓派 theremin 制作)