newbie cant implement boost #1074
-
Hi all, sorry for having to ask this as I have been trying my best to avoid asking for help & work things out myself but I have hit a stumbling block which apparently "throw more money at it" does not solve :( I'm working on an animatronic parrot which contains 2 DC motors, one for the mouth other for the body to dance. I have a selection of stereo wav files, left channel with only vocals, right channel with music/sounds. Using the .volume() command I can read the levels of the wav channels & map that to the motor speeds. This works perfectly for what I need. My issue is that the MAX98357 is not outputting enough volume to the speaker. This is a 3w i2s amplifier. If I plug the same speaker & files into a dedicated 3w mp3/wav modules there is plenty of volume. I have also added a 100k resistor between the rain / gnd pins of the MAX98357 to make the gain 15db. I have also tried powering the board from the ESP32, batteries, bench power supply & the volume stays the same. I have also tried with some success to pre-process the wav files to +15db boost the output before saving to SD card......... this kind of works, it reaches the volume needed but because the file has been boosted to the point that it is mostly peaking in the red it is causing distortion of the wave form so I can no longer get useful information by reading .volume() Beling a complete newbie at this (previous to this project the most advance arduino/ESP projects were simple led or servo toys!) so I thought maybe I could get a CS4344 headphone DAC to run into a 5w amplifier...... that arrived today hooked up & no sounds! I did not have a clue until a few hours ago that it needed a MCLK input. So I think my next test would be to implement the "allow_boost = true" setting to see if I can increase the output of the MAX98357, however because of how new I am to coding I do not have the required knowledge as to how I change this setting in my sketch. It has taken me about 40-50 hours in the past week of reading & trying things just to be able to change volume & read the channel outputs! (I am unable to work currently so no I really dont have anything better to do than sink all of my time into this lol!) Thanks in advance for any help / suggestions. And again my apologies to ask for help on what I am sure is a very trivial thing but I am pulling my hair out to try & solve it! If you need more information on my exact setup then I have all the details in this Arduino.cc thread I am working on. also if needed, please find attached my current script...... please bare in mind that as I said I am new to all of this so my code is not the cleanest and there may be parts which look like dead ends, but is in fact the early foundations for expanding control of the animatronic. #include <SPI.h>
#include <SD.h>
#include "AudioTools.h"
#include "AudioCodecs/CodecWAV.h"
#include <FastLED.h>
#include <esp_now.h>
#include <wifi.h>
#define LED_PIN 27
#define NUM_LEDS 3
#define BRIGHTNESS 255
#define LED_TYPE WS2812
#define COLOR_ORDER RGB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// remoteMac[] = {0x2c, 0x3a, 0xe8, 0x16, 0x77, 0xae};
int command = 0; // Command recieved from remote
int glow = 255;
const int potPin = 34; // Volume control potentiometer
const int chipSelect=5;
const int mouthMotor = 26; // Mouth / Eye motor, anly high one input at a time
//const int eyeMotor = 25;
const int bodyMotorB = 12;
const int bodyMotorA = 25;
const int mouthThresh = 5000;
const int bodyThresh = 5000;
float potValue = 0; // Volume control
int volMillis = 0; // Volume control
int bodyMotorDir = 0;
int bodySpeed = 0;
int dirChangeMillis = 0;
// Setup audio configs
I2SStream i2s; // final i2sput of decoded stream
AudioInfo info(44100, 2, 16);
VolumeStream volume(i2s);
VolumeOutput volLev;
EncodedAudioStream audOut(&volume, new WAVDecoder()); // Decoding stream
StreamCopy copier;
MultiOutput multiOut;
File audioFile;
// Setup ESP-Now network
// Structure example to receive data
typedef struct struct_message {
int a;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Command: ");
Serial.println(myData.a);
Serial.println();
command = int(myData.a);
}
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Initilize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register callback function
esp_now_register_recv_cb(OnDataRecv);
// pinModes
pinMode(mouthMotor, OUTPUT);
pinMode(bodyMotorA, OUTPUT);
pinMode(bodyMotorB, OUTPUT);
// setup file
SD.begin(chipSelect);
audioFile = SD.open("/living on a prayer.wav");
// setup multi output
multiOut.add(volLev);
multiOut.add(audOut);
// setup i2s
auto config = i2s.defaultConfig(TX_MODE);
i2s.begin(config);
// setup I2S based on sampling rate provided by decoder
audOut.setNotifyAudioChange(i2s);
audOut.begin();
// set initial volume
auto Config = volume.defaultConfig();
volume.begin(config); // we need to provide the bits_per_sample and channels
volume.setVolume(1.0);
// Volume output begin //
volLev.begin(info);
//attach WS812 LED
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
FastLED.setBrightness(255);
FastLED.show();
// begin copy
copier.begin(multiOut, audioFile);
}
void loop(){
if (!copier.copy()) {
stop();
}
if (millis() >= (volMillis + 250)){
potValue = float(map((analogRead(potPin)), 0, 4095, 0, 100))/100;
volume.setVolume(potValue);
volMillis = millis();
}
if (volLev.volume(0) >= mouthThresh){analogWrite(mouthMotor, (map(volLev.volume(0), (mouthThresh * 0.5), (mouthThresh * 1.5), 100, 255)));}
if (volLev.volume(0) < mouthThresh){analogWrite(mouthMotor, 0);}
if (volLev.volume() >= bodyThresh){
bodySpeed = (map(volLev.volume(), 3000, 10000, 0, 255));
if (bodyMotorDir ==0) {analogWrite(bodyMotorA, bodySpeed);}
if (bodyMotorDir ==1) {analogWrite(bodyMotorB, bodySpeed);}
if (bodyMotorDir ==2) {analogWrite(bodyMotorA, 0);
analogWrite(bodyMotorB, 0);}
}
if (volLev.volume() < bodyThresh){
analogWrite(bodyMotorA, 0);
analogWrite(bodyMotorB, 0);
}
if (millis() >= (dirChangeMillis + random(500, 2000))){
dirChangeMillis = millis();
bodyMotorDir = random(3);
Serial.println(bodyMotorDir);
}
// Serial.print("Volume: ");
Serial.print(volLev.volume());
Serial.print(",");
// Serial.print(" left: ");
Serial.print(volLev.volume(0));
Serial.print(",");
// Serial.print(" right: ");
Serial.println(volLev.volume(1));
leds[1].setRGB(0, 0, (map(volLev.volume(), 0, 35000, 0, 255)));
leds[2].setRGB(0, (map(volLev.volume(0), 0, 35000, 0, 255)), 0);
leds[0].setRGB((map(volLev.volume(1), 0, 35000, 0, 255)), 0, 0);
FastLED.show();
}` |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
It seems you already thought of most causes for low volume. The only thing that I can think of is some 16bit vs 32bit samples conversion without scaling. It is just a guess, but have you checked all audio streams work with the same size or conversion is done properly? I see you have an AudioInfo variable that at first glance seems unused… P.S. nice project by the way |
Beta Was this translation helpful? Give feedback.
-
There is a small bug in your code when you set up the volume. // set initial volume
auto Config = volume.defaultConfig();
volume.begin(config); // we need to provide the bits_per_sample and channels
volume.setVolume(1.0); I suggest that you use some speaking variable names. // set initial volume
auto vol_config = volume.defaultConfig();
vol_config.volume = 1.1;
vol_config.allow_boost = true;
volume.begin(vol_config); But be careful, if you set the volume too high you will get distortions. And of cause you can change the volume at any time after by calling volume.setVolume(vol); Recently, there was some bug in my library which prevented the allow_boost from working properly, so you might need to upgrade to the latest version. Finally, I recommend to follow this advice if something is not working as expected. |
Beta Was this translation helpful? Give feedback.
-
@joba-1 Thank you for your suggestion & to be honest I had not tried that (this project is full of "I have not tried that because I didn't even know that was a thing!" problems) @pschatzmann OMG!! OMG!! OMGEEEEE!! Thank you so much, that worked!! Unfortunately its 0330 here (UK) so I cant give it a proper test while other people in the house are sleeping, but as soon as I flashed those four lines I could here an instant volume increase! I'm not sure why I had not put that combination before, I think I was trying to apply it to the volume function itself which was giving me an error so I was running out of ideas. And yes, I have had that page, the examples page, several of the wiki pages opened on tabs most of the week trying to work things out! Again thank you both for your support. Christmas might be a month away, but for the time it cost you to reply it has made it feel like Christmas arrived today for me!! I'll now end the thread & go back over to my forum post to continue so that I am not bloating up here. Thank you!!! Pearl |
Beta Was this translation helpful? Give feedback.
There is a small bug in your code when you set up the volume.
You don't use the right variable in the begin:
I suggest that you use some speaking variable names.
If you want to allow volume values > 1.0 you need to set allow_boost to true.
So I suggest you do something like this:
But be careful, if you set the volume too high you will get distortions. And of cause you can chan…