3

I'm an experienced Python developer, and a complete newbie at electronics.

I got an ESP32 board with MicroPython installed, and a microphone connected to it.

I'm able to get a reading off the microphone, like this:

MicroPython v1.9.4 on 2018-05-11; ESP32 module with ESP32
Type "help()" for more information.
>>> import machine
>>> a = machine.ADC(machine.Pin(32))
>>> a.read()
488
>>> a.read()
496
>>> a.read()
449

My question is: How do I actually record audio so I could send it via email? Wave format or any other format. I guess that any implementation would just sample the level in a loop and build an array, but if there's an implementation that already exists instead of me having to write it, that'd be great.

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
  • essential ideas on digital audio are : sample rate which is number of times per second the microphone is sampled to output a single integer value which represents the height of the raw audio curve at that instant in time ... other idea is bit depth which is indicates storage size devoted to each audio sample ... CD quality uses bit depth of 16 meaning its using a datatype of size 2 bytes ... overall notion here is PCM audio - pulse code modulation - its very handy when getting into audio to install Audacity which can play and visualize audio files ... WAV format is simplest – Scott Stensland Jun 01 '19 at 14:17
  • Are you confident that the way you have connected the microphone to the ESP32 is appropriate? In general you can't just wire a mic directly to a MCU analogue input without some sort of level shifting and possibly amplification. Electronics questions would be off topic for this site but welcome at [Electrical Engineering](https://electronics.stackexchange.com/). For questions about audio recording with MicroPython I would try asking on [the forum](https://forum.micropython.org/). – nekomatic Jun 03 '19 at 12:15

1 Answers1

0

A basic idea would be to sample [1] and encode [2] at CD quality: 16 bits at 44100 Hz.

import machine, time
a = machine.ADC(machine.Pin(32))
while True:
    sample = a.read() * 2**(16-10)  # we want 16 bits, a.read() returns 10 bits
    print(sample)
    time.sleep(1/44100)

You should be able to run this code and see the value of each sample printing of the console. It should go up and down when you speak in the microphone.

It is very close to the PCM audio format (WAV): a sequence of values encoded on 16 bits at a frequency of 44100 per second.

More information on:

Damien
  • 1,624
  • 2
  • 19
  • 26