|
@@ -765,6 +765,238 @@ extern "C" {
|
765
|
765
|
|
766
|
766
|
|
767
|
767
|
//------------------------------------------------------------------------------------------------------------
|
|
768
|
+ // cmVectArray buffers row vectors of arbitrary lenght in memory.
|
|
769
|
+ // The buffers may then be access using the cmVectArrayGetXXX() functions.
|
|
770
|
+ // The entire contents of the file may be written to a file using atVectArrayWrite().
|
|
771
|
+ // The file may then be read in back into memory using cmVectArrayAllocFromFile()
|
|
772
|
+ // or in octave via readVectArray.m.
|
|
773
|
+ // A rectantular matrix in memory may be written to a VectArray file in one operation
|
|
774
|
+ // via the function cmVectArrayWriteMatrixXXX().
|
|
775
|
+
|
|
776
|
+ typedef struct cmVectArrayVect_str
|
|
777
|
+ {
|
|
778
|
+ unsigned n; // length of this vector in values (not bytes)
|
|
779
|
+
|
|
780
|
+ union
|
|
781
|
+ {
|
|
782
|
+ char* v; // raw memory vector pointer
|
|
783
|
+ double* dV; // dV[n] vector of doubles
|
|
784
|
+ float* fV; // fV[n] vecotr of floats
|
|
785
|
+ cmSample_t* sV; // sV[n] vector of cmSample_t
|
|
786
|
+ } u;
|
|
787
|
+
|
|
788
|
+ struct cmVectArrayVect_str* link; // link to next element record
|
|
789
|
+
|
|
790
|
+ } cmVectArrayVect_t;
|
|
791
|
+
|
|
792
|
+ enum
|
|
793
|
+ {
|
|
794
|
+ kFloatVaFl = 0x01,
|
|
795
|
+ kDoubleVaFl = 0x02,
|
|
796
|
+ kSampleVaFl = 0x04,
|
|
797
|
+ kRealVaFl = 0x08,
|
|
798
|
+ kIntVaFl = 0x02, // int and uint is converted to double
|
|
799
|
+ kUIntVaFl = 0x02 //
|
|
800
|
+ };
|
|
801
|
+
|
|
802
|
+ typedef struct
|
|
803
|
+ {
|
|
804
|
+ cmObj obj;
|
|
805
|
+ cmVectArrayVect_t* bp; // first list element
|
|
806
|
+ cmVectArrayVect_t* ep; // last list element
|
|
807
|
+ unsigned vectCnt; // count of elements in linked list
|
|
808
|
+ unsigned flags; // data vector type (See: kFloatVaFl, kDoubleVaFl, ... )
|
|
809
|
+ unsigned typeByteCnt; // size of a single data vector value (e.g. 4=float 8=double)
|
|
810
|
+ unsigned maxEleCnt; // length of the longest data vector
|
|
811
|
+ double* tempV;
|
|
812
|
+ cmVectArrayVect_t* cur;
|
|
813
|
+ } cmVectArray_t;
|
|
814
|
+
|
|
815
|
+ // Flags must be set to one of the kXXXVAFl flag values.
|
|
816
|
+ cmVectArray_t* cmVectArrayAlloc( cmCtx* ctx, unsigned flags );
|
|
817
|
+ cmVectArray_t* cmVectArrayAllocFromFile(cmCtx* ctx, const char* fn );
|
|
818
|
+
|
|
819
|
+ cmRC_t cmVectArrayFree( cmVectArray_t** pp );
|
|
820
|
+
|
|
821
|
+ // Release all the stored vectors but do not release the object.
|
|
822
|
+ cmRC_t cmVectArrayClear( cmVectArray_t* p );
|
|
823
|
+
|
|
824
|
+ // Return the count of vectors contained in the vector array.
|
|
825
|
+ cmRC_t cmVectArrayCount( const cmVectArray_t* p );
|
|
826
|
+
|
|
827
|
+ // Store a new vector by appending it to the end of the internal vector list.
|
|
828
|
+ cmRC_t cmVectArrayAppendS( cmVectArray_t* p, const cmSample_t* v, unsigned vn );
|
|
829
|
+ cmRC_t cmVectArrayAppendR( cmVectArray_t* p, const cmReal_t* v, unsigned vn );
|
|
830
|
+ cmRC_t cmVectArrayAppendF( cmVectArray_t* p, const float* v, unsigned vn );
|
|
831
|
+ cmRC_t cmVectArrayAppendD( cmVectArray_t* p, const double* v, unsigned vn );
|
|
832
|
+ cmRC_t cmVectArrayAppendI( cmVectArray_t* p, const int* v, unsigned vn );
|
|
833
|
+ cmRC_t cmVectArrayAppendU( cmVectArray_t* p, const unsigned* v, unsigned vn );
|
|
834
|
+
|
|
835
|
+ // Write a vector array in a format that can be read by readVectArray.m.
|
|
836
|
+ cmRC_t cmVectArrayWrite( cmVectArray_t* p, const char* fn );
|
|
837
|
+
|
|
838
|
+ typedef cmRC_t (*cmVectArrayForEachFuncS_t)( void* arg, unsigned idx, const cmSample_t* xV, unsigned xN );
|
|
839
|
+ unsigned cmVectArrayForEachS( cmVectArray_t* p, unsigned idx, unsigned cnt, cmVectArrayForEachFuncS_t func, void* arg );
|
|
840
|
+
|
|
841
|
+ cmRC_t cmVectArrayWriteVectorS( cmCtx* ctx, const char* fn, const cmSample_t* v, unsigned vn );
|
|
842
|
+ cmRC_t cmVectArrayWriteVectorI( cmCtx* ctx, const char* fn, const int* v, unsigned vn );
|
|
843
|
+
|
|
844
|
+ // Write the column-major matrix m[rn,cn] to the file 'fn'. Note that the matrix is transposed as it is
|
|
845
|
+ // written and therefore will be read back as a 'cn' by 'rn' matrix.
|
|
846
|
+ cmRC_t cmVectArrayWriteMatrixS( cmCtx* ctx, const char* fn, const cmSample_t* m, unsigned rn, unsigned cn );
|
|
847
|
+ cmRC_t cmVectArrayWriteMatrixI( cmCtx* ctx, const char* fn, const int* m, unsigned rn, unsigned cn );
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+ cmRC_t cmVectArrayRewind( cmVectArray_t* p );
|
|
851
|
+ cmRC_t cmVectArrayAdvance( cmVectArray_t* p, unsigned n );
|
|
852
|
+ bool cmVectArrayIsEOL( const cmVectArray_t* p );
|
|
853
|
+ unsigned cmVectArrayEleCount( const cmVectArray_t* p );
|
|
854
|
+ cmRC_t cmVectArrayGetF( cmVectArray_t* p, float* v, unsigned* vnRef );
|
|
855
|
+ cmRC_t cmVectArrayGetD( cmVectArray_t* p, double* v, unsigned* vnRef );
|
|
856
|
+ cmRC_t cmVectArrayGetI( cmVectArray_t* p, int* v, unsigned* vnRef );
|
|
857
|
+ cmRC_t cmVectArrayGetU( cmVectArray_t* p, unsigned* v, unsigned* vnRef );
|
|
858
|
+
|
|
859
|
+ // If a vector array is composed of repeating blocks of 'groupCnt' sub-vectors
|
|
860
|
+ // where the concatenated ith sub-vectors in each group form a single super-vector then
|
|
861
|
+ // this function will return the super-vector. Use cmMemFree(*vRef) to release
|
|
862
|
+ // the returned super-vector.
|
|
863
|
+ cmRC_t cmVectArrayFormVectF( cmVectArray_t* p, unsigned groupIdx, unsigned groupCnt, float** vRef, unsigned* vnRef );
|
|
864
|
+
|
|
865
|
+ cmRC_t cmVectArrayFormVectColF( cmVectArray_t* p, unsigned groupIdx, unsigned groupCnt, unsigned colIdx, float** vRef, unsigned* vnRef );
|
|
866
|
+ cmRC_t cmVectArrayFormVectColU( cmVectArray_t* p, unsigned groupIdx, unsigned groupCnt, unsigned colIdx, unsigned** vRef, unsigned* vnRef );
|
|
867
|
+ cmRC_t cmVectArrayTest( cmCtx* ctx, const char* fn );
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+#if CM_FLOAT_SMP == 1
|
|
871
|
+#define cmVectArrayGetS cmVectArrayGetF
|
|
872
|
+#define cmVectArrayFormVectS cmVectArrayFormVectF
|
|
873
|
+#else
|
|
874
|
+#define cmVectArrayGetS cmVectArrayGetD
|
|
875
|
+#define cmVectArrayFormVectS cmVectArrayFormVectD
|
|
876
|
+#endif
|
|
877
|
+
|
|
878
|
+ //-----------------------------------------------------------------------------------------------------------------------
|
|
879
|
+ // Spectral whitening filter.
|
|
880
|
+ // Based on: Klapuri, A., 2006: Multiple fundamental frequency estimation by summing
|
|
881
|
+ // harmonic amplitudes.
|
|
882
|
+
|
|
883
|
+ typedef struct
|
|
884
|
+ {
|
|
885
|
+ cmObj obj;
|
|
886
|
+ unsigned binCnt; //
|
|
887
|
+ cmReal_t binHz; //
|
|
888
|
+ unsigned bandCnt; //
|
|
889
|
+ cmReal_t coeff; //
|
|
890
|
+ cmReal_t* whiV; // whiV[bandCnt+2] - fractional bin index of each center frequency
|
|
891
|
+ cmReal_t* whM; // whM[binCnt,bandCnt]
|
|
892
|
+ cmReal_t* iV; // iV[ binCnt ] - working memory
|
|
893
|
+ } cmWhFilt;
|
|
894
|
+
|
|
895
|
+ cmWhFilt* cmWhFiltAlloc( cmCtx* c, cmWhFilt* p, unsigned binCnt, cmReal_t binHz, cmReal_t coeff, cmReal_t maxHz );
|
|
896
|
+ cmRC_t cmWhFiltFree( cmWhFilt** pp );
|
|
897
|
+ cmRC_t cmWhFiltInit( cmWhFilt* p, unsigned binCnt, cmReal_t binHz, cmReal_t coeff, cmReal_t maxHz );
|
|
898
|
+ cmRC_t cmWhFiltFinal( cmWhFilt* p );
|
|
899
|
+ cmRC_t cmWhFiltExec( cmWhFilt* p, const cmReal_t* xV, cmReal_t* yV, unsigned xyN );
|
|
900
|
+
|
|
901
|
+ //-----------------------------------------------------------------------------------------------------------------------
|
|
902
|
+
|
|
903
|
+ typedef struct
|
|
904
|
+ {
|
|
905
|
+ double srate; // system sample rate
|
|
906
|
+ unsigned chCnt; // tracking channel count
|
|
907
|
+ unsigned binCnt; // count of spectrum elements passed in each call to cmFrqTrkExec()
|
|
908
|
+ unsigned hopSmpCnt; // phase vocoder hop count in samples
|
|
909
|
+ cmReal_t stRange; // maximum allowable semi-tones between a tracker and a peak
|
|
910
|
+ cmReal_t wndSecs; // duration of the
|
|
911
|
+ cmReal_t minTrkSec; // minimum track length before track is considered stable
|
|
912
|
+ cmReal_t maxTrkDeadSec; // maximum length of time a tracker may fail to connect to a peak before being declared disconnected.
|
|
913
|
+ cmReal_t pkThreshDb; // minimum amplitide in Decibels of a selected spectral peak.
|
|
914
|
+ cmReal_t pkAtkThreshDb; // minimum amplitude in Decibels for the first frame of a new track.
|
|
915
|
+ cmReal_t pkAtkMaxHz; // maximum frequency for a new track.
|
|
916
|
+ const char* logFn; // log file name or NULL if no file is to be written
|
|
917
|
+ const char* levelFn; // level file name or NULL if no file is to be written
|
|
918
|
+ const char* specFn; // spectrum file name or NULL if no file is to be written
|
|
919
|
+
|
|
920
|
+ } cmFrqTrkArgs_t;
|
|
921
|
+
|
|
922
|
+ typedef struct
|
|
923
|
+ {
|
|
924
|
+ bool activeFl;
|
|
925
|
+ unsigned id;
|
|
926
|
+ unsigned tN; // age of this track in frames
|
|
927
|
+ unsigned dN; // count of consecutive times this ch has not connected
|
|
928
|
+ cmReal_t hz; // current center frequency
|
|
929
|
+ cmReal_t db; // current magnitude
|
|
930
|
+
|
|
931
|
+ cmReal_t* dbV; // dbV[]
|
|
932
|
+ cmReal_t* hzV; // hzV[]
|
|
933
|
+ unsigned si;
|
|
934
|
+ unsigned sn;
|
|
935
|
+
|
|
936
|
+ cmReal_t db_mean;
|
|
937
|
+ cmReal_t db_std;
|
|
938
|
+ cmReal_t hz_mean;
|
|
939
|
+ cmReal_t hz_std;
|
|
940
|
+
|
|
941
|
+ } cmFrqTrkCh_t;
|
|
942
|
+
|
|
943
|
+ struct cmBinMtxFile_str;
|
|
944
|
+
|
|
945
|
+ typedef struct cmFrqTrk_str
|
|
946
|
+ {
|
|
947
|
+ cmObj obj;
|
|
948
|
+ cmFrqTrkArgs_t a;
|
|
949
|
+ cmFrqTrkCh_t* ch; // ch[ a.chCnt ]
|
|
950
|
+ unsigned hN; // count of magnitude buffer frames
|
|
951
|
+ unsigned sN; // count of frames in channel statistics buffers
|
|
952
|
+ unsigned bN; // count of bins in peak matrices
|
|
953
|
+ cmReal_t* dbM; // dbM[ hN, bN ]
|
|
954
|
+ unsigned hi; // next row of dbM to fill
|
|
955
|
+ unsigned fN; // total count of frames processed.
|
|
956
|
+ cmReal_t binHz;
|
|
957
|
+
|
|
958
|
+ cmReal_t* dbV;
|
|
959
|
+ unsigned* pkiV;
|
|
960
|
+ unsigned deadN_max; // max. count of hops a tracker may fail to connect before being set to inactive
|
|
961
|
+ unsigned minTrkN; // minimum track length in hops
|
|
962
|
+ unsigned nextTrkId;
|
|
963
|
+
|
|
964
|
+ unsigned newTrkCnt;
|
|
965
|
+ unsigned curTrkCnt;
|
|
966
|
+ unsigned deadTrkCnt;
|
|
967
|
+
|
|
968
|
+ cmWhFilt* wf;
|
|
969
|
+
|
|
970
|
+ cmVectArray_t* logVa;
|
|
971
|
+ cmVectArray_t* levelVa;
|
|
972
|
+ cmVectArray_t* specVa;
|
|
973
|
+
|
|
974
|
+ cmChar_t* logFn;
|
|
975
|
+ cmChar_t* levelFn;
|
|
976
|
+ cmChar_t* specFn;
|
|
977
|
+
|
|
978
|
+ } cmFrqTrk;
|
|
979
|
+
|
|
980
|
+ //
|
|
981
|
+ // 1. Calculate the mean spectral magnitude profile over the last hN frames.
|
|
982
|
+ // 2. Locate the peaks in the profile.
|
|
983
|
+ // 3. Allow each active tracker to select the closest peak to extend its life.
|
|
984
|
+ // a) The distance between the trackers current location and a given
|
|
985
|
+ // peak is measured based on magnitude and frequency over time.
|
|
986
|
+ // b) There is a frequency range limit outside of which a given track-peak
|
|
987
|
+ // connection may not go.
|
|
988
|
+ // c) There is an amplitude threshold below which a track may not fall.
|
|
989
|
+
|
|
990
|
+ cmFrqTrk* cmFrqTrkAlloc( cmCtx* c, cmFrqTrk* p, const cmFrqTrkArgs_t* a );
|
|
991
|
+ cmRC_t cmFrqTrkFree( cmFrqTrk** pp );
|
|
992
|
+ cmRC_t cmFrqTrkInit( cmFrqTrk* p, const cmFrqTrkArgs_t* a );
|
|
993
|
+ cmRC_t cmFrqTrkFinal( cmFrqTrk* p );
|
|
994
|
+ cmRC_t cmFrqTrkExec( cmFrqTrk* p, const cmReal_t* magV, const cmReal_t* phsV, const cmReal_t* hzV );
|
|
995
|
+ void cmFrqTrkPrint( cmFrqTrk* p );
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+ //------------------------------------------------------------------------------------------------------------
|
768
|
1000
|
|
769
|
1001
|
enum
|
770
|
1002
|
{
|
|
@@ -787,6 +1019,8 @@ extern "C" {
|
787
|
1019
|
|
788
|
1020
|
cmPvAnl* pva;
|
789
|
1021
|
cmPvSyn* pvs;
|
|
1022
|
+
|
|
1023
|
+ cmFrqTrk* ft;
|
790
|
1024
|
|
791
|
1025
|
unsigned mode;
|
792
|
1026
|
double thresh;
|
|
@@ -869,6 +1103,9 @@ extern "C" {
|
869
|
1103
|
// colCntV[colCnt] is optional.
|
870
|
1104
|
cmRC_t cmBinMtxFileRead( cmCtx_t* ctx, const cmChar_t* fn, unsigned rowCnt, unsigned colCnt, unsigned eleByteCnt, void* buf, unsigned* colCntV );
|
871
|
1105
|
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
872
|
1109
|
#ifdef __cplusplus
|
873
|
1110
|
}
|
874
|
1111
|
#endif
|