|
| 1 | +# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import json |
| 5 | +import time |
| 6 | +import glob |
| 7 | +import os |
| 8 | +import tomllib |
| 9 | +from datetime import datetime |
| 10 | +import board |
| 11 | +from digitalio import DigitalInOut, Direction, Pull |
| 12 | +from adafruit_debouncer import Debouncer |
| 13 | + |
| 14 | + |
| 15 | +from kittentts import KittenTTS |
| 16 | +import soundfile as sf |
| 17 | + |
| 18 | + |
| 19 | +print("initializing...") |
| 20 | + |
| 21 | +with open("weather_narrator.toml", "rb") as f: |
| 22 | + config = tomllib.load(f) |
| 23 | +voice = config.get("voice", None) |
| 24 | +sound_device = config.get("sound_device", None) |
| 25 | + |
| 26 | +day_of_month_words = [ |
| 27 | + "1st", |
| 28 | + "2nd", |
| 29 | + "3rd", |
| 30 | + "4th", |
| 31 | + "5th", |
| 32 | + "6th", |
| 33 | + "7th", |
| 34 | + "8th", |
| 35 | + "9th", |
| 36 | + "10th", |
| 37 | + "11th", |
| 38 | + "12th", |
| 39 | + "13th", |
| 40 | + "14th", |
| 41 | + "15th", |
| 42 | + "16th", |
| 43 | + "17th", |
| 44 | + "18th", |
| 45 | + "19th", |
| 46 | + "20th", |
| 47 | + "21st", |
| 48 | + "22nd", |
| 49 | + "23rd", |
| 50 | + "24th", |
| 51 | + "25th", |
| 52 | + "26th", |
| 53 | + "27th", |
| 54 | + "28th", |
| 55 | + "29th", |
| 56 | + "30th", |
| 57 | + "31st", |
| 58 | +] |
| 59 | + |
| 60 | +button = DigitalInOut(board.D17) |
| 61 | +button.direction = Direction.INPUT |
| 62 | +button.pull = Pull.UP |
| 63 | + |
| 64 | +debounced_btn = Debouncer(button) |
| 65 | + |
| 66 | +with open("forecast.json", "r") as f: |
| 67 | + forecast = json.load(f) |
| 68 | + |
| 69 | +m = KittenTTS("KittenML/kitten-tts-nano-0.1") |
| 70 | + |
| 71 | + |
| 72 | +def generate_date_time_audio(date_obj): |
| 73 | + replacements = { |
| 74 | + "00": "oh clock", |
| 75 | + "01": "oh 1", |
| 76 | + "02": "oh 2", |
| 77 | + "03": "oh 3", |
| 78 | + "04": "oh 4", |
| 79 | + "05": "oh 5", |
| 80 | + "06": "oh 6", |
| 81 | + "07": "oh 7", |
| 82 | + "08": "oh 8", |
| 83 | + "09": "oh 9", |
| 84 | + } |
| 85 | + |
| 86 | + now_date_obj = datetime.now() |
| 87 | + try: |
| 88 | + os.remove("date.wav") |
| 89 | + except FileNotFoundError: |
| 90 | + pass |
| 91 | + month = date_obj.strftime("%B") |
| 92 | + day_word = day_of_month_words[date_obj.day - 1] |
| 93 | + date_script = f"{month} {day_word}, {date_obj.year}." |
| 94 | + |
| 95 | + time_script = now_date_obj.strftime("%-I %M %p") |
| 96 | + for key, val in replacements.items(): |
| 97 | + time_script = time_script.replace(key, val) |
| 98 | + |
| 99 | + date_script += f" The time is: {time_script}." |
| 100 | + audio = m.generate(date_script, voice=voice) |
| 101 | + sf.write("date.wav", audio, 24000) |
| 102 | + |
| 103 | + |
| 104 | +print("Press button to hear time and weather...") |
| 105 | +while True: |
| 106 | + debounced_btn.update() |
| 107 | + if debounced_btn.fell: |
| 108 | + print("just pressed") |
| 109 | + |
| 110 | + dt_format = "%Y-%m-%dT%H:%M:%S%z" |
| 111 | + forecast_date_obj = datetime.strptime( |
| 112 | + forecast["properties"]["periods"][0]["startTime"], dt_format |
| 113 | + ) |
| 114 | + |
| 115 | + generate_date_time_audio(forecast_date_obj) |
| 116 | + |
| 117 | + files_to_read = glob.glob("sound_files/*.wav") |
| 118 | + sorted_files_asc = sorted(files_to_read, key=os.path.getmtime) |
| 119 | + sorted_files_asc.insert(0, "date.wav") |
| 120 | + for file in sorted_files_asc: |
| 121 | + if sound_device is None: |
| 122 | + os.system(f"aplay {file}") |
| 123 | + else: |
| 124 | + os.system(f"aplay -D {sound_device} {file}") |
| 125 | + |
| 126 | + time.sleep(0.01) |
0 commit comments