简体   繁体   English

Sonic Pi 不会从远程 Python 脚本播放声音

[英]Sonic Pi won't play sound from remote Python script

I am using Sonic Pi 3.3.1 on my Windows PC and a Python script( Python3.7.2 ) on my raspberry pi( Raspbian Buster ) that detects distance from an HC-SR04 -Ultrasonic Sensor .The program then creates a tune with a pitch that ranges higher if the object is further away, this tune is then sent over OSC to Sonic Pi.我在我的 Windows PC 上使用Sonic Pi 3.3.1 ,在我的树莓派( Raspbian Buster )上使用Python脚本( Python3.7.2 ),它检测与HC-SR04的音高距离,然后使用超声波传感器创建一个调谐程序。如果 object 距离较远,则该范围会更高,然后此曲调会通过OSC发送到 Sonic Pi。 External OSC is enabled on my windows PC.我的 windows PC 上启用了外部OSC I also checked the Port and IP addresses, and they are correct.我还检查了PortIP地址,它们是正确的。

I have tested my circuit extensively and I can confidently say this isn't the source of the problem, and that I added it for documentation purposes only at the bottom of my post for anyone who is interested, so I will move on.我已经对我的电路进行了广泛的测试,我可以自信地说这不是问题的根源,并且我仅在我的帖子底部为任何感兴趣的人添加了它以用于文档目的,所以我会继续前进。

My Python code:我的 Python 代码:

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('10.0.0.119', 4560)

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

Output: Output:

60
60
58
59

The problem doesn't seem to be my python code as it logs the correct values and successfully sends the pitch so once again I will move on.问题似乎不是我的python代码,因为它记录了正确的值并成功发送了音调,所以我将再次继续。

My sonic Pi code:我的声波 Pi 代码:

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

Output: Output:

索尼克派输出

Sonic Pi log's the notes correctly but doesn't play it and this so the data is being received but isn't being played so the problem is most likely how I am trying to play the note but I can't find the correct way to do this Sonic Pi 正确记录了音符,但没有播放,因此数据正在接收但未播放,因此问题很可能是我尝试播放音符的方式,但我找不到正确的方法做这个


I then tried this, to test if sonic pi installed correctly and is able to play sound :然后我尝试了这个,以测试sonic pi是否正确安装并能够播放声音

live_loop :foo do
  play 60
  sleep 1
end

Sonic Pi did play a sound so I can rule out a defective installation where sound is not installed properly. Sonic Pi 确实播放了声音,因此我可以排除声音未正确安装的有缺陷的安装。


My Circuit:我的电路:

The Vcc pin on the HC-SR04 -Ultrasonic Sensor is connected to the 5V power supply on the raspberry pi, then the Trig pin is connected to the 4th GPIO connector on the board the echo pin is then connected to the 17th GPIO connector between a voltage divider consisting of a 330 ohm resistor and a 470 ohm resistor which protects the raspberry from a full 5V . HC-SR04 -Ultrasonic Sensor上的Vcc引脚连接到树莓派上的5V电源,然后将Trig引脚连接到板上的第 4 个GPIO连接器,然后将 echo 引脚连接到 a 之间的第 17 个GPIO连接器分压器由一个330 ohm电阻和一个470 ohm电阻组成,可保护覆盆子免受5V的影响。

My circuit looks like:我的电路看起来像: 电路

Solution解决方案


The problem is in the Sonic Pi que path , that receives the note from the python scipt over a UDP connection.More specifically it is in the sync statement of the que path .问题出在 Sonic Pi que path中,它通过UDP连接从python scipt 接收注释。更具体地说,它在que pathsync语句中。 The sync statement is missing the senders Ip address and Port which is necessary when using multiple computers unlike in a local environment同步语句缺少发件人Ip地址和Port ,这在使用多台计算机时是必需的,这与在本地环境中不同

You can either explicitly mention the Ip address like so:您可以像这样明确提及Ip地址:

live_loop :listen do
  use_real_time
  note = sync "/osc/10.0.0.80:56505/play_this "#Mention the senders Ip address and port
  play note[0]
end

Or you can tell it to accept input regardless of the sender like so:或者你可以告诉它接受输入,不管发送者是这样的:

live_loop :listen do
  use_real_time
  note = sync "/osc*/play_this "#This accepts a message regardless of the sender
  play note[0]
end

I would like to end my post by thanking Sam Aaron who gave me insight on this problem on the Raspberry pi Stack Exchange community which enabled me to answer my this question我想通过感谢Sam Aaron来结束我的帖子,他在 Raspberry pi Stack Exchange 社区上让我深入了解了这个问题,这使我能够回答我的这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM