Adding delays to get script to work

Posted on
Fri Apr 29, 2022 4:56 am
boisy offline
User avatar
Posts: 273
Joined: Jun 25, 2013

Adding delays to get script to work

Hi all,

I'm experimenting with Airfoil and the plug-in to get speech to text announcements going for my security system. The script below works, but with some caveats:

Code: Select all
#!/usr/local/bin/indigo-host -x
from  AppKit import NSSpeechSynthesizer
import time

studyHomePodID = 86734491
airfoilPlugin = indigo.server.getPlugin("com.perceptiveautomation.indigoplugin.airfoilpro")
if airfoilPlugin.isEnabled():
   result = airfoilPlugin.executeAction("connect", deviceId=studyHomePodID, waitUntilDone=True)  # ID of Speaker device
   time.sleep(1.0) # Required or else audio comes through Mac speaker

text = "Hi"

nssp = NSSpeechSynthesizer
ve = nssp.alloc().init()
   
ve.setVoice_("com.apple.speech.synthesis.voice.samantha")
ve.startSpeakingString_(text)

while not ve.isSpeaking():
   time.sleep(0.1)

while ve.isSpeaking():
   time.sleep(0.1)
   
time.sleep(2.0) # Required or else no audio

result = airfoilPlugin.executeAction("disconnect", deviceId=studyHomePodID, waitUntilDone=True)  # ID of Speaker device


Note the two comments starting with 'Required or else'. It appears I need to force some delays in the code in order for the audio to propagate out to the HomePod speaker that I'm trying to play audio to.

Is this normal? I noticed the 'waitUntilDone' parameter of the executeAction is set to True, but delays are still necessary.

Just wondering if this is expected.

Posted on
Fri Apr 29, 2022 7:22 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Adding delays to get script to work

Yeah that’s not surprising - even when you manually make adjustments in the airfoil UI it can take a second or two for it to negotiate the AirPlay speaker connections.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Apr 29, 2022 10:54 am
boisy offline
User avatar
Posts: 273
Joined: Jun 25, 2013

Re: Adding delays to get script to work

Thanks for confirming, Jay. It's not a big deal, but I wanted to make sure it was expected.

Posted on
Fri Apr 29, 2022 11:39 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Adding delays to get script to work

Their own remote speaker negotiation/connectivity used to be much faster than AirPlay, and I'm guessing that AirPlay 2 is even slower. Complexity has it's costs... ;)

BTW, you probably want to use self.sleep() (rather than time.sleep()) just in case the negotiation takes a long time or goes off the rails - it'll be easier for Indigo to kill it.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Apr 29, 2022 2:16 pm
boisy offline
User avatar
Posts: 273
Joined: Jun 25, 2013

Re: Adding delays to get script to work

Thanks Jay. This code seems to work:

Code: Select all
   indigo.activePlugin.sleep(0.1)

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests