Skip to content

Portenta: PDM: cut out unusable starting PDM samples. #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libraries/PDM/src/PDM.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class PDMClass
int _gain;
int _init;

int _cutSamples;

PDMDoubleBuffer _doubleBuffer;

void (*_onReceive)(void);
Expand Down
3 changes: 2 additions & 1 deletion libraries/PDM/src/nrf52/PDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
_gain(-1),
_channels(-1),
_samplerate(-1),
_init(-1)
_init(-1),
_cutSamples(0)
{
}

Expand Down
13 changes: 7 additions & 6 deletions libraries/PDM/src/rp2040/PDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
_gain(-1),
_channels(-1),
_samplerate(-1),
_init(-1)
_init(-1),
_cutSamples(100)
{
}

Expand Down Expand Up @@ -138,6 +139,8 @@ int PDMClass::begin(int channels, int sampleRate)
true // Start immediately
);

_cutSamples = 100;

_init = 1;

return 1;
Expand Down Expand Up @@ -190,8 +193,6 @@ void PDMClass::setBufferSize(int bufferSize)

void PDMClass::IrqHandler(bool halftranfer)
{
static int cutSamples = 100;

// Clear the interrupt request.
dma_hw->ints0 = 1u << dmaChannel;
// Restart dma pointing to the other buffer
Expand All @@ -210,9 +211,9 @@ void PDMClass::IrqHandler(bool halftranfer)
Open_PDM_Filter_64(rawBuffer[rawBufferIndex], finalBuffer, 1, &filter);
}

if (cutSamples) {
memset(finalBuffer, 0, cutSamples);
cutSamples = 0;
if (_cutSamples) {
memset(finalBuffer, 0, _cutSamples);
_cutSamples = 0;
}

// swap final buffer and raw buffers' indexes
Expand Down
13 changes: 10 additions & 3 deletions libraries/PDM/src/stm32/PDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
_gain(-1),
_channels(-1),
_samplerate(-1),
_init(-1)
_init(-1),
_cutSamples(4)
{
_instance = this;
}
Expand Down Expand Up @@ -80,6 +81,8 @@ int PDMClass::begin(int channels, int sampleRate) {
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
_doubleBuffer.swap(0);

_cutSamples = 4 * channels;

if(py_audio_init(channels, sampleRate, _gain, 0.9883f)) {
py_audio_start_streaming();
_init = 1;
Expand Down Expand Up @@ -146,8 +149,12 @@ void PDMClass::IrqHandler(bool halftranfer)
_doubleBuffer.swap(g_pcmbuf_size);
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
g_pcmbuf_size = 0;
if (_onReceive) {
_onReceive();
if(_cutSamples == 0) {
if (_onReceive) {
_onReceive();
}
} else {
_cutSamples--;
}
}
}
Expand Down