add save range to file

This commit is contained in:
2025-04-27 16:55:45 +03:00
parent 4a005fb977
commit 05fea77f5e
5 changed files with 47 additions and 2 deletions

View File

@@ -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

View File

@@ -8,7 +8,7 @@
namespace hack::audio
{
// пока только wav
inline void save(const std::string& path, const std::vector<float>& samples, int sample_rate)
inline void save(const std::string& path, const std::vector<double>& 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);
}
}

View File

@@ -0,0 +1,40 @@
#pragma once
#include <filesystem>
#include <fstream>
#include <iostream>
#include "hack/logger/logger.hpp"
namespace hack::range
{
namespace {
template<typename T>
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<typename T>
void save_to_file(const std::filesystem::path& p, T& v, std::string delemiter = ", ")
{
save_to_file_impl(p, v, delemiter);
}
}

View File

@@ -11,6 +11,7 @@ headers = [
'hack/math/max.hpp',
'hack/range/sort.hpp',
'hack/range/save_to_file.hpp',
'hack/patterns/ring_buffer.hpp',

1
test.txt Normal file
View File

@@ -0,0 +1 @@
1, 2, 3, 4, 4, 4, 6