0

How can i use micropython firmware alongside a Max9814? I have written the code below but cant hear clear voice in audacity...

from machine import Pin, ADC
import ustruct , time

analog_value = machine.ADC(26)
conversion_factor =3.3/(65536)

samples = []

while True:
    reading = analog_value.read_u16()*conversion_factor   
samples.append(int(reading)) #print("ADC: ",reading)
time.sleep(0.002)



with open('Voice.bin', 'wb') as output:
     for sample in samples:
        output.write(struct.pack('<h', sample))

Jonas
  • 121,568
  • 97
  • 310
  • 388

1 Answers1

1

Try changing

conversion_factor = 3.3/(65536)

to

conversion_factor = 3.3/(4096)

This is because, although the ADC result is returned as a 16-bit integer the actual result is only the lower 12 bits - it is a 12-bit ADC!

Using 65536 (16 bits), the resulting audio will seem quiet as it is only capable of reaching 1/16 of the full-scale range of a 16-bit value.

I would also suggest using the Normalise effect in Audacity, bearing in mind the audio will always sound a bit noisy.

A further point to bear in mind is that you sample rate is unlikely to 100% stable by doing the timing using code. If you want hardware-timed audio it is worth learning to use DMA. e.g. https://iosoft.blog/2021/10/26/pico-adc-dma/

  • thanks a lot friend! ill test and write the result – Alirezadigi May 22 '22 at 12:02
  • sorry but how can i import audio in audiacity better? and is it possible to convert audio to sth like mp3 or wav by python or ūpython? – Alirezadigi May 22 '22 at 12:04
  • @Alirezadigi I assume you are managing to get as far as having a file containing just a sequence of 16-bit samples that you have captured from your mic? In which case it is relatively easy to convert it to an "official" .wav file in Python. The easiest way is to create a new file with the name you want plus .wav on the end. Then write the data required for a WAV file header, followed by reading the data from your existing file and adding it to the new one. When that's done close the new file and you now have the wav. – Robin McBurnie May 24 '22 at 13:31
  • @Alirezadigi Have a look here (or search MSDN) for WAV header details: Check header details here: https://isip.piconepress.com/projects/speech/software/tutorials/production/fundamentals/v1.0/section_02/s02_01_p05.html Mostly it's self-explanatory, note that: Block Alignment = Bytes per Sample x Number of Channels - so if you are recording Mono 16-bit the value will be 2. Watch out for which values are Little-Endian! Also see this site for handy guide to formats: https://docs.fileformat.com/audio/wav/ – Robin McBurnie May 24 '22 at 13:49
  • Hi again , thanks for everything, i tried some of your recommendations and now it works , but idk how to use DMA... Can you please help me @robin-mcburnie – Alirezadigi Jul 02 '22 at 09:31