Does anyone know how to execute a script when a certain sound plays?

I have a dumb work related chrome thing, i'd like to make it so that when a certain notification sound plays in chromium, my computer does a few things automatically for me

Does anyone know a good way to make this happen?

I imagine it'd have to be setup like:

when chrome starts playing audio && check if that audio matches soundfile.ogg && myscript.sh, but I don't know any good cli utilities that could get something like that done, and if there are any better ideas!

edit: to avoid X/Y issues i've summarized the problem in full here:

  1. I have a work program, this notifies me if I get a call or email, the work program then presents an accept/decline page, and does not proceed until I either accept, decline, or it times out.
  2. I want it to do two different things depending on if it's a call or email
  3. It provides no notification other than the sound and an "accept" button on the page
  4. I have a chrome window open that does nothing but this, and I never use chrome for anything else
  5. I want to automatically do various things when I receive either this call or email
  6. I want it to be broadly applicable rather than a script designed for the specific website giving me the notification (so not a chrome extension). This prevents me from having to update any code in the event that the backend changes dramatically, and even if the notification sound changes, i'd just record a new sound as the activation noise.
  7. The noise is always the same, and hasn't changed for many years, and there is a distinct noise between calls and emails
  8. They never overlap, they never play multiple times at the same time, and they never make any noises other than those two. The noises are distinct.

These factors cause me to want to run a script once the noise is recognized, only if the noise is playing in a particular app. I'm using pipewire/hyprland on arch.

My current plan for isolating the noise is to do the following:

pactl load-module module-combine-sink sink_name='Work' slaves='easyeffects_sink'

and then set chrome exclusively to play audio on work.

Then set a script to check the sink work for audio that matches what I want. That should be simpler than the other methods i've seen to isolate the noise.

D_Air1 ,
@D_Air1@lemmy.ml avatar

Sounds like a case where machine learning would actually be useful.

flashgnash ,

Sometimes the easiest solution is multiple solutions

Maybe just write something to hook into the notification in Chrome, there's probably a way to get that working within an electron app too if the desktop app (teams?) is electron

Communist OP ,
@Communist@lemmy.ml avatar

That won't work if the backend ever changes, and will be locked into a single program

https://github.com/JorenSix/Olaf I've decided to use this, i'll probably have a solution this week, i have to actually record the sounds my next workday, then i'll test it. Seems much easier to do than making a chrome extension, honestly.

bloodfart ,

My assumption is that you don’t care if your notification gets spoofed, ex. Someone rings a little bell and the script deletes all cookies from porn websites as if the little bell notification played.

So I think the hardest and best way to do this is to have the script run on a separate device than the sound plays on.

First record the sound you want to trigger with. Use the script executing device with the microphone and interface you’ll be using in production set up in the location of production to make it easier on yourself.

Now reduce the bitrate of the target sound a lot. No, more than that, keep going, a little more, that’s perfect.

Now write something that will capture the last target_sound_length seconds of audio and compare it with the bitcrushed version. Depending on your device, there may be a buffer object in the adc you can interface with, although if it’s running a normal operating system you won’t be able to just get to it without going through the os first.

If you can go through the chrome notificationing machine, figure out the hook used to trigger the notification you want to respond to and intercept and perform the script. No nyquist needed!

mypasswordis1234 ,
@mypasswordis1234@lemmy.world avatar
Communist OP , (edited )
@Communist@lemmy.ml avatar

It's really not in this case, I can see why people think that since i've been vague, but tbh I thought somebody would have already made an easy sound recognition program and I just hadn't seen it, and that once someone pointed that to me the rest would be easy.

Here is the entirety of the problem:

  1. I have a work program, this notifies me if I get a call or email, the work program then presents an accept/decline page, and does not proceed until I either accept, decline, or it times out.
  2. I want it to do two different things depending on if it's a call or email
  3. It provides no notification other than the sound and an "accept" button on the page
  4. I have a chrome window open that does nothing but this, and I never use chrome for anything else
  5. I want to automatically do various things when I receive either this call or email
  6. I want it to be broadly applicable rather than a script designed for the specific website giving me the notification (so not a chrome extension). This prevents me from having to update any code in the event that the backend changes dramatically, and even if the notification sound changes, i'd just record a new sound as the activation noise.
  7. The noise is always the same, and hasn't changed for many years, and there is a distinct noise between calls and emails
  8. They never overlap, they never play multiple times at the same time, and they never make any noises other than those two. The noises are distinct.

These factors cause me to want to run a script once the noise is recognized, only if the noise is playing in a particular app. I'm using pipewire/hyprland on arch.

edit: actually they have, it should be really easy with this: https://github.com/worldveil/dejavu

flashgnash ,

This is absolutely an xy problem. Your problem is that you need to programmatically respond to notifications across multiple applications

You are asking for help with a solution based on notification sounds which is one possible solution but a bit of a weird one

Communist OP ,
@Communist@lemmy.ml avatar

It does not give a desktop notification, or even a proper chrome notification, it's just a dialogue on a page that says accept/deny

I said that in the post. The sound is the only thing to hook into. It doesn't even set chrome as urgent.

DaGeek247 ,
@DaGeek247@fedia.io avatar
  1. It provides no notification other than the sound and an "accept" button on the page
  2. I have a chrome window open that does nothing but this, and I never use chrome for anything else

https://chromewebstore.google.com/detail/distill-web-monitor/inlikjemeeknofckkjolnjbpehgadgge?pli=1

Communist OP ,
@Communist@lemmy.ml avatar

That's an interesting solution that i'd rather avoid because it's proprietary

Also, that wouldn't distinguish the two states of call/email, I don't think.

DaGeek247 ,
@DaGeek247@fedia.io avatar

It was the first result i found for 'monitor webpage chrome'. There's a very good chance that what you're after is just two or three results lower. I promise it would be a lot easier than developing an entirely new solution that works based on speaker sounds. Bonus, these extensions have basic stuff like sms or email support right out of the box.

Communist OP ,
@Communist@lemmy.ml avatar

I'll consider that after i try and fail with olaf

thevoidzero ,

Someone already talked about the XY problem, so I'll say this.

Why sound notification instead of notification content? If your notification program (dunst in my case) have pattern matching or calling scripts based on patterns and the script has access to which app, notification title, contents etc. then it's just about calling something in your bash script.

And any time you wanna add that functionality to something else, add one more line with a different pattern or add a condition in your script. Comparing text is lot more reliable than audio.

Of course your use case could be completely different, so maybe give some examples of use case so people can give you different ways to solve that instead of just the one you're thinking of.

Communist OP ,
@Communist@lemmy.ml avatar

It does not make a desktop notification at all, so that won't work

onlinepersona ,

I think it would possibly be easier to write an extension. You can inspire yourself from this extension, intercept media playback, possibly hash the media being played, compare the hash to a known DB you create, and call a script in response to a positive detection.

CC BY-NC-SA 4.0

ace_garp ,
@ace_garp@lemmy.world avatar

Expect automates things, based on text-input captured from a terminal.

Not sure if it has been extended/hacked to take sound as an input.

BCsven ,

This smells of "So I work from home, but want to sleep, but if my boss pings me on teams I want an alarm to wake me up"

Communist OP ,
@Communist@lemmy.ml avatar

Is there anything wrong with that? Hahaha, it's pretty similar but not quite that

Quazatron ,
@Quazatron@lemmy.world avatar

Lazy people tend to be creative people, which is good, especially when confronted with boring activities.

I'd solve it in hardware, maybe an ESP32 dongle with a mic pretending to be a keyboard.

Seriously though, sounds like you need a more creative or fulfilling job.

TCB13 ,
@TCB13@lemmy.world avatar

Modern problems require overkill solutions :P

Quazatron ,
@Quazatron@lemmy.world avatar

If it works it ain't stupid. 😄

Communist OP ,
@Communist@lemmy.ml avatar

Yeah the most fulfilling thing about this job has been figuring out how to automate as much of it as possible while still pretending to be a normal worker. It's pretty terrible, i'm going to switch to herpetology eventually, but can't do that right now for various reasons I don't want to get into on a public forum.

I'm at the top of every performance metric because of my inclination to be lazy as fuck with it though, so, it works.

ReversalHatchery ,

"So I work from home, but want to sleep play games, but if my boss pings me on teams I want an alarm to wake me up"

Depending on your employer it could be well okay

marcie ,
@marcie@lemmy.ml avatar

youre just thinking about it wrong. get an llm going, voice to text, and have a synthesizer copy your voice. 99% of your workload is now gone, no more endless meetings, and you got notes that can be quickly summarized.

lets just hope that they dont ask you what 27 times 38 or something is. could maybe prompt it to say 'Lets circle back around on that in an email' whenever a complex question is asked, lmao

Communist OP ,
@Communist@lemmy.ml avatar

I don't have any meetings ever. An LLM really wouldn't be able to do almost any of my work.

OsaErisXero ,

Modern problems require modern solutions

interdimensionalmeme ,

What about have an LLM autoanswer and only wake me up if the bossman keeps talking and also autofill my timecard so I don't have to

flashgnash ,

I'll have an llm auto answer the day I fancy getting an angry call from my manager because I responded to him asking me to give him access to something with

"I'm sorry, but as an AI language model I am unable to do that"

interdimensionalmeme ,

"As an AI model, I cannot in good conscience give you access, explain clearly why you think you need access to this system and I will forward your message to a more competent authority"

flashgnash ,

It's got the right level of sass at least

qjkxbmwvz ,

Can you isolate the call to the sound from the DevTools? And if so, does DevTools allow you to edit the function? Perhaps you could GET/POST something on localhost which could trigger a shell script.

iiGxC ,

Be careful: this could be you https://xkcd.com/1172/

tunetardis ,

I have some vague recollection of a hacker convention from the 90s where people were challenged to come up with wireless networking in a one night coding marathon. (This was long before wifi.) So some dude used speech synthesis to get a machine to say "one zero one one zero…" and another to assemble the binary data into packets using speech recognition. It was hilarious, and the dev had to keep telling people to shut up and stop laughing so he could complete the demo.

But anyways… what I'm trying to suggest here is you might have the best luck if your notification sounds contain spoken commands and you use speech recognition to trigger scripts? That tech is pretty mature at this point.

Communist OP ,
@Communist@lemmy.ml avatar

Unfortunately the notification sound can't be changed

enix ,

I think sox is what you want

I went ahead and asked a free AI how to use sox and played audio to trigger a script. Probably won't be 100% accurate but maybe send you down the right path. Good luck.

  1. Install 'sox' if you haven't already. You can use the package manager of your Linux distribution to install it.

  2. Open a terminal and use the 'rec' command from 'sox' to continuously listen to the audio input:

    sox -d -t .wav - silence 1 0.1 3% 1 1.0 3%
    

    This command will listen for audio and create a .wav file when it detects sound.

  3. Write a script that will be triggered when a sound is detected. For example, you can create a script called "myscript.sh" with the following content:

    #!/bin/bash
    echo "Sound detected! Running my script."
    # Add your desired actions here
    
  4. Make the script executable by running the following command in the terminal:

    chmod +x myscript.sh
    
  5. Use the 'rec' command along with the 'play' command from 'sox' to continuously listen for sound and execute your script when sound is detected:

    sox -d -t .wav - silence 1 0.1 3% 1 1.0 3% | while read -r; do ./myscript.sh; done
    

    This command will continuously listen for sound and execute your script each time sound is detected.

Remember to customize the script "myscript.sh" with your desired actions.

tmjaea ,

How would this not just trigger from any sound rather one specific?

t0mri , (edited )

I dunno exactly but, for shell script, playerctl can detect the audio output ig.

There might be some audio library. May be you can write a daemon or something to watch for events. That way you can you use it on anywhere not just on chrome.

Can you add more specifications, coz id like to help

slickJujitsu ,
@slickJujitsu@lemmy.today avatar

This Github is for detecting sound playing and sending it to Shazam. Perhaps you can use the features to capture audio and find another example of audio comparison for the other half?

Communist OP ,
@Communist@lemmy.ml avatar

That's a good start, thank you!

d3Xt3r Mod ,

And the second half of that solution (audio fingerprinting) can be solved using this: https://github.com/worldveil/dejavu

Communist OP ,
@Communist@lemmy.ml avatar

This is incredible and exactly what i'm looking for, i'll post my completed solution when i'm done, thank you!

tsonfeir ,
@tsonfeir@lemm.ee avatar

Is the sound screaming?

Communist OP ,
@Communist@lemmy.ml avatar

it might as well be, I hallucinate the noise regularly, although if it actually was screaming that would be a lot scarier.

davel ,
@davel@lemmy.ml avatar
Communist OP ,
@Communist@lemmy.ml avatar

Hallucinating a noise that you hear constantly very loudly is normal. Pretty sure.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • random
  • SciFi
  • Fediverse
  • fountainpens
  • dev_playground
  • Mdev
  • test
  • announcements
  • vexblue
  • anki
  • VideoEditingRequests
  • kbinrun
  • pamasich
  • All magazines