TeaSpeak-Client/native/serverconnection/test/audio/main.cpp

93 lines
2.2 KiB
C++
Raw Normal View History

2019-06-26 16:09:01 -04:00
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <deque>
#include "portaudio.h"
#ifndef WIN32
#include <pa_jack.h>
#endif
#include "../../src/audio/AudioOutput.h"
#include "../../src/audio/AudioInput.h"
#include "../../src/audio/filter/FilterVad.h"
#include "../../src/audio/filter/FilterThreshold.h"
#ifdef WIN32
#include <windows.h>
#if PA_USE_ASIO
#include "pa_asio.h"
#endif
#endif
using namespace std;
using namespace tc;
int main() {
string error;
PaError err;
#ifndef WIN32
PaJack_SetClientName("TeaClient-Test");
#endif
err = Pa_Initialize();
if( err != paNoError )
{
printf( "ERROR: Pa_Initialize returned 0x%x\n", err );
return 0;
}
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;
}
playback_manager.playback();
auto input = audio::AudioInput(2, 48000);
if(!input.open_device(error, Pa_GetDefaultInputDevice())) {
cerr << "Failed to open input device (" << error << ")" << endl;
return 1;
}
input.record();
{
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) {
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(10));
/*
while(true) {
this_thread::sleep_for(chrono::seconds(1000));
}
*/
Pa_Terminate();
return err;
}