diff --git a/bin/main.fft.cpp b/bin/main.fft.cpp index 63f8006..6e52331 100644 --- a/bin/main.fft.cpp +++ b/bin/main.fft.cpp @@ -8,8 +8,7 @@ auto main() -> int // данных для чтения m_block_size; см. установки по умолчанию. // Передается по ссылке и заполняется необходимыми данными hr::setup setup; - setup.m_domain = hr::DOMAIN_PLUGIN::FREQUENSY; - setup.m_file = "./sin.wav"; + setup.init(hr::DOMAIN_PLUGIN::FREQUENSY, "./sin.wav"); auto r = hr::run(setup); hack::log()("grad:", r.m_grad); diff --git a/bin/main.raw_data.cpp b/bin/main.raw_data.cpp index 1f8f223..788a241 100644 --- a/bin/main.raw_data.cpp +++ b/bin/main.raw_data.cpp @@ -8,8 +8,7 @@ auto main() -> int // данных для чтения m_block_size; см. установки по умолчанию. // Передается по ссылке и заполняется необходимыми данными hr::setup setup; - setup.m_domain = hr::DOMAIN_PLUGIN::TIME; - setup.m_file = "./sin.wav"; + setup.init(hr::DOMAIN_PLUGIN::TIME, "./sin.wav"); auto r = hr::run(setup); hack::log()("grad:", r.m_grad); diff --git a/src/adapter/adapter.hpp b/src/adapter/adapter.hpp index a113be6..497274f 100644 --- a/src/adapter/adapter.hpp +++ b/src/adapter/adapter.hpp @@ -57,7 +57,7 @@ namespace hr private: void swap_buffer(fvec_t& in) { - size_t i = 0; + std::size_t i = 0; for (i = 0; i < m_end; ++i) m_data[i] = m_data_old[i]; for (i = 0; i < m_plugin.m_setup.m_step_size; ++i) m_data[m_end + i] = in[i]; for (i = 0; i < m_end; ++i) m_data_old[i] = m_data[i + m_plugin.m_setup.m_step_size]; @@ -71,5 +71,29 @@ namespace hr auto r = m_fft.process(m_data); m_plugin.process(r, in, m_timestamp); } + + + + }; + + + + inline void sum0(int a, int b) + { + int z = a - b * a; + int c = a + z; + } + + + inline int sum1(int a, int b) + { + int z = a - b * a; + int c = a + z; + + return c; + } + + int r = sum1(4, 5); + } diff --git a/src/harmonica.hpp b/src/harmonica.hpp index 3b1f77b..5a1a215 100644 --- a/src/harmonica.hpp +++ b/src/harmonica.hpp @@ -26,8 +26,10 @@ namespace hr { // Инициализация структуры для libsndfile и открытие файла SF_INFO sf_info; - SNDFILE* file = sf_open(setup.m_file.c_str(), SFM_READ, &sf_info); - if (!file) + auto deleter = [&](SNDFILE* f) { if (f) sf_close(f); hack::log()("reading file completed", setup.m_file);}; + std::unique_ptr sf_file(sf_open(setup.m_file.c_str(), SFM_READ, &sf_info), deleter); + + if (!sf_file) { // Обработка ошибки открытия файла hack::exception ex; @@ -35,19 +37,12 @@ namespace hr hack::log().on_func(); hack::log().on_row(); ex.title("Error of open file"); - ex.description(sf_strerror(file)); + ex.description(sf_strerror(sf_file.get())); ex.set("file", setup.m_file); hack::error()(ex); throw ex; } - // Сохранение информации о файле в настройки - setup.m_sample_rate = sf_info.samplerate; - setup.m_frames = sf_info.frames; - setup.m_channels = sf_info.channels; - - if (setup.m_channels == 0) throw std::runtime_error("Нет каналов в аудио файле"); - // Инициализация переменных для чтения std::size_t read = 0; // Количество обработанных кадров fvec_t read_data(setup.m_channels * setup.m_step_size, .0); // Буфер для чтения (интерливированные данные) @@ -61,7 +56,7 @@ namespace hr { // Определение длины читаемого блока (защита от выхода за границы) auto length = hack::math::min(setup.m_step_size, in.size()); - auto read_samples = sf_read_float(file, read_data.data(), read_data.size()); + auto read_samples = sf_read_float(sf_file.get(), read_data.data(), read_data.size()); uint_t read_length = read_samples / setup.m_channels; // Перевод в кадры read_length = hack::math::min(length, read_length); // Ограничение длиной буфера @@ -84,12 +79,11 @@ namespace hr if (in.size() > read) std::fill(in.begin() + read, in.end(), 0.0); // Вычисление временной метки и обработка данных через адаптер - real_time timestamp = real_time::frame2rt(read, sf_info.samplerate); + real_time timestamp = real_time::frame2rt(read, setup.m_sample_rate); ad.process(in, timestamp); } while (read == setup.m_step_size); - sf_close(file); return ad.get_result(); } } diff --git a/src/utils/workers/setup.hpp b/src/utils/workers/setup.hpp index 1988497..08426c0 100644 --- a/src/utils/workers/setup.hpp +++ b/src/utils/workers/setup.hpp @@ -1,6 +1,9 @@ #pragma once #include +#include +#include +#include namespace hr { @@ -10,6 +13,21 @@ namespace hr FREQUENSY }; + inline std::string domain_to_string(DOMAIN_PLUGIN d) + { + std::string res; + switch (d) + { + case hr::DOMAIN_PLUGIN::FREQUENSY: + res = "FREQUENSY"; + break; + case hr::DOMAIN_PLUGIN::TIME: + res = "TIME"; + break; + } + return res; + } + struct setup { // Эти данные заполняются из прочитанного файла (sndfile) @@ -27,5 +45,52 @@ namespace hr std::string m_plugin_description; DOMAIN_PLUGIN m_domain = DOMAIN_PLUGIN::TIME; + + void init(DOMAIN_PLUGIN domain, std::filesystem::path file) + { + m_domain = domain; + m_file = file; + + // Инициализация структуры для libsndfile и открытие файла + SF_INFO sf_info; + auto deleter = [&](SNDFILE* f) { if (f) sf_close(f); hack::log()("setup file completed", m_file);}; + std::unique_ptr sf_file(sf_open(m_file.c_str(), SFM_READ, &sf_info), deleter); + + if (!sf_file) + { + // Обработка ошибки открытия файла + hack::exception ex; + hack::log().on_file(); + hack::log().on_func(); + hack::log().on_row(); + ex.title("Error of open file"); + ex.description(sf_strerror(sf_file.get())); + ex.set("file", m_file); + hack::error()(ex); + throw ex; + } + + // HERE + // тут нужен DEBUG + print_info(); + + // Сохранение информации о файле в настройки + m_sample_rate = sf_info.samplerate; + m_frames = sf_info.frames; + m_channels = sf_info.channels; + + // можно конечн throw, но не нужно... + if (m_channels == 0) + hack::error()("Нет каналов в аудиофайле"); + } + + void print_info() const noexcept + { + hack::log()("file", m_file); + hack::log()("domain", domain_to_string(m_domain)); + hack::log()("sample rate", m_sample_rate); + hack::log()("channels", m_channels); + hack::log()("frames", m_frames); + } }; }