c@10
|
1
|
c@10
|
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 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@10
|
14
|
c@10
|
15 { pow, round, floor, ceil, log2, nextPowerOfTwo } = load may.mathmisc;
|
c@10
|
16
|
c@10
|
17 cqt str =
|
c@10
|
18 (sampleRate = str.sampleRate;
|
c@10
|
19 maxFreq = sampleRate/2;
|
c@10
|
20 minFreq = 40;
|
c@10
|
21 binsPerOctave = 24;
|
c@10
|
22
|
c@10
|
23 println "Here";
|
c@10
|
24
|
c@10
|
25 octaves = ceil (log2 (maxFreq / minFreq));
|
c@10
|
26
|
c@10
|
27 println "Here: about to calculate stuff with \(octaves)";
|
c@10
|
28
|
c@10
|
29 actualMinFreq = (maxFreq / (pow 2 octaves)) * (pow 2 (1/binsPerOctave));
|
c@10
|
30
|
c@10
|
31 println "sampleRate = \(sampleRate), maxFreq = \(maxFreq), minFreq = \(minFreq), actualMinFreq = \(actualMinFreq), octaves = \(octaves), binsPerOctave = \(binsPerOctave)";
|
c@10
|
32
|
c@10
|
33 kdata = cqtkernel.makeKernel { sampleRate, maxFreq, binsPerOctave };
|
c@10
|
34
|
c@11
|
35 println "atomsPerFrame = \(kdata.atomsPerFrame)";
|
c@11
|
36
|
c@10
|
37 streams = manipulate.duplicated octaves str;
|
c@10
|
38
|
c@10
|
39 //!!! can't be right!
|
c@10
|
40 kernel = cm.transposed (cm.conjugateTransposed kdata.kernel);
|
c@10
|
41
|
c@10
|
42 println "have kernel";
|
c@10
|
43
|
c@10
|
44 fftFunc = fft.forward kdata.fftSize;
|
c@10
|
45
|
c@10
|
46 cqblocks =
|
c@10
|
47 map do octave:
|
c@10
|
48 frames = framer.monoFrames //!!! mono for now
|
c@10
|
49 { framesize = kdata.fftSize, hop = kdata.fftHop }
|
c@10
|
50 (resample.decimated (pow 2 octave) streams[octave]);
|
c@10
|
51 map do frame:
|
c@10
|
52 freq = fftFunc (cplx.complexArray frame (vec.zeros kdata.fftSize));
|
c@10
|
53 cm.product kernel (cm.newComplexColumnVector freq);
|
c@10
|
54 done frames;
|
c@10
|
55 done [0..octaves-1];
|
c@10
|
56
|
c@13
|
57 // The cqblocks list is a list<list<matrix>>. Each top-level list
|
c@11
|
58 // corresponds to an octave, from highest to lowest, each having
|
c@11
|
59 // twice as many elements in its list as the next octave. The
|
c@11
|
60 // sub-lists are sampled in time with an effective spacing of
|
c@11
|
61 // fftSize * 2^(octave-1) audio frames, and the matrices are row
|
c@11
|
62 // vectors with atomsPerFrame * binsPerOctave complex elements.
|
c@13
|
63 //
|
c@13
|
64 // ***
|
c@13
|
65 //
|
c@13
|
66 // In a typical constant-Q structure, each (2^(octaves-1) *
|
c@13
|
67 // fftHop) input frames gives us an output structure conceptually
|
c@13
|
68 // like this:
|
c@10
|
69 //
|
c@10
|
70 // [][][][][][][][] <- fftHop frames per highest-octave output value
|
c@10
|
71 // [][][][][][][][] layered as many times as binsPerOctave (here 2)
|
c@10
|
72 // [--][--][--][--] <- fftHop*2 frames for the next lower octave
|
c@10
|
73 // [--][--][--][--] etc
|
c@10
|
74 // [------][------]
|
c@10
|
75 // [------][------]
|
c@10
|
76 // [--------------]
|
c@10
|
77 // [--------------]
|
c@10
|
78 //
|
c@13
|
79 // ***
|
c@13
|
80 //
|
c@13
|
81 // But the kernel we're using here has more than one temporally
|
c@13
|
82 // spaced atom; each individual cell is a row vector with
|
c@13
|
83 // atomsPerFrame * binsPerOctave elements, but that actually
|
c@13
|
84 // represents a rectangular matrix of result cells with width
|
c@13
|
85 // atomsPerFrame and height binsPerOctave. The columns of this
|
c@13
|
86 // matrix (the atoms) then need to be spaced by 2^(octave-1)
|
c@13
|
87 // relative to those from the highest octave.
|
c@10
|
88
|
c@14
|
89 // Probably a better way to do this somehow... Reshape each row
|
c@14
|
90 // vector into the appropriate rectangular matrix
|
c@14
|
91 map2 do octlist octave:
|
c@14
|
92 map do rv:
|
c@14
|
93 cv = cm.getColumn 0 rv;
|
c@14
|
94 reals = cplx.reals cv;
|
c@14
|
95 imags = cplx.imaginaries cv;
|
c@14
|
96 reshape v = mat.newMatrix (ColumnMajor ())
|
c@14
|
97 (map do n:
|
c@14
|
98 vec.slice v (n * kdata.atomsPerFrame) ((n+1) * kdata.atomsPerFrame)
|
c@14
|
99 done [0..kdata.binsPerOctave-1]);
|
c@14
|
100 cm.complex (reshape reals) (reshape imags);
|
c@14
|
101 done octlist
|
c@14
|
102 done cqblocks [0..octaves-1];
|
c@14
|
103
|
c@10
|
104 );
|
c@10
|
105
|
c@10
|
106 testStream = manipulate.withDuration 96000 (syn.sinusoid 48000 500);
|
c@10
|
107
|
c@10
|
108 println "have test stream";
|
c@10
|
109
|
c@10
|
110 cqt testStream;
|
c@10
|
111
|
c@10
|
112
|
c@10
|
113
|