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