-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef957e5
commit 6688224
Showing
7 changed files
with
213 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 100 additions & 1 deletion
101
firmware/components/presets/src/presets_json_builder.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,100 @@ | ||
#include "presets_json_builder.h" | ||
#include "presets_json_builder.h" | ||
#include "presets.pb-c.h" | ||
#include "uuid.h" | ||
#include <cppcodec/base64_default_rfc4648.hpp> | ||
#include <esp_log.h> | ||
|
||
namespace shrapnel::presets { | ||
|
||
template <> | ||
rapidjson::Value to_json(rapidjson::Document &document, | ||
const ParametersData &object) | ||
{ | ||
struct PresetParameters preset = PRESET_PARAMETERS__INIT; | ||
preset_parameters__init(&preset); | ||
|
||
preset.amp_gain = object.amp_gain * 1000; | ||
preset.amp_channel = object.amp_channel * 1000; | ||
preset.bass = object.bass * 1000; | ||
preset.middle = object.middle * 1000; | ||
preset.treble = object.treble * 1000; | ||
preset.contour = object.contour * 1000; | ||
preset.volume = object.volume * 1000; | ||
preset.noise_gate_threshold = object.noise_gate_threshold * 1000; | ||
preset.noise_gate_hysteresis = object.noise_gate_hysteresis * 1000; | ||
preset.noise_gate_attack = object.noise_gate_attack * 1000; | ||
preset.noise_gate_hold = object.noise_gate_hold * 1000; | ||
preset.noise_gate_release = object.noise_gate_release * 1000; | ||
preset.noise_gate_bypass = object.noise_gate_bypass * 1000; | ||
preset.chorus_rate = object.chorus_rate * 1000; | ||
preset.chorus_depth = object.chorus_depth * 1000; | ||
preset.chorus_mix = object.chorus_mix * 1000; | ||
preset.chorus_bypass = object.chorus_bypass * 1000; | ||
preset.wah_position = object.wah_position * 1000; | ||
preset.wah_vocal = object.wah_vocal * 1000; | ||
preset.wah_bypass = object.wah_bypass * 1000; | ||
|
||
uint8_t buffer[100] = {}; | ||
auto packed_size = preset_parameters__get_packed_size(&preset); | ||
if(packed_size > 100) | ||
{ | ||
return {/* null */}; | ||
} | ||
|
||
preset_parameters__pack(&preset, buffer); | ||
|
||
ESP_LOG_BUFFER_HEX("presets", buffer, packed_size); | ||
ESP_LOGI("presets", "Packed size: %zu", packed_size); | ||
|
||
using base64_codec = cppcodec::base64_rfc4648; | ||
|
||
std::array<char, 200> base64_buffer; | ||
auto encoded_size = base64_codec::encoded_size(packed_size); | ||
if(encoded_size > base64_buffer.size()) | ||
{ | ||
return {/* null */}; | ||
} | ||
|
||
base64_codec::encode( | ||
base64_buffer.data(), base64_buffer.size(), buffer, packed_size); | ||
|
||
auto json = rapidjson::Value{}; | ||
json.SetString(base64_buffer.data(), encoded_size, document.GetAllocator()); | ||
return json; | ||
} | ||
|
||
template <> | ||
rapidjson::Value to_json(rapidjson::Document &document, | ||
const PresetData &object) | ||
{ | ||
rapidjson::Value json; | ||
json.SetObject(); | ||
|
||
json.AddMember( | ||
"id", uuid::to_json(document, object.id), document.GetAllocator()); | ||
rapidjson::Value name{object.name.c_str(), document.GetAllocator()}; | ||
json.AddMember("name", name, document.GetAllocator()); | ||
json.AddMember("parameters", | ||
to_json(document, object.parameters), | ||
document.GetAllocator()); | ||
|
||
return json; | ||
} | ||
|
||
template <> | ||
rapidjson::Value to_json(rapidjson::Document &document, const Notify &object) | ||
{ | ||
rapidjson::Value json; | ||
json.SetObject(); | ||
|
||
json.AddMember("messageType", | ||
rapidjson::StringRef("Presets::notify"), | ||
document.GetAllocator()); | ||
|
||
json.AddMember( | ||
"preset", to_json(document, object.preset), document.GetAllocator()); | ||
|
||
return json; | ||
} | ||
|
||
} // namespace shrapnel::presets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "presets_json_builder.h" | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
// Disable warning inside rapidjson | ||
// https://github.com/Tencent/rapidjson/issues/1700 | ||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Wclass-memaccess" | ||
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" | ||
#pragma GCC diagnostic ignored "-Wsign-conversion" | ||
#pragma GCC diagnostic ignored "-Wswitch-enum" | ||
#include "rapidjson/document.h" | ||
#include "rapidjson/prettywriter.h" | ||
#include "rapidjson/stringbuffer.h" | ||
#pragma GCC diagnostic pop | ||
|
||
namespace { | ||
|
||
using namespace shrapnel; | ||
using namespace shrapnel::presets; | ||
|
||
template <typename T> | ||
std::string write_json(const T &object) | ||
{ | ||
rapidjson::Document document; | ||
auto result = to_json(document, object); | ||
|
||
result.Swap(document); | ||
|
||
rapidjson::StringBuffer buffer; | ||
rapidjson::PrettyWriter writer(buffer); | ||
document.Accept(writer); | ||
|
||
return buffer.GetString(); | ||
} | ||
|
||
std::string normalise_json(const std::string &json) | ||
{ | ||
rapidjson::Document document; | ||
document.Parse(json.c_str()); | ||
|
||
rapidjson::StringBuffer buffer; | ||
rapidjson::PrettyWriter writer(buffer); | ||
document.Accept(writer); | ||
|
||
return buffer.GetString(); | ||
} | ||
|
||
TEST(PresetsJsonBuilder, Notify) | ||
{ | ||
Notify input{.preset{.id{1, 2, 3}, | ||
.name{"test"}, | ||
.parameters{ | ||
.amp_gain{0.001}, | ||
.amp_channel{0.002}, | ||
.bass{0.003}, | ||
.middle{0.004}, | ||
.treble{0.005}, | ||
.contour{0.006}, | ||
.volume{0.007}, | ||
.noise_gate_threshold{0.008}, | ||
.noise_gate_hysteresis{0.009}, | ||
.noise_gate_attack{0.010}, | ||
.noise_gate_hold{0.011}, | ||
.noise_gate_release{0.012}, | ||
.noise_gate_bypass{0.013}, | ||
.chorus_rate{0.014}, | ||
.chorus_depth{0.015}, | ||
.chorus_mix{0.016}, | ||
.chorus_bypass{0.017}, | ||
.wah_position{0.018}, | ||
.wah_vocal{0.019}, | ||
.wah_bypass{0.020}, | ||
}}}; | ||
|
||
rapidjson::Document document; | ||
|
||
auto reference = normalise_json(R"({ | ||
"messageType": "Presets::notify", | ||
"preset": { | ||
"id": "01020300-0000-0000-0000-000000000000", | ||
"name": "test", | ||
"parameters": "CAEQAhgDIAQoBTAGOAdACEgJUApYC2AMaA1wDngPgAEQiAERkAESmAEToAEU" | ||
} | ||
})"); | ||
|
||
EXPECT_THAT(write_json(input), reference); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
add_subdirectory(cppcodec) | ||
add_subdirectory(esp-dsp) | ||
add_subdirectory(etl) | ||
add_subdirectory(rapidjson) |