How to convert QByteArray from QAudioInput to float

Hello,

I'm a beginner in c++ and especially in Qt.
I took the data that come from the QAudioInput and put them in a QByteArray.
But now I want to convert the QByteArray into a float array.
I search on many websites and I tried many ways to do it but nothing is working.
I used the function toFloat(bool ok) to convert it but every time that I'm doing this, ok is false (which mean that an error occured).
So I was thinking that maybe it's because there is some bytes equal to '\0' sometimes but even when there is no bytes like that, it still don't work.

Can anyone help me to solve this problem please ???
Thank you.

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;
}
QByteArray is essentially a string. The toFloat() is like atoi().

However, are you converting text to a number, or raw bytes to a float? If the latter, then:
http://stackoverflow.com/questions/3991478/building-a-32bit-float-out-of-its-4-composite-bytes-c
Thank you for your answer.
I want to convert raw bytes to a float.
So I already used the kind of solution that you can find here :
http://stackoverflow.com/questions/3991478/building-a-32bit-float-out-of-its-4-composite-bytes-c
But then I found just really high or small values like 1.1543E38 or -3.564E-40.
I don't think that this is right but even if it is, I need values between around +/- 1E4 so does anyone know how to do that or the reason of my high values here ?
I also this kind of error : -1.#QNAN so maybe it come from my code but I don't know ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void AudioInput::readMore(){

	byteArray=device->readAll();
	

	bool ok=true;
	int byteArraySize=byteArray.size();
	float data;
	
	QByteArray ba,baL,baR;
	
	for(int i=0;i<(int)(byteArraySize/4);i++){//byteArray.size()/16=220

		char myByte[]={byteArray.at(i*4),byteArray.at(i*4+1),byteArray.at(i*4+2),byteArray.at(i*4+3)};


		memcpy(&data,&myByte,sizeof(data));
		cout<<" ok : "<<ok<<" data: "<<setprecision(5)<<data<<" ba size: "<<byteArray.size()<<endl;
		
	}
	
	  audioInput->stop();

}
Topic archived. No new replies allowed.