tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/microphone/main.go (about)

     1  // Example using the i2s hardware interface on the Adafruit Circuit Playground Express
     2  // to read data from the onboard MEMS microphone.
     3  //
     4  // Uses ideas from the https://github.com/adafruit/Adafruit_CircuitPlayground repo.
     5  package main
     6  
     7  import (
     8  	"machine"
     9  
    10  	"tinygo.org/x/drivers/microphone"
    11  )
    12  
    13  const (
    14  	defaultSampleRate        = 22000
    15  	quantizeSteps            = 64
    16  	msForSPLSample           = 50
    17  	defaultSampleCountForSPL = (defaultSampleRate / 1000) * msForSPLSample
    18  )
    19  
    20  func main() {
    21  	machine.I2S0.Configure(machine.I2SConfig{
    22  		Mode:           machine.I2SModePDM,
    23  		AudioFrequency: defaultSampleRate * quantizeSteps / 16,
    24  		ClockSource:    machine.I2SClockSourceExternal,
    25  		Stereo:         true,
    26  	})
    27  
    28  	mic := microphone.New(machine.I2S0)
    29  	mic.SampleCountForSPL = defaultSampleCountForSPL
    30  	mic.Configure()
    31  
    32  	for {
    33  		spl, maxval := mic.GetSoundPressure()
    34  		println("C", spl, "max", maxval)
    35  	}
    36  }