Skip to content

Working with PlatformIO

Phil Schatzmann edited this page Oct 10, 2021 · 33 revisions

The library has been developed to be used in Arduino, but it can also be used easily in PlatformIO.

  1. Create a new PlatformIO Project.

  2. Update the platformio.ini file in the root of the project:

[platformio]
description = Audio Example
default_envs = esp32dev

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps = https://github.com/pschatzmann/arduino-audio-tools
build_flags = -DCORE_DEBUG_LEVEL=5
monitor_speed = 115200
  1. Add your desired Arduino sketch into the src directory e.g.
#include <Arduino.h>
#include "AudioTools.h"

using namespace audio_tools;  

typedef int16_t sound_t;                                   // sound will be represented as int16_t (with 2 bytes)
uint16_t sample_rate=44100;
uint8_t channels = 2;                                      // The stream will have 2 channels 
SineWaveGenerator<sound_t> sineWave(32000);                // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<sound_t> sound(sineWave);   // Stream generated from sine wave
I2SStream out; 
StreamCopy copier(out, sound);                             // copies sound into i2s

// Arduino Setup
void setup(void) {  
  // Open Serial 
  Serial.begin(115200);

  // start the bluetooth
  Serial.println("starting I2S...");
  I2SConfig config = out.defaultConfig(TX_MODE);
  config.sample_rate = sample_rate; 
  config.channels = channels;
  config.bits_per_sample = 16;
  out.begin(config);

  // Setup sine wave
  sineWave.begin(channels, sample_rate, N_B4);
}

// Arduino loop - copy sound to out 
void loop() {
  copier.copy();
}

The file name does not matter but is should have a cpp or ino extension - you could use e.g. simple.ino or main.cpp. You could also take e.g. any sketch from the samples directory.

  1. Compile the project

This step also automatically installs the ESP32-A2DP project into the .pio/libdeps/esp32dev/ESP32-A2DP/ directory.

  1. Upload the binary

  2. Open a new Platformio Terminal and confirm that you get log messages.

  3. Make adjustments

  • You can extend the sketch with your own logic
  • You could replace the sketch with any other example that can be found in .pio/libdeps/esp32dev/ESP32-A2DP/examples
  • After you confirmed that everything is working you should consider to lower the debug level in platformio.ini because a high log level might impact the sound quality!

Final Comments

I was using the lib_deps functionality. As an alternative you could remove the lib_deps = https://github.com/pschatzmann/arduino-audio-tools and do a git clone https://github.com/pschatzmann/arduino-audio-tools into the lib folder