#include #include #include #include #include "signalstats.h" #include "ThreadPool.h" #include "RingQueueManualMutex.hpp" #include "spdlog/spdlog.h" #include #include #include #include #include #include void test1(); void test2(); void test3(); void test4(int argc, char** argv); int main(int argc, char* argv[]) { // 初始化Python解释器 // signalstats_wrapper::initialize(); // CPPTP.add_task(test1); // std::this_thread::sleep_for(std::chrono::seconds(1)); // CPPTP.add_task(test2); // std::this_thread::sleep_for(std::chrono::seconds(5)); // test3(); // std::this_thread::sleep_for(std::chrono::seconds(2)); // 等待任务执行 // signalstats_wrapper::finalize(); test4(argc, argv); return 0; } void test1() { try { // 创建测试音频信号 (C++版本) const int sample_rate = 44100; const int duration_seconds = 1; const int num_samples = sample_rate * duration_seconds; std::vector audio_signal(num_samples); std::random_device rd; std::mt19937 gen(rd()); std::normal_distribution<> dist(0.0, 1.0); for (auto& sample : audio_signal) { sample = dist(gen); } std::cout << "生成测试音频信号完成" << std::endl; // 调用C++接口函数 nlohmann::json output; std::vector window_params = {"tukey", "0.25"}; nlohmann::json& result = signalstats::detect_signal( output, audio_signal, sample_rate, 3e-3, // silence_threshold -70.0, // db_threshold -70.0, // cv_threshold window_params, 256, // nperseg 32, // noverlap 256 // nfft ); std::cout << "源数据: " << std::endl; for(const auto it : audio_signal) { std::cout << it << " "; } std::cout << std::endl; // 处理输出结果 if (!output.empty()) { std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl; // 这里可以添加具体的结果解析逻辑 } else { std::cout << "信号检测完成,但无输出结果" << std::endl; } std::cout << "程序执行完成" << std::endl; } catch (const std::exception& e) { std::cerr << "错误: " << e.what() << std::endl; return; } // signalstats_wrapper::finalize(); } void test2() { // signalstats_wrapper::initialize(); try { // 创建测试音频信号 (C++版本) const int sample_rate = 44100; const int duration_seconds = 1; const int num_samples = sample_rate * duration_seconds; std::vector audio_signal(num_samples); std::random_device rd; std::mt19937 gen(rd()); std::normal_distribution<> dist(0.0, 1.0); for (auto& sample : audio_signal) { sample = dist(gen); } std::cout << "生成测试音频信号完成" << std::endl; // 调用C++接口函数 nlohmann::json output; std::vector window_params = {"tukey", "0.25"}; nlohmann::json& result = signalstats::detect_signal( output, audio_signal, sample_rate, 3e-3, // silence_threshold -70.0, // db_threshold -70.0, // cv_threshold window_params, 256, // nperseg 32, // noverlap 256 // nfft ); // 处理输出结果 if (!output.empty()) { std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl; // 这里可以添加具体的结果解析逻辑 } else { std::cout << "信号检测完成,但无输出结果" << std::endl; } std::cout << "程序执行完成" << std::endl; } catch (const std::exception& e) { std::cerr << "错误: " << e.what() << std::endl; return; } // signalstats_wrapper::finalize(); } /* 测试环形队列 */ void test3() { RingQueueManualMutex queue(10); for(int i = 1; i <= 20; ++i) { int pop = queue.push(i); SPDLOG_INFO("Push: {}, pop:{}, Queue Size: {}", i, pop, queue.QueueSize()); } } void test4(int argc, char** argv) { QCoreApplication a(argc, argv); QAudioFormat format; format.setSampleRate(44100); format.setChannelCount(2); format.setSampleSize(16); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::SignedInt); QAudioOutput* audioOutput = new QAudioOutput(format); QIODevice* device = audioOutput->start(); if (!device) { SPDLOG_ERROR("Failed to start audio output device"); return; } QFile* pFile = new QFile(QCoreApplication::applicationDirPath() + "/test.wav"); if(!pFile->open(QIODevice::ReadOnly)) { SPDLOG_ERROR("Failed to open audio file: {}", pFile->errorString().toStdString()); return; } int readSize = 4410 * 4; char* buffer = new char[readSize]; // 1 second buffer for stereo 16-bit audio QTimer* ptimer = new QTimer(); ptimer->setInterval(100); ptimer->setSingleShot(false); ptimer->setTimerType(Qt::PreciseTimer); QObject::connect(ptimer, &QTimer::timeout, [&, device, pFile, buffer]() { SPDLOG_INFO("Timer triggered, reading audio data"); pFile->read(buffer, readSize); device->write(buffer, readSize); if(pFile->atEnd()) { pFile->seek(0); // 重新开始播放 SPDLOG_INFO("Reached end of file, restarting playback"); } }); ptimer->start(); a.exec(); }