1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include "audioinput.h"
#include "ui_audioinput.h"
#include <QFile>
#include <iostream>
#include <QByteArray>
using namespace std;
AudioInput::AudioInput(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AudioInput)
{
ui->setupUi(this);
mutex=vtkSmartPointer<vtkMutexLock>::New();
timeRef=new timeReference();
timeRef->initializeTimeReference();
}
void AudioInput::startRecording()
{
QAudioFormat format,format2;
format.setFrequency(80000);
format.setSampleRate(44000);
format.setChannels(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::Float);
audioInput = new QAudioInput(format,reinterpret_cast<QObject*>(this));
audioOutput = new QAudioOutput(format,reinterpret_cast<QObject*>(this));
audioInput->setNotifyInterval((int)(5));
QTimer::singleShot(10000, reinterpret_cast<QObject*>(this), SLOT(stopRecording()));
device=audioInput->start();
listen();
connect(audioInput, SIGNAL(notify()), this, SLOT(readMore()));
}
void AudioInput::readMore(){
byteArray=device->readAll();
int N=1;
bool ok=true;
int byteArraySize=byteArray.size();
vector<float> data;
char myByte;
QByteArray ba,baL,baR;
for(int i=0;i<(int)(byteArraySize/4);i++){
//ba.setNum(1.56308,'g',8);
for(int t=0;t<4;t++){
myByte = byteArray.at(i*4+t);
//for (int j = 7; j >= 0; --j) {
// cout << ((myByte >> j) & 1);
//}
//cout<<endl;
baL.push_back(myByte);
}
baL.push_back('\0');
data.push_back(baL.toFloat(&ok));
cout<<" ok : "<<ok<<" data: "<<data[i]<<" ba size: "<<byteArray.size()<<endl;
}
}
void AudioInput::listen(){
audioOutput->start(device);
}
void AudioInput::stopRecording()
{
audioInput->stop();
delete audioInput;
}
AudioInput::~AudioInput()
{
delete ui;
}
|