Skip to content

Commit 4e82d4d

Browse files
M4_Eyes: use max peak-to-peak for sound detect
1 parent b3bafd7 commit 4e82d4d

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

M4_Eyes/pdmvoice.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ static const uint16_t recBufSize = (uint16_t)(sampleRate / (float)MIN_PITC
6161
static int16_t recIndex = 0;
6262
static int16_t playbackIndex = 0;
6363

64-
volatile uint16_t voiceLastReading = 0;
64+
volatile uint16_t voiceLastReading = 32768;
65+
volatile uint16_t voiceMin = 32768;
66+
volatile uint16_t voiceMax = 32768;
6567

6668
#define DC_PERIOD 4096 // Recalculate DC offset this many samplings
6769
// DC_PERIOD does NOT need to be a power of 2, but might save a few cycles.
@@ -359,6 +361,12 @@ void PDM_SERCOM_HANDLER(void) {
359361
// the last thing that was stored prior to whatever time you polled it,
360362
// but may still have some uses.
361363
voiceLastReading = adjusted;
364+
365+
// Similarly, user code can extern these variables and monitor the
366+
// peak-to-peak range. They are never reset in the voice code itself,
367+
// it's the duty of the user code to reset both to 32768 periodically.
368+
if(adjusted < voiceMin) voiceMin = adjusted;
369+
else if(adjusted > voiceMax) voiceMax = adjusted;
362370
}
363371
evenWord ^= 1;
364372
}

M4_Eyes/user_hid.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <Arduino.h>
44
#include "Adafruit_TinyUSB.h"
55
#include "globals.h"
6-
extern volatile uint16_t voiceLastReading; // In pdmvoice.cpp
6+
extern volatile uint16_t voiceLastReading, voiceMin, voiceMax; // In pdmvoice.cpp
77

88
#define UP_BUTTON_KEYCODE_TO_SEND HID_KEY_U
99
#define A_BUTTON_KEYCODE_TO_SEND HID_KEY_A
@@ -13,7 +13,7 @@ extern volatile uint16_t voiceLastReading; // In pdmvoice.cpp
1313
// even if not using sound output...we're using it to steal
1414
// access to the mic here.
1515
#define SOUND_KEYCODE_TO_SEND HID_KEY_SPACE
16-
#define SOUND_THRESHOLD 1000
16+
#define SOUND_THRESHOLD 10000
1717

1818
// HID report descriptor using TinyUSB's template
1919
// Single Report (no ID) descriptor
@@ -67,13 +67,14 @@ void user_loop(void) {
6767
keycode[3] = SHAKE_KEYCODE_TO_SEND;
6868
}
6969

70-
if(voiceLastReading) { // usu. non-zero if voice changer enabled
71-
int level = abs((int32_t)voiceLastReading - 32768);
72-
if(level > SOUND_THRESHOLD) {
70+
uint16_t p2p = voiceMax - voiceMin; // Max peak-to-peak since last reading
71+
if(p2p) { // usu. non-zero if voice changer enabled
72+
if(p2p > SOUND_THRESHOLD) {
7373
Serial.println("Sound");
7474
keycode[4] = SOUND_KEYCODE_TO_SEND;
7575
}
7676
}
77+
voiceMin = voiceMax = 32768; // Reset p2p range for next pass
7778

7879
bool anypressed = false;
7980
for (int k=0; k<sizeof(keycode); k++) {

0 commit comments

Comments
 (0)