arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioMP34DT05.h
1#pragma once
2#include "AudioTools/CoreAudio/AudioStreams.h"
3#include "PDM.h"
4
5namespace audio_tools {
6
14 channels = 1;
15 sample_rate = 16000;
16 bits_per_sample = 16;
17 }
18 int gain = 20; // value of DEFAULT_PDM_GAIN
19 int buffer_size = 512;
20 int buffer_count = 2;
21 // define pins
22 // int pin_data = PIN_PDM_DIN;
23 // int pin_clk = PIN_PDM_CLK;
24 // int pin_pwr = PIN_PDM_PWR;
25 void logInfo() {
26 AudioInfo::logInfo();
27 LOGI("gain: %d", gain);
28 LOGI("buffer_size: %d", buffer_size);
29 }
30};
31
32class AudioMP34DT05 *selfAudioMP34DT05 = nullptr;
33
39class AudioMP34DT05 : public AudioStream {
40 public:
41 AudioMP34DT05() { selfAudioMP34DT05 = this; };
42 virtual ~AudioMP34DT05() {
43 if (p_buffer != nullptr) delete p_buffer;
44 };
45
46 AudioMP34DT05Config defaultConfig(int mode = RX_MODE) {
48 if (mode != RX_MODE) {
49 LOGE("TX_MODE is not supported");
50 }
51 return cfg;
52 }
53
54 bool begin() { return begin(config); }
55
56 bool begin(AudioMP34DT05Config cfg) {
57 TRACEI();
58 config = cfg;
59 cfg.logInfo();
60 if (p_buffer == nullptr) {
61 p_buffer = new NBuffer<uint8_t>(cfg.buffer_size, cfg.buffer_count);
62 }
63 p_mic->setBufferSize(cfg.buffer_size);
64 p_mic->onReceive(onReceiveStatic);
65 LOGD("begin(%d,%d)", cfg.channels, cfg.sample_rate);
66 bool result = p_mic->begin(cfg.channels, cfg.sample_rate);
67 if (!result) {
68 LOGE("begin(%d,%d)", cfg.channels, cfg.sample_rate);
69 }
70 LOGD("setGain: %d", cfg.gain);
71 p_mic->setGain(cfg.gain);
72 return result;
73 }
74
75 void end() {
76 TRACEI();
77 if (p_mic != nullptr) {
78 p_mic->end();
79 }
80
81 delete p_buffer;
82 p_buffer=nullptr;
83 }
84
85 size_t readBytes(uint8_t *data, size_t len) override {
86 if (p_buffer == nullptr) return 0;
87 return p_buffer->readArray(data, len);
88 }
89
90 int available() override {
91 if (p_buffer == nullptr) return 0;
92 return p_buffer->available();
93 }
94
95 protected:
96 PDMClass *p_mic = &PDM;
97 NBuffer<uint8_t> *p_buffer = nullptr;
99
102 void onReceive() {
103 int bytesAvailable = p_mic->available();
104 // Read into the sample buffer
105 uint8_t sampleBuffer[bytesAvailable]={0};
106 int read = PDM.read(sampleBuffer, bytesAvailable);
107 p_buffer->writeArray(sampleBuffer, read);
108 }
109
110 static void onReceiveStatic() { selfAudioMP34DT05->onReceive(); }
111};
112
113} // namespace
MP34DT05 Microphone of Nano BLE Sense. We provide a proper Stream implementation. See https://github....
Definition AudioMP34DT05.h:39
void onReceive()
Definition AudioMP34DT05.h:102
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:119
virtual int readArray(T data[], int len)
reads multiple values
Definition Buffers.h:37
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:59
A lock free N buffer. If count=2 we create a DoubleBuffer, if count=3 a TripleBuffer etc.
Definition Buffers.h:617
int available()
provides the number of entries that are available to read
Definition Buffers.h:664
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:53
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:59
Config for MP34DT05 Microphone. Supported sample rates 16000, 41667, Supported bits_per_sample only 1...
Definition AudioMP34DT05.h:12