Example #1
0
void
AnalyserNode::GetFloatFrequencyData(const Float32Array& aArray)
{
  if (!FFTAnalysis()) {
    // Might fail to allocate memory
    return;
  }

  float* buffer = aArray.Data();
  uint32_t length = std::min(aArray.Length(), mOutputBuffer.Length());

  for (uint32_t i = 0; i < length; ++i) {
    buffer[i] = WebAudioUtils::ConvertLinearToDecibels(mOutputBuffer[i], mMinDecibels);
  }
}
Example #2
0
void
AnalyserNode::GetByteFrequencyData(const Uint8Array& aArray)
{
  if (!FFTAnalysis()) {
    // Might fail to allocate memory
    return;
  }

  const double rangeScaleFactor = 1.0 / (mMaxDecibels - mMinDecibels);

  unsigned char* buffer = aArray.Data();
  uint32_t length = std::min(aArray.Length(), mOutputBuffer.Length());

  for (uint32_t i = 0; i < length; ++i) {
    const double decibels = WebAudioUtils::ConvertLinearToDecibels(mOutputBuffer[i], mMinDecibels);
    // scale down the value to the range of [0, UCHAR_MAX]
    const double scaled = std::max(0.0, std::min(double(UCHAR_MAX),
                                                 UCHAR_MAX * (decibels - mMinDecibels) * rangeScaleFactor));
    buffer[i] = static_cast<unsigned char>(scaled);
  }
}
Example #3
0
double  ReadData()
{
    //Return if audio input is null
    if(!m_audioInput)
        return 0;

    //Check the number of samples in input buffer
    qint64 len = m_audioInput->bytesReady();

    if(len >= BufferSize/2)
    {
//        printf("bytesReady = %lld \n", len);
         m_buffer.resize(len);

        //Read sound samples from input device to buffer
        qint64 ret = m_input->read(m_buffer.data(), len);

//        printf("bytesReady = %lld, byte_read = %lld\n", len, ret);

        //Assign sound samples to short array
        short* data = (short*)m_buffer.data();

        m_output->write((char*)data, ret); // play sound!!

        int word_read = ret/sizeof(short);

        double* result = (double*)malloc(sizeof(double)*len);
        int i;
//       for (i=0; i < word_read; i++)
//        {
//            data[i] = data[i]; // можно менять громкость с микро))
//            if (data[i]/35000.0 < 0.105)
//            {
//                data[i] = 0;
//            }
//       }
        qint64 len2 = 1024;
        while (2*len2 <= word_read)
        {
            len2 = 2*len2;
        }
//       printf("len2 = %lld\n", len2);
        double* data2 = (double*)malloc(sizeof(double)*len2);
       for (i=0; i < len2; i++)
        {
            data2[i] = data[i]/35000.0; // а прост) честно я не знаю нафига я это сделал. Мне маленькие числа нравятся больше)))
       }

        FFTAnalysis(data2, result, len2, len2);

//        FILE *f = fopen("spectrum.txt", "w");
        double max = 0;
        double maxi = -1;
          for(i=0; i<len2/2; i++)
          {
//            fprintf(f, "%d  %lf  %lf\n", data[i], data2[i], result[i]);
            if(max < result[i])
            {
                max = result[i];
                maxi = i;
            }
          }
 //         fclose(f);
           printf("%lf %lf \n", max, maxi*SampleRate/len2);

        free(data2);
        free(result);

//        m_audioOutput->stop();
//        m_audioInput->stop();

//        m_output= m_audioOutput->start();
//        m_input = m_audioInput->start();

        return max;
    }

    return 0;
}