Skip to content

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

Merged
merged 1 commit into from
Jul 5, 2019

Conversation

deadprogram
Copy link
Member

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..

@deadprogram deadprogram force-pushed the features/microphone branch from b9d2809 to 7e29aec Compare May 6, 2019 21:25
@deadprogram
Copy link
Member Author

Any implementation feedback for this PR?

@deadprogram deadprogram requested a review from aykevl May 14, 2019 12:41
Copy link
Member

@aykevl aykevl left a 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,
Copy link
Member

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.

Copy link
Member Author

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.

d.data = make([]int32, d.SampleCountForSPL)
d.buf = make([]uint32, (quantizeSteps / 16))

return
Copy link
Member

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.

Copy link
Member Author

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)
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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.

for i := 0; i < len(d.data); i++ {
v := d.data[i]
if v < 0 {
v *= -1
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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)
Copy link
Member

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).

Copy link
Member Author

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.

@deadprogram deadprogram changed the base branch from master to dev May 27, 2019 15:26
@deadprogram deadprogram force-pushed the features/microphone branch 2 times, most recently from 24d2b73 to 7654861 Compare July 5, 2019 07:44
@deadprogram
Copy link
Member Author

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... ;)

@deadprogram deadprogram requested a review from conejoninja July 5, 2019 09:54
@deadprogram deadprogram force-pushed the features/microphone branch from 7654861 to f1b126a Compare July 5, 2019 11:08
@deadprogram
Copy link
Member Author

Squashed everything here into single commit.

@aykevl aykevl merged commit f4583f5 into dev Jul 5, 2019
@aykevl aykevl deleted the features/microphone branch July 5, 2019 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants