slm_open_api_package 0.2.11__tar.gz → 0.2.13__tar.gz

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 (39) hide show
  1. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/PKG-INFO +3 -1
  2. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/README.md +2 -0
  3. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/pyproject.toml +1 -1
  4. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/pyproject.toml.orig +1 -1
  5. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/fast_log_example.py +3 -3
  6. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/data_handler.py +11 -2
  7. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/stream_handlers.py +30 -4
  8. slm_open_api_package-0.2.13/src/slm_api/leq.py +73 -0
  9. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/CPB_example.py +0 -0
  10. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/__init__.py +0 -0
  11. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/enums/fast_log_intervals_enum.py +0 -0
  12. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/enums/sequence_id_enum.py +0 -0
  13. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/example_multi.py +0 -0
  14. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/flac_example.py +0 -0
  15. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/FigureHandler.py +0 -0
  16. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/Leq.py +0 -0
  17. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/__init__.py +0 -0
  18. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/buffer.py +0 -0
  19. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/fft.py +0 -0
  20. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/flac_stream_2_samples.py +0 -0
  21. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/https_requests.py +0 -0
  22. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/measurment_handler.py +0 -0
  23. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/sequence_handler.py +0 -0
  24. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/stream_handler.py +0 -0
  25. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/tmpFileStateMachine.py +0 -0
  26. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/websocket_handler.py +0 -0
  27. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/webxi_helper_functions.py +0 -0
  28. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/helpers/webxi_root.pem +0 -0
  29. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/mp3_example.py +0 -0
  30. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/multi_device_flac.py +0 -0
  31. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/multi_instrument_example.py +0 -0
  32. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/plot_saved_data_example.py +0 -0
  33. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/screen_record_example.py +0 -0
  34. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/webxi/README.md +0 -0
  35. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/webxi/__init__.py +0 -0
  36. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/webxi/webxi-header.ksy +0 -0
  37. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/webxi/webxi-stream.ksy +0 -0
  38. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/webxi/webxi_header.py +0 -0
  39. {slm_open_api_package-0.2.11 → slm_open_api_package-0.2.13}/src/slm_api/webxi/webxi_stream.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: slm_open_api_package
3
- Version: 0.2.11
3
+ Version: 0.2.13
4
4
  Summary: Add your description here
5
5
  Author: Sofus Vingaard
6
6
  Author-email: Sofus Vingaard <sofus.krebsvingaard@hbkworld.com>
@@ -161,6 +161,8 @@ except KeyboardInterrupt:
161
161
 
162
162
  The matching `flac` form uses `SequenceIdEnums.FLACSignal.value` and `mode="flac"`. Audio setup is enabled by the handler. The SLM can take a few seconds to initialize audio recording when its quality setting changes.
163
163
 
164
+ For GUI plots, check the read-only `streamer.audio_ready` property in the plot's timer callback before reading `DataBuffer`. It becomes `True` after the first nonempty decoded audio has been written to the buffer, without requiring a custom `DataHandler` or event. It resets when the streaming session starts, when a stop is requested, and when the session exits. This is a startup gate, not a full-window availability check or a detector for stalled streams. Keep running the GUI event loop while waiting for audio.
165
+
164
166
  WAV saving appends decoded mono samples as recording happens, updating the file header after each chunk so the file is readable before shutdown. Saving happens after decoding and calibration to pascals. Samples are divided by the fixed full-scale pressure `calibrationFactor * (2**23 - 1)` and saved as `PCM_24`, not peak-normalized per chunk. The filename includes `_fullscale-<value>Pa.wav`. To recover pressure values, multiply floating-point samples read from the WAV by that full-scale value (within 24-bit quantization accuracy). Samples outside the calibrated range produce a clipping warning; this scaling cannot repair upstream clipping. MP3 uses the decoded sample rate; the current FLAC decoder uses 65536 Hz. JSON and CSV saving are rejected for audio modes, and WAV saving is rejected for measurement modes. Run `uv sync` to install the `soundfile` dependency used for progressive WAV writing.
165
167
 
166
168
  To attach the handler directly, use `WavDataHandler("saved_data/audio", sample_rate=65536, full_scale_pa=calibrationFactor * (2**23 - 1))` from `slm_api.helpers.data_handler`, using the device's actual calibration factor. You can instead supply `sample_rate` and `full_scale_pa` to `handle()`; the stream supplies both automatically. Call `close()` when finished. Use a separate WAV handler and output path for each device. Existing float32 pressure WAVs are unchanged; this format applies to new recordings.
@@ -139,6 +139,8 @@ except KeyboardInterrupt:
139
139
 
140
140
  The matching `flac` form uses `SequenceIdEnums.FLACSignal.value` and `mode="flac"`. Audio setup is enabled by the handler. The SLM can take a few seconds to initialize audio recording when its quality setting changes.
141
141
 
142
+ For GUI plots, check the read-only `streamer.audio_ready` property in the plot's timer callback before reading `DataBuffer`. It becomes `True` after the first nonempty decoded audio has been written to the buffer, without requiring a custom `DataHandler` or event. It resets when the streaming session starts, when a stop is requested, and when the session exits. This is a startup gate, not a full-window availability check or a detector for stalled streams. Keep running the GUI event loop while waiting for audio.
143
+
142
144
  WAV saving appends decoded mono samples as recording happens, updating the file header after each chunk so the file is readable before shutdown. Saving happens after decoding and calibration to pascals. Samples are divided by the fixed full-scale pressure `calibrationFactor * (2**23 - 1)` and saved as `PCM_24`, not peak-normalized per chunk. The filename includes `_fullscale-<value>Pa.wav`. To recover pressure values, multiply floating-point samples read from the WAV by that full-scale value (within 24-bit quantization accuracy). Samples outside the calibrated range produce a clipping warning; this scaling cannot repair upstream clipping. MP3 uses the decoded sample rate; the current FLAC decoder uses 65536 Hz. JSON and CSV saving are rejected for audio modes, and WAV saving is rejected for measurement modes. Run `uv sync` to install the `soundfile` dependency used for progressive WAV writing.
143
145
 
144
146
  To attach the handler directly, use `WavDataHandler("saved_data/audio", sample_rate=65536, full_scale_pa=calibrationFactor * (2**23 - 1))` from `slm_api.helpers.data_handler`, using the device's actual calibration factor. You can instead supply `sample_rate` and `full_scale_pa` to `handle()`; the stream supplies both automatically. Call `close()` when finished. Use a separate WAV handler and output path for each device. Existing float32 pressure WAVs are unchanged; this format applies to new recordings.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "slm_open_api_package"
3
- version = "0.2.11"
3
+ version = "0.2.13"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "slm_open_api_package"
3
- version = "0.2.11"
3
+ version = "0.2.13"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -53,8 +53,8 @@ FAST_LOGGING_INTERVAL_MS = FastLogInterval.interval_1000ms.value
53
53
  SEQUENCE_ID = SequenceIdEnums.FastLAF.value
54
54
 
55
55
  class PrintDataHandler(DataHandler):
56
- def handle(self, timestamp, name, local, value, unit, **data):
57
- print(f"{timestamp} {name} ({local}): {value:7.2f} {unit}")
56
+ def handle(self, timestamp, name, local, value, **data):
57
+ print(f"{timestamp} {name} ({local}): {value:7.2f}")
58
58
 
59
59
 
60
60
 
@@ -170,7 +170,7 @@ if __name__ == "__main__":
170
170
  sequenceID = SEQUENCE_ID,
171
171
  mode="fast_logging",
172
172
  fast_logging_interval=FAST_LOGGING_INTERVAL_MS,
173
- saving="json",
173
+ saving="csv",
174
174
  saving_path=f"./saved_data/{filename}",
175
175
  time=True)
176
176
  streamer.setDataHandler(PrintDataHandler())
@@ -53,7 +53,10 @@ class _BufferedFileDataHandler(DataHandler):
53
53
  if self._closed:
54
54
  return
55
55
  if self._write_error is not None:
56
- raise RuntimeError("Background snapshot saving failed") from self._write_error
56
+ raise RuntimeError(
57
+ f"Background snapshot saving failed for {self._path}: "
58
+ f"{type(self._write_error).__name__}: {self._write_error}"
59
+ ) from self._write_error
57
60
  if "values" in data:
58
61
  data["values"] = [float(v) for v in data["values"]]
59
62
  elif "samples" in data:
@@ -110,8 +113,14 @@ class _BufferedFileDataHandler(DataHandler):
110
113
  self._closed = True
111
114
  self._stop_event.set()
112
115
  worker = self._worker
116
+
113
117
  if worker is not None:
114
- worker.join()
118
+ worker.join(timeout=5.0)
119
+ if worker.is_alive():
120
+ raise TimeoutError(
121
+ f"Snapshot writer did not stop: {self._path}"
122
+ )
123
+
115
124
  self._flush_snapshot()
116
125
 
117
126
  def _group_records(self, records=None):
@@ -221,6 +221,7 @@ class WebXiStreamHandler(stream.StreamHandler):
221
221
  self.time = time
222
222
  self.screen_record = self.mode == "screen_record"
223
223
  self.image_bytes = None
224
+ self._audio_ready = threading.Event()
224
225
  self.fast_logging_interval = fast_logging_interval
225
226
  self.max_input = 15.6263 / np.sqrt(2)
226
227
  self._saving_handler = None
@@ -237,6 +238,22 @@ class WebXiStreamHandler(stream.StreamHandler):
237
238
  self.tmpFileSM = TmpfileStateMachine()
238
239
  self.streamInit()
239
240
 
241
+ @property
242
+ def audio_ready(self):
243
+ """Whether nonempty decoded audio has reached the buffer in this session."""
244
+ return self._audio_ready.is_set()
245
+
246
+ async def runStream(self):
247
+ self._audio_ready.clear()
248
+ try:
249
+ await super().runStream()
250
+ finally:
251
+ self._audio_ready.clear()
252
+
253
+ def request_stop(self):
254
+ super().request_stop()
255
+ self._audio_ready.clear()
256
+
240
257
  def setDataHandler(self, data_handler):
241
258
  """Attach a DataHandler (see data_handler.py) before calling startStream().
242
259
  Without one, WebXiStreamHandler stays silent and does nothing with decoded values.
@@ -263,6 +280,10 @@ class WebXiStreamHandler(stream.StreamHandler):
263
280
  raise ValueError(
264
281
  "FLAC and MP3 streams must be saved as WAV, not JSON or CSV."
265
282
  )
283
+ if (self.mp3 or self.flac) and self.time == True:
284
+ raise ValueError(
285
+ "Time is not supported for flac and mp3."
286
+ )
266
287
  if self.mp3 or self.flac:
267
288
  quality_setting = "AudioRecordingListenQuality" if self.mp3 else "AudioRecordingAnalysisQuality"
268
289
  response = https_requests.put(self.host, f"/WebXi/Applications/SLM/setup/{quality_setting}", json=1)
@@ -360,9 +381,10 @@ class WebXiStreamHandler(stream.StreamHandler):
360
381
  def _msg_func_laeq(self, message):
361
382
  package = webxi_stream.WebxiStream.from_bytes(message)
362
383
  if package.header.message_type == webxi_stream.WebxiStream.Header.EMessageType.e_sequence_data:
363
- value = package.content.sequence_blocks[0].values
364
- leq_value = stream.data_type_conv(self.data_type, value, None) / 100
365
- self._handle_data(timestamp=self._time_prefix(), value=leq_value)
384
+ for block in package.content.sequence_blocks:
385
+ if block.sequence_id == self.ID:
386
+ leq_value = stream.data_type_conv(self.data_type, block.values, None) / 100
387
+ self._handle_data(timestamp=self._time_prefix(), name=self.sequence["Name"], value=leq_value)
366
388
 
367
389
  def _msg_func_cpb(self, message):
368
390
  package = webxi_stream.WebxiStream.from_bytes(message)
@@ -420,7 +442,7 @@ class WebXiStreamHandler(stream.StreamHandler):
420
442
  unit = self.sequence.get("Unit", "dB re 20uPa")
421
443
  for sample_index, value in enumerate(samples):
422
444
  timestamp = self._time_prefix(start + sample_index * self.fast_logging_interval_s)
423
- self._handle_data(timestamp=timestamp, name=name, local=local, value=value, unit=unit)
445
+ self._handle_data(timestamp=timestamp, name=name, local=local, value=value)
424
446
 
425
447
  def _msg_func_screen_record(self, message):
426
448
  package = webxi_stream.WebxiStream.from_bytes(message)
@@ -446,7 +468,11 @@ class WebXiStreamHandler(stream.StreamHandler):
446
468
  else:
447
469
  decoded = flac2samples.decode(block, self.calibrationFactor)
448
470
  sample_rate = 2 ** 16
471
+ if len(decoded) == 0:
472
+ continue
449
473
  DataBuffer.append(decoded)
474
+ if not self._stop_requested:
475
+ self._audio_ready.set()
450
476
  self._handle_data(
451
477
  samples=decoded, sample_rate=sample_rate,
452
478
  full_scale_pa=self.calibrationFactor * (2 ** 23 - 1),
@@ -0,0 +1,73 @@
1
+ from slm_api.helpers import webxi_helper_functions as webxi_helper
2
+ from slm_api.helpers.stream_handlers import WebXiStreamHandler
3
+ from slm_api.enums.sequence_id_enum import SequenceIdEnums
4
+ from slm_api.helpers.stream_handler import delete_stream
5
+ from slm_api.helpers.measurment_handler import stop_measurement
6
+ from slm_api.helpers.data_handler import DataHandler
7
+ from slm_api.helpers.https_requests import UseHttps
8
+ from slm_api.helpers import webxi_helper_functions as webxi_helper
9
+ import datetime
10
+
11
+
12
+
13
+ """
14
+ set_host creates/reads the `slm_ip` file in the project root. If the IP changes, update or delete `slm_ip` to be prompted again.
15
+ """
16
+ host = webxi_helper.set_host(__file__)
17
+
18
+ # By default https is enabled
19
+ # If https is not needed the line below this shows how to disable it for all request calls
20
+ UseHttps(use_https=False)
21
+
22
+
23
+ # Used to create file with a custom name and attach the current date and time to it.
24
+ filename = f'name-of-file_{datetime.datetime.now().strftime("%H%M_%m%d%Y")}'
25
+
26
+
27
+ # Setup what sequence to stream on
28
+ sequenceId = SequenceIdEnums.LAeq.value
29
+ sequenceName = SequenceIdEnums.LAeq.name
30
+
31
+ class PrintHandler(DataHandler):
32
+ """Handler to control what gets printed. It must subclass DataHandler and
33
+ implement a handle() method. If unsure what data is available, print data
34
+ itself to see it in the terminal, e.g.:
35
+ def handle(self, **data):
36
+ print(data)
37
+ """
38
+ def handle(self, value, **data):
39
+ print(f"{sequenceName}: " + "%.1f" % value)
40
+
41
+
42
+
43
+
44
+ if __name__ == "__main__":
45
+ # turns off all BB freq weights to prevent interference
46
+ webxi_helper.turn_off_bb_freq_weight(host)
47
+
48
+ # turns on the wanted BB freq weights for this example
49
+ webxi_helper.turn_on_bb_freq_weight(host, ['A'])
50
+
51
+ # sets the sequences to true.
52
+ webxi_helper.turn_on_bb_leq(host, ['LAeq'])
53
+
54
+ streamer = None
55
+ try:
56
+ # WebXiStreamHandler takes several parameters to control what data is streamed:
57
+ # host - needed to connect to the device
58
+ # sequenceID - an enum selecting which sequence to listen on
59
+ # leq_window_sec - moving average window length in seconds (default 10 if not specified)
60
+ # saving - "csv", "json", or "pickle"; the format to save data as
61
+ # saving_path - the file path to save the data to
62
+ # mode - selects which mode to use. leq is the default mode.
63
+ streamer = WebXiStreamHandler(host, sequenceID=sequenceId, mode="leq", time=True, saving="json", saving_path=f"./saved_data/{filename}")
64
+ # To print incoming data, call setDataHandler() with an instance of your own
65
+ # DataHandler subclass (see the PrintHandler class above for an example).
66
+ streamer.setDataHandler(PrintHandler())
67
+ # Start the stream
68
+ streamer.startStream()
69
+ except KeyboardInterrupt:
70
+ if streamer is not None:
71
+ # stop measurements, closes the stream and saves the data if saving is enabled.
72
+ streamer.stopStream()
73
+ print("User exited the program")