summaryrefslogtreecommitdiffstats
path: root/noatun/modules/winskin/vis/winSkinFFT_impl.cpp
blob: 5396ac3cf1cedc26436f808a6b12f01337168a1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
  implementation for winskin fft
  Copyright (C) 2000  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */



#include "winSkinFFT_impl.h"
#include <audiosubsys.h>
#include <cstring>

#define __BANDS  75

namespace Noatun {

WinSkinFFT_impl::WinSkinFFT_impl() {
  fftBands_short=256;
  realFFTFilter= new RealFFTFilter(fftBands_short);
  fftArray=new int[fftBands_short];
  bands=0;

  fragCnt=(int)(AudioSubSystem::the()->fragmentCount());
  visQueue=new VISQueue(fragCnt);
  writePos=0;

}

WinSkinFFT_impl::~WinSkinFFT_impl(){
  delete realFFTFilter;
  delete fftArray;
  delete visQueue;
}

void WinSkinFFT_impl::streamInit() {
}


void WinSkinFFT_impl::streamStart() {
}


void WinSkinFFT_impl::calculateBlock(unsigned long samples) {


  unsigned long i;

  // monitoring only tasks can't be done with that StereoEffect
  // interface nicely - copy input to output until there is
  // something better
  // (when?)
  int n=sizeof(float)*samples;
  memcpy(outleft,inleft,n);
  memcpy(outright,inright,n);


  if (realFFTFilter->fft16(inleft,inright,samples) == false) {
    return;
  }


  
  //
  // The following modifications have nothing to do
  // with an fft, they only make the output look nice.
  // (mostly scaling)

  short* fftPtr;
  int* bitReversed;

  fftPtr=realFFTFilter->getPointPtr();
  bitReversed=realFFTFilter->getBitReversed();

  int pos=0;
  int step=realFFTFilter->getPoints()/__BANDS;


  int re;
  int im;
  int tmp;

  float max=0.0;
  float avg=0.0;



  for (i=0;i<__BANDS;i++) {
    re=(int)fftPtr[bitReversed[pos]];
    im=(int)fftPtr[bitReversed[pos]+1];
    
    tmp=re*re+im*im;  
    // Here I check a new idea. We remove all low values
    // and all values over xyz to xyz.
    fftArray[pos]=(int)(::sqrt(::sqrt(tmp)));
    
    if (fftArray[pos]<=15) {
      max+=fftArray[pos];
    } else {
      max+=15+fftArray[pos]/2;
    }
    pos=pos+step;
  }
  avg=0.65*max/(float)__BANDS;
  
  pos=0;
  vector<float>* visAnalyserArray=visQueue->getElement(writePos);
  visAnalyserArray->clear();
  visAnalyserArray->reserve(__BANDS);
  for (i=0;i<__BANDS;i++) {
    float val=(float)(fftArray[pos]-avg);
    visAnalyserArray->push_back(val);
    pos=pos+step;
  }
  writePos++;
  if (writePos >= fragCnt) writePos=0;

}


void WinSkinFFT_impl::bandResolution(float res) {
  bands=(int)res;
}

float WinSkinFFT_impl::bandResolution() {
  return (float)bands;
}


vector<float>* WinSkinFFT_impl::scope() {
  int delay=writePos+1;
  if (delay >= fragCnt) delay=0;
 

  vector<float>* visAnalyserArray=visQueue->getElement(delay);
    
  return new vector<float>(*visAnalyserArray);
}


REGISTER_IMPLEMENTATION(WinSkinFFT_impl);
  
}