XCORE SDK
XCORE Software Development Kit
OutputHandler.hpp
1 // Copyright 2022 XMOS LIMITED.
2 // This Software is subject to the terms of the XMOS Public Licence: Version 1.
3 
4 #pragma once
5 
6 #include <cstdint>
7 #include <string>
8 #include <cassert>
9 #include <iostream>
10 #include <type_traits>
11 #include <functional>
12 
13 #include "mic_array/frame_transfer.h"
14 
15 #include <xcore/channel.h>
16 
17 
18 // This has caused problems previously, so just catch the problems here.
19 #if defined(MIC_COUNT) || defined(SAMPLE_COUNT) || defined(FRAME_COUNT)
20 # error Application must not define the following as precompiler macros: MIC_COUNT, SAMPLE_COUNT, FRAME_COUNT.
21 #endif
22 
23 using namespace std;
24 
25 namespace mic_array {
26 
43  template <unsigned MIC_COUNT>
45  {
46  private:
47 
55  chanend_t c_sample_out;
56 
57  public:
58 
63  : c_sample_out(0) { }
64 
70  ChannelSampleTransmitter(chanend_t c_sample_out)
71  : c_sample_out(c_sample_out) { }
72 
78  void SetChannel(chanend_t c_sample_out);
79 
85  void ProcessSample(int32_t sample[MIC_COUNT]);
86  };
87 
88 
89 
99  template <unsigned MIC_COUNT,
100  unsigned SAMPLE_COUNT,
101  template <unsigned, unsigned> class FrameTransmitter,
102  unsigned FRAME_COUNT = 1>
104  {
105  private:
106 
110  unsigned current_frame = 0;
111 
115  unsigned current_sample = 0;
116 
120  int32_t frames[FRAME_COUNT][MIC_COUNT][SAMPLE_COUNT];
121 
122  public:
123 
128  FrameTransmitter<MIC_COUNT, SAMPLE_COUNT> FrameTx;
129 
130  public:
131 
139 
147  FrameOutputHandler(FrameTransmitter<MIC_COUNT, SAMPLE_COUNT> frame_tx)
148  : FrameTx(frame_tx) { }
149 
155  void OutputSample(int32_t sample[MIC_COUNT]);
156  };
157 
158 
173  template <unsigned MIC_COUNT, unsigned SAMPLE_COUNT>
175  {
176  private:
177 
181  chanend_t c_frame_out;
182 
183  public:
184 
192  ChannelFrameTransmitter() : c_frame_out(0) { }
193 
199  ChannelFrameTransmitter(chanend_t c_frame_out) : c_frame_out(c_frame_out) { }
200 
206  void SetChannel(chanend_t c_frame_out);
207 
213  chanend_t GetChannel();
214 
220  void OutputFrame(int32_t frame[MIC_COUNT][SAMPLE_COUNT]);
221  };
222 }
223 
225 // Template function implementations below. //
227 
228 
229 template <unsigned MIC_COUNT>
231  chanend_t c_sample_out)
232 {
233  this->c_sample_out = c_sample_out;
234 }
235 
236 
237 template <unsigned MIC_COUNT>
239  int32_t sample[MIC_COUNT])
240 {
241  ma_frame_tx(this->c_sample_out, sample, MIC_COUNT, 1);
242 }
243 
244 
245 
246 template <unsigned MIC_COUNT,
247  unsigned SAMPLE_COUNT,
248  template <unsigned, unsigned> class FrameTransmitter,
249  unsigned FRAME_COUNT>
250 void mic_array::FrameOutputHandler<MIC_COUNT,SAMPLE_COUNT,
251  FrameTransmitter,FRAME_COUNT>::OutputSample(
252  int32_t sample[MIC_COUNT])
253 {
254  auto* cur_frame = reinterpret_cast<int32_t (*)[SAMPLE_COUNT]>(
255  &this->frames[this->current_frame][0][0]);
256 
257  for(int k = 0; k < MIC_COUNT; k++)
258  cur_frame[k][this->current_sample] = sample[k];
259 
260  if(++current_sample == SAMPLE_COUNT){
261  current_sample = 0;
262  current_frame++;
263  if(current_frame == FRAME_COUNT) current_frame = 0;
264 
265  FrameTx.OutputFrame( cur_frame );
266  }
267 }
268 
269 
270 
271 template <unsigned MIC_COUNT, unsigned SAMPLE_COUNT>
273  chanend_t c_frame_out)
274 {
275  this->c_frame_out = c_frame_out;
276 }
277 
278 template <unsigned MIC_COUNT, unsigned SAMPLE_COUNT>
280 {
281  return this->c_frame_out;
282 }
283 
284 
285 template <unsigned MIC_COUNT, unsigned SAMPLE_COUNT>
287  int32_t frame[MIC_COUNT][SAMPLE_COUNT])
288 {
289  ma_frame_tx(this->c_frame_out,
290  reinterpret_cast<int32_t*>(frame),
291  MIC_COUNT, SAMPLE_COUNT);
292 }
Frame transmitter which transmits frame over a channel.
Definition: OutputHandler.hpp:175
chanend_t GetChannel()
Get channel used for frame transfers.
Definition: OutputHandler.hpp:279
void OutputFrame(int32_t frame[MIC_COUNT][SAMPLE_COUNT])
Transmit the specified frame.
Definition: OutputHandler.hpp:286
ChannelFrameTransmitter(chanend_t c_frame_out)
Construct a ChannelFrameTransmitter.
Definition: OutputHandler.hpp:199
ChannelFrameTransmitter()
Construct a ChannelFrameTransmitter.
Definition: OutputHandler.hpp:192
void SetChannel(chanend_t c_frame_out)
Set channel used for frame transfers.
Definition: OutputHandler.hpp:272
OutputHandler which transmits samples over a channel.
Definition: OutputHandler.hpp:45
void SetChannel(chanend_t c_sample_out)
Set the channel used for sending samples.
Definition: OutputHandler.hpp:230
ChannelSampleTransmitter(chanend_t c_sample_out)
Construct a ChannelSampleTransmitter.
Definition: OutputHandler.hpp:70
void ProcessSample(int32_t sample[MIC_COUNT])
Transmit the specified sample.
Definition: OutputHandler.hpp:238
ChannelSampleTransmitter()
Construct a ChannelSampleTransmitter.
Definition: OutputHandler.hpp:62
OutputHandler for grouping samples into frames and sending frames to subsequent processing stages.
Definition: OutputHandler.hpp:104
FrameOutputHandler()
Construct new FrameOutputHandler.
Definition: OutputHandler.hpp:138
FrameOutputHandler(FrameTransmitter< MIC_COUNT, SAMPLE_COUNT > frame_tx)
Construct new FrameOutputHandler.
Definition: OutputHandler.hpp:147
FrameTransmitter< MIC_COUNT, SAMPLE_COUNT > FrameTx
FrameTransmitter used to transmit frames to the next stage for processing.
Definition: OutputHandler.hpp:128