mirror of
https://github.com/clangen/musikcube.git
synced 2025-03-14 04:18:36 +00:00
- The audio deinterleaving process works now
- Apply a Hamming Window to FFT input to reduce spectral leakage - Reduced memory usage by doing FFT with real inputs
This commit is contained in:
parent
793bb9102b
commit
32618f533b
2
src/3rdparty/3rdparty.vcxproj
vendored
2
src/3rdparty/3rdparty.vcxproj
vendored
@ -85,6 +85,7 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\kiss_fft.c" />
|
||||
<ClCompile Include="src\kiss_fftr.c" />
|
||||
<ClCompile Include="src\sqlite\sqlite3.c">
|
||||
<WarningLevel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Level1</WarningLevel>
|
||||
</ClCompile>
|
||||
@ -93,6 +94,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="include\kiss_fft.h" />
|
||||
<ClInclude Include="include\kiss_fftr.h" />
|
||||
<ClInclude Include="include\sqlite\sqlite3.h" />
|
||||
<ClInclude Include="include\sqlite\sqlite3ext.h" />
|
||||
<ClInclude Include="include\sigslot\sigslot.h" />
|
||||
|
6
src/3rdparty/3rdparty.vcxproj.filters
vendored
6
src/3rdparty/3rdparty.vcxproj.filters
vendored
@ -28,6 +28,9 @@
|
||||
<ClCompile Include="src\kiss_fft.c">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\kiss_fftr.c">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="include\sqlite\sqlite3.h">
|
||||
@ -51,5 +54,8 @@
|
||||
<ClInclude Include="include\kiss_fft.h">
|
||||
<Filter>src</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\kiss_fftr.h">
|
||||
<Filter>src</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
46
src/3rdparty/include/kiss_fftr.h
vendored
Normal file
46
src/3rdparty/include/kiss_fftr.h
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef KISS_FTR_H
|
||||
#define KISS_FTR_H
|
||||
|
||||
#include "kiss_fft.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
|
||||
Real optimized version can save about 45% cpu time vs. complex fft of a real seq.
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
typedef struct kiss_fftr_state *kiss_fftr_cfg;
|
||||
|
||||
|
||||
kiss_fftr_cfg kiss_fftr_alloc(int nfft, int inverse_fft, void * mem, size_t * lenmem);
|
||||
/*
|
||||
nfft must be even
|
||||
|
||||
If you don't care to allocate space, use mem = lenmem = NULL
|
||||
*/
|
||||
|
||||
|
||||
void kiss_fftr(kiss_fftr_cfg cfg, const kiss_fft_scalar *timedata, kiss_fft_cpx *freqdata);
|
||||
/*
|
||||
input timedata has nfft scalar points
|
||||
output freqdata has nfft/2+1 complex points
|
||||
*/
|
||||
|
||||
void kiss_fftri(kiss_fftr_cfg cfg, const kiss_fft_cpx *freqdata, kiss_fft_scalar *timedata);
|
||||
/*
|
||||
input freqdata has nfft/2+1 complex points
|
||||
output timedata has nfft scalar points
|
||||
*/
|
||||
|
||||
#define kiss_fftr_free free
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
160
src/3rdparty/src/kiss_fftr.c
vendored
Normal file
160
src/3rdparty/src/kiss_fftr.c
vendored
Normal file
@ -0,0 +1,160 @@
|
||||
/*
|
||||
Copyright (c) 2003-2004, Mark Borgerding
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "kiss_fftr.h"
|
||||
#include "_kiss_fft_guts.h"
|
||||
|
||||
struct kiss_fftr_state {
|
||||
kiss_fft_cfg substate;
|
||||
kiss_fft_cpx * tmpbuf;
|
||||
kiss_fft_cpx * super_twiddles;
|
||||
#ifdef USE_SIMD
|
||||
void * pad;
|
||||
#endif
|
||||
};
|
||||
|
||||
kiss_fftr_cfg kiss_fftr_alloc(int nfft, int inverse_fft, void * mem, size_t * lenmem)
|
||||
{
|
||||
int i;
|
||||
kiss_fftr_cfg st = NULL;
|
||||
size_t subsize, memneeded;
|
||||
|
||||
if (nfft & 1) {
|
||||
fprintf(stderr, "Real FFT optimization must be even.\n");
|
||||
return NULL;
|
||||
}
|
||||
nfft >>= 1;
|
||||
|
||||
kiss_fft_alloc(nfft, inverse_fft, NULL, &subsize);
|
||||
memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * (nfft * 3 / 2);
|
||||
|
||||
if (lenmem == NULL) {
|
||||
st = (kiss_fftr_cfg)KISS_FFT_MALLOC(memneeded);
|
||||
}
|
||||
else {
|
||||
if (*lenmem >= memneeded)
|
||||
st = (kiss_fftr_cfg)mem;
|
||||
*lenmem = memneeded;
|
||||
}
|
||||
if (!st)
|
||||
return NULL;
|
||||
|
||||
st->substate = (kiss_fft_cfg)(st + 1); /*just beyond kiss_fftr_state struct */
|
||||
st->tmpbuf = (kiss_fft_cpx *)(((char *)st->substate) + subsize);
|
||||
st->super_twiddles = st->tmpbuf + nfft;
|
||||
kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
|
||||
|
||||
for (i = 0; i < nfft / 2; ++i) {
|
||||
double phase =
|
||||
-3.14159265358979323846264338327 * ((double)(i + 1) / nfft + .5);
|
||||
if (inverse_fft)
|
||||
phase *= -1;
|
||||
kf_cexp(st->super_twiddles + i, phase);
|
||||
}
|
||||
return st;
|
||||
}
|
||||
|
||||
void kiss_fftr(kiss_fftr_cfg st, const kiss_fft_scalar *timedata, kiss_fft_cpx *freqdata)
|
||||
{
|
||||
/* input buffer timedata is stored row-wise */
|
||||
int k, ncfft;
|
||||
kiss_fft_cpx fpnk, fpk, f1k, f2k, tw, tdc;
|
||||
|
||||
if (st->substate->inverse) {
|
||||
fprintf(stderr, "kiss fft usage error: improper alloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ncfft = st->substate->nfft;
|
||||
|
||||
/*perform the parallel fft of two real signals packed in real,imag*/
|
||||
kiss_fft(st->substate, (const kiss_fft_cpx*)timedata, st->tmpbuf);
|
||||
/* The real part of the DC element of the frequency spectrum in st->tmpbuf
|
||||
* contains the sum of the even-numbered elements of the input time sequence
|
||||
* The imag part is the sum of the odd-numbered elements
|
||||
*
|
||||
* The sum of tdc.r and tdc.i is the sum of the input time sequence.
|
||||
* yielding DC of input time sequence
|
||||
* The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
|
||||
* yielding Nyquist bin of input time sequence
|
||||
*/
|
||||
|
||||
tdc.r = st->tmpbuf[0].r;
|
||||
tdc.i = st->tmpbuf[0].i;
|
||||
C_FIXDIV(tdc, 2);
|
||||
CHECK_OVERFLOW_OP(tdc.r, +, tdc.i);
|
||||
CHECK_OVERFLOW_OP(tdc.r, -, tdc.i);
|
||||
freqdata[0].r = tdc.r + tdc.i;
|
||||
freqdata[ncfft].r = tdc.r - tdc.i;
|
||||
#ifdef USE_SIMD
|
||||
freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
|
||||
#else
|
||||
freqdata[ncfft].i = freqdata[0].i = 0;
|
||||
#endif
|
||||
|
||||
for (k = 1; k <= ncfft / 2; ++k) {
|
||||
fpk = st->tmpbuf[k];
|
||||
fpnk.r = st->tmpbuf[ncfft - k].r;
|
||||
fpnk.i = -st->tmpbuf[ncfft - k].i;
|
||||
C_FIXDIV(fpk, 2);
|
||||
C_FIXDIV(fpnk, 2);
|
||||
|
||||
C_ADD(f1k, fpk, fpnk);
|
||||
C_SUB(f2k, fpk, fpnk);
|
||||
C_MUL(tw, f2k, st->super_twiddles[k - 1]);
|
||||
|
||||
freqdata[k].r = HALF_OF(f1k.r + tw.r);
|
||||
freqdata[k].i = HALF_OF(f1k.i + tw.i);
|
||||
freqdata[ncfft - k].r = HALF_OF(f1k.r - tw.r);
|
||||
freqdata[ncfft - k].i = HALF_OF(tw.i - f1k.i);
|
||||
}
|
||||
}
|
||||
|
||||
void kiss_fftri(kiss_fftr_cfg st, const kiss_fft_cpx *freqdata, kiss_fft_scalar *timedata)
|
||||
{
|
||||
/* input buffer timedata is stored row-wise */
|
||||
int k, ncfft;
|
||||
|
||||
if (st->substate->inverse == 0) {
|
||||
fprintf(stderr, "kiss fft usage error: improper alloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ncfft = st->substate->nfft;
|
||||
|
||||
st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
|
||||
st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
|
||||
C_FIXDIV(st->tmpbuf[0], 2);
|
||||
|
||||
for (k = 1; k <= ncfft / 2; ++k) {
|
||||
kiss_fft_cpx fk, fnkc, fek, fok, tmp;
|
||||
fk = freqdata[k];
|
||||
fnkc.r = freqdata[ncfft - k].r;
|
||||
fnkc.i = -freqdata[ncfft - k].i;
|
||||
C_FIXDIV(fk, 2);
|
||||
C_FIXDIV(fnkc, 2);
|
||||
|
||||
C_ADD(fek, fk, fnkc);
|
||||
C_SUB(tmp, fk, fnkc);
|
||||
C_MUL(fok, tmp, st->super_twiddles[k - 1]);
|
||||
C_ADD(st->tmpbuf[k], fek, fok);
|
||||
C_SUB(st->tmpbuf[ncfft - k], fek, fok);
|
||||
#ifdef USE_SIMD
|
||||
st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
|
||||
#else
|
||||
st->tmpbuf[ncfft - k].i *= -1;
|
||||
#endif
|
||||
}
|
||||
kiss_fft(st->substate, st->tmpbuf, (kiss_fft_cpx *)timedata);
|
||||
}
|
@ -27,6 +27,7 @@ set(CORE_SOURCES
|
||||
../3rdparty/src/md5/md5.c
|
||||
../3rdparty/src/sqlite/sqlite3.c
|
||||
../3rdparty/src/kiss_fft.c
|
||||
../3rdparty/src/kiss_fftr.c
|
||||
)
|
||||
|
||||
include_directories(
|
||||
|
@ -34,16 +34,18 @@
|
||||
|
||||
#include "pch.hpp"
|
||||
|
||||
#include <kiss_fft.h>
|
||||
#include <kiss_fftr.h>
|
||||
#include <core/debug.h>
|
||||
#include <core/audio/Player.h>
|
||||
#include <core/audio/Stream.h>
|
||||
#include <core/audio/Visualizer.h>
|
||||
#include <core/plugin/PluginFactory.h>
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
#define MAX_PREBUFFER_QUEUE_COUNT 8
|
||||
#define FFT_BUFFER_SIZE 512
|
||||
#define FFT_N 512
|
||||
#define PI 3.14159265358979323846
|
||||
|
||||
using namespace musik::core::audio;
|
||||
using namespace musik::core::sdk;
|
||||
@ -52,6 +54,7 @@ using std::min;
|
||||
using std::max;
|
||||
|
||||
static std::string TAG = "Player";
|
||||
static float* hammingWindow = nullptr;
|
||||
|
||||
namespace musik {
|
||||
namespace core {
|
||||
@ -69,26 +72,27 @@ namespace musik {
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
kiss_fft_free(cfg);
|
||||
kiss_fftr_free(cfg);
|
||||
delete[] deinterleaved;
|
||||
delete[] scratch;
|
||||
cfg = NULL;
|
||||
deinterleaved = scratch = nullptr;
|
||||
cfg = nullptr;
|
||||
deinterleaved = nullptr;
|
||||
scratch = nullptr;
|
||||
}
|
||||
|
||||
void Init(int samples) {
|
||||
if (!cfg || samples != this->samples) {
|
||||
Reset();
|
||||
cfg = kiss_fft_alloc(FFT_BUFFER_SIZE, false, 0, 0);
|
||||
deinterleaved = new kiss_fft_cpx[samples];
|
||||
scratch = new kiss_fft_cpx[FFT_BUFFER_SIZE];
|
||||
cfg = kiss_fftr_alloc(FFT_N, false, 0, 0);
|
||||
deinterleaved = new float[samples];
|
||||
scratch = new kiss_fft_cpx[(FFT_N / 2) + 1];
|
||||
this->samples = samples;
|
||||
}
|
||||
}
|
||||
|
||||
int samples;
|
||||
kiss_fft_cfg cfg;
|
||||
kiss_fft_cpx* deinterleaved;
|
||||
kiss_fftr_cfg cfg;
|
||||
float* deinterleaved;
|
||||
kiss_fft_cpx* scratch;
|
||||
};
|
||||
}
|
||||
@ -124,7 +128,7 @@ Player::Player(const std::string &url, OutputPtr output)
|
||||
, fftContext(nullptr) {
|
||||
musik::debug::info(TAG, "new instance created");
|
||||
|
||||
this->spectrum = new float[FFT_BUFFER_SIZE];
|
||||
this->spectrum = new float[FFT_N / 2];
|
||||
|
||||
/* we allow callers to specify an output device; but if they don't,
|
||||
we will create and manage our own. */
|
||||
@ -359,42 +363,55 @@ bool Player::Exited() {
|
||||
return (this->state == Player::Quit);
|
||||
}
|
||||
|
||||
static inline void initHammingWindow() {
|
||||
delete hammingWindow;
|
||||
hammingWindow = new float[FFT_N];
|
||||
|
||||
for (int i = 0; i < FFT_N; i++) {
|
||||
hammingWindow[i] = 0.54f - 0.46f * (float) cos((2 * PI * i) / (FFT_N - 1));
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool performFft(IBuffer* buffer, FftContext* fft, float* output, int outputSize) {
|
||||
long samples = buffer->Samples();
|
||||
int channels = buffer->Channels();
|
||||
long samplesPerChannel = samples / channels;
|
||||
|
||||
if (samplesPerChannel < FFT_BUFFER_SIZE ||
|
||||
outputSize != FFT_BUFFER_SIZE)
|
||||
{
|
||||
if (samplesPerChannel < FFT_N || outputSize != FFT_N / 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hammingWindow) {
|
||||
initHammingWindow();
|
||||
}
|
||||
|
||||
memset(output, 0, outputSize * sizeof(float));
|
||||
|
||||
float* input = buffer->BufferPointer();
|
||||
|
||||
fft->Init(samples);
|
||||
|
||||
for (int i = 0; i < samples; i++) {
|
||||
const int to = ((i % channels) * samplesPerChannel) + (i / channels);
|
||||
fft->deinterleaved[i].r = input[i];
|
||||
fft->deinterleaved[i].i = 0;
|
||||
fft->deinterleaved[to] = input[i];
|
||||
}
|
||||
|
||||
/* the frequency bands seem offset about 1/4 of the buffer */
|
||||
const int rotate = FFT_BUFFER_SIZE / 4;
|
||||
for (int i = 0; i < samples; i++) {
|
||||
fft->deinterleaved[i] *= hammingWindow[i % FFT_N];
|
||||
}
|
||||
|
||||
int offset = 0;
|
||||
int iterations = samples / FFT_BUFFER_SIZE;
|
||||
int iterations = samples / FFT_N;
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
kiss_fft(fft->cfg, &fft->deinterleaved[offset], fft->scratch);
|
||||
kiss_fftr(fft->cfg, fft->deinterleaved + offset, fft->scratch);
|
||||
|
||||
for (int z = 0; z < outputSize; z++) {
|
||||
/* convert to decibels */
|
||||
double db = (fft->scratch[z].r * fft->scratch[z].r + fft->scratch[z].i + fft->scratch[z].i);
|
||||
output[(z + rotate) % FFT_BUFFER_SIZE] = (db < 1 ? 0 : 20 * log(db)) / iterations;
|
||||
output[z] += (db < 1 ? 0 : 20 * log10(db)) / iterations; /* frequencies over all channels */
|
||||
}
|
||||
|
||||
offset += FFT_BUFFER_SIZE;
|
||||
offset += FFT_N;
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -414,8 +431,8 @@ void Player::OnBufferProcessed(IBuffer *buffer) {
|
||||
fftContext = new FftContext();
|
||||
}
|
||||
|
||||
if (performFft(buffer, fftContext, spectrum, FFT_BUFFER_SIZE)) {
|
||||
vis::SpectrumVisualizer()->Write(spectrum, FFT_BUFFER_SIZE);
|
||||
if (performFft(buffer, fftContext, spectrum, FFT_N / 2)) {
|
||||
vis::SpectrumVisualizer()->Write(spectrum, FFT_N / 2);
|
||||
}
|
||||
}
|
||||
else if (pcmVis && pcmVis->Visible()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user