File indexing completed on 2024-11-10 05:11:09

0001 #include "audiorec.h"
0002 #include "controllerconfig.h"
0003 
0004 #include <QUrl>
0005 #include <QFile>
0006 #include <QDir>
0007 #include <QDebug>
0008 #include <QAudioBuffer>
0009 #include <QAudioDevice>
0010 #include <QMediaDevices>
0011 #include <QIODevice>
0012 #include <QSysInfo>
0013 
0014 AudioRec::AudioRec(QObject *parent) :
0015     QObject(parent),
0016     m_controller(MycroftController::instance())
0017 {
0018 
0019 }
0020 
0021 void AudioRec::recordTStart()
0022 {
0023     QByteArray byteArray;
0024     QBuffer buffer(&byteArray);
0025     buffer.open( QIODevice::ReadWrite | QIODevice::Truncate );
0026     QAudioFormat format;
0027     format.setSampleRate(8000);
0028     format.setChannelCount(1);
0029     // format.setSampleSize(8);
0030     // format.setCodec(QStringLiteral("audio/pcm"));
0031     // format.setByteOrder(QSysInfo::LittleEndian);
0032     format.setSampleFormat(QAudioFormat::UInt8);
0033 
0034     QAudioDevice info = QMediaDevices::defaultAudioInput();
0035     if (!info.isFormatSupported(format)) {
0036         qWarning() << "Format is not supported.";
0037         format = info.preferredFormat();
0038     }
0039 
0040     audio = new QAudioSource(format, this);
0041     device = audio->start();
0042     destinationFile.setFileName(QStringLiteral("/tmp/mycroft_in.raw"));
0043     destinationFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
0044     connect(device, &QIODevice::readyRead, this, &AudioRec::captureDataFromDevice);
0045 }
0046 
0047 void AudioRec::recordTStop()
0048 {
0049     audio->stop();
0050     destinationFile.close();
0051     emit recordTStatus(QStringLiteral("Completed"));
0052 }
0053 
0054 void AudioRec::readStream()
0055 {
0056     QFile inputFile;
0057     QDir::setCurrent(QStringLiteral("/tmp"));
0058     inputFile.setFileName(QStringLiteral("mycroft_in.raw"));
0059     m_audStream_size = inputFile.size();
0060     qDebug() << "filesize: " << m_audStream_size;
0061     inputFile.open(QIODevice::ReadWrite);
0062     while(!inputFile.atEnd()) {
0063       m_audStream = inputFile.readAll();
0064     }
0065     inputFile.close();
0066 }
0067 
0068 void AudioRec::returnStream()
0069 {
0070     QJsonObject dataObject;
0071     QByteArray utteranceArray;
0072     utteranceArray.push_front(m_audStream.toHex());
0073     dataObject.insert(QStringLiteral("lang"), QStringLiteral("en-us"));
0074     dataObject.insert(QStringLiteral("audio"), QJsonValue::fromVariant(utteranceArray));
0075     m_controller->sendBinary(QStringLiteral("recognizer_loop:incoming_aud"), dataObject);
0076 }
0077 
0078 void AudioRec::captureDataFromDevice()
0079 {
0080     QByteArray inputByteArray = device->readAll();
0081     destinationFile.write(inputByteArray);
0082     const int channelbytes = (audio->format().sampleRate() * audio->format().channelCount()) / 8;
0083     const int samplecount = inputByteArray.size() / channelbytes;
0084     quint32 maxvalue= 0;
0085     quint32 value = 0;
0086     quint32 amp = 255;
0087     const unsigned char* ptr = reinterpret_cast<const unsigned char*>(inputByteArray.data());
0088     for (int i = 0; i < samplecount; ++i) {
0089         for (int j = 0; j < audio->format().channelCount(); ++j) {
0090             value = *reinterpret_cast<const quint8*>(ptr);
0091         }
0092         maxvalue = qMax(value, maxvalue);
0093         ptr += channelbytes;
0094     }
0095     maxvalue = qMin(maxvalue, amp);
0096     qreal m_level = qreal(maxvalue) / amp;
0097     emit micAudioLevelChanged(m_level);
0098 }