|
|
@ -4,13 +4,51 @@ using namespace std; |
|
|
|
using namespace iflytop; |
|
|
|
using namespace core; |
|
|
|
|
|
|
|
#define TEST
|
|
|
|
|
|
|
|
AudioRecoderService::AudioRecoderService() {} |
|
|
|
void AudioRecoderService::initialize(const char *pcmName, unsigned int channels, unsigned int sample_rate, |
|
|
|
snd_pcm_format_t format) { |
|
|
|
#ifdef TEST
|
|
|
|
logger->info("In Test Mode"); |
|
|
|
m_inputFileStream.open("test_in_audio.wav", ios::in | ios::binary); |
|
|
|
if (!m_inputFileStream.is_open()) { |
|
|
|
logger->error("open file {} failed", "test_in_audio.wav"); |
|
|
|
return; |
|
|
|
} |
|
|
|
m_inputFileStream.seekg(44, ios::beg); |
|
|
|
#else
|
|
|
|
m_audioRecoder = make_shared<AudioRecoder>(); |
|
|
|
m_audioRecoder->initialize(pcmName, channels, sample_rate, format); |
|
|
|
|
|
|
|
m_audioRecoder->onRecordData.connect([this](shared_ptr<AudioClip> audioclip) { onRecordData(audioclip); }); |
|
|
|
#endif
|
|
|
|
} |
|
|
|
|
|
|
|
void AudioRecoderService::startRecord() { |
|
|
|
#ifdef TEST
|
|
|
|
if (m_thread) { |
|
|
|
m_thread->join(); |
|
|
|
m_thread = nullptr; |
|
|
|
} |
|
|
|
m_thread.reset(new Thread("testAudio", [&]() { |
|
|
|
ThisThread thisThread; |
|
|
|
uint8_t buf[8000]; |
|
|
|
while (!thisThread.getExitFlag()) { |
|
|
|
m_inputFileStream.read((char *)buf, 8000); |
|
|
|
if (m_inputFileStream.gcount() != 8000) { |
|
|
|
logger->info("read file end"); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
void AudioRecoderService::startRecord() { m_audioRecoder->startRecord(); } |
|
|
|
shared_ptr<AudioClip> audioclip; |
|
|
|
audioclip.reset(new AudioClip(buf, 8000, 5, 16000, AudioFormat::S16_LE)); |
|
|
|
logger->info("report audioclip {}", 8000); |
|
|
|
onRecordData(audioclip); |
|
|
|
thisThread.sleepForMs(60); |
|
|
|
} |
|
|
|
logger->info("testAudio thread exit"); |
|
|
|
})); |
|
|
|
#else
|
|
|
|
m_audioRecoder->startRecord(); |
|
|
|
#endif
|
|
|
|
} |