Ejemplo n.º 1
0
void SonicBoosterTest::signed16Data() {
  QAudioBuffer buffer = getBuffer<qint16>();

  qint16* raw_data = (qint16*)buffer.data();
  raw_data[0] = 0;
  raw_data[1] = 200;
  raw_data[2] = -200;

  QVERIFY(m_booster_m_6->boost(buffer));
  QVERIFY(m_booster_p_6->boost(buffer));

  int len0_5 = 0;
  const qint16* boosted0_5 = (qint16*)m_booster_m_6->getBoostedBuffer(len0_5);
  QCOMPARE(len0_5, 6);
  QCOMPARE((int)boosted0_5[0], 0);
  QCOMPARE((int)boosted0_5[1], (int)(200 * m_boost_factor_m_6));
  QCOMPARE((int)boosted0_5[2], (int)(-200 * m_boost_factor_m_6));

  int len1_5 = 0;
  const qint16* boosted1_5 = (qint16*)m_booster_p_6->getBoostedBuffer(len1_5);
  QCOMPARE(len1_5, 6);
  QCOMPARE((int)boosted1_5[0], 0);
  QCOMPARE((int)boosted1_5[1], (int)(200 * m_boost_factor_p_6));
  QCOMPARE((int)boosted1_5[2], (int)(-200 * m_boost_factor_p_6));
}
Ejemplo n.º 2
0
void SonicBoosterTest::unsigned8Data() {
  QAudioBuffer buffer = getBuffer<quint8>();

  quint8* raw_data = (quint8*)buffer.data();
  raw_data[0] = 128;
  raw_data[1] = 128 + 50;
  raw_data[2] = 128 - 50;

  QVERIFY(m_booster_m_6->boost(buffer));
  QVERIFY(m_booster_p_6->boost(buffer));

  int len0_5 = 0;
  const quint8* boosted0_5 = (quint8*)m_booster_m_6->getBoostedBuffer(len0_5);
  QCOMPARE(len0_5, 3);
  QCOMPARE((int)boosted0_5[0], 128);
  QCOMPARE((int)boosted0_5[1], 128 + (int)(50 * m_boost_factor_m_6));
  QCOMPARE((int)boosted0_5[2], 128 - (int)(50 * m_boost_factor_m_6));

  int len1_5 = 0;
  const quint8* boosted1_5 = (quint8*)m_booster_p_6->getBoostedBuffer(len1_5);
  QCOMPARE(len1_5, 3);
  QCOMPARE((int)boosted1_5[0], 128);
  QCOMPARE((int)boosted1_5[1], 128 + (int)(50 * m_boost_factor_p_6));
  QCOMPARE((int)boosted1_5[2], 128 - (int)(50 * m_boost_factor_p_6));
}
void AudioBuffer::init(QAudioBuffer &qtbuffer){
    qDebug() << "void AudioBuffer::init(...) called";
    QAudioFormat audioFormat = qtbuffer.format();
    this->hzFreq = audioFormat.sampleRate();
    this->durationInMs = qtbuffer.duration()*1000;
    QAudioFormat::SampleType sampleType
            = audioFormat.sampleType();
    int frameCount = qtbuffer.frameCount();
    int nChannels = audioFormat.channelCount();
    int bytesPerFrame = audioFormat.bytesPerFrame();
    int bytesPerValue = bytesPerFrame/nChannels;
    void *firstData = qtbuffer.data();
    this->bufferSize = frameCount;
    this->buffer = QSharedPointer<SharedBuffer>(
                new SharedBuffer);
    this->buffer->buffer = new int[this->bufferSize];
    this->mean = 0;
    const int tempReducFactor = 500;
    qint64 meanOfSquare = 0;
    for(int i=0; i<frameCount; i++){
        int currentValue = 0;
        for(int j=0; j<nChannels; j++){
            int currentPos
                    = i*bytesPerFrame
                    + j*bytesPerValue;
            void *valPos = (void *)(((quint8 *)firstData) + currentPos);
            if(sampleType == QAudioFormat::SignedInt){
                if(bytesPerValue == 1){
                    quint8 val = *((quint8*)valPos);
                    currentValue += val;
                }else if(bytesPerValue == 2){
                    quint16 val = *((quint16*)valPos);
                    currentValue += val;
                }else if(bytesPerValue == 4){
                    quint32 val = *((quint32*)valPos);
                    currentValue += val;
                }else if(bytesPerValue == 8){
                    quint64 val = *((quint64*)valPos);
                    currentValue += val;
                }
            }else if(sampleType == QAudioFormat::UnSignedInt){
                if(bytesPerValue == 1){
                    qint8 val = *((qint8*)valPos);
                    currentValue += val;
                }else if(bytesPerValue == 2){
                    qint16 val = *((qint16*)valPos);
                    currentValue += val;
                }else if(bytesPerValue == 4){
                    qint32 val = *((qint32*)valPos);
                    currentValue += val;
                }else if(bytesPerValue == 8){
                    qint64 val = *((qint64*)valPos);
                    currentValue += val;
                }
            }else if(sampleType == QAudioFormat::Float){
                qreal val = *((qreal*)valPos);
                currentValue += val;
            }
        }
        currentValue /= nChannels;
        currentValue /= tempReducFactor;
        this->buffer->buffer[i] = currentValue;
        this->mean += currentValue;
        meanOfSquare += currentValue*currentValue;
        if(meanOfSquare < 0){
            Q_ASSERT(false);
        }
    }
    this->mean /= frameCount;
    meanOfSquare /= frameCount;
    qint64 squaredMean =
            this->mean
            * this->mean;
    this->var = meanOfSquare
            - squaredMean;
    this->var *= tempReducFactor * tempReducFactor;
    this->mean *= tempReducFactor;
    this->sd = qSqrt(this->var);
    qDebug() << "void AudioBuffer::init(...) end";
}