I have extended the I2S interface in Mozzi, so that we can can use the getAudioInput() which reads the audio via I2S: This can be used e.g. by the AudioKit. Until this change has been merged, you need to use my extended project.

Mozzi brings your Arduino to life by allowing it to produce much more complex and interesting growls, sweeps and chorusing atmospherics. These sounds can be quickly and easily constructed from familiar synthesis units like oscillators, delays, filters and envelopes.

The AudioKit is a cheap ESP32 based audio board that usually uses an ES8388 audio chip. This chip needs to be configured via I2C before it can be used.

If you want to use Mozzi with the AudioKit you still have a pretty easy way to deal with this using my AudioKitHAL library directly.

So here are the steps that are necessary:

Select Your Board

There a quite of few different variants out there with different audio chips and different pin assignments, so follow the instructions that you can find in the README of the Audiokit project to select your board!

Mozzi Configuration for the ESP32

Make sure that Mozzi is using I2S with a DAC and ADC and the correct pins. This is done in AudioConfigESP32.h by using the following settings:

 // Set output mode
#define ESP32_AUDIO_OUT_MODE I2S_DAC_AND_I2S_ADC

// For external I2S use I2S pins of your AudioKit
#define ESP32_I2S_BCK_PIN 27
#define ESP32_I2S_WS_PIN 25
#define ESP32_I2S_DATA_PIN 26
#define ESP32_I2S_DATA_PIN_IN 35 

// Select a supported Sample Rate
#define ESP32_AUDIO_RATE 32000

You can determine the relevant pins for your model by consulting this table.

Using the Codec

In the sketch you need to set up the audio chip of the AudioKit: We need to provide the sample rate and tell it not to mess with I2S because this is done by Mozzi:

  // setup of AudioKit codec
  auto cfg = kit.defaultConfig(kit::AudioInputOutput);
  cfg.is_i2s_active = false; // deactivate I2S because this is managed by mozzi
  cfg.sample_rate = AUDIO_HAL_32K_SAMPLES;
  cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2; // microphone?
  kit.begin(cfg);

Unfortunately the input device selection of the es8388 based AudioKits does not work because of a hardware bug and you get the input both from the microphones and the aux in. The LyraT boards however work correctly!

The Complete Arduino Sketch

We just copy the data from the output: This Sketch works on an AudioKit or LyraT board:

#include <MozziGuts.h>
#include "AudioKitHAL.h" // Using the AudioKit

AudioKit kit;

void setup(){
  auto cfg = kit.defaultConfig(audiokit::AudioInputOutput);
  cfg.i2s_active = false; // deactivate I2S because this is managed by mozzi
  cfg.sample_rate = AUDIO_HAL_08K_SAMPLES;
  cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2; // microphone?
  kit.begin(cfg);
  kit.setVolume(50);

  startMozzi();
}


void updateControl(){
}


AudioOutput_t updateAudio(){
  int asig = getAudioInput(); // range 0-1023
  asig = asig - 512; // now range is -512 to 511
  // output range in STANDARD mode is -244 to 243,
  // we scale down only by one bit, so you might need
  // to adjust your signal to suit
  return MonoOutput::fromAlmostNBit(9, asig).clip();
}


void loop(){
  audioHook();
}

You can adapt the range of values that the getAudioInput() provides in the AudioConfigESP32.h: Just use

// Select the data range of the ADC
#define ADC_VALUE(in) (in) // no scaling
//#define ADC_VALUE(in) ((in + 32768)*0.015625) // scale to 0 to 1023

and you get the proper int16_t values !


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *