// Copy constructor. FFTFrame::FFTFrame(const FFTFrame& frame) : m_FFTSize(frame.m_FFTSize), m_log2FFTSize(frame.m_log2FFTSize), m_realData(frame.m_FFTSize / 2), m_imagData(frame.m_FFTSize / 2), m_forwardContext(nullptr), m_inverseContext(nullptr), m_complexData(frame.m_FFTSize) { m_forwardContext = contextForSize(m_FFTSize, DFT_R2C); m_inverseContext = contextForSize(m_FFTSize, IDFT_C2R); // Copy/setup frame data. unsigned nbytes = sizeof(float) * (m_FFTSize / 2); memcpy(realData(), frame.realData(), nbytes); memcpy(imagData(), frame.imagData(), nbytes); }
void Chroma::Consume(const FFTFrame &frame) { fill(m_features.begin(), m_features.end(), 0.0); for (int i = m_min_index; i < m_max_index; i++) { int note = m_notes[i]; double energy = frame.Energy(i); if (m_interpolate) { int note2 = note; double a = 1.0; if (m_notes_frac[i] < 0.5) { note2 = (note + NUM_BANDS - 1) % NUM_BANDS; a = 0.5 + m_notes_frac[i]; } if (m_notes_frac[i] > 0.5) { note2 = (note + 1) % NUM_BANDS; a = 1.5 - m_notes_frac[i]; } m_features[note] += energy * a; m_features[note2] += energy * (1.0 - a); } else { m_features[note] += energy; } } m_consumer->Consume(m_features); }
// Copy constructor. FFTFrame::FFTFrame(const FFTFrame& frame) : m_FFTSize(frame.m_FFTSize) , m_log2FFTSize(frame.m_log2FFTSize) , m_realData(unpackedFFTDataSize(frame.m_FFTSize)) , m_imagData(unpackedFFTDataSize(frame.m_FFTSize)) { m_complexData = WTF::fastNewArray<GstFFTF32Complex>(unpackedFFTDataSize(m_FFTSize)); int fftLength = gst_fft_next_fast_length(m_FFTSize); m_fft = gst_fft_f32_new(fftLength, FALSE); m_inverseFft = gst_fft_f32_new(fftLength, TRUE); // Copy/setup frame data. memcpy(realData(), frame.realData(), sizeof(float) * unpackedFFTDataSize(m_FFTSize)); memcpy(imagData(), frame.imagData(), sizeof(float) * unpackedFFTDataSize(m_FFTSize)); }
void FFTLib::Compute(FFTFrame &frame) { kiss_fftr(m_cfg, m_input, m_output); auto input = m_output; auto output = frame.data(); for (size_t i = 0; i <= m_frame_size / 2; ++i, ++input, ++output) { *output = input->r * input->r + input->i * input->i; } }
// Copy constructor. FFTFrame::FFTFrame(const FFTFrame& frame) : m_FFTSize(frame.m_FFTSize) , m_log2FFTSize(frame.m_log2FFTSize) , m_forwardPlan(0) , m_backwardPlan(0) , m_data(2 * (3 + unpackedFFTWDataSize(fftSize()))) // enough space for real and imaginary data plus 16-byte alignment padding { // See the normal constructor for an explanation of the temporary pointer. float temporary; m_forwardPlan = fftwPlanForSize(m_FFTSize, Forward, &temporary, realData(), imagData()); m_backwardPlan = fftwPlanForSize(m_FFTSize, Backward, realData(), imagData(), &temporary); // Copy/setup frame data. size_t nbytes = sizeof(float) * unpackedFFTWDataSize(fftSize()); memcpy(realData(), frame.realData(), nbytes); memcpy(imagData(), frame.imagData(), nbytes); }
void FFTLib::Compute(FFTFrame &frame) { av_rdft_calc(m_rdft_ctx, m_input); auto input = m_input; auto output = frame.begin(); output[0] = input[0] * input[0]; output[m_frame_size / 2] = input[1] * input[1]; output += 1; input += 2; for (size_t i = 1; i < m_frame_size / 2; i++) { *output++ = input[0] * input[0] + input[1] * input[1]; input += 2; } }
void FFTLib::Compute(FFTFrame &frame) { fftw_execute(m_plan); auto output = frame.data(); auto in_ptr = m_output; auto rev_in_ptr = m_output + m_frame_size - 1; output[0] = in_ptr[0] * in_ptr[0]; output[m_frame_size / 2] = in_ptr[m_frame_size / 2] * in_ptr[m_frame_size / 2]; in_ptr += 1; output += 1; for (size_t i = 1; i < m_frame_size / 2; i++) { *output++ = in_ptr[0] * in_ptr[0] + rev_in_ptr[0] * rev_in_ptr[0]; in_ptr++; rev_in_ptr--; } }
std::unique_ptr<FFTFrame> FFTFrame::createInterpolatedFrame(const FFTFrame& frame1, const FFTFrame& frame2, double x) { std::unique_ptr<FFTFrame> newFrame(new FFTFrame(frame1.fftSize())); newFrame->interpolateFrequencyComponents(frame1, frame2, x); // In the time-domain, the 2nd half of the response must be zero, to avoid circular convolution aliasing... int fftSize = newFrame->fftSize(); AudioFloatArray buffer(fftSize); newFrame->doInverseFFT(buffer.data()); buffer.zeroRange(fftSize / 2, fftSize); // Put back into frequency domain. newFrame->doFFT(buffer.data()); return newFrame; }
void FFTFrame::interpolateFrequencyComponents(const FFTFrame& frame1, const FFTFrame& frame2, double interp) { // FIXME : with some work, this method could be optimized float* realP = realData(); float* imagP = imagData(); const float* realP1 = frame1.realData(); const float* imagP1 = frame1.imagData(); const float* realP2 = frame2.realData(); const float* imagP2 = frame2.imagData(); m_FFTSize = frame1.fftSize(); m_log2FFTSize = frame1.log2FFTSize(); double s1base = (1.0 - interp); double s2base = interp; double phaseAccum = 0.0; double lastPhase1 = 0.0; double lastPhase2 = 0.0; realP[0] = static_cast<float>(s1base * realP1[0] + s2base * realP2[0]); imagP[0] = static_cast<float>(s1base * imagP1[0] + s2base * imagP2[0]); int n = m_FFTSize / 2; for (int i = 1; i < n; ++i) { Complex c1(realP1[i], imagP1[i]); Complex c2(realP2[i], imagP2[i]); double mag1 = abs(c1); double mag2 = abs(c2); // Interpolate magnitudes in decibels double mag1db = 20.0 * log10(mag1); double mag2db = 20.0 * log10(mag2); double s1 = s1base; double s2 = s2base; double magdbdiff = mag1db - mag2db; // Empirical tweak to retain higher-frequency zeroes double threshold = (i > 16) ? 5.0 : 2.0; if (magdbdiff < -threshold && mag1db < 0.0) { s1 = pow(s1, 0.75); s2 = 1.0 - s1; } else if (magdbdiff > threshold && mag2db < 0.0) { s2 = pow(s2, 0.75); s1 = 1.0 - s2; } // Average magnitude by decibels instead of linearly double magdb = s1 * mag1db + s2 * mag2db; double mag = pow(10.0, 0.05 * magdb); // Now, deal with phase double phase1 = arg(c1); double phase2 = arg(c2); double deltaPhase1 = phase1 - lastPhase1; double deltaPhase2 = phase2 - lastPhase2; lastPhase1 = phase1; lastPhase2 = phase2; // Unwrap phase deltas if (deltaPhase1 > piDouble) deltaPhase1 -= 2.0 * piDouble; if (deltaPhase1 < -piDouble) deltaPhase1 += 2.0 * piDouble; if (deltaPhase2 > piDouble) deltaPhase2 -= 2.0 * piDouble; if (deltaPhase2 < -piDouble) deltaPhase2 += 2.0 * piDouble; // Blend group-delays double deltaPhaseBlend; if (deltaPhase1 - deltaPhase2 > piDouble) deltaPhaseBlend = s1 * deltaPhase1 + s2 * (2.0 * piDouble + deltaPhase2); else if (deltaPhase2 - deltaPhase1 > piDouble) deltaPhaseBlend = s1 * (2.0 * piDouble + deltaPhase1) + s2 * deltaPhase2; else deltaPhaseBlend = s1 * deltaPhase1 + s2 * deltaPhase2; phaseAccum += deltaPhaseBlend; // Unwrap if (phaseAccum > piDouble) phaseAccum -= 2.0 * piDouble; if (phaseAccum < -piDouble) phaseAccum += 2.0 * piDouble; Complex c = std::polar(mag, phaseAccum); realP[i] = static_cast<float>(c.real()); imagP[i] = static_cast<float>(c.imag()); } }