-
Notifications
You must be signed in to change notification settings - Fork 211
microphone: PDM MEMS microphone support using I2S interface #50
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
Conversation
b9d2809
to
7e29aec
Compare
Any implementation feedback for this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the main purpose of this driver is reading the current sound pressure?
It looks good. I have a few nits here and there but in general it's fine. It is maybe not very obvious how to use it, though.
Mode: machine.I2SModePDM, | ||
AudioFrequency: defaultSampleRate * quantizeSteps / 16, | ||
ClockSource: machine.I2SClockSourceExternal, | ||
Stereo: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this only now after the I2S PR was merged but a channel count might have been more flexible.
On the other hand, AFAIK I2S has really been built for mono/stereo and there are very few DAC/ADCs with more than two channels. I could be wrong, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of research shows a number of multi-channel chips, but not too many MCUs. I suggest we defer that until a future iteration.
microphone/microphone.go
Outdated
d.data = make([]int32, d.SampleCountForSPL) | ||
d.buf = make([]uint32, (quantizeSteps / 16)) | ||
|
||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this return is unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
sum = applySincFilter(d.buf) | ||
|
||
// adjust to 10 bit value | ||
s := int32(sum >> 6) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to cap at 10bit?
While cheap microphones probably don't have anything but noise beyond that point, it seems better to me to not lose bits unnecessarily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be somewhat specific to the mic in the Circuit Playground Express. Might be good to make that a setting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a setting? No common devices are more efficient at 10bit than at 16bit (and most even support 32bit just as efficiently). Using more bits always avoids all this and is most likely both simpler to implement and to use, apart from being more efficient.
Or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a separate method ReadWithFilter()
to provide this capability, and put in a simpler raw Read()
implementation instead for the more general cases.
microphone/microphone.go
Outdated
for i := 0; i < len(d.data); i++ { | ||
v := d.data[i] | ||
if v < 0 { | ||
v *= -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just v = -v
?
The effect should be the same, but -v
seems easier to read for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not realize you could do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
pos := 0 | ||
for j := 0; j < len(samples); j++ { | ||
// takes only the low order 16-bits | ||
sample = uint16(samples[j] & 0xffff) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically the & 0xffff
us unnecessary here but I agree it may be easier to read (to make the capping explicit).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to be explicit here, probably.
24d2b73
to
7654861
Compare
This PR is ready for review now. @conejoninja you might be interested in this, as you have a CPE board. You could make a very cool VU meter with it, I bet... ;) |
Signed-off-by: Ron Evans <[email protected]>
7654861
to
f1b126a
Compare
Squashed everything here into single commit. |
This PR implements support for PDM MEMS microphones using I2S interface, such as the microphone built-in to the Adafruit Circuit Playground Express.
The PR in the TinyGo repo tinygo-org/tinygo#331 must be merged before this PR can be merged, since it requires the I2S implementation..