Path: blob/master/servers/audio/effects/audio_effect_spectrum_analyzer.cpp
10278 views
/**************************************************************************/1/* audio_effect_spectrum_analyzer.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_effect_spectrum_analyzer.h"31#include "servers/audio_server.h"3233static void smbFft(float *fftBuffer, long fftFrameSize, long sign)34/*35FFT routine, (C)1996 S.M.Bernsee. Sign = -1 is FFT, 1 is iFFT (inverse)36Fills fftBuffer[0...2*fftFrameSize-1] with the Fourier transform of the37time domain data in fftBuffer[0...2*fftFrameSize-1]. The FFT array takes38and returns the cosine and sine parts in an interleaved manner, ie.39fftBuffer[0] = cosPart[0], fftBuffer[1] = sinPart[0], asf. fftFrameSize40must be a power of 2. It expects a complex input signal (see footnote 2),41ie. when working with 'common' audio signals our input signal has to be42passed as {in[0],0.,in[1],0.,in[2],0.,...} asf. In that case, the transform43of the frequencies of interest is in fftBuffer[0...fftFrameSize].44*/45{46float wr, wi, arg, *p1, *p2, temp;47float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;48long i, bitm, j, le, le2, k;4950for (i = 2; i < 2 * fftFrameSize - 2; i += 2) {51for (bitm = 2, j = 0; bitm < 2 * fftFrameSize; bitm <<= 1) {52if (i & bitm) {53j++;54}55j <<= 1;56}57if (i < j) {58p1 = fftBuffer + i;59p2 = fftBuffer + j;60temp = *p1;61*(p1++) = *p2;62*(p2++) = temp;63temp = *p1;64*p1 = *p2;65*p2 = temp;66}67}68for (k = 0, le = 2; k < (long)(std::log((double)fftFrameSize) / std::log(2.) + .5); k++) {69le <<= 1;70le2 = le >> 1;71ur = 1.0;72ui = 0.0;73arg = Math::PI / (le2 >> 1);74wr = std::cos(arg);75wi = sign * std::sin(arg);76for (j = 0; j < le2; j += 2) {77p1r = fftBuffer + j;78p1i = p1r + 1;79p2r = p1r + le2;80p2i = p2r + 1;81for (i = j; i < 2 * fftFrameSize; i += le) {82tr = *p2r * ur - *p2i * ui;83ti = *p2r * ui + *p2i * ur;84*p2r = *p1r - tr;85*p2i = *p1i - ti;86*p1r += tr;87*p1i += ti;88p1r += le;89p1i += le;90p2r += le;91p2i += le;92}93tr = ur * wr - ui * wi;94ui = ur * wi + ui * wr;95ur = tr;96}97}98}99100void AudioEffectSpectrumAnalyzerInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {101uint64_t time = OS::get_singleton()->get_ticks_usec();102103//copy everything over first, since this only really does capture104for (int i = 0; i < p_frame_count; i++) {105p_dst_frames[i] = p_src_frames[i];106}107108//capture spectrum109while (p_frame_count) {110int to_fill = fft_size * 2 - temporal_fft_pos;111to_fill = MIN(to_fill, p_frame_count);112const double to_fill_step = Math::TAU / (double)fft_size;113114float *fftw = temporal_fft.ptrw();115for (int i = 0; i < to_fill; i++) { //left and right buffers116float window = -0.5 * Math::cos(to_fill_step * (double)temporal_fft_pos) + 0.5;117fftw[temporal_fft_pos * 2] = window * p_src_frames->left;118fftw[temporal_fft_pos * 2 + 1] = 0;119fftw[(temporal_fft_pos + fft_size * 2) * 2] = window * p_src_frames->right;120fftw[(temporal_fft_pos + fft_size * 2) * 2 + 1] = 0;121++p_src_frames;122++temporal_fft_pos;123}124125p_frame_count -= to_fill;126127if (temporal_fft_pos == fft_size * 2) {128//time to do a FFT129smbFft(fftw, fft_size * 2, -1);130smbFft(fftw + fft_size * 4, fft_size * 2, -1);131int next = (fft_pos + 1) % fft_count;132133AudioFrame *hw = (AudioFrame *)fft_history[next].ptr(); //do not use write, avoid cow134135for (int i = 0; i < fft_size; i++) {136//abs(vec)/fft_size normalizes each frequency137hw[i].left = Vector2(fftw[i * 2], fftw[i * 2 + 1]).length() / float(fft_size);138hw[i].right = Vector2(fftw[fft_size * 4 + i * 2], fftw[fft_size * 4 + i * 2 + 1]).length() / float(fft_size);139}140141fft_pos = next; //swap142temporal_fft_pos = 0;143}144}145146//determine time of capture147double remainder_sec = (temporal_fft_pos / mix_rate); //subtract remainder from mix time148last_fft_time = time - uint64_t(remainder_sec * 1000000.0);149}150151void AudioEffectSpectrumAnalyzerInstance::_bind_methods() {152ClassDB::bind_method(D_METHOD("get_magnitude_for_frequency_range", "from_hz", "to_hz", "mode"), &AudioEffectSpectrumAnalyzerInstance::get_magnitude_for_frequency_range, DEFVAL(MAGNITUDE_MAX));153BIND_ENUM_CONSTANT(MAGNITUDE_AVERAGE);154BIND_ENUM_CONSTANT(MAGNITUDE_MAX);155}156157Vector2 AudioEffectSpectrumAnalyzerInstance::get_magnitude_for_frequency_range(float p_begin, float p_end, MagnitudeMode p_mode) const {158if (last_fft_time == 0) {159return Vector2();160}161uint64_t time = OS::get_singleton()->get_ticks_usec();162float diff = double(time - last_fft_time) / 1000000.0 + base->get_tap_back_pos();163diff -= AudioServer::get_singleton()->get_output_latency();164float fft_time_size = float(fft_size) / mix_rate;165166int fft_index = fft_pos;167168while (diff > fft_time_size) {169diff -= fft_time_size;170fft_index -= 1;171if (fft_index < 0) {172fft_index = fft_count - 1;173}174}175176int begin_pos = p_begin * fft_size / (mix_rate * 0.5);177int end_pos = p_end * fft_size / (mix_rate * 0.5);178179begin_pos = CLAMP(begin_pos, 0, fft_size - 1);180end_pos = CLAMP(end_pos, 0, fft_size - 1);181182if (begin_pos > end_pos) {183SWAP(begin_pos, end_pos);184}185const AudioFrame *r = fft_history[fft_index].ptr();186187if (p_mode == MAGNITUDE_AVERAGE) {188Vector2 avg;189190for (int i = begin_pos; i <= end_pos; i++) {191avg += Vector2(r[i]);192}193194avg /= float(end_pos - begin_pos + 1);195196return avg;197} else {198Vector2 max;199200for (int i = begin_pos; i <= end_pos; i++) {201max.x = MAX(max.x, r[i].left);202max.y = MAX(max.y, r[i].right);203}204205return max;206}207}208209Ref<AudioEffectInstance> AudioEffectSpectrumAnalyzer::instantiate() {210Ref<AudioEffectSpectrumAnalyzerInstance> ins;211ins.instantiate();212ins->base = Ref<AudioEffectSpectrumAnalyzer>(this);213static const int fft_sizes[FFT_SIZE_MAX] = { 256, 512, 1024, 2048, 4096 };214ins->fft_size = fft_sizes[fft_size];215ins->mix_rate = AudioServer::get_singleton()->get_mix_rate();216ins->fft_count = (buffer_length / (float(ins->fft_size) / ins->mix_rate)) + 1;217ins->fft_pos = 0;218ins->last_fft_time = 0;219ins->fft_history.resize(ins->fft_count);220ins->temporal_fft.resize(ins->fft_size * 8); //x2 stereo, x2 amount of samples for freqs, x2 for input221ins->temporal_fft_pos = 0;222for (int i = 0; i < ins->fft_count; i++) {223ins->fft_history.write[i].resize(ins->fft_size); //only magnitude matters224for (int j = 0; j < ins->fft_size; j++) {225ins->fft_history.write[i].write[j] = AudioFrame(0, 0);226}227}228return ins;229}230231void AudioEffectSpectrumAnalyzer::set_buffer_length(float p_seconds) {232buffer_length = p_seconds;233}234235float AudioEffectSpectrumAnalyzer::get_buffer_length() const {236return buffer_length;237}238239void AudioEffectSpectrumAnalyzer::set_tap_back_pos(float p_seconds) {240tapback_pos = p_seconds;241}242243float AudioEffectSpectrumAnalyzer::get_tap_back_pos() const {244return tapback_pos;245}246247void AudioEffectSpectrumAnalyzer::set_fft_size(FFTSize p_fft_size) {248ERR_FAIL_INDEX(p_fft_size, FFT_SIZE_MAX);249fft_size = p_fft_size;250}251252AudioEffectSpectrumAnalyzer::FFTSize AudioEffectSpectrumAnalyzer::get_fft_size() const {253return fft_size;254}255256void AudioEffectSpectrumAnalyzer::_bind_methods() {257ClassDB::bind_method(D_METHOD("set_buffer_length", "seconds"), &AudioEffectSpectrumAnalyzer::set_buffer_length);258ClassDB::bind_method(D_METHOD("get_buffer_length"), &AudioEffectSpectrumAnalyzer::get_buffer_length);259260ClassDB::bind_method(D_METHOD("set_tap_back_pos", "seconds"), &AudioEffectSpectrumAnalyzer::set_tap_back_pos);261ClassDB::bind_method(D_METHOD("get_tap_back_pos"), &AudioEffectSpectrumAnalyzer::get_tap_back_pos);262263ClassDB::bind_method(D_METHOD("set_fft_size", "size"), &AudioEffectSpectrumAnalyzer::set_fft_size);264ClassDB::bind_method(D_METHOD("get_fft_size"), &AudioEffectSpectrumAnalyzer::get_fft_size);265266ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "buffer_length", PROPERTY_HINT_RANGE, "0.1,4,0.1,suffix:s"), "set_buffer_length", "get_buffer_length");267ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "tap_back_pos", PROPERTY_HINT_RANGE, "0.1,4,0.1"), "set_tap_back_pos", "get_tap_back_pos");268ADD_PROPERTY(PropertyInfo(Variant::INT, "fft_size", PROPERTY_HINT_ENUM, "256,512,1024,2048,4096"), "set_fft_size", "get_fft_size");269270BIND_ENUM_CONSTANT(FFT_SIZE_256);271BIND_ENUM_CONSTANT(FFT_SIZE_512);272BIND_ENUM_CONSTANT(FFT_SIZE_1024);273BIND_ENUM_CONSTANT(FFT_SIZE_2048);274BIND_ENUM_CONSTANT(FFT_SIZE_4096);275BIND_ENUM_CONSTANT(FFT_SIZE_MAX);276}277278AudioEffectSpectrumAnalyzer::AudioEffectSpectrumAnalyzer() {279buffer_length = 2;280tapback_pos = 0.01;281fft_size = FFT_SIZE_1024;282}283284285