diff --git a/bin/main.cpp b/bin/main.cpp index aa1b2ac..a953135 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -4,6 +4,7 @@ #include "hack/logger/logger.hpp" #include "hack/range/sort.hpp" +#include "hack/range/save_to_file.hpp" #include "hack/math/max.hpp" #include "hack/patterns/ring_buffer.hpp" @@ -53,6 +54,8 @@ auto main(int argc, char *argv[]) -> int hack::log()(v); hack::log()(l); + + // hack::range::save_to_file("/mnt/raid/projects/hack/hack/test.txt", v); } // math::max diff --git a/src/hack/audio/save.hpp b/src/hack/audio/save.hpp index 4ee05c7..c64cc13 100644 --- a/src/hack/audio/save.hpp +++ b/src/hack/audio/save.hpp @@ -8,7 +8,7 @@ namespace hack::audio { // пока только wav - inline void save(const std::string& path, const std::vector& samples, int sample_rate) + inline void save(const std::string& path, const std::vector& samples, int sample_rate) { SF_INFO sf_info; sf_info.samplerate = sample_rate; @@ -30,7 +30,7 @@ namespace hack::audio return; } - sf_write_float(outfile, samples.data(), samples.size()); + sf_write_double(outfile, samples.data(), samples.size()); sf_close(outfile); } } diff --git a/src/hack/range/save_to_file.hpp b/src/hack/range/save_to_file.hpp new file mode 100644 index 0000000..85c9ac1 --- /dev/null +++ b/src/hack/range/save_to_file.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include +#include "hack/logger/logger.hpp" + +namespace hack::range +{ + namespace { + template + void save_to_file_impl(const std::filesystem::path& p, T& v, std::string delemiter = ", ") + { + std::ofstream file(p); + if (!file) + { + error()("cant open file", p); + return; + } + + for (std::size_t i = 0; i < v.size(); ++i) + { + if (i == v.size() - 1) delemiter = ""; + file << v[i] << delemiter; + } + file.close(); + if (file.fail()) + error()("file dont save", p); + } + } + + template + void save_to_file(const std::filesystem::path& p, T& v, std::string delemiter = ", ") + { + save_to_file_impl(p, v, delemiter); + } +} + + + diff --git a/src/meson.build b/src/meson.build index 0f847e5..18165fa 100755 --- a/src/meson.build +++ b/src/meson.build @@ -11,6 +11,7 @@ headers = [ 'hack/math/max.hpp', 'hack/range/sort.hpp', + 'hack/range/save_to_file.hpp', 'hack/patterns/ring_buffer.hpp', diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..f7fa9aa --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +1, 2, 3, 4, 4, 4, 6 \ No newline at end of file