omnipython-apps 0.1.4.dev8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. apps/__init__.py +5 -0
  2. apps/about/__init__.py +257 -0
  3. apps/about/app.json +1 -0
  4. apps/about/credits.py +12 -0
  5. apps/about/facts.py +398 -0
  6. apps/about/fonts/OFL.txt +93 -0
  7. apps/about/fonts/latin1_14.bin +0 -0
  8. apps/about/fonts/latin1_16.bin +0 -0
  9. apps/about/fonts/latin1_24.bin +0 -0
  10. apps/about/fonts/latin1_32.bin +0 -0
  11. apps/about/fonts/latin1_40.bin +0 -0
  12. apps/backline/__init__.py +58 -0
  13. apps/backline/app.json +1 -0
  14. apps/backline/app.py +475 -0
  15. apps/backline/art.py +717 -0
  16. apps/backline/ask.py +781 -0
  17. apps/backline/box.py +502 -0
  18. apps/backline/cast.py +137 -0
  19. apps/backline/desk/__init__.py +329 -0
  20. apps/backline/desk/editor.py +331 -0
  21. apps/backline/desk/grid.py +154 -0
  22. apps/backline/desk/look.py +169 -0
  23. apps/backline/desk/pedals.py +102 -0
  24. apps/backline/desk/perform.py +157 -0
  25. apps/backline/desk/prompt.py +210 -0
  26. apps/backline/desk/strip.py +159 -0
  27. apps/backline/desk_main.py +226 -0
  28. apps/backline/engine.py +622 -0
  29. apps/backline/floor.py +509 -0
  30. apps/backline/footswitch.py +119 -0
  31. apps/backline/glyphs.py +82 -0
  32. apps/backline/kits.py +428 -0
  33. apps/backline/midipedal.py +362 -0
  34. apps/backline/page_data.py +413 -0
  35. apps/backline/parts.py +145 -0
  36. apps/backline/pedal.py +107 -0
  37. apps/backline/remote.py +134 -0
  38. apps/backline/songs/ballad_68.json +1 -0
  39. apps/backline/songs/bossa.json +1 -0
  40. apps/backline/songs/country_train.json +1 -0
  41. apps/backline/songs/funk.json +1 -0
  42. apps/backline/songs/metal.json +1 -0
  43. apps/backline/songs/pop.json +1 -0
  44. apps/backline/songs/rock.json +1 -0
  45. apps/backline/songs/shuffle.json +1 -0
  46. apps/battery/__init__.py +101 -0
  47. apps/battery/app.json +1 -0
  48. apps/bit_world/act_deck.py +683 -0
  49. apps/bit_world/act_game.py +762 -0
  50. apps/bit_world/act_pet.py +1032 -0
  51. apps/bit_world/app.json +1 -0
  52. apps/bit_world/bit_output.py +45 -0
  53. apps/bit_world/bit_palette.py +503 -0
  54. apps/bit_world/bit_sound.py +431 -0
  55. apps/bit_world/bit_sprites.py +632 -0
  56. apps/bit_world/bit_world.py +534 -0
  57. apps/btreceiver/__init__.py +690 -0
  58. apps/btreceiver/app.json +1 -0
  59. apps/btreceiver/chime.py +155 -0
  60. apps/btreceiver/demo.py +529 -0
  61. apps/btreceiver/screen.py +633 -0
  62. apps/camera/__init__.py +316 -0
  63. apps/camera/app.json +1 -0
  64. apps/card/__init__.py +113 -0
  65. apps/card/app.json +1 -0
  66. apps/cast/__init__.py +631 -0
  67. apps/cast/app.json +1 -0
  68. apps/cast/receivers.py +193 -0
  69. apps/chess/__init__.py +18 -0
  70. apps/chess/app.json +1 -0
  71. apps/chess/app.py +922 -0
  72. apps/chess/art.py +323 -0
  73. apps/chess/ble.py +411 -0
  74. apps/chess/engine.py +925 -0
  75. apps/chess/game.py +327 -0
  76. apps/chess/net.py +261 -0
  77. apps/clock/__init__.py +38 -0
  78. apps/clock/app.json +1 -0
  79. apps/detect/__init__.py +214 -0
  80. apps/detect/app.json +1 -0
  81. apps/fmsynth/__init__.py +41 -0
  82. apps/fmsynth/algorithms.py +61 -0
  83. apps/fmsynth/app.json +1 -0
  84. apps/fmsynth/app.py +355 -0
  85. apps/fmsynth/editor.py +206 -0
  86. apps/fmsynth/engine.py +257 -0
  87. apps/fmsynth/meters.py +262 -0
  88. apps/fmsynth/midi.py +375 -0
  89. apps/fmsynth/panel_view.py +1071 -0
  90. apps/fmsynth/patches/fmsynth-01.syx +0 -0
  91. apps/fmsynth/settings.py +138 -0
  92. apps/fmsynth/voice.py +398 -0
  93. apps/gamepad/__init__.py +685 -0
  94. apps/gamepad/app.json +1 -0
  95. apps/gamepad/pad.py +94 -0
  96. apps/gamepad/watch.py +352 -0
  97. apps/hidden/__init__.py +72 -0
  98. apps/hidden/app.json +1 -0
  99. apps/home/__init__.py +483 -0
  100. apps/home/app.json +1 -0
  101. apps/home/main.py +36 -0
  102. apps/homeassistant/__init__.py +213 -0
  103. apps/homeassistant/api.py +104 -0
  104. apps/homeassistant/app.json +1 -0
  105. apps/huey/__init__.py +287 -0
  106. apps/huey/acting.py +184 -0
  107. apps/huey/actor.py +194 -0
  108. apps/huey/app.json +1 -0
  109. apps/huey/client.py +125 -0
  110. apps/huey/servers.py +121 -0
  111. apps/huey/settings.example.json +8 -0
  112. apps/huey/settings.py +97 -0
  113. apps/huey/turn.py +504 -0
  114. apps/huey/voice.py +217 -0
  115. apps/info/__init__.py +810 -0
  116. apps/info/app.json +1 -0
  117. apps/lora/__init__.py +62 -0
  118. apps/lora/app.json +1 -0
  119. apps/manifest.py +71 -0
  120. apps/medialink_baby_monitor/__init__.py +222 -0
  121. apps/medialink_baby_monitor/app.json +1 -0
  122. apps/medialink_doorbell/__init__.py +251 -0
  123. apps/medialink_doorbell/app.json +1 -0
  124. apps/medialink_hands_free/__init__.py +217 -0
  125. apps/medialink_hands_free/app.json +1 -0
  126. apps/medialink_surveillance_camera/__init__.py +258 -0
  127. apps/medialink_surveillance_camera/app.json +1 -0
  128. apps/medialink_video_doorbell/__init__.py +289 -0
  129. apps/medialink_video_doorbell/app.json +1 -0
  130. apps/medialink_videophone/__init__.py +273 -0
  131. apps/medialink_videophone/app.json +1 -0
  132. apps/medialink_walkie_talkie/__init__.py +228 -0
  133. apps/medialink_walkie_talkie/app.json +1 -0
  134. apps/midicontroller/__init__.py +774 -0
  135. apps/midicontroller/app.json +1 -0
  136. apps/midicontroller/links.py +421 -0
  137. apps/midicontroller/surface.py +343 -0
  138. apps/midiengine/__init__.py +977 -0
  139. apps/midiengine/app.json +1 -0
  140. apps/midiengine/banks.py +85 -0
  141. apps/midiengine/midiin.py +165 -0
  142. apps/midiengine/player.py +392 -0
  143. apps/midiengine/umidiparser.py +1413 -0
  144. apps/mp3/__init__.py +240 -0
  145. apps/mp3/app.json +1 -0
  146. apps/perch/__init__.py +119 -0
  147. apps/perch/app.json +1 -0
  148. apps/perch/huey.py +481 -0
  149. apps/perch/huey_props.py +386 -0
  150. apps/perch/huey_shift.py +42 -0
  151. apps/perch/huey_views.py +296 -0
  152. apps/perch/scene_bench.py +680 -0
  153. apps/perch/scene_coast.py +479 -0
  154. apps/perch/scene_desk.py +492 -0
  155. apps/perch/stage.py +1274 -0
  156. apps/perch/vec.py +366 -0
  157. apps/repl/__init__.py +325 -0
  158. apps/repl/app.json +1 -0
  159. apps/roku/__init__.py +156 -0
  160. apps/roku/app.json +1 -0
  161. apps/rokucam/__init__.py +362 -0
  162. apps/rokucam/app.json +1 -0
  163. apps/security/__init__.py +261 -0
  164. apps/security/app.json +1 -0
  165. apps/settings/__init__.py +221 -0
  166. apps/settings/app.json +1 -0
  167. apps/shell/__init__.py +33 -0
  168. apps/shell/app.json +1 -0
  169. apps/spotify/__init__.py +669 -0
  170. apps/spotify/app.json +1 -0
  171. apps/spotify/screen.py +82 -0
  172. apps/steps/__init__.py +58 -0
  173. apps/steps/app.json +1 -0
  174. apps/stompbox/__init__.py +31 -0
  175. apps/stompbox/app.json +1 -0
  176. apps/stompbox/app.py +668 -0
  177. apps/stompbox/boards.json +20 -0
  178. apps/stompbox/effects.py +241 -0
  179. apps/stompbox/engine.py +340 -0
  180. apps/stompbox/footswitch.py +331 -0
  181. apps/stompbox/inputs.py +449 -0
  182. apps/stompbox/meter.py +72 -0
  183. apps/stompbox/outputs.py +135 -0
  184. apps/stompbox/pedals.txt +45 -0
  185. apps/stompbox/presets.py +106 -0
  186. apps/stompbox/styles.py +135 -0
  187. apps/stompbox/ui.py +817 -0
  188. apps/stompbox/wavfile.py +73 -0
  189. apps/synthstation/__init__.py +20 -0
  190. apps/synthstation/app.json +1 -0
  191. apps/synthstation/audio.py +397 -0
  192. apps/synthstation/catalog.py +50 -0
  193. apps/synthstation/meters.py +136 -0
  194. apps/synthstation/midi.py +380 -0
  195. apps/synthstation/pads.py +177 -0
  196. apps/synthstation/parts.py +128 -0
  197. apps/synthstation/station.py +1069 -0
  198. apps/system/__init__.py +11 -0
  199. apps/system/app.json +1 -0
  200. apps/touchcal/__init__.py +324 -0
  201. apps/touchcal/app.json +1 -0
  202. apps/touchcal/fit.py +262 -0
  203. apps/wake/__init__.py +268 -0
  204. apps/wake/app.json +1 -0
  205. apps/wake/feed.py +155 -0
  206. apps/watch/__init__.py +355 -0
  207. apps/watch/app.json +1 -0
  208. apps/watch/faces.py +35 -0
  209. apps/watch/wheel.py +157 -0
  210. apps/weather/__init__.py +278 -0
  211. apps/weather/app.json +1 -0
  212. apps/weather/icons.py +172 -0
  213. apps/weather/sizing.py +71 -0
  214. apps/weather/zippad.py +172 -0
  215. omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
  216. omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
  217. omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
  218. omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
@@ -0,0 +1,155 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Two-note chimes for a ringing call and a new text, over the music.
5
+
6
+ `Chimes.mix` puts the music into one voice of an `audiomixer.Mixer`, which
7
+ the speaker plays in place of the music, and keeps the other voice for a
8
+ small synthio synthesizer. A chime is two sine notes pressed a moment apart,
9
+ each fading out on its own. The synthesizer plays only while a chime sounds:
10
+ left running beside the music, it takes enough of a small board's time to
11
+ starve its Bluetooth link. The music dips under each chime and comes back
12
+ after it, so the chime stands clear however loud the music is and whatever
13
+ the phone does with its own volume. The chime peaks at about -3 dBFS, and
14
+ with the music dipped the two together stay below full scale.
15
+ """
16
+
17
+ import math
18
+ from array import array
19
+
20
+ from multimer import after
21
+
22
+ #: The two notes of each chime, in Hz: a text rises (E5, A5) and a call
23
+ #: falls (A5, E5).
24
+ NOTES = {"text": (659.25, 880.0), "call": (880.0, 659.25)}
25
+ GAP_MS = 180 # from the first note to the second
26
+ LEVEL = 0.8 # each note's peak: the chime peaks at about -3 dBFS where they overlap
27
+ DUCK = 0.18 # the music's level under a chime, about -15 dB
28
+ DUCK_MS = 50 # the music dips over this long before the first note
29
+ HOLD_MS = 800 # from the second note to when the music comes back
30
+ RISE_MS = 300 # the music comes back over this long
31
+ ATTACK_S = 0.006 # long enough not to click
32
+ FADE_S = 0.8 # each note fades from its peak to nothing over this long
33
+ WAVE_POINTS = 256
34
+
35
+
36
+ class Chimes:
37
+ """The chimes, and the mixer that carries them beside the music."""
38
+
39
+ def __init__(self):
40
+ self.synth = None
41
+ self.mixer = None
42
+ #: The chimes asked for, oldest first: "text" or "call". The last 8.
43
+ self.played = []
44
+ self._synthio = None
45
+ self._timer = None
46
+ self._wave = None
47
+ self._silence = None
48
+ self._missing = False
49
+ self._rate = 0
50
+ self._music = None
51
+ self._ramps = False
52
+ self._sounding = False
53
+
54
+ def mix(self, rate, music=None):
55
+ """An audiosample for the speaker at *rate*: *music* (an audiosample,
56
+ or None for the chimes alone) with the chimes mixed in. On a firmware
57
+ without audiomixer or synthio it's *music* itself, and the chimes are
58
+ silent."""
59
+ self.close()
60
+ try:
61
+ import audiocore
62
+ import audiomixer
63
+ import synthio
64
+ except ImportError as e:
65
+ if not self._missing:
66
+ self._missing = True
67
+ print("btreceiver: no chimes:", e)
68
+ return music
69
+ if self._wave is None:
70
+ self._wave = array("h", [int(32767 * math.sin(2 * math.pi * i / WAVE_POINTS)) for i in range(WAVE_POINTS)])
71
+ # sustain_level 0: each note fades from its peak and frees itself.
72
+ env = synthio.Envelope(attack_time=ATTACK_S, decay_time=FADE_S, release_time=FADE_S, attack_level=1.0,
73
+ sustain_level=0.0)
74
+ synth = synthio.Synthesizer(sample_rate=rate, channel_count=2, waveform=self._wave, envelope=env)
75
+ mixer = audiomixer.Mixer(voice_count=2, sample_rate=rate, channel_count=2, bits_per_sample=16,
76
+ samples_signed=True)
77
+ if music is not None:
78
+ mixer.voice[0].play(music, loop=True)
79
+ # A level move walks over level_ramp frames; a mixer without it holds
80
+ # the move to a zero crossing, which doesn't click either.
81
+ self._ramps = hasattr(mixer, "level_ramp")
82
+ if self._ramps:
83
+ # On from the start: a voice's first level lands at once, and
84
+ # walks only once the mixer has played with a ramp set.
85
+ mixer.level_ramp = rate * DUCK_MS // 1000
86
+ # A few frames of nothing that end by themselves: playing them in the
87
+ # synthesizer's place lets its voice go quiet without stopping it.
88
+ self._silence = audiocore.RawSample(array("h", [0] * 64), sample_rate=rate, channel_count=2)
89
+ self._rate = rate
90
+ self._music = mixer.voice[0] if music is not None else None
91
+ self._sounding = False
92
+ self._synthio = synthio
93
+ self.synth = synth
94
+ self.mixer = mixer
95
+ return mixer
96
+
97
+ def play(self, kind):
98
+ """Chime *kind*, "text" or "call". False when there's no speaker to
99
+ play it on."""
100
+ self.played.append(kind)
101
+ del self.played[:-8]
102
+ synth = self.synth
103
+ if synth is None:
104
+ return False
105
+ first, second = NOTES[kind]
106
+ voice = self.mixer.voice[1]
107
+ self._cancel()
108
+
109
+ def done(t):
110
+ self._timer = None
111
+ voice.play(self._silence)
112
+ self._sounding = False
113
+ if self._music is not None:
114
+ self._level(1.0, RISE_MS)
115
+
116
+ def then(t):
117
+ synth.press(self._note(second))
118
+ self._timer = after(HOLD_MS, done)
119
+
120
+ def start(t):
121
+ if not self._sounding:
122
+ voice.play(synth, loop=True)
123
+ self._sounding = True
124
+ synth.press(self._note(first))
125
+ self._timer = after(GAP_MS, then)
126
+
127
+ if self._music is None:
128
+ start(None)
129
+ else:
130
+ self._level(DUCK, DUCK_MS)
131
+ self._timer = after(DUCK_MS, start)
132
+ return True
133
+
134
+ def close(self):
135
+ """Let the mixer and its synthesizer go, with the speaker that played
136
+ them."""
137
+ self._cancel()
138
+ self.synth = None
139
+ self.mixer = None
140
+ self._music = None
141
+
142
+ def _level(self, level, ms):
143
+ """Move the music to *level* over *ms*."""
144
+ if self._ramps:
145
+ self.mixer.level_ramp = self._rate * ms // 1000
146
+ self._music.level = level
147
+
148
+ def _note(self, hz):
149
+ return self._synthio.Note(frequency=hz, amplitude=LEVEL)
150
+
151
+ def _cancel(self):
152
+ t = self._timer
153
+ self._timer = None
154
+ if t is not None:
155
+ t.deinit()
@@ -0,0 +1,529 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """A pretend phone, for running the receiver on a computer with no radio.
5
+
6
+ `DemoSink` answers like ``btclassic.AudioSink``: the same properties, the
7
+ same ``changes()`` bits and the same transport. It pairs a few seconds after
8
+ the app opens, plays a short list of made-up tracks, moves their position
9
+ along and follows play, pause, next, previous, volume, repeat and shuffle. Its covers are
10
+ drawn by svgio and made into JPEGs by jpegio, so the app decodes them the
11
+ way it decodes a phone's. `DemoHandsFree` answers like
12
+ ``btclassic.HandsFree``: the phone's battery and signal, and its calls.
13
+
14
+ ``BTRECEIVER_DEMO`` holds it in one state, for screenshots: ``pairing``,
15
+ ``idle``, ``playing``, ``paused``, ``nocover`` (a podcast with no cover, from
16
+ a sender with no hands-free link), ``long`` (a long title, with every line
17
+ filled in, and a low battery), ``untitled`` (a computer streaming with no
18
+ track details), ``incoming`` (a call ringing), ``call`` (a call answered on
19
+ the board) or ``text`` (playing, and a text arrives). Without
20
+ it the demo runs through pairing, connecting and playing on its own, and a
21
+ text arrives a few seconds in. `DemoMessages` answers like
22
+ ``btclassic.Messages``, for those texts.
23
+ """
24
+
25
+ import os
26
+ import time
27
+
28
+ CONNECTION = 1
29
+ STREAM = 2
30
+ FORMAT = 4
31
+ TRACK = 8
32
+ PLAYING = 16
33
+ VOLUME = 32
34
+ POSITION = 64
35
+ PEER = 128
36
+ COVER = 256
37
+ SETTINGS = 1 << 26
38
+ PHONE = 512
39
+ CALL = 1024
40
+ AUDIO = 2048
41
+ NOTIFY = 1 << 20
42
+
43
+ TEXT_MS = 1500 # from the message service's link to a text, in the text state
44
+ FREE_TEXT_MS = 8000 # and when the demo runs on its own
45
+
46
+ PAIR_MS = 4000 # how long the demo waits to be paired, from the app opening
47
+ PLAY_MS = 2500 # and then before it plays
48
+
49
+ # Made-up tracks; a cover is two colours and a shape, or None for no cover.
50
+ TRACKS = (
51
+ {"title": "Neon Tides", "artist": "Halcyon Drive", "album": "Afterglow Coast", "genre": "Synthwave",
52
+ "track_number": 3, "track_count": 11, "duration": 252000, "cover": (0xFF6B3D, 0x7B2CBF, "sun")},
53
+ {"title": "Glass Orchard", "artist": "Mira Quell", "album": "Small Hours", "genre": "Indie Pop",
54
+ "track_number": 7, "track_count": 9, "duration": 218000, "cover": (0x2EC4B6, 0x0B3954, "rings")},
55
+ {"title": "Paper Satellites (Extended Mix)", "artist": "The Low Hum", "album": "Orbit Theory",
56
+ "genre": "Electronic", "track_number": 1, "track_count": 14, "duration": 365000,
57
+ "cover": (0xF2C14E, 0x1D3461, "orbit")},
58
+ {"title": "Episode 42: Signals in the Noise", "artist": "Field Notes", "album": "", "genre": "",
59
+ "track_number": None, "track_count": None, "duration": 2712000, "cover": None},
60
+ )
61
+
62
+ _SHAPES = {
63
+ "sun": '<circle cx="100" cy="112" r="58" fill="url(#g)"/>'
64
+ '<rect x="0" y="128" width="200" height="72" fill="#%06x" opacity=".92"/>'
65
+ '<g stroke="#%06x" stroke-width="3" opacity=".6"><path d="M0 146h200M0 162h200M0 178h200M0 194h200"/></g>',
66
+ "rings": '<g fill="none" stroke="url(#g)" stroke-width="10">'
67
+ '<circle cx="70" cy="80" r="44"/><circle cx="130" cy="120" r="44"/></g>'
68
+ '<circle cx="100" cy="100" r="12" fill="#%06x"/><circle cx="100" cy="100" r="5" fill="#%06x"/>',
69
+ "orbit": '<g fill="none" stroke="#%06x" stroke-width="2" opacity=".75">'
70
+ '<ellipse cx="100" cy="100" rx="78" ry="30" transform="rotate(-24 100 100)"/>'
71
+ '<ellipse cx="100" cy="100" rx="78" ry="30" transform="rotate(36 100 100)"/></g>'
72
+ '<circle cx="100" cy="100" r="30" fill="url(#g)"/><circle cx="160" cy="60" r="8" fill="#%06x"/>',
73
+ }
74
+
75
+
76
+ def cover_jpeg(spec):
77
+ """A 200 by 200 JPEG of *spec*, ``(light, dark, shape)``."""
78
+ import jpegio
79
+ import svgio
80
+
81
+ light, dark, shape = spec
82
+ svg = (
83
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">'
84
+ '<defs><linearGradient id="g" x1="0" y1="0" x2="0" y2="1">'
85
+ '<stop offset="0" stop-color="#%06x"/><stop offset="1" stop-color="#%06x"/></linearGradient>'
86
+ '<linearGradient id="b" x1="0" y1="0" x2="1" y2="1">'
87
+ '<stop offset="0" stop-color="#%06x"/><stop offset="1" stop-color="#0b0b12"/></linearGradient></defs>'
88
+ '<rect width="200" height="200" fill="url(#b)"/>' % (light, dark, dark)
89
+ + _SHAPES[shape] % (dark, light)
90
+ + "</svg>"
91
+ )
92
+ dec = svgio.SvgDecoder()
93
+ w, h = dec.open(svg.encode(), width=200)
94
+ px = bytearray(w * h * 2)
95
+ dec.decode(px, bytearray(w * h))
96
+ return jpegio.JpegEncoder(quality=88).encode(px, w, h)
97
+
98
+
99
+ def _env(name):
100
+ fn = getattr(os, "getenv", None)
101
+ return fn(name) if fn is not None else None
102
+
103
+
104
+ def _now():
105
+ return time.monotonic() if hasattr(time, "monotonic") else time.ticks_ms() / 1000
106
+
107
+
108
+ class DemoRadio:
109
+ """Stands in for ``btclassic.Bluetooth``: its name, and nothing else."""
110
+
111
+ def __init__(self, name="pydevices-speaker"):
112
+ self._name = name
113
+
114
+ def config(self, *names, **settings):
115
+ if names == ("name",):
116
+ return self._name
117
+ if names == ("pin",):
118
+ return "0000"
119
+ if "name" in settings:
120
+ self._name = settings["name"]
121
+ return None
122
+
123
+ def active(self, active=None):
124
+ return True
125
+
126
+
127
+ class DemoSink:
128
+ """A pretend ``btclassic.AudioSink`` with a phone on the other end."""
129
+
130
+ def __init__(self, state=None):
131
+ self.state = state if state is not None else _env("BTRECEIVER_DEMO") or None
132
+ self._t0 = _now()
133
+ self._bits = 0
134
+ self._covers = {}
135
+ self.connected = False
136
+ self.streaming = False
137
+ self.playing = False
138
+ self.status = None
139
+ self.sample_rate = 0
140
+ self.channel_count = 2
141
+ self.bits_per_sample = 16
142
+ self._volume = 92
143
+ self.peer = None
144
+ self.peer_name = ""
145
+ self._index = 0
146
+ self._pos = 0
147
+ self._pos_at = self._t0
148
+ self._rate = 1 # how fast the position moves: 8 or -8 while seeking
149
+ # The phone's battery, as AVRCP reports it; the states show each one.
150
+ self.battery = {"idle": "external", "paused": "full", "long": "warning", "nocover": None,
151
+ "untitled": None}.get(self.state, "normal")
152
+ # The player's modes; the long title's phone doesn't report them.
153
+ self.repeat, self.shuffle = {"playing": ("off", "all"), "paused": ("single", "off"),
154
+ "long": (None, None)}.get(self.state, ("off", "off"))
155
+ self._clear_track()
156
+ st = self.state
157
+ if st in ("idle", "playing", "paused", "nocover", "long", "incoming", "call", "text"):
158
+ self._connect()
159
+ if st != "idle":
160
+ self._load({"nocover": 3, "long": 2}.get(st, 0))
161
+ self._pos = 1385000 if st == "nocover" else 102000
162
+ self._set_playing(st != "paused")
163
+ self.sample_rate = 44100 # it streamed before it paused
164
+ elif st == "untitled":
165
+ # A computer: audio flows, but it sends no title and no play
166
+ # status.
167
+ self._connect()
168
+ self.peer_name = "Desktop PC"
169
+ self.streaming = True
170
+ self.sample_rate = 48000
171
+
172
+ # -- the properties btclassic has -----------------------------------
173
+
174
+ @property
175
+ def volume(self):
176
+ return self._volume
177
+
178
+ @volume.setter
179
+ def volume(self, v):
180
+ self._volume = max(0, min(127, int(v)))
181
+ self._bits |= VOLUME
182
+
183
+ @property
184
+ def position(self):
185
+ if not self.connected or self.duration is None:
186
+ return None
187
+ pos = self._pos
188
+ if self.playing or self._rate != 1:
189
+ pos += int((_now() - self._pos_at) * 1000 * self._rate)
190
+ return max(0, min(pos, self.duration))
191
+
192
+ @property
193
+ def cover_size(self):
194
+ c = self._cover()
195
+ return len(c) if c else 0
196
+
197
+ @property
198
+ def cover(self):
199
+ return self._cover()
200
+
201
+ def cover_into(self, buf):
202
+ c = self._cover()
203
+ if not c:
204
+ return 0
205
+ if len(buf) < len(c):
206
+ raise ValueError("buffer smaller than the cover")
207
+ buf[: len(c)] = c
208
+ return len(c)
209
+
210
+ def changes(self):
211
+ self._advance()
212
+ bits, self._bits = self._bits, 0
213
+ return bits
214
+
215
+ def levels(self):
216
+ return (12000, 11000) if self.playing else (0, 0)
217
+
218
+ def link(self):
219
+ if not self.connected:
220
+ return {"address": None, "name": ""}
221
+ return {"address": self.peer, "name": self.peer_name, "rssi_delta": 0, "codec": "SBC",
222
+ "sample_rate": self.sample_rate, "channels": 2, "channel_mode": "joint stereo",
223
+ "battery": self.battery, "cover_art": True}
224
+
225
+ def stats(self):
226
+ return {}
227
+
228
+ # -- transport ------------------------------------------------------
229
+
230
+ def play(self):
231
+ if self.connected:
232
+ if self.title == "":
233
+ self._load(self._index)
234
+ self._set_playing(True)
235
+
236
+ def pause(self):
237
+ if self.connected:
238
+ self._set_playing(False)
239
+
240
+ def stop(self):
241
+ self.pause()
242
+
243
+ def next(self):
244
+ if self.connected:
245
+ self._load((self._index + 1) % len(TRACKS))
246
+ self._set_playing(True)
247
+
248
+ def previous(self):
249
+ if self.connected:
250
+ if (self.position or 0) > 3000:
251
+ self._pos, self._pos_at = 0, _now()
252
+ self._bits |= POSITION
253
+ else:
254
+ self._load((self._index - 1) % len(TRACKS))
255
+ self._set_playing(True)
256
+
257
+ def seek(self, direction):
258
+ if not self.connected or not self.title:
259
+ return
260
+ self._pos = self.position or 0
261
+ self._pos_at = _now()
262
+ self._rate = 8 * direction if direction else 1
263
+ self.status = {1: "seeking forward", -1: "seeking back"}.get(direction, "playing" if self.playing else "paused")
264
+ self._bits |= PLAYING | POSITION
265
+
266
+ def refresh(self):
267
+ self._bits |= POSITION
268
+
269
+ def setting(self, name, value):
270
+ if name not in ("equalizer", "repeat", "shuffle", "scan"):
271
+ raise ValueError("setting is equalizer, repeat, shuffle or scan")
272
+ if self.connected and name in ("repeat", "shuffle") and getattr(self, name) is not None:
273
+ setattr(self, name, value)
274
+ self._bits |= SETTINGS
275
+
276
+ def disconnect(self):
277
+ self.connected = False
278
+ self._clear_track()
279
+ self._bits |= CONNECTION | TRACK | COVER | PEER | PLAYING
280
+
281
+ def deinit(self):
282
+ self.disconnect()
283
+
284
+ # -- the pretend phone ----------------------------------------------
285
+
286
+ def _advance(self):
287
+ if self.state is not None:
288
+ return
289
+ age = (_now() - self._t0) * 1000
290
+ if not self.connected and age >= PAIR_MS:
291
+ self._connect()
292
+ elif self.connected and not self.title and age >= PAIR_MS + PLAY_MS:
293
+ self._load(0)
294
+ self._set_playing(True)
295
+ elif self.playing and self.duration and self.position >= self.duration:
296
+ self.next()
297
+
298
+ def _connect(self):
299
+ self.connected = True
300
+ self.peer = b"\x5c\xcb\x99\x1a\x2e\x07"
301
+ self.peer_name = "Galaxy S21"
302
+ self._bits |= CONNECTION | PEER
303
+
304
+ def _clear_track(self):
305
+ self.title = ""
306
+ self.artist = ""
307
+ self.album = ""
308
+ self.genre = ""
309
+ self.track_number = None
310
+ self.track_count = None
311
+ self.duration = None
312
+ self.playing = False
313
+ self.streaming = False
314
+ self.status = None
315
+
316
+ def _load(self, i):
317
+ self._index = i
318
+ t = TRACKS[i]
319
+ for k in ("title", "artist", "album", "genre", "track_number", "track_count", "duration"):
320
+ setattr(self, k, t[k])
321
+ self._pos, self._pos_at = 0, _now()
322
+ self._bits |= TRACK | COVER | POSITION
323
+
324
+ def _set_playing(self, on):
325
+ self._pos = self.position or 0
326
+ self._pos_at = _now()
327
+ self.playing = on
328
+ self.status = "playing" if on else "paused"
329
+ if on and not self.streaming:
330
+ self.streaming = True
331
+ self.sample_rate = 44100
332
+ self._bits |= STREAM | FORMAT
333
+ self._bits |= PLAYING | POSITION
334
+
335
+ def _cover(self):
336
+ if not self.connected or not self.title:
337
+ return None
338
+ spec = TRACKS[self._index]["cover"]
339
+ if spec is None:
340
+ return None
341
+ c = self._covers.get(self._index)
342
+ if c is None:
343
+ c = self._covers[self._index] = cover_jpeg(spec)
344
+ return c
345
+
346
+
347
+ # The phone's battery and signal, 0 to 5, in each held state. A sender with no
348
+ # hands-free link, such as a computer, reports neither.
349
+ _PHONE = {"idle": (3, 4), "playing": (4, 3), "paused": (5, 4), "long": (1, 1), "incoming": (4, 3), "call": (4, 3)}
350
+ CALLER = "+1 206 555 0143"
351
+
352
+
353
+ class DemoHandsFree:
354
+ """A pretend ``btclassic.HandsFree`` that follows *sink*'s phone.
355
+
356
+ It links when the sink connects, reports a battery and a signal, and in
357
+ the ``incoming`` and ``call`` states has a call. The phone pauses its
358
+ music for a call and plays it again after, as a real one does.
359
+ """
360
+
361
+ def __init__(self, sink, state=None, call_audio=True):
362
+ self.sink = sink
363
+ # False is a board whose Bluetooth can't carry a call's audio: the
364
+ # call stays on the phone.
365
+ self.call_audio = call_audio
366
+ self.state = state if state is not None else sink.state
367
+ self._bits = 0
368
+ self.connected = False
369
+ self.peer = None
370
+ self.battery = None
371
+ self.signal = None
372
+ self.service = None
373
+ self.operator = ""
374
+ self.call = None
375
+ self.caller = ""
376
+ self.ringing = False
377
+ # True is a phone that sends its own ringtone as the call's audio.
378
+ self.in_band_ring = False
379
+ self.audio = False
380
+ self.sample_rate = 0
381
+ self.mic = None # what uplink() was last given
382
+ self._resume = False
383
+ self._followed = False # the sink's connection, as last seen
384
+ if self.state == "incoming":
385
+ self._follow()
386
+ self._ring()
387
+ elif self.state == "call":
388
+ self._follow()
389
+ self._ring()
390
+ self.answer()
391
+
392
+ def changes(self):
393
+ self._follow()
394
+ bits, self._bits = self._bits, 0
395
+ return bits
396
+
397
+ def answer(self):
398
+ if self.call == "incoming":
399
+ self.call = "active"
400
+ self.ringing = False
401
+ self._audio(True)
402
+ self._bits |= CALL
403
+
404
+ def hangup(self):
405
+ if self.call in ("incoming", "dialing", "alerting", "active", "held"):
406
+ self.call = "idle"
407
+ self.ringing = False
408
+ self._audio(False)
409
+ self._bits |= CALL
410
+ if self._resume:
411
+ self._resume = False
412
+ self.sink.play()
413
+
414
+ def connect_audio(self):
415
+ if not self.call_audio:
416
+ raise OSError("this board doesn't take calls' audio")
417
+ if self.call in ("active", "dialing", "alerting"):
418
+ self._audio(True)
419
+
420
+ def disconnect_audio(self):
421
+ self._audio(False)
422
+
423
+ def uplink(self, mic):
424
+ self.mic = mic
425
+
426
+ def stats(self):
427
+ return {"sent": 0, "unsent": 0}
428
+
429
+ def deinit(self):
430
+ self._audio(False)
431
+ self.connected = False
432
+ self.call = None
433
+
434
+ # -- the pretend phone ----------------------------------------------
435
+
436
+ def _follow(self):
437
+ # The hands-free link comes up and goes down with the music's.
438
+ if self.sink.connected == self._followed:
439
+ return
440
+ self._followed = self.sink.connected
441
+ # A computer has no hands-free side: the nocover state's sender.
442
+ self.connected = self._followed and self.state != "nocover"
443
+ level = _PHONE.get(self.state, (4, 3)) if self.connected else (None, None)
444
+ self.battery, self.signal = level
445
+ self.service = None if level[1] is None else True
446
+ self.operator = "Northwind" if self.connected else ""
447
+ self.peer = self.sink.peer if self.connected else None
448
+ self.call = "idle" if self.connected else None
449
+ self._bits |= CONNECTION | PHONE | CALL
450
+
451
+ def _ring(self):
452
+ self.call = "incoming"
453
+ self.ringing = True
454
+ self.caller = CALLER
455
+ self._bits |= CALL
456
+ if self.sink.playing:
457
+ self._resume = True
458
+ self.sink.pause()
459
+
460
+ def _audio(self, on):
461
+ if on == self.audio or (on and not self.call_audio):
462
+ return
463
+ self.audio = on
464
+ self.sample_rate = 16000 if on else 0
465
+ self._bits |= AUDIO
466
+
467
+
468
+ class DemoMessages:
469
+ """A pretend ``btclassic.Messages`` on *sink*'s phone.
470
+
471
+ It connects when asked while the sink is connected, except to a sender
472
+ with no message service (the nocover state's computer), and once asked
473
+ to report changes, reports a new text: after `TEXT_MS` in the ``text``
474
+ state, after `FREE_TEXT_MS` when the demo runs on its own, and never in
475
+ the other held states. `arrive` makes one at any time.
476
+ """
477
+
478
+ def __init__(self, sink, state=None):
479
+ self.sink = sink
480
+ self.state = state if state is not None else sink.state
481
+ self.connected = False
482
+ self.notifying = False
483
+ self._bits = 0
484
+ self._events = []
485
+ self._due = None
486
+
487
+ @property
488
+ def events(self):
489
+ return len(self._events)
490
+
491
+ def connect(self, addr=None, *, instance=None, timeout=30000):
492
+ if self.sink.connected and self.state != "nocover":
493
+ self.connected = True
494
+ self._bits |= CONNECTION
495
+
496
+ def notify(self, on=True):
497
+ if not self.connected:
498
+ raise OSError("not connected")
499
+ self.notifying = on
500
+ wait = {"text": TEXT_MS, None: FREE_TEXT_MS}.get(self.state)
501
+ self._due = _now() + wait / 1000 if on and wait is not None else None
502
+
503
+ def arrive(self, folder="telecom/msg/inbox", sender="Sam"):
504
+ """A text arrives, or, with *folder* "telecom/msg/sent", the phone
505
+ sent one."""
506
+ self._events.append({"type": "NewMessage", "handle": "%04d" % len(self._events), "folder": folder,
507
+ "msg_type": "SMS_GSM", "sender_name": sender})
508
+ self._bits |= NOTIFY
509
+
510
+ def event(self):
511
+ return self._events.pop(0) if self._events else None
512
+
513
+ def changes(self):
514
+ if self._due is not None and _now() >= self._due:
515
+ self._due = None
516
+ self.arrive()
517
+ if self.connected and not self.sink.connected:
518
+ self.connected = False
519
+ self.notifying = False
520
+ self._bits |= CONNECTION
521
+ bits, self._bits = self._bits, 0
522
+ return bits
523
+
524
+ def disconnect(self):
525
+ self.connected = False
526
+ self._bits |= CONNECTION
527
+
528
+ def deinit(self):
529
+ self.disconnect()