Add stereo and bass enhance APIs via PEQ overlay.

Expose POST /api/audio/stereo-enhance and bass-enhance with 0–100 levels,
persist enhancements in AudioProfile, and document the virtual EQ mapping in
the manual and SigmaStudio chapter.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-06 17:00:12 +02:00
co-authored by Cursor
parent 6c350f5c48
commit fa532e068f
22 changed files with 826 additions and 19 deletions
+84 -3
View File
@@ -25,6 +25,7 @@
#include "core/HealthStatusJson.hpp"
#include "core/ParseError.hpp"
#include "core/SeekDirection.hpp"
#include "core/StoreError.hpp"
#include "core/TunerJson.hpp"
#include "core/WifiProvisionJson.hpp"
#include "tuner/TunerService.hpp"
@@ -129,7 +130,8 @@ void rebootTask(void* arg)
return "tuner_error";
}
[[nodiscard]] bool readRequestBody(httpd_req_t* req, std::array<char, 512>& body)
template <std::size_t N>
[[nodiscard]] bool readRequestBodyImpl(httpd_req_t* req, std::array<char, N>& body)
{
int received = 0;
while (received < static_cast<int>(body.size()) - 1) {
@@ -144,6 +146,12 @@ void rebootTask(void* arg)
return received > 0;
}
template <std::size_t N>
[[nodiscard]] bool readRequestBody(httpd_req_t* req, std::array<char, N>& body)
{
return readRequestBodyImpl(req, body);
}
/**
* @brief healthGetHandler — serve GET /api/health as JSON.
*
@@ -470,9 +478,66 @@ esp_err_t audioResetPostHandler(httpd_req_t* req)
}
/**
* @brief indexGetHandler — serve gzipped setup page from flash.
* @brief audioEnhancePostHandler — apply stereo or bass enhancement level.
*
* @dname indexGetHandler
* @dname audioEnhancePostHandler
* @param req HTTP request handle from esp_http_server.
* @return ESP_OK on success, or an esp_err_t error code.
* @pubstate parses level, updates AudioService, persists to NVS.
*
* @author Michele Bigi
* @date 2026-07-06
*/
esp_err_t audioEnhancePostHandler(httpd_req_t* req, bool stereo)
{
auto* ctx = routeContextFrom(req);
if (ctx == nullptr || ctx->audio == nullptr) {
httpd_resp_set_status(req, "503 Service Unavailable");
return httpd_resp_send(req, nullptr, 0);
}
std::array<char, 512> body{};
if (!readRequestBody(req, body)) {
httpd_resp_set_status(req, "400 Bad Request");
return httpd_resp_send(req, nullptr, 0);
}
const auto parsed = core::parseEnhanceLevelJson(std::string_view(body.data()));
if (!parsed) {
const std::string json =
core::serializeAudioErrorJson(parseErrorToken(parsed.error()));
httpd_resp_set_status(req, "400 Bad Request");
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
const std::expected<void, core::StoreError> applied = stereo
? ctx->audio->setStereoEnhance(*parsed, true)
: ctx->audio->setBassEnhance(*parsed, true);
if (!applied) {
const std::string json = core::serializeAudioErrorJson("store_failed");
httpd_resp_set_status(req, "500 Internal Server Error");
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
const std::string json = core::serializeAudioSavedJson();
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
esp_err_t audioStereoEnhancePostHandler(httpd_req_t* req)
{
return audioEnhancePostHandler(req, true);
}
esp_err_t audioBassEnhancePostHandler(httpd_req_t* req)
{
return audioEnhancePostHandler(req, false);
}
/**
* @brief indexGetHandler — serve gzipped setup page from flash.
* @param req HTTP request handle from esp_http_server.
* @return ESP_OK on success, or an esp_err_t error code.
* @pubstate none; reads embedded www/index.html.gz blob.
@@ -726,6 +791,22 @@ std::expected<void, NetError> SetupWebServer::start(
};
httpd_register_uri_handler(server_, &audioResetUri);
const httpd_uri_t audioStereoEnhanceUri = {
.uri = "/api/audio/stereo-enhance",
.method = HTTP_POST,
.handler = audioStereoEnhancePostHandler,
.user_ctx = routeCtx,
};
httpd_register_uri_handler(server_, &audioStereoEnhanceUri);
const httpd_uri_t audioBassEnhanceUri = {
.uri = "/api/audio/bass-enhance",
.method = HTTP_POST,
.handler = audioBassEnhancePostHandler,
.user_ctx = routeCtx,
};
httpd_register_uri_handler(server_, &audioBassEnhanceUri);
ESP_LOGI(kTag, "HTTP server listening on port 80");
return {};
}
+44
View File
@@ -207,6 +207,10 @@
<input id="si4684-db" type="range" min="-60" max="12" step="0.5" value="0">
<label for="esp32-db">ESP32 path (dB)</label>
<input id="esp32-db" type="range" min="-60" max="12" step="0.5" value="0">
<label for="stereo-level">Stereo depth (0100)</label>
<input id="stereo-level" type="range" min="0" max="100" step="1" value="0">
<label for="bass-level">Bass enhance (0100)</label>
<input id="bass-level" type="range" min="0" max="100" step="1" value="0">
<div class="row">
<button type="button" id="save-audio">Save profile</button>
<button type="button" class="secondary" id="reset-audio">Reset flat</button>
@@ -426,6 +430,10 @@
document.getElementById("master-db").value = d.master.left_db;
document.getElementById("si4684-db").value = d.mixer.si4684_left_db;
document.getElementById("esp32-db").value = d.mixer.esp32_left_db;
if (d.enhancements) {
document.getElementById("stereo-level").value = d.enhancements.stereo_level;
document.getElementById("bass-level").value = d.enhancements.bass_level;
}
showMsg(msg, "", true);
})
.catch(function () { showMsg(msg, "Audio profile load failed.", false); });
@@ -446,6 +454,13 @@
audioProfile.mixer.si4684_right_db = si4684;
audioProfile.mixer.esp32_left_db = esp32;
audioProfile.mixer.esp32_right_db = esp32;
if (!audioProfile.enhancements) {
audioProfile.enhancements = { stereo_level: 0, bass_level: 0 };
}
audioProfile.enhancements.stereo_level =
parseInt(document.getElementById("stereo-level").value, 10);
audioProfile.enhancements.bass_level =
parseInt(document.getElementById("bass-level").value, 10);
fetch("/api/audio/profile", {
method: "PUT",
headers: { "Content-Type": "application/json" },
@@ -462,6 +477,35 @@
.catch(function () { showMsg(msg, "Save request failed.", false); });
});
function postEnhance(url, level, msg) {
fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ level: level })
})
.then(function (r) { return r.json().then(function (d) { return { ok: r.ok, d: d }; }); })
.then(function (res) {
if (res.ok && res.d.status === "saved") {
showMsg(msg, "Enhancement applied.", true);
} else {
showMsg(msg, "Enhance failed: " + (res.d.reason || "unknown"), false);
}
})
.catch(function () { showMsg(msg, "Enhance request failed.", false); });
}
document.getElementById("stereo-level").addEventListener("change", function () {
var msg = document.getElementById("audio-msg");
var level = parseInt(document.getElementById("stereo-level").value, 10);
postEnhance("/api/audio/stereo-enhance", level, msg);
});
document.getElementById("bass-level").addEventListener("change", function () {
var msg = document.getElementById("audio-msg");
var level = parseInt(document.getElementById("bass-level").value, 10);
postEnhance("/api/audio/bass-enhance", level, msg);
});
document.getElementById("reset-audio").addEventListener("click", function () {
var msg = document.getElementById("audio-msg");
fetch("/api/audio/reset", { method: "POST" })
Binary file not shown.