87 lines
2.3 KiB
C++
87 lines
2.3 KiB
C++
#include <stdio.h>
|
|
#include <math.h>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <thread>
|
|
#include <chrono>
|
|
#include <deque>
|
|
|
|
#include "../../src/audio/AudioOutput.h"
|
|
#include "../../src/audio/AudioInput.h"
|
|
#include "../../src/audio/filter/FilterVad.h"
|
|
#include "../../src/audio/filter/FilterThreshold.h"
|
|
#include "../../src/audio/Audio.h"
|
|
|
|
#ifdef WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
using namespace std;
|
|
using namespace tc;
|
|
|
|
int main() {
|
|
string error;
|
|
if(!tc::audio::initialize(error)) {
|
|
cerr << "Failed to initialize audio: " << error << endl;
|
|
return 1;
|
|
}
|
|
auto playback_manager = audio::AudioOutput(2, 48000);
|
|
if(!playback_manager.open_device(error, Pa_GetDefaultOutputDevice())) {
|
|
cerr << "Failed to open output device (" << error << ")" << endl;
|
|
return 1;
|
|
}
|
|
if(!playback_manager.playback()) {
|
|
cerr << "failed to start playback" << endl;
|
|
return 1;
|
|
}
|
|
|
|
auto input = audio::AudioInput(2, 48000);
|
|
if(!input.open_device(error, Pa_GetDefaultInputDevice())) {
|
|
cerr << "Failed to open input device (" << error << ")" << endl;
|
|
return 1;
|
|
}
|
|
if(!input.record()) {
|
|
cerr << "failed to start record" << endl;
|
|
return 1;
|
|
}
|
|
|
|
{
|
|
auto consumer = input.create_consumer(960);
|
|
auto target_stream = playback_manager.create_source();
|
|
auto vad_handler = make_shared<audio::filter::VadFilter>(2, 48000, 960);
|
|
if(!vad_handler->initialize(error, 3, 4)) {
|
|
cerr << "failed to initialize vad handler (" << error << ")";
|
|
return 1;
|
|
}
|
|
|
|
auto threshold_filter = make_shared<audio::filter::ThresholdFilter>(2, 48000, 960);
|
|
if(!threshold_filter->initialize(error, .5, 5)) {
|
|
cerr << "failed to initialize threashold handler (" << error << ")";
|
|
return 1;
|
|
}
|
|
|
|
consumer->on_read = [target_stream, vad_handler, threshold_filter](const void* buffer, size_t samples) {
|
|
target_stream->enqueue_samples(buffer, samples);
|
|
|
|
cout << "T: " << threshold_filter->analyze(buffer, 0) << endl;
|
|
if(vad_handler->process(buffer)) {
|
|
cout << "Read " << samples << endl;
|
|
target_stream->enqueue_samples(buffer, samples);
|
|
} else {
|
|
cout << "Drop " << samples << endl;
|
|
}
|
|
};
|
|
cout << "Read started" << endl;
|
|
}
|
|
|
|
this_thread::sleep_for(chrono::seconds(60));
|
|
|
|
/*
|
|
while(true) {
|
|
this_thread::sleep_for(chrono::seconds(1000));
|
|
}
|
|
*/
|
|
Pa_Terminate();
|
|
return 1;
|
|
} |