c@10
|
1
|
c@37
|
2 module cqt;
|
c@10
|
3
|
c@10
|
4 cqtkernel = load cqtkernel;
|
c@10
|
5 resample = load may.stream.resample;
|
c@10
|
6 manipulate = load may.stream.manipulate;
|
c@10
|
7 cm = load may.matrix.complex;
|
c@10
|
8 framer = load may.stream.framer;
|
c@10
|
9 cplx = load may.complex;
|
c@10
|
10 fft = load may.transform.fft;
|
c@10
|
11 vec = load may.vector;
|
c@10
|
12
|
c@10
|
13 { pow, round, floor, ceil, log2, nextPowerOfTwo } = load may.mathmisc;
|
c@10
|
14
|
c@37
|
15 cqt { maxFreq, minFreq, binsPerOctave } str =
|
c@10
|
16 (sampleRate = str.sampleRate;
|
c@10
|
17 octaves = ceil (log2 (maxFreq / minFreq));
|
c@10
|
18 actualMinFreq = (maxFreq / (pow 2 octaves)) * (pow 2 (1/binsPerOctave));
|
c@10
|
19
|
c@16
|
20 eprintln "sampleRate = \(sampleRate), maxFreq = \(maxFreq), minFreq = \(minFreq), actualMinFreq = \(actualMinFreq), octaves = \(octaves), binsPerOctave = \(binsPerOctave)";
|
c@10
|
21
|
c@10
|
22 kdata = cqtkernel.makeKernel { sampleRate, maxFreq, binsPerOctave };
|
c@10
|
23
|
c@16
|
24 eprintln "atomsPerFrame = \(kdata.atomsPerFrame)";
|
c@11
|
25
|
c@10
|
26 streams = manipulate.duplicated octaves str;
|
c@10
|
27
|
c@10
|
28 //!!! can't be right!
|
c@10
|
29 kernel = cm.transposed (cm.conjugateTransposed kdata.kernel);
|
c@10
|
30
|
c@16
|
31 eprintln "have kernel";
|
c@10
|
32
|
c@10
|
33 fftFunc = fft.forward kdata.fftSize;
|
c@10
|
34
|
c@10
|
35 cqblocks =
|
c@10
|
36 map do octave:
|
c@10
|
37 frames = framer.monoFrames //!!! mono for now
|
c@10
|
38 { framesize = kdata.fftSize, hop = kdata.fftHop }
|
c@10
|
39 (resample.decimated (pow 2 octave) streams[octave]);
|
c@10
|
40 map do frame:
|
c@10
|
41 freq = fftFunc (cplx.complexArray frame (vec.zeros kdata.fftSize));
|
c@10
|
42 cm.product kernel (cm.newComplexColumnVector freq);
|
c@10
|
43 done frames;
|
c@10
|
44 done [0..octaves-1];
|
c@10
|
45
|
c@13
|
46 // The cqblocks list is a list<list<matrix>>. Each top-level list
|
c@11
|
47 // corresponds to an octave, from highest to lowest, each having
|
c@11
|
48 // twice as many elements in its list as the next octave. The
|
c@11
|
49 // sub-lists are sampled in time with an effective spacing of
|
c@11
|
50 // fftSize * 2^(octave-1) audio frames, and the matrices are row
|
c@11
|
51 // vectors with atomsPerFrame * binsPerOctave complex elements.
|
c@13
|
52 //
|
c@13
|
53 // ***
|
c@13
|
54 //
|
c@13
|
55 // In a typical constant-Q structure, each (2^(octaves-1) *
|
c@13
|
56 // fftHop) input frames gives us an output structure conceptually
|
c@13
|
57 // like this:
|
c@10
|
58 //
|
c@10
|
59 // [][][][][][][][] <- fftHop frames per highest-octave output value
|
c@10
|
60 // [][][][][][][][] layered as many times as binsPerOctave (here 2)
|
c@10
|
61 // [--][--][--][--] <- fftHop*2 frames for the next lower octave
|
c@10
|
62 // [--][--][--][--] etc
|
c@10
|
63 // [------][------]
|
c@10
|
64 // [------][------]
|
c@10
|
65 // [--------------]
|
c@10
|
66 // [--------------]
|
c@10
|
67 //
|
c@13
|
68 // ***
|
c@13
|
69 //
|
c@13
|
70 // But the kernel we're using here has more than one temporally
|
c@13
|
71 // spaced atom; each individual cell is a row vector with
|
c@13
|
72 // atomsPerFrame * binsPerOctave elements, but that actually
|
c@13
|
73 // represents a rectangular matrix of result cells with width
|
c@13
|
74 // atomsPerFrame and height binsPerOctave. The columns of this
|
c@13
|
75 // matrix (the atoms) then need to be spaced by 2^(octave-1)
|
c@13
|
76 // relative to those from the highest octave.
|
c@10
|
77
|
c@15
|
78 // Reshape each row vector into the appropriate rectangular matrix
|
c@21
|
79 // and split into single-atom columns
|
c@19
|
80
|
c@21
|
81 emptyHops = kdata.firstCentre / kdata.atomSpacing;
|
c@21
|
82 maxDrop = emptyHops * (pow 2 (octaves-1)) - emptyHops;
|
c@21
|
83 eprintln "maxDrop = \(maxDrop)";
|
c@21
|
84
|
c@21
|
85 cqblocks = map do octlist:
|
c@21
|
86 concat
|
c@21
|
87 (map do rv:
|
c@21
|
88 cm.asColumns
|
c@21
|
89 (cm.generate do row col:
|
c@21
|
90 cm.at rv ((row * kdata.atomsPerFrame) + col) 0
|
c@21
|
91 done {
|
c@21
|
92 rows = kdata.binsPerOctave,
|
c@21
|
93 columns = kdata.atomsPerFrame
|
c@21
|
94 })
|
c@21
|
95 done octlist)
|
c@21
|
96 done cqblocks;
|
c@21
|
97
|
c@21
|
98 cqblocks = array (map2 do octlist octave:
|
c@21
|
99 d = emptyHops * (pow 2 (octaves-octave)) - emptyHops;
|
c@22
|
100
|
c@22
|
101 d = 0; //!!!
|
c@22
|
102
|
c@21
|
103 eprintln "dropping \(d)";
|
c@21
|
104 drop d octlist;
|
c@21
|
105 done cqblocks [1..octaves]);
|
c@14
|
106
|
c@17
|
107 assembleBlock bits =
|
c@19
|
108 (eprintln "assembleBlock: structure of bits is:";
|
c@19
|
109 eprintln (map length bits);
|
c@19
|
110
|
c@19
|
111 rows = octaves * kdata.binsPerOctave;
|
c@19
|
112 columns = (pow 2 (octaves - 1)) * kdata.atomsPerFrame;
|
c@19
|
113
|
c@18
|
114 cm.generate do row col:
|
c@19
|
115
|
c@19
|
116 // bits structure: [1,2,4,8,...]
|
c@19
|
117
|
c@19
|
118 // each elt of bits is a list of the chunks that should
|
c@19
|
119 // make up this block in that octave (lowest octave first)
|
c@19
|
120
|
c@19
|
121 // each chunk has atomsPerFrame * binsPerOctave elts in it
|
c@19
|
122
|
c@19
|
123 // row is disposed with 0 at the top, highest octave (in
|
c@19
|
124 // both pitch and index into bits structure)
|
c@19
|
125
|
c@18
|
126 oct = int (row / binsPerOctave);
|
c@19
|
127 binNo = row % kdata.binsPerOctave;
|
c@21
|
128
|
c@19
|
129 chunks = pow 2 oct;
|
c@21
|
130 colsPerAtom = int (columns / (chunks * kdata.atomsPerFrame));
|
c@21
|
131 atomNo = int (col / colsPerAtom);
|
c@21
|
132 atomOffset = col % colsPerAtom;
|
c@18
|
133
|
c@22
|
134 if /*!!! atomOffset == 0 and */ atomNo < length bits[oct] then
|
c@21
|
135 bits[oct][atomNo][binNo];
|
c@20
|
136 else
|
c@20
|
137 cplx.zero
|
c@20
|
138 fi;
|
c@19
|
139
|
c@19
|
140 done { rows, columns };
|
c@19
|
141 );
|
c@15
|
142
|
c@17
|
143 processOctaveLists octs =
|
c@17
|
144 case octs[0] of
|
c@17
|
145 block::rest:
|
c@19
|
146 (toAssemble = array
|
c@19
|
147 (map do oct:
|
c@21
|
148 n = kdata.atomsPerFrame * pow 2 oct;
|
c@17
|
149 if not empty? octs[oct] then
|
c@19
|
150 forBlock = array (take n octs[oct]);
|
c@17
|
151 octs[oct] := drop n octs[oct];
|
c@17
|
152 forBlock
|
c@17
|
153 else
|
c@19
|
154 array []
|
c@17
|
155 fi
|
c@19
|
156 done (keys octs));
|
c@17
|
157 assembleBlock toAssemble :. \(processOctaveLists octs));
|
c@17
|
158 _: []
|
c@15
|
159 esac;
|
c@15
|
160
|
c@19
|
161 eprintln "cqblocks has \(length cqblocks) entries";
|
c@15
|
162
|
c@17
|
163 octaveLists = [:];
|
c@19
|
164
|
c@19
|
165 cqblocks = array cqblocks;
|
c@17
|
166 for [1..octaves] do oct:
|
c@17
|
167 octaveLists[octaves - oct] := cqblocks[oct-1];
|
c@17
|
168 done;
|
c@17
|
169 /*
|
c@17
|
170 \() (map2 do octlist octave:
|
c@17
|
171 println "oct \(octaves) - \(octave) = \(octaves - octave)";
|
c@17
|
172 octaveLists[octaves - octave] := octlist
|
c@17
|
173 done cqblocks [1..octaves]);
|
c@17
|
174 */
|
c@19
|
175 eprintln "octaveLists keys are: \(keys octaveLists)";
|
c@17
|
176
|
c@17
|
177 processOctaveLists octaveLists;
|
c@15
|
178
|
c@10
|
179 );
|
c@10
|
180
|
c@37
|
181 { cqt }
|
c@10
|
182
|