XCORE SDK
XCORE Software Development Kit
MicArray.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 <cstdio>
10 #include <type_traits>
11 #include <functional>
12 
13 #include "PdmRx.hpp"
14 #include "Decimator.hpp"
15 #include "SampleFilter.hpp"
16 #include "OutputHandler.hpp"
17 
18 #include "mic_array.h"
19 
20 #include <xcore/channel.h>
21 
22 using namespace std;
23 
24 // This has caused problems previously, so just catch the problems here.
25 #if defined(MIC_COUNT)
26 # error Application must not define the following as precompiler macros: MIC_COUNT.
27 #endif
28 
29 
30 namespace mic_array {
31 
42  template <unsigned MIC_COUNT,
43  class TDecimator,
44  class TPdmRx,
45  class TSampleFilter,
46  class TOutputHandler>
47  class MicArray
48  {
49 
50  public:
51 
55  static constexpr unsigned MicCount = MIC_COUNT;
56 
57 
63  TPdmRx PdmRx;
64 
71  TDecimator Decimator;
72 
79  TSampleFilter SampleFilter;
80 
87  TOutputHandler OutputHandler;
88 
89  public:
90 
94  MicArray() { }
95 
103  MicArray(TPdmRx pdm_rx,
104  TSampleFilter sample_filter,
105  TOutputHandler output_handler)
106  : PdmRx(pdm_rx),
107  SampleFilter(sample_filter),
108  OutputHandler(output_handler) { }
109 
116  MicArray(TPdmRx pdm_rx,
117  TOutputHandler output_handler)
118  : MicArray(pdm_rx, TSampleFilter(), output_handler) { }
119 
123  void ThreadEntry();
124  };
125 
126 }
127 
129 // Template function implementations below. //
131 
132 
133 
134 template <unsigned MIC_COUNT,
135  class TDecimator,
136  class TPdmRx,
137  class TSampleFilter,
138  class TOutputHandler>
139 void mic_array::MicArray<MIC_COUNT,TDecimator,TPdmRx,
140  TSampleFilter,
141  TOutputHandler>::ThreadEntry()
142 {
143  int32_t sample_out[MIC_COUNT] = {0};
144 
145  while(1){
146  uint32_t* pdm_samples = PdmRx.GetPdmBlock();
147  Decimator.ProcessBlock(sample_out, pdm_samples);
148  SampleFilter.Filter(sample_out);
149  OutputHandler.OutputSample(sample_out);
150  }
151 }
Represents the microphone array component of an application.
Definition: MicArray.hpp:48
TSampleFilter SampleFilter
The output filter.
Definition: MicArray.hpp:79
MicArray(TPdmRx pdm_rx, TOutputHandler output_handler)
Construct a MicArray
Definition: MicArray.hpp:116
MicArray()
Construct a MicArray.
Definition: MicArray.hpp:94
void ThreadEntry()
Entry point for the decimation thread.
Definition: MicArray.hpp:141
TPdmRx PdmRx
The PDM rx service.
Definition: MicArray.hpp:63
TDecimator Decimator
The Decimator.
Definition: MicArray.hpp:71
MicArray(TPdmRx pdm_rx, TSampleFilter sample_filter, TOutputHandler output_handler)
Construct a MicArray.
Definition: MicArray.hpp:103
TOutputHandler OutputHandler
The output handler.
Definition: MicArray.hpp:87