qreal widthLevel = m_level * height(); painter.fillRect(0,0, 10,height(), Qt::black); painter.fillRect(0,height()-widthLevel,10, height(), Qt::red);
qreal getPeakValue(const QAudioFormat& format){ // qDebug() << format.codec(); // Note: Only the most common sample formats are supported if (!format.isValid()) return qreal(0); if (format.codec() != "audio/pcm") return qreal(0); switch (format.sampleType()) { case QAudioFormat::Unknown: break; case QAudioFormat::Float: if (format.sampleSize() != 32) // other sample formats are not supported return qreal(0); return qreal(1.00003); case QAudioFormat::SignedInt: if (format.sampleSize() == 32) return qreal(INT_MAX); if (format.sampleSize() == 16) return qreal(SHRT_MAX); if (format.sampleSize() == 8) return qreal(CHAR_MAX); break; case QAudioFormat::UnSignedInt: if (format.sampleSize() == 32) return qreal(UINT_MAX); if (format.sampleSize() == 16) return qreal(USHRT_MAX); if (format.sampleSize() == 8) return qreal(UCHAR_MAX); break; } return qreal(0);}// returns the audio level for each channelQVector<qreal> getBufferLevels(const QAudioBuffer& buffer){ QVector<qreal> values; if (!buffer.format().isValid() || buffer.format().byteOrder() != QAudioFormat::LittleEndian) return values; if (buffer.format().codec() != "audio/pcm") return values; int channelCount = buffer.format().channelCount(); values.fill(0, channelCount); qreal peak_value = getPeakValue(buffer.format()); if (qFuzzyCompare(peak_value, qreal(0))) return values; switch (buffer.format().sampleType()) { case QAudioFormat::Unknown: case QAudioFormat::UnSignedInt: if (buffer.format().sampleSize() == 32) values = getBufferLevels(buffer.constData<quint32>(), buffer.frameCount(), channelCount); if (buffer.format().sampleSize() == 16) values = getBufferLevels(buffer.constData<quint16>(), buffer.frameCount(), channelCount); if (buffer.format().sampleSize() == 8) values = getBufferLevels(buffer.constData<quint8>(), buffer.frameCount(), channelCount); for (int i = 0; i < values.size(); ++i) values[i] = qAbs(values.at(i) - peak_value / 2) / (peak_value / 2); break; case QAudioFormat::Float: if (buffer.format().sampleSize() == 32) { values = getBufferLevels(buffer.constData<float>(), buffer.frameCount(), channelCount); for (int i = 0; i < values.size(); ++i) values[i] /= peak_value; } break; case QAudioFormat::SignedInt: if (buffer.format().sampleSize() == 32) values = getBufferLevels(buffer.constData<qint32>(), buffer.frameCount(), channelCount); if (buffer.format().sampleSize() == 16) values = getBufferLevels(buffer.constData<qint16>(), buffer.frameCount(), channelCount); if (buffer.format().sampleSize() == 8) values = getBufferLevels(buffer.constData<qint8>(), buffer.frameCount(), channelCount); for (int i = 0; i < values.size(); ++i) values[i] /= peak_value; break; } return values;}template <class T>QVector<qreal> getBufferLevels(const T *buffer, int frames, int channels){ QVector<qreal> max_values; max_values.fill(0, channels); for (int i = 0; i < frames; ++i) { for (int j = 0; j < channels; ++j) { qreal value = qAbs(qreal(buffer[i * channels + j])); if (value > max_values.at(j)) max_values.replace(j, value); } } return max_values;}
QAudioEncoderSettings settings; settings.setCodec(QString('audio/mp3')); settings.setSampleRate(96000); settings.setBitRate(192); settings.setChannelCount(2); settings.setQuality(QMultimedia::EncodingQuality(100)); QAudioRecorder *recorder = new QAudioRecorder(); recorder->setEncodingSettings(settings, QVideoEncoderSettings(), QString('mp3'));