MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioBackendService.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @brief Backend audio subsystem service interface.
7 *
8 * Registered into BackendRegistry by AudioSubsystem during initialize().
9 * Follows the same pattern as NetworkService, BufferService, DisplayService,
10 * and InputService: plain struct of std::function members, natural C++ types
11 * only, no engine-internal pointers.
12 *
13 * Two consumption models:
14 *
15 * Polling: call get_output_snapshot() from any non-RT thread to read the
16 * last committed interleaved output buffer.
17 *
18 * Per-cycle notification: call register_output_observer() with a callback.
19 * The callback fires on a dedicated notification thread (not the RT audio
20 * thread) once per output cycle. Any number of observers may be registered
21 * at any time. Each receives a unique id for later unregistration.
22 * The callback must not block; post to a lock-free queue if further work
23 * is needed.
24 *
25 * @code
26 * auto* svc = BackendRegistry::instance().get_service<AudioBackendService>();
27 * if (!svc) return;
28 *
29 * // Polling:
30 * auto snap = svc->get_output_snapshot();
31 *
32 * // Per-cycle observer:
33 * auto id = svc->register_output_observer([](const double* data, uint32_t total_samples) {
34 * // runs on notification thread, not RT thread
35 * });
36 *
37 * // Later:
38 * svc->unregister_output_observer(id);
39 * @endcode
40 */
41struct MAYAFLUX_API AudioBackendService {
42
43 /**
44 * @brief Returns a span over the last committed interleaved output buffer.
45 *
46 * Lock-free. Safe from any non-RT thread. The underlying pointer is
47 * engine-owned and valid between the last output cycle and the next.
48 * Copy the data if it is needed beyond the current cycle.
49 *
50 * Returns an empty span if no output cycle has completed yet.
51 */
52 std::function<std::span<const double>()> get_output_snapshot;
53
54 /**
55 * @brief Register a per-cycle output observer.
56 *
57 * The observer fires on the dedicated notification thread once per
58 * output cycle. May be called from any thread at any time.
59 *
60 * @param observer Callable receiving (interleaved data ptr, total samples).
61 * @return Opaque id for use with unregister_output_observer.
62 */
63 std::function<uint32_t(std::function<void(const double*, uint32_t)>)> register_output_observer;
64
65 /**
66 * @brief Unregister a previously registered observer.
67 *
68 * Safe to call from any thread at any time, including from within
69 * the observer callback itself.
70 *
71 * @param id Id returned by register_output_observer.
72 */
73 std::function<void(uint32_t)> unregister_output_observer;
74};
75
76} // namespace MayaFlux::Registry::Service
std::function< uint32_t(std::function< void(const double *, uint32_t)>)> register_output_observer
Register a per-cycle output observer.
std::function< void(uint32_t)> unregister_output_observer
Unregister a previously registered observer.
std::function< std::span< const double >()> get_output_snapshot
Returns a span over the last committed interleaved output buffer.
Backend audio subsystem service interface.