Author Topic: Playing WAV audio files via I2S, Adafruit MAX98357 amplifier board  (Read 1342 times)

postuma

  • Jr. Member
  • **
  • Posts: 64
This one's a work in progress - unless anyone already has something working?

Several problems already noted/resolved:

Pin declaration for I2S in Oak's core_esp8266_i2s.c, line 222, is incorrect:

Code: [Select]
  pinMode(esp8266_gpioToPin[5], FUNCTION_1); //I2SO_BCK (SCLK)
should read

Code: [Select]
  pinMode(esp8266_gpioToPin[15], FUNCTION_1); //I2SO_BCK (SCLK)
(I've already logged a pull request for this one.) Similar declaration on line 244 is correct.

Pin mappings:  LRC - Oak pin 0 (ESP8266 pin 2)
               BCLK - Oak pin 6 (ESP8266 pin 15)
               DIN - Oak pin 3 (ESP8266 pin 3)


Next: I2S syntax is implemented differently in the ESP8266 <I2S.h> file than it is for plain-vanilla Arduino. Sample code will not work. Example:

ArduinoESP8266/Oak
I2S.begin(I2S_PHILIPS_MODE, sampleRate, 16);i2s_begin();
i2s_set_rate(sampleRate);
I2S.write(sample);i2s_write_sample(sample);


Sample working sketch:

Code: [Select]
#include <i2s.h>

const int frequency = 880;        //frequency of square wave in Hz
int amplitude = 1000;             //amplitude of square wave
const int sampleRate = 16000;     //sample rate in Hz
const int halfWavelength = sampleRate / frequency;    //half wavelength of square wave

int count = 0;

void setup() {
  Particle.begin();
  Particle.println("I2S simple tone");

  i2s_begin();                    //initialize i2s
  i2s_set_rate(sampleRate);       //set sample rate
}

void loop() {
  if (count % halfWavelength == 0) amplitude = -1 * amplitude;    //invert sample every half wavelength count multiple to generate square wave

  i2s_write_sample(amplitude);    //write sample twice, once for each channel
  i2s_write_sample(amplitude);
  count++;
}


Other issues: other more-sophisticated I2S WAV libraries I've found haven't worked yet, and need to be heavily adapted for the Oak. In particular, some use the math-avr library. This one is very different from the ESP8266 math library so a straight swap just doesn't work.

Will be posting more once I get WAV-playing working - again, unless anyone else has already done it?

postuma

  • Jr. Member
  • **
  • Posts: 64
Re: Playing WAV audio files via I2S, Adafruit MAX98357 amplifier board
« Reply #1 on: April 16, 2017, 07:13:58 am »
Right. Got WAV audio playing from SPIFFS to the MAX98357. Description and code at https://github.com/ppostuma/SFX-I2S-web-trigger

Paul