Skip to content

Instantly share code, notes, and snippets.

@kaanaksit
Last active January 5, 2020 07:28
Show Gist options
  • Save kaanaksit/859337e52175913dc706a0140387573f to your computer and use it in GitHub Desktop.
Save kaanaksit/859337e52175913dc706a0140387573f to your computer and use it in GitHub Desktop.
Dance of a Smart bulb with music
'''
This is tested with an Ubuntu 19.10. I tested this script with %60 volume and a Bistee Smart WiFi Bulb E26 7W RGBW --> https://smile.amazon.com/gp/product/B07YDKD274/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1
(1) Assuming that you set the default input device on your machine as monitor of your soundcard (check input devices in `pavucontrol`).
(2) Assuming that you installed sounddevice, flux_led using pip3: sudo pip3 install sounddevice, flux_led .
(3) make sure that you set the ip address of your smart bulb correctly. Check ip_address line in this script.
(4) Simply run this script to make your light dance!
(5) If the light transition isn't good as you want, try playing with kd and kp variables in below.
'''
import sounddevice as sd
import numpy as np
import subprocess
global volume
volume=0
duration=10
kp=100.
kd=1000.
ip_address='192.168.0.100'
def audio_callback(indata, frames, time, status):
global volume
volume = (np.amax(indata)-np.amin(indata))*kd+np.linalg.norm(indata)*kp
volume = int(volume)
if volume > 100:
volume = 100
elif volume < 1:
volume = 1
def sound_loop():
stream = sd.InputStream(callback=audio_callback)
with stream:
sd.sleep(duration)
def main():
while True:
sound_loop()
cmd = [
'flux_led',
'%s' % ip_address,
'-w',
'%s' % volume,
'-1'
]
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE
)
proc.wait()
# print(cmd,volume)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment