Microphone_PDM
Loading...
Searching...
No Matches
Static Public Attributes | Protected Member Functions
Microphone_PDM_nRF52 Class Reference

#include <Microphone_PDM_nRF52.h>

Inheritance diagram for Microphone_PDM_nRF52:
Microphone_PDM_Base Microphone_PDM_MCU Microphone_PDM

Static Public Attributes

static const size_t BUFFER_SIZE_SAMPLES = 512
 
static const size_t NUM_BUFFERS = 2
 

Protected Member Functions

 Microphone_PDM_nRF52 ()
 
virtual ~Microphone_PDM_nRF52 ()
 
virtual int init ()
 
virtual int uninit ()
 
virtual int start ()
 
virtual int stop ()
 
virtual bool samplesAvailable () const
 
virtual bool copySamples (void *pSamples)
 
virtual bool noCopySamples (std::function< void(void *pSamples, size_t numSamples)>callback)
 
size_t getNumberOfSamples () const
 
size_t copySrcIncrement () const
 
- Protected Member Functions inherited from Microphone_PDM_Base
 Microphone_PDM_Base (size_t numSamples)
 
virtual ~Microphone_PDM_Base ()
 
void copySamplesInternal (const int16_t *src, uint8_t *dst) const
 
virtual size_t copySrcIncrement () const
 

Additional Inherited Members

- Public Types inherited from Microphone_PDM_Base
enum class  OutputSize { UNSIGNED_8 , SIGNED_16 , RAW_SIGNED_16 }
 
enum class  Range {
  RANGE_128 = 0 , RANGE_256 , RANGE_512 , RANGE_1024 ,
  RANGE_2048 , RANGE_4096 , RANGE_8192 , RANGE_16384 ,
  RANGE_32768
}
 
- Public Member Functions inherited from Microphone_PDM_Base
int getSampleRate () const
 
- Protected Attributes inherited from Microphone_PDM_Base
pin_t clkPin = A0
 
pin_t datPin = A1
 
bool stereoMode = false
 
int sampleRate
 
OutputSize outputSize = OutputSize::SIGNED_16
 
Range range = Range::RANGE_2048
 
size_t numSamples
 

Detailed Description

MCU-specific implementation of PDM for the nRF52.

This class is used on the Boron, B Series SoM, Tracker, and Argon.

You do not instantiate this class directly; it's automatically created when you use the Microphone_PDM singleton.

Member Function Documentation

◆ copySamples()

virtual bool Microphone_PDM_nRF52::copySamples ( void *  pSamples)
protectedvirtual

Copy samples from the DMA buffer to your buffer.

Parameters
pSamplesPointer to buffer to copy samples to. It must be at least getNumberOfSamples() samples in length.
Returns
true There were samples available and they were copied.
false There were no samples available. Your buffer is unmodified.

The size of the buffer in bytes will depend on the outputSize. If UNSIGNED_8, then it's getNumberOfSamples() bytes. If SIGNED_16 or RAW_SIGNED_16, then it's 2 * getNumberOfSamples().

You can skip calling samplesAvailable() and just call copySamples which will return false in the same cases where samplesAvailable() would have returned false.

Reimplemented in Microphone_PDM.

◆ copySrcIncrement()

size_t Microphone_PDM_nRF52::copySrcIncrement ( ) const
protectedvirtual

How much to increment src in copySamplesInternal. Used internally.

Returns
size_t number of bytes

This is almost always 1. The exception is if you are using 8000 Hz sampling on the nRF52. In this case, every other sample is skipped and the subclass overrides this to return 2.

Override of virtual method in base class Microphone_PDM_Base

Reimplemented from Microphone_PDM_Base.

◆ getNumberOfSamples()

size_t Microphone_PDM_nRF52::getNumberOfSamples ( ) const
inlineprotected

Return the number of int16_t samples that copySamples will copy.

Returns
size_t Number of uint16_t samples. Number of bytes is twice that value

You will never get a partial buffer of data. The number of samples is a constant that is determined by the MCU type at compile time and does not change.

On the nRF52, it's 512 samples (1024 bytes), except in one case: If you set a sample rate of 8000 Hz, it will be 256 samples because the hardware only samples at 16000 Hz but the code will automatically discard every other sample so there will only be 256 samples.

◆ init()

virtual int Microphone_PDM_nRF52::init ( )
protectedvirtual

Initialize the PDM module.

This is often done from setup(). You can defer it until you're ready to sample if desired, calling right before start().

Reimplemented in Microphone_PDM.

◆ noCopySamples()

virtual bool Microphone_PDM_nRF52::noCopySamples ( std::function< void(void *pSamples, size_t numSamples)>  callback)
protectedvirtual

Alternative API to get samples.

Parameters
callbackCallback function or lambda
Returns
true
false

Alternative API that does not require a buffer to be passed in. You should only use this if you can consume the buffer immediately without blocking.

The callback function or lamba has this prototype:

void callback(void *pSamples, size_t numSamples)

It will be called with a pointer to the samples (in the DMA buffer) and the number of samples (not bytes!) of data. The number of bytes will vary depending on the outputSize.

You can skip calling samplesAvailable() and just call noCopySamples which will return false in the same cases where samplesAvailable() would have returned false.

Reimplemented in Microphone_PDM.

◆ samplesAvailable()

virtual bool Microphone_PDM_nRF52::samplesAvailable ( ) const
protectedvirtual

Return true if there is data available to be copied using copySamples.

Returns
true
false

Reimplemented in Microphone_PDM.

◆ start()

virtual int Microphone_PDM_nRF52::start ( )
protectedvirtual

Start sampling.

Reimplemented in Microphone_PDM.

◆ stop()

virtual int Microphone_PDM_nRF52::stop ( )
protectedvirtual

Stop sampling.

Reimplemented in Microphone_PDM.

◆ uninit()

virtual int Microphone_PDM_nRF52::uninit ( )
protectedvirtual

Uninitialize the PDM module.

You normally will just initialize it once and only start and stop it as necessary, however you can completely uninitialize it if desired. The clkPin will be reset to INPUT mode.

Reimplemented in Microphone_PDM.


The documentation for this class was generated from the following file: