Doorbell sound via amplipi and speakers

anyone who can share some experience with attempting to have some dedicated doorbell-sound.mp3 be played via amplipi?

my home domotica can trigger the required code that talks amplipi-rest-api to get the following effect

  • capture current config
  • mute all
  • play mp3 on all speakers
  • go back to previous config and happily play along

any advise, suggestions or tips welcome

NOTE: I have the mp3 up on a local http server and configured as an internet radio on the amplipi - would be nice though if I could ā€œhideā€ it from the other inputs (that or have some hierarchy in the input selection)

Have you tried looking into the announce endpoint? It does all of this in one API call, the zones to play the sounds on can be specified but by default it plays on all zones, the sound file is expected to be given as a url.

2 Likes

Have you tried looking into the announce endpoint?

Wow. Sounds like a perfect match !

Had just started reading up on the API + using the pyamplipi lib - but this answer allows me to skip ahead quite a bit :slight_smile:

In case anyone else looks here for something like an answer:

just add your own API_URI and SOUND_URI to a .env and use:

#! /usr/bin/env bash
source .env

ANNOUNCE="${API_URI}/announce"
CT_JSON="Content-Type: application/json"
CONTENT_JSON=$(printf '{"media": "%s"}' ${SOUND_URI})

printf "Using announce\n  at %s\n  to post data %s\n\n" "${ANNOUNCE}" "${CONTENT_JSON}"

curl -s --url ${ANNOUNCE} -X POST -v -H "${CT_JSON}" -d "${CONTENT_JSON}" -o /dev/null >> /dev/null 2>&1

echo "done"

Or using the pyamplipi lib (current version):

#! /usr/bin/env python3
"""
Playing a sound over the amplipi-announce
"""
import asyncio
import os
from collections.abc import Iterable
from pyamplipi.amplipi import AmpliPi
from pyamplipi.models import Announcement, Status
from dotenv import load_dotenv


async def play_sound(sndsrv, sound_uri):
    sound: Announcement  = Announcement(media=sound_uri, vol_f=0.6, source_id=3)
    await sndsrv.announce(sound)


async def runit(): 
    load_dotenv()
    api_uri = os.getenv("API_URI")
    sound_uri = os.getenv("SOUND_URI")
    print(f"Using announce\n  at {api_uri}\n  to play sound at {sound_uri}\n\n")

    sndsrv = AmpliPi(api_uri, timeout=30)
    await play_sound(sndsrv, sound_uri)
    await sndsrv.close()
    print("done")


def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(runit())


if __name__ == '__main__':
    main()

3 Likes

Nice example. Thanks for posting that!

Care to explain a little more about your doorbell setup? I wasnā€™t expecting to see python.

Care to explain a little more about your doorbell setup? I wasnā€™t expecting to see python.

Sure.

Here in Belgium Niko is a well known and appreciated brand for home-electricity wall mount power-plugs, switches, ā€¦ since many generations. Their contemporary offering includes a good and affordable ā€˜home-automationā€™ solution called niko home control

It is proprietary, but with some open api support and gateways to other brands and products. Most importantly they allow triggering ā€œvirtual devicesā€ which generate events that can be trapped through the nhc2-coco python library ā†’ with open code at github

The aim is to develop a simple local running service (I have small server running docker) that just listens to these events to trigger some neat effects involving the amplipi:

  • for starters this very thing ā†’ have the doorbell push trigger the /announce of some dingdong.mp3
  • in some zones (the rooms with built-in speakers) Iā€™ve foreseen these 6-function-controlpads that should be driving some useful effects like volume up/down, next/prev source, room-preset, mute/play, ā€¦
  • finally I have an ā€œall shutā€ switch in the house that not only should close the doors, shut the lights but also mute-all zones
  • to avoid boredom after the above I am open to wild suggestions: joking, but all should be possible (e.g. bird-song-announcements when the sun rises, family-birthday announcements, personalized text-to-speech-train-commute-delay-messages, and reversely ā†’ have the lights flicker - or romantically dimmed ? when certain spotify-tracks are played ā€¦ :person_shrugging:)

So yeah, you might expect some more questions from me while I get through this little project :slight_smile:

Thx already for the fast responses, will do my best to document my progress, share alike, and pass the occasional suggestion, bug-report or PR via githubā€¦

2 Likes

Sounds like a fun project. Keep these questions coming.

1 Like

Similiar to @mpo 's thread here.

Is there a local location on the AmpliPi I can reference an mp3 from using the announce API?

I uploaded an MP3 to
/home/pi/ampli-dev/web/static/audio/youve-got-mail.mp3

but then I go to use the announce endpoint and try both:

  • /home/pi/ampli-dev/web/static/audio/youve-got-mail.mp3
  • static/audio/youve-got-mail.mp3

but neither work. The example URL does work from nasa, but Iā€™m not looking to host a mp3 file elsewhere.

1 Like

Hmm that is weird. I just tested on my test setup with the following media file (included in amplipi for testing)

I browsed to the built in api tester via http://amplipi.localdoc#post-/api/announce

Then I filled in the announce example with the JSON below and clicked Try

{
  "source_id": 0,
  "media": "/home/pi/amplipi-dev/web/static/audio/optical_in2.mp3"
}

This example specifies using source_id 0 to make the announcement (the default is source_id 3).

Does that work for you?

I went ahead and tried it again today and it seems like itā€™s working without the need for the source_id. I could have sworn I was doing everything the same but it is working now with both paths:

  • /home/pi/amplipi-dev/web/static/audio/youve-got-mail.mp3
  • web/static/audio/youve-got-mail.mp3

No idea what was going on yesterday.

Hmm weird. Let us know if you figure out what was going on. Good to know it is working now.

1 Like