所以我一直在尝试制作一个实际上可以实时工作的音乐可视化工具。我为了让它发挥作用而挣扎了很多,但是,昨天,无处不在,它在发送数据包后添加了一小段延迟时开始工作。我把它取下来并且 voila ,它正在实时工作。
所以我稍后把他们带回家,发现他们仍然没有在我的家庭网络上工作,我觉得这很奇怪。然后我今天带他们回到学校,发现他们甚至不再在学校工作了!是什么赋予了?我很难理解为什么这些东西无处不在地起作用,然后再次停止工作。
我尝试添加一个延迟,但它有效,但延迟非常糟糕,很难说它甚至对麦克风做出了反应。
我的GitHub在这里:https://github.com/HoldenGs/LIFX_controller
以及相关代码:
#!/usr/bin/env python3
import time
import socket
import argparse
import random
import queue
import sys
from tools import get_colour_zones_packet, APPLY
RETRIES = 1
UDP_PORT = 56700
SEQ_NUM = random.randint(0, 255)
def int_or_str(text):
"""Helper function for argument parsing."""
try:
return int(text)
except ValueError:
return text
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'-l', '--list-devices', action='store_true',
help='show list of audio devices and exit')
parser.add_argument(
'-d', '--device', type=int_or_str,
help='input device (numeric ID or substring)')
parser.add_argument(
'-w', '--window', type=float, default=200, metavar='DURATION',
help='visible time slot (default: %(default)s ms)')
parser.add_argument(
'-i', '--interval', type=float, default=30,
help='minimum time between plot updates (default: %(default)s ms)')
parser.add_argument(
'-b', '--blocksize', type=int, help='block size (in samples)')
parser.add_argument(
'-r', '--samplerate', type=float, help='sampling rate of audio device')
parser.add_argument(
'-n', '--downsample', type=int, default=10, metavar='N',
help='display every Nth sample (default: %(default)s)')
parser.add_argument(
'channels', type=int, default=[1], nargs='*', metavar='CHANNEL',
help='input channels to plot (default: the first)')
args = parser.parse_args()
if any(c < 1 for c in args.channels):
parser.error('argument CHANNEL: must be >= 1')
mapping = [c - 1 for c in args.channels] # Channel numbers start with 1
q = queue.Queue(64)
def audio_callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
if status:
print(status, file=sys.stderr)
# Fancy indexing with mapping creates a (necessary!) copy:
q.put(indata[::args.downsample, mapping])
def update_lights(hue):
"""This is called by matplotlib for each plot update.
Typically, audio callbacks happen more frequently than plot updates,
therefore the queue tends to contain multiple blocks of audio data.
"""
while True:
try:
data = q.get_nowait()
except queue.Empty:
break
max_amp = max(data)[0] * 500
if max_amp > 100:
max_amp = 100
if max_amp < 1:
max_amp = 1
print("amp: {} ".format(int(max_amp)), end="\r")
bulb_ip = "192.168.1.5"
start_index = 0
end_index = 11
sat = 0
kel = 3500
packet = get_colour_zones_packet(start_index, end_index,
hue, sat, int(max_amp), kel, APPLY, SEQ_NUM)
sock.sendto(packet, (bulb_ip, UDP_PORT))
if len(sys.argv) > 0:
import numpy as np
import sounddevice as sd
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if args.list_devices:
print(sd.query_devices())
parser.exit(0)
if args.samplerate is None:
device_info = sd.query_devices(args.device, 'input')
args.samplerate = device_info['default_samplerate']
print("Starting...")
# start stream
stream = sd.InputStream(
device=args.device, channels=max(args.channels),
samplerate=args.samplerate, callback=audio_callback)
try:
with stream:
hue = 0
add = 1
while True:
# if add:
# hue += 0.00005
# if hue >= 255:
# add = 0
# hue = 255
# else:
# hue -= 0.00005
# if hue <= 0:
# add = 1
# hue = 0
update_lights(int(hue))
except KeyboardInterrupt as e:
parser.exit(type(e).__name__ + ': ' + str(e))
else:
print("No lights with MultiZone capability detected.")
我对如何处理这个难以捉摸的问题感到有些迷茫。非常感谢任何提示!
答案 0 :(得分:0)
所以回答我自己的问题,似乎没有可靠的方法来控制这些灯而没有一些限速,所以此时我发送数据包后只有0.025秒的小延迟,这会导致一些音频输入不会显示在灯光中,但它仍然可以作为一个基本的解决方案。如果您想查看该项目,请点击此处:https://github.com/HoldenGs/LIFX_controller