Skip to content

Instantly share code, notes, and snippets.

@SimedruF
Created January 11, 2025 16:23
Show Gist options
  • Save SimedruF/23ec34227789a395d2ac8303d1c55d16 to your computer and use it in GitHub Desktop.
Save SimedruF/23ec34227789a395d2ac8303d1c55d16 to your computer and use it in GitHub Desktop.
ESP32_AudioKit_2.2_AC101
/**
* @file player-sd-audiokit.ino
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-audiokit/player-sdfat-audiokit/README.md
* Make sure that the pins are set to off, on, on, off, off
* @author Phil Schatzmann
* Adapted by Florin Simedru - https://blog.automatic-house.ro
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/AudioLibs/AudioSourceSD.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
#include "SD.h"
const char *startFilePath="/";
const char* ext="mp3";
AudioSourceSD source(startFilePath, ext, PIN_AUDIO_KIT_SD_CARD_CS);
AudioBoardStream kit(AudioKitAC101);
MP3DecoderHelix decoder; // or change to MP3DecoderMAD
AudioPlayer player(source, kit, decoder);
StreamCopy copier;
File audioFile;
const byte led_4 = 22;
const byte led_5 = 19;
float volume=0.7;
void next(bool, int, void*) {
player.next();
}
void previous(bool, int, void*) {
player.previous();
}
void setvolume_cbk_plus(bool, int, void*) {
volume = volume + 0.1;
if(volume >= 1.0)
{
volume = 1.0;
}
player.setVolume(volume);
}
void setvolume_cbk_minus(bool, int, void*) {
volume = volume - 0.1;
if(volume <= 0.0)
{
volume =0.0;
}
player.setVolume(volume);
}
void startStop(bool, int, void*) {
player.setActive(!player.isActive());
}
void setup() {
Serial.begin(115200);
pinMode(led_5, OUTPUT);
pinMode(led_4, OUTPUT);
digitalWrite(led_4, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led_5, HIGH); // turn the LED on (HIGH is the voltage level)
Scan_i2c();
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// setup output
auto cfg = kit.defaultConfig(TX_MODE);
cfg.sd_active = true;
kit.begin(cfg);
// setup additional buttons
kit.addDefaultActions();
kit.addAction(kit.getKey(1), startStop);
kit.addAction(kit.getKey(4), next);
kit.addAction(kit.getKey(3), previous);
kit.addAction(kit.getKey(5), setvolume_cbk_plus);
kit.addAction(kit.getKey(6), setvolume_cbk_minus);
// setup player
player.setVolume(volume);
player.begin();
// select file with setPath() or setIndex()
//player.setPath("/ZZ Top/Unknown Album/Lowrider.mp3");
//player.setIndex(1); // 2nd file
// player.setPath("/Fools Garden - Lemon Tree (Official HD Video).mp3");
player.setIndex(0); // 2nd file
}
void Scan_i2c()
{
byte error, address;
int nDevices;
Serial.println("Scanning i2c address...");
Wire.begin(33, 32); // AC-101 Audio Codec Chip
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print(" I2C device found at address 0x");
digitalWrite(led_5, HIGH); // turn LED4 on
if (address<16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
}
void loop() {
player.copy();
kit.processActions();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment