summaryrefslogtreecommitdiff
path: root/src/audio/include/audio_element.hpp
blob: 06e47b355d144f8d65670e7427e6e0acfbb7bb69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once

#include <cstdint>

#include "freertos/FreeRTOS.h"

#include "freertos/message_buffer.h"
#include "freertos/portmacro.h"
#include "result.hpp"
#include "span.hpp"

#include "stream_info.hpp"
#include "types.hpp"

namespace audio {

/*
 * Errors that may be returned by any of the Process* methods of an audio
 * element.
 */
enum AudioProcessingError {
  // Indicates that this element is unable to handle the upcoming chunks.
  UNSUPPORTED_STREAM,
  // Indicates an error with reading or writing stream data.
  IO_ERROR,
};

/*
 * One indepedentent part of an audio pipeline. Each element has an input and
 * output message stream, and is responsible for taking data from the input
 * stream, applying some kind of transformation to it, then sending the result
 * out via the output stream. All communication with an element should be done
 * over these streams, as an element's methods are only safe to call from the
 * task that owns it.
 *
 * Each element implementation will have its input stream automatically parsed
 * by its owning task, and its various Process* methods will be invoked
 * accordingly. Element implementations are responsible for managing their own
 * writes to their output streams.
 */
class IAudioElement {
 public:
  IAudioElement() : input_buffer_(nullptr), output_buffer_(nullptr) {}
  virtual ~IAudioElement();

  /*
   * Returns the stack size in bytes that this element requires. This should
   * be tuned according to the observed stack size of each element, as different
   * elements have fairly different stack requirements.
   */
  virtual auto StackSizeBytes() -> std::size_t { return 2048; };

  /*
   * How long to wait for new data on the input stream before triggering a call
   * to ProcessIdle(). If this is portMAX_DELAY (the default), then ProcessIdle
   * will never be called.
   */
  virtual auto IdleTimeout() -> TickType_t { return portMAX_DELAY; }

  /* Returns this element's input buffer. */
  auto InputBuffer() -> MessageBufferHandle_t* { return input_buffer_; }

  /* Returns this element's output buffer. */
  auto OutputBuffer() -> MessageBufferHandle_t* { return output_buffer_; }

  /*
   * Called when a StreamInfo message is received. Used to configure this
   * element in preperation for incoming chunks.
   */
  virtual auto ProcessStreamInfo(StreamInfo& info)
      -> cpp::result<void, AudioProcessingError> = 0;

  /*
   * Called when a ChunkHeader message is received. Includes the data associated
   * with this chunk of stream data. This method should return the number of
   * bytes in this chunk that were actually used; leftover bytes will be
   * prepended to the next call.
   */
  virtual auto ProcessChunk(cpp::span<std::byte>& chunk)
      -> cpp::result<std::size_t, AudioProcessingError> = 0;

  /*
   * Called when there has been no data received over the input buffer for some
   * time. This could be used to synthesize output, or to save memory by
   * releasing unused resources.
   */
  virtual auto ProcessIdle() -> cpp::result<void, AudioProcessingError> = 0;

 protected:
  MessageBufferHandle_t* input_buffer_;
  MessageBufferHandle_t* output_buffer_;
};

}  // namespace audio