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