I am a newbie of C++ and QT and i am actually on a project of writing a sound signal generator. But my problem is,
I am creating the floats to be implemented in qbytearray which i will use to fill qbuffer. But i can not get float into the qbytearray, it gives a warning saying "losing precision". And qbytearray consists of only integer values from -100 to 100. i need the floats with desired precision.
Can you help me ?
void MainWindow::toneGenerate(){
int len= m_seconds*SAMPLE_RATE;
sinbuf.resize(len);
for(int i=0;i<len;i++){
qreal t = m_freq*i;
t *= FREQ_CONST;
t = t+ m_ph;
t = qSin(t);
t*= m_amp;
sinbuf[i] = t;
}
sininput.setBuffer(&sinbuf);
sininput.open(QIODevice::ReadWrite);
}
When writing code for sound development, is important take care about the size of each sample, the byte order for storing the samples as binary data, and if is necessary to write a header to the data, or if it's raw, header-less.
And if your goal is fill a
QBufferyou can write to it troughQDataStream, and read it back if you will.In my answer, I'll assume Little Endian, and instead of float, I'll use 16 bits signed integer samples, 1 channel and 8000Hz frequency.
I'm providing a simple example of tone generator, please adapt it to your needs!
Let's see the following console example: