import subprocess import shlex from threading import Thread from time import sleep import pyaudio #pip3 install pyaudio import wave # set desired values start = 1 length = 3 # open wave file wave_file = wave.open('door.wav', 'rb') # initialize audio py_audio = pyaudio.PyAudio() stream = py_audio.open(format=py_audio.get_format_from_width(wave_file.getsampwidth()), channels=wave_file.getnchannels(), rate=wave_file.getframerate(), output=True) # skip unwanted frames n_frames = int(start * wave_file.getframerate()) wave_file.setpos(n_frames) # write desired frames to audio buffer n_frames = int(length * wave_file.getframerate()) frames = wave_file.readframes(n_frames) def run_command(command): process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE) while True: output = process.stdout.readline().decode() if output == '' and process.poll() is not None: break if output: output = output.strip() return int(output[20:]) rc = process.poll() return rc class Worker(Thread): def __init__(self, custom_handler): self.handler = custom_handler def run(self): currentValue = 0 #store values to check if ambient light is increasing or decreasing, dont do anything if no change, play from position based on scale(0-100k is mapped to 0-100% of song) previousValue = 0 while(True): sleep(0.05) #sleep(0.05) currentValue = run_command("./lmutracker3") if currentValue > previousValue: print("light is increasing") stream.write(frames) # close and terminate everything properly #stream.close() #py_audio.terminate() #wave_file.close() elif currentValue < previousValue: print("light is decreasing") else: print("light remains the same") previousValue = currentValue #print(testval) #check handler if self.handler: #do something pass handler = {} worker = Worker(handler) #start thread worker.run()