quickspec.h
Go to the documentation of this file.
1 /*
2  Harmonic sinusoidal modelling and tools
3 
4  C++ code package for harmonic sinusoidal modelling and relevant signal processing.
5  Centre for Digital Music, Queen Mary, University of London.
6  This file copyright 2011 Wen Xue.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12 */
13 #ifndef QuickSpecH
14 #define QuickSpecH
15 
16 /*
17  \file quickspec.h - TQuickSpectrogram class
18 
19  TQuickSpectrogram implements a compute-on-request spectrogram class. Every time one frame of the
20  spectrogram is read from this class, it checks if the single-frame spectrogram has been computed
21  already, computes it if not, and return the pointer to the spectrum.
22 
23  Further reading: "A buffering technique for real-time spectrogram displaying.pdf"
24 */
25 
26 
27 #include <cstddef>
28 #include <stdlib.h>
29 #include "xcomplex.h"
30 #include "windowfunctions.h"
31 #include "fft.h"
32 //---------------------------------------------------------------------------
33 
34 #define QSPEC_FORMAT float //default spectral data format
35 #define QSpec_BufferSize 1024 //default initial buffer size, in frames, of TQuickSpectrogram
36 
37 
52 #ifndef INT24
53 #define INT24
54 struct __int24;
55 struct __pint24
56 {
57  char* p;
58  __fastcall __pint24(){}
59  __fastcall __pint24(const void* ap){p=(char*)ap;}
60  __pint24& __fastcall operator=(const void* ap){p=(char*)ap; return *this;}
61  __int24& __fastcall operator*(){return *(__int24*)p;}
62  __int24& __fastcall operator[](int index){return *(__int24*)&p[3*index];}
63  __pint24 __fastcall operator++(int){__pint24 result=*this; p+=3; return result;}
64  __pint24& __fastcall operator++(){p+=3; return *this;}
65  __pint24 __fastcall operator--(int){__pint24 result=*this; p-=3; return result;}
66  __pint24& __fastcall operator--(){p-=3; return *this;}
67  __pint24& __fastcall operator+=(int a){p+=3*a; return *this;}
68  __pint24& __fastcall operator-=(int a){p-=3*a; return *this;}
69  __fastcall operator void*() const{return p;}
70 };
71 struct __int24
72 {
73  __int16 loword;
74  __int8 hibyte;
75  __fastcall __int24(){}
76  __fastcall __int24(const __int32 a){loword=*(__int16*)&a; hibyte=((__int16*)&a)[1];}
77  __fastcall __int24(const double f){__int32 a=f; loword=*(__int16*)&a; hibyte=((__int16*)&a)[1];}
78  __int24& __fastcall operator=(const __int32 a){loword=*(__int16*)&a; hibyte=((__int16*)&a)[1]; return *this;}
79  __int24& __fastcall operator=(const double f){__int32 a=f; loword=*(__int16*)&a; hibyte=((__int16*)&a)[1]; return *this;}
80  __fastcall operator __int32() const{__int32 result; *(__int16*)&result=loword; ((__int16*)&result)[1]=hibyte; return result;}
81  __pint24 operator &(){return (__pint24)this;}
82  void* operator new[](size_t count){void* result=malloc(3*count); return result;}
83  void operator delete[](void* p){free(p);}
84 };
85 #endif
86 
87 
88 typedef void (*GetBuf)(int Id, cdouble* &w, cdouble* &x, double* &win, int* &hbi, void* Parent);
111 {
112  int BufferCount;
113  int BufferSize;
114  int FrCount;
115 
116  //internal storage of spectrogram
117  QSPEC_FORMAT** fPh;
118  QSPEC_FORMAT** fA;
119  cmplx<QSPEC_FORMAT>** fSpec;
120 
121  //internal buffers (optional) for FFT
122  WindowType fwt;
123  int fWid;
124  int* fhbi;
125  double fwdp;
126  double* fwin;
127  cdouble* fw;
128  cdouble* fx;
129 
130  //x and ph create-time switch
131  bool usex;
132  bool useph;
133 
134  //internal methods
135  void AddBuffer();
136  void AddBuffer(int AddFrCount);
137  void __fastcall CalculateSpectrum(int fr);
138 
139 public:
140  //if specified, Parent is responsible to supply FFT buffers through GetFFTBuffers (optional)
141  int Id;
142  void* Parent;
143  GetBuf GetFFTBuffers;
144 
145  //index and validity arrays associated with internal storage
146  int Capacity;
147  int* Frame;
148  int* Valid;
149 
150  WindowType WinType;
151  double WinParam;
152 
153  void* Data;
156 
157  int Offst;
158  int Wid;
159 
160  __fastcall TQuickSpectrogram(void* AParent, int AnId, bool Ausex, bool Auseph, GetBuf G);
161  __fastcall ~TQuickSpectrogram();
162 
163  QSPEC_FORMAT* __fastcall A(int fr);
164  void FreeBuffers();
165  int Invalidate(int From, int To);
166  QSPEC_FORMAT* __fastcall Ph(int fr);
167  void SetFrCapacity(int AnFrCapacity);
168  cmplx<QSPEC_FORMAT>* __fastcall Spec(int fr);
169 };
170 #endif
int Wid
frame size, the same as window size
Definition: quickspec.h:158
void * Parent
a pointer used as argument for calling GetFFTBuffers()
Definition: quickspec.h:142
int * Frame
indices to individual frames in internal storage
Definition: quickspec.h:147
void * Data
pointer to waveform audio
Definition: quickspec.h:153
WindowType WinType
window type for computing spectrogram
Definition: quickspec.h:150
int Id
an identifier given at create time, used as argument for calling GetFFTBuffers()
Definition: quickspec.h:141
Definition: quickspec.h:110
int DataLength
length of waveform audio, in samples
Definition: quickspec.h:154
int BytesPerSample
bytes per sample of waveform audio
Definition: quickspec.h:155
int * Valid
validity tags to individual frames in internal storage
Definition: quickspec.h:148
Definition: quickspec.h:55
double WinParam
additional parameter specifying certain window types (Gaussian, Kaiser, etc.)
Definition: quickspec.h:151
Definition: quickspec.h:71
GetBuf GetFFTBuffers
if specified, this supplies FFT buffers
Definition: quickspec.h:143
int Offst
frame offset
Definition: quickspec.h:157
Definition: xcomplex.h:26
int Capacity
size of $Frame[] and &Valid[], usually set to the total number of frames of the data ...
Definition: quickspec.h:146