Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
driver.hpp
Go to the documentation of this file.
1/**
2 * @file driver.hpp
3 * @brief Audio driver interface - canonical types and convenience functions
4 * @ingroup hal
5 *
6 * This header provides the portable audio API for Sound Byte Libs.
7 *
8 * ## Audio Driver Contract
9 *
10 * Drivers implement the following static interface:
11 * @code
12 * static void init(); // configure with defaults
13 * static void init(const AudioConfig&); // configure with custom settings
14 * static void set_callback(AudioCallback); // register the processing function
15 * static void start(); // begin streaming
16 * static void stop(); // stop streaming
17 * @endcode
18 *
19 * Before each block's callback the driver stamps the block with the system
20 * timer's count (FrameCount::stamp), and after it adds the block's frames to
21 * the frame count (FrameCount::advance); see frame_count.hpp.
22 *
23 * ## Convenience Functions
24 *
25 * @code
26 * sbl::audio::start<sbl::driver::Sai>(my_callback);
27 * sbl::audio::stop<sbl::driver::Sai>();
28 * @endcode
29 *
30 * Usage in application:
31 * @code
32 * #include <sbl/hw/hal/audio/driver.hpp>
33 * #include <sbl/hw/driver/sai.hpp>
34 *
35 * sbl::audio::start<sbl::driver::Sai>(audio_callback);
36 * @endcode
37 */
38
39#ifndef SBL_HW_HAL_AUDIO_DRIVER_HPP_
40#define SBL_HW_HAL_AUDIO_DRIVER_HPP_
41
42#include "types.hpp"
43
44namespace sbl {
45
46namespace audio {
48using AudioCallback = sbl::hal::audio::AudioCallback;
49
50 /**
51 * @brief Configure audio with defaults, set callback, and start streaming
52 *
53 * @tparam Driver Audio driver type (e.g., sbl::driver::Sai)
54 * @param cb Audio processing callback (called from ISR context)
55 *
56 * @context Not ISR-safe: init-time only (configures audio hardware + DMA)
57 */
58template<typename Driver>
59inline void start(AudioCallback cb) {
60 Driver::init();
61 Driver::set_callback(cb);
62 Driver::start();
63}
64
65 /**
66 * @brief Configure with custom settings, set callback, and start streaming
67 *
68 * @tparam Driver Audio driver type (e.g., sbl::driver::Sai)
69 * @param config Audio configuration (sample rate, bit depth, block size)
70 * @param cb Audio processing callback (called from ISR context)
71 *
72 * @context Not ISR-safe: init-time only (configures audio hardware + DMA)
73 */
74template<typename Driver>
75inline void start(const AudioConfig& config, AudioCallback cb) {
76 Driver::init(config);
77 Driver::set_callback(cb);
78 Driver::start();
79}
80
81 /**
82 * @brief Stop audio streaming
83 *
84 * @tparam Driver Audio driver type (e.g., sbl::driver::Sai)
85 *
86 * @context Not ISR-safe: tears down audio streaming hardware
87 */
88template<typename Driver>
89inline void stop() {
90 Driver::stop();
91}
92
93} // namespace audio
94
95} // namespace sbl
96
97#endif // SBL_HW_HAL_AUDIO_DRIVER_HPP_
void start(AudioCallback cb)
Configure audio with defaults, set callback, and start streaming.
Definition driver.hpp:59
void stop()
Stop audio streaming.
Definition driver.hpp:89
void(*)(int32_t *tx_buf, const int32_t *rx_buf, uint16_t frames) AudioCallback
Audio processing callback (called from ISR context)
Definition types.hpp:60
Root namespace for all Sound Byte Libs code.
MIDI message types.
Audio stream configuration.
Definition types.hpp:41