very bad thing

This commit is contained in:
2025-07-01 19:29:38 +02:00
parent ee0b1731e8
commit e566f14cd8
4 changed files with 133 additions and 104 deletions

View File

@@ -10,10 +10,26 @@
</head> </head>
<body> <body>
<midi-player src="indie.mid" sound-font <!-- <midi-player src="indie.mid" sound-font
visualizer="#myVisualizer"> visualizer="#myVisualizer">
</midi-player> </midi-player>
<midi-visualizer type="piano-roll" id="myVisualizer"></midi-visualizer> <midi-visualizer type="piano-roll" id="myVisualizer"></midi-visualizer> -->
<h1>Défi :</h1>
<div id="content">
<audio id="one" controls></audio>
<audio id="two" controls></audio>
<audio id="three" controls></audio>
<audio id="four" controls></audio>
<br/>
</div>
<button onclick="document.getElementById('answer').hidden = false">Révéler la réponse</button>
<p id="answer" hidden="true">T'es pas censé voir ça ...</p>
<script src="monscript.js"></script>
</body> </body>
</html> </html>

25
html/monscript.js Normal file
View File

@@ -0,0 +1,25 @@
document.getElementById("");
// src="data:audio/ogg;base64,BASE64CODE"
function updateAudios(response) {
document.getElementById("one").src = "data:audio/ogg;base64," + response.dataOne;
document.getElementById("two").src = "data:audio/ogg;base64," + response.dataTwo;
document.getElementById("three").src = "data:audio/ogg;base64," + response.dataThree;
document.getElementById("four").src = "data:audio/ogg;base64," + response.dataFour;
document.getElementById("answer").innerText = "Réponse : " + response.answer;
const select = document.createElement("select");
response.answers.forEach(element => {
const option = document.createElement("option");
option.innerText = element;
select.append(option);
});
document.getElementById("content").append(select);
}
fetch("/newchallenge", {
method: "POST",
})
.then((response) => response.json())
.then((json) => updateAudios(json));

View File

@@ -5,22 +5,26 @@
#include <string> #include <string>
#include <random> #include <random>
static const std::vector<std::string> links = { #include <httplib.h>
"2gQ--SCEfmI", #include <nlohmann/json.hpp>
"9xBeLSXOvSY", #include <turbobase64/turbob64.h>
"gv7QwZqMFYc",
"tFRStkEkhbY", static const std::map<std::string, std::string> links = {
"OecoHvuxkxY", {"2gQ--SCEfmI", "What I Want"},
"7SVKgjhpTHY", {"9xBeLSXOvSY", "Drunk"},
"KF63Z27NPTE", {"gv7QwZqMFYc", "Can't Wait"},
"7fKPxTbFt88", {"tFRStkEkhbY", "Lazy"},
"ZccDikDUHmA", {"OecoHvuxkxY", "Sunburn"},
"PjnHPuEUbjU" {"7SVKgjhpTHY", "Long Time Friends"},
}; {"KF63Z27NPTE", "zero_one"},
{"7fKPxTbFt88", "Animal"},
{"ZccDikDUHmA", "Fly Home"},
{"PjnHPuEUbjU", "Chosen"}};
static const std::string YTDLP_BINARY = "/home/simon/.local/bin/yt-dlp"; static const std::string YTDLP_BINARY = "/home/simon/.local/bin/yt-dlp";
std::string GetAudioPath(const std::string& id) { std::string GetAudioPath(const std::string &id)
{
return "audio/" + id + ".ogg"; return "audio/" + id + ".ogg";
} }
@@ -36,17 +40,14 @@ void DownloadFile(const std::string &id)
ep.run(); ep.run();
} }
void DownloadFiles() { void DownloadFiles()
for (const std::string& id : links) { {
for (const auto &[id, title] : links)
{
DownloadFile(id); DownloadFile(id);
} }
} }
/**
* std::vector<std::string> args = {"/usr/bin/ffmpeg", "-hide_banner", "-loglevel", "panic", "-i", "pipe:0", "-b:a", "32k", "-map",
"a", "-c:a", "libopus", "-f", "ogg", "-y", "pipe:1"};
*/
float GetDuration(const std::string &id) float GetDuration(const std::string &id)
{ {
stx::ExecPipe ep; stx::ExecPipe ep;
@@ -77,40 +78,93 @@ float GetRandomReal(float min, float max)
return distrib(generator); return distrib(generator);
} }
void ExtractAudio(const std::string &id, float begin, float end, const std::string& outputName) std::string ExtractAudio(const std::string &id, float begin, float end)
{ {
stx::ExecPipe ep; stx::ExecPipe ep;
std::vector<std::string> args = {"/usr/bin/ffmpeg", "-y", "-loglevel", "panic", "-i", GetAudioPath(id), "-ss", std::to_string(begin), "-to", std::to_string(end), outputName}; std::vector<std::string> args = {"/usr/bin/ffmpeg", "-y", "-loglevel", "panic", "-i", GetAudioPath(id), "-ss", std::to_string(begin), "-to", std::to_string(end), "-f", "ogg", "pipe:1"};
ep.add_exec(&args); ep.add_exec(&args);
std::string output;
ep.set_output_string(&output);
ep.run(); ep.run();
return output;
} }
std::string GetRandomLink() { std::pair<std::string, std::string> GetRandomLink()
return links.at(rand() % links.size()); {
auto it = links.begin();
std::advance(it, rand() % links.size());
return *it;
} }
void GenRandomChallenge() { struct Challenge
std::string randomLink = GetRandomLink(); {
std::string one, two, three, four;
std::string answer;
};
Challenge GenRandomChallenge()
{
Challenge challenge;
auto [randomLink, randomTitle] = GetRandomLink();
float duration = GetDuration(randomLink); float duration = GetDuration(randomLink);
float randomSeek = GetRandomReal(duration / 4.0f, duration * 3.0f / 4.0f); float randomSeek = GetRandomReal(duration / 4.0f, duration * 3.0f / 4.0f);
ExtractAudio(randomLink, randomSeek, randomSeek + 0.5f, "challenges/1.ogg"); challenge.answer = randomTitle;
ExtractAudio(randomLink, randomSeek, randomSeek + 1.0f, "challenges/2.ogg"); challenge.one = ExtractAudio(randomLink, randomSeek, randomSeek + 0.5f);
ExtractAudio(randomLink, randomSeek, randomSeek + 1.5f, "challenges/3.ogg"); challenge.two = ExtractAudio(randomLink, randomSeek, randomSeek + 1.0f);
ExtractAudio(randomLink, randomSeek, randomSeek + 2.0f, "challenges/4.ogg"); challenge.three = ExtractAudio(randomLink, randomSeek, randomSeek + 1.5f);
challenge.four = ExtractAudio(randomLink, randomSeek, randomSeek + 2.0f);
std::cout << "Done !\n"; std::cout << "Done !\n";
return challenge;
} }
std::string toBase64(const std::string &input)
{
std::string result;
result.resize(tb64enclen(input.length()));
tb64enc(reinterpret_cast<const unsigned char *>(input.data()), input.size(), reinterpret_cast<unsigned char *>(result.data()));
return result;
}
std::vector<std::string> GetPossibleAnswers() {
std::vector<std::string> answers;
for (const auto& [link, title] : links) {
answers.push_back(title);
}
return answers;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
srand(time(0)); srand(time(0));
GenRandomChallenge(); httplib::Server server;
server.Post("/newchallenge", [](const httplib::Request &req, httplib::Response &res){
Challenge challenge = GenRandomChallenge();
nlohmann::json j;
j["answer"] = challenge.answer;
j["dataOne"] = toBase64(challenge.one);
j["dataTwo"] = toBase64(challenge.two);
j["dataThree"] = toBase64(challenge.three);
j["dataFour"] = toBase64(challenge.four);
j["answers"] = GetPossibleAnswers();
res.set_content(j.dump(), "application/json");
});
server.set_mount_point("/", "html");
server.listen("0.0.0.0", 8080);
return 0; return 0;
} }

View File

@@ -1,78 +1,12 @@
add_rules("mode.debug", "mode.release") add_rules("mode.debug", "mode.release")
set_languages("c++11") set_languages("c++17")
add_requires("cpp-httplib", "nlohmann_json", "turbobase64")
target("Songdle") target("Songdle")
set_kind("binary") set_kind("binary")
add_files("src/*.cpp") add_files("src/*.cpp")
set_rundir(".") set_rundir(".")
add_packages("cpp-httplib", "nlohmann_json", "turbobase64")
--
-- If you want to known more usage about xmake, please see https://xmake.io
--
-- ## FAQ
--
-- You can enter the project directory firstly before building project.
--
-- $ cd projectdir
--
-- 1. How to build project?
--
-- $ xmake
--
-- 2. How to configure project?
--
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
--
-- 3. Where is the build output directory?
--
-- The default output directory is `./build` and you can configure the output directory.
--
-- $ xmake f -o outputdir
-- $ xmake
--
-- 4. How to run and debug target after building project?
--
-- $ xmake run [targetname]
-- $ xmake run -d [targetname]
--
-- 5. How to install target to the system directory or other output directory?
--
-- $ xmake install
-- $ xmake install -o installdir
--
-- 6. Add some frequently-used compilation flags in xmake.lua
--
-- @code
-- -- add debug and release modes
-- add_rules("mode.debug", "mode.release")
--
-- -- add macro definition
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
--
-- -- set warning all as error
-- set_warnings("all", "error")
--
-- -- set language: c99, c++11
-- set_languages("c99", "c++11")
--
-- -- set optimization: none, faster, fastest, smallest
-- set_optimize("fastest")
--
-- -- add include search directories
-- add_includedirs("/usr/include", "/usr/local/include")
--
-- -- add link libraries and search directories
-- add_links("tbox")
-- add_linkdirs("/usr/local/lib", "/usr/lib")
--
-- -- add system link libraries
-- add_syslinks("z", "pthread")
--
-- -- add compilation and link flags
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
--
-- @endcode
--