main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <nlohmann/json.hpp>
  5. #include "signalstats_wrapper.h"
  6. #include "ThreadPool.h"
  7. void test1();
  8. void test2();
  9. int main() {
  10. // 初始化Python解释器
  11. signalstats_wrapper::initialize();
  12. // try {
  13. // // 创建测试音频信号 (C++版本)
  14. // const int sample_rate = 44100;
  15. // const int duration_seconds = 1;
  16. // const int num_samples = sample_rate * duration_seconds;
  17. // std::vector<double> audio_signal(num_samples);
  18. // std::random_device rd;
  19. // std::mt19937 gen(rd());
  20. // std::normal_distribution<> dist(0.0, 1.0);
  21. // for (auto& sample : audio_signal) {
  22. // sample = dist(gen);
  23. // }
  24. // std::cout << "生成测试音频信号完成" << std::endl;
  25. // // 调用C++接口函数
  26. // nlohmann::json output;
  27. // std::vector<std::string> window_params = {"tukey", "0.25"};
  28. // nlohmann::json& result = signalstats_wrapper::detect_signal_wrapper(
  29. // output,
  30. // audio_signal,
  31. // sample_rate,
  32. // 3e-3, // silence_threshold
  33. // -70.0, // db_threshold
  34. // -70.0, // cv_threshold
  35. // window_params,
  36. // 256, // nperseg
  37. // 32, // noverlap
  38. // 256, // nfft
  39. // true // debug
  40. // );
  41. // // 处理输出结果
  42. // if (!output.empty()) {
  43. // std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  44. // // 这里可以添加具体的结果解析逻辑
  45. // } else {
  46. // std::cout << "信号检测完成,但无输出结果" << std::endl;
  47. // }
  48. // std::cout << "程序执行完成" << std::endl;
  49. // } catch (const std::exception& e) {
  50. // std::cerr << "错误: " << e.what() << std::endl;
  51. // return 1;
  52. // }
  53. // test1();
  54. CPPTP.add_task(test1);
  55. // std::this_thread::sleep_for(std::chrono::seconds(1));
  56. CPPTP.add_task(test2);
  57. // std::this_thread::sleep_for(std::chrono::seconds(5));
  58. std::this_thread::sleep_for(std::chrono::seconds(10)); // 等待任务执行
  59. signalstats_wrapper::finalize();
  60. return 0;
  61. }
  62. void test1()
  63. {
  64. // signalstats_wrapper::initialize();
  65. try {
  66. // 创建测试音频信号 (C++版本)
  67. const int sample_rate = 44100;
  68. const int duration_seconds = 1;
  69. const int num_samples = sample_rate * duration_seconds;
  70. std::vector<double> audio_signal(num_samples);
  71. std::random_device rd;
  72. std::mt19937 gen(rd());
  73. std::normal_distribution<> dist(0.0, 1.0);
  74. for (auto& sample : audio_signal) {
  75. sample = dist(gen);
  76. }
  77. std::cout << "生成测试音频信号完成" << std::endl;
  78. // 调用C++接口函数
  79. nlohmann::json output;
  80. std::vector<std::string> window_params = {"tukey", "0.25"};
  81. nlohmann::json& result = signalstats_wrapper::detect_signal_wrapper(
  82. output,
  83. audio_signal,
  84. sample_rate,
  85. 3e-3, // silence_threshold
  86. -70.0, // db_threshold
  87. -70.0, // cv_threshold
  88. window_params,
  89. 256, // nperseg
  90. 32, // noverlap
  91. 256, // nfft
  92. true // debug
  93. );
  94. // 处理输出结果
  95. if (!output.empty()) {
  96. std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  97. // 这里可以添加具体的结果解析逻辑
  98. } else {
  99. std::cout << "信号检测完成,但无输出结果" << std::endl;
  100. }
  101. std::cout << "程序执行完成" << std::endl;
  102. } catch (const std::exception& e) {
  103. std::cerr << "错误: " << e.what() << std::endl;
  104. return;
  105. }
  106. // signalstats_wrapper::finalize();
  107. }
  108. void test2()
  109. {
  110. // signalstats_wrapper::initialize();
  111. try {
  112. // 创建测试音频信号 (C++版本)
  113. const int sample_rate = 44100;
  114. const int duration_seconds = 1;
  115. const int num_samples = sample_rate * duration_seconds;
  116. std::vector<double> audio_signal(num_samples);
  117. std::random_device rd;
  118. std::mt19937 gen(rd());
  119. std::normal_distribution<> dist(0.0, 1.0);
  120. for (auto& sample : audio_signal) {
  121. sample = dist(gen);
  122. }
  123. std::cout << "生成测试音频信号完成" << std::endl;
  124. // 调用C++接口函数
  125. nlohmann::json output;
  126. std::vector<std::string> window_params = {"tukey", "0.25"};
  127. nlohmann::json& result = signalstats_wrapper::detect_signal_wrapper(
  128. output,
  129. audio_signal,
  130. sample_rate,
  131. 3e-3, // silence_threshold
  132. -70.0, // db_threshold
  133. -70.0, // cv_threshold
  134. window_params,
  135. 256, // nperseg
  136. 32, // noverlap
  137. 256, // nfft
  138. true // debug
  139. );
  140. // 处理输出结果
  141. if (!output.empty()) {
  142. std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  143. // 这里可以添加具体的结果解析逻辑
  144. } else {
  145. std::cout << "信号检测完成,但无输出结果" << std::endl;
  146. }
  147. std::cout << "程序执行完成" << std::endl;
  148. } catch (const std::exception& e) {
  149. std::cerr << "错误: " << e.what() << std::endl;
  150. return;
  151. }
  152. // signalstats_wrapper::finalize();
  153. }