annotate yetilab/stream/framer.yeti @ 285:be39f21456a1

More on overlap-add for frames
author Chris Cannam
date Thu, 30 May 2013 17:34:35 +0100
parents 7932bbb7bacb
children 6ed6af2fc794
rev   line source
Chris@8 1
Chris@93 2 module yetilab.stream.framer;
Chris@8 3
Chris@25 4 /**
Chris@25 5 * Framer expresses a stream (or a file) as a lazy list of (possibly
Chris@158 6 * overlapping) frames of data.
Chris@25 7 */
Chris@25 8
Chris@273 9 vec = load yetilab.vector;
Chris@222 10 bf = load yetilab.vector.blockfuncs;
Chris@93 11 af = load yetilab.stream.audiofile;
Chris@264 12 win = load yetilab.signal.window;
Chris@93 13 fft = load yetilab.transform.fft;
Chris@273 14 mat = load yetilab.matrix;
Chris@158 15 ch = load yetilab.stream.channels;
Chris@282 16 syn = load yetilab.stream.syntheticstream;
Chris@284 17 filt = load yetilab.stream.filter;
Chris@8 18
Chris@32 19 blockList framesize stream =
Chris@14 20 if stream.finished? then
Chris@158 21 stream.close ();
Chris@160 22 []
Chris@13 23 else
Chris@158 24 mat.resizedTo { rows = stream.channels, columns = framesize }
Chris@158 25 (stream.read framesize)
Chris@32 26 :. \(blockList framesize stream);
Chris@13 27 fi;
Chris@13 28
Chris@47 29 overlappingBlockList size hop stream valid buffer =
Chris@29 30 (
Chris@158 31 m = stream.read hop;
Chris@158 32 obtained = mat.width m;
Chris@23 33
Chris@32 34 // Retain framesize - hop samples from old buffer, add hop samples
Chris@29 35 // (zero-padded if necessary) just read
Chris@158 36 buffer = map2
Chris@158 37 do buf row:
Chris@218 38 vec.concat
Chris@260 39 [vec.slice buf hop size,
Chris@218 40 vec.resizedTo hop (mat.getRow row m)];
Chris@158 41 done buffer [0..stream.channels-1];
Chris@23 42
Chris@23 43 // Number of "valid" elements (not tail-end zero-padding) left in buffer
Chris@24 44 remaining = valid - (hop - obtained);
Chris@23 45
Chris@23 46 if remaining <= 0 then
Chris@23 47 stream.close ();
Chris@23 48 [];
Chris@12 49 else
Chris@163 50 mat.newMatrix (RowMajor ()) buffer
Chris@158 51 :. \(overlappingBlockList size hop stream remaining buffer);
Chris@23 52 fi);
Chris@14 53
Chris@32 54 frames { framesize, hop } stream =
Chris@32 55 if framesize == hop then
Chris@32 56 blockList framesize stream
Chris@30 57 else
Chris@32 58 overlappingBlockList framesize hop stream
Chris@218 59 framesize (map \(vec.zeros framesize) [0..stream.channels-1]);
Chris@30 60 fi;
Chris@14 61
Chris@284 62 streamContiguous rate framesize frames =
Chris@284 63 (var remaining = frames;
Chris@284 64 var buffered = mat.zeroSizeMatrix ();
Chris@284 65 var position = 0;
Chris@284 66 channels = mat.height (head frames); // so we don't need to keep a head ptr
Chris@284 67 {
Chris@284 68 get position () = position,
Chris@284 69 get channels () = channels,
Chris@284 70 get sampleRate () = rate,
Chris@284 71 get finished? () = empty? remaining and mat.empty? buffered,
Chris@284 72 get available () =
Chris@284 73 // Don't take length of frames -- we don't want to lose laziness.
Chris@284 74 // If the caller cares that much, they can measure frames themselves
Chris@284 75 if empty? remaining then
Chris@284 76 Known (mat.width buffered)
Chris@284 77 else
Chris@284 78 Unknown ()
Chris@284 79 fi,
Chris@284 80 read count =
Chris@284 81 (framesFor samples acc =
Chris@284 82 if samples <= 0 or empty? remaining then
Chris@284 83 acc
Chris@284 84 else
Chris@284 85 this = head remaining;
Chris@284 86 remaining := tail remaining;
Chris@284 87 framesFor (samples - mat.width this) (acc ++ [this])
Chris@284 88 fi;
Chris@284 89 source = mat.concat (Horizontal ()) (framesFor count [buffered]);
Chris@284 90 toReturn = mat.columnSlice source 0 count;
Chris@284 91 position := position + mat.width toReturn;
Chris@284 92 buffered := mat.columnSlice source count (mat.width source);
Chris@284 93 toReturn),
Chris@284 94 close = \(),
Chris@284 95 });
Chris@284 96
Chris@285 97 overlapAdd channels framesize hop frames =
Chris@285 98 (ola fr acc =
Chris@285 99 if empty? fr then
Chris@285 100 [acc]
Chris@285 101 else
Chris@285 102 frameSized = mat.resizedTo
Chris@285 103 { columns = framesize, rows = channels };
Chris@285 104 extended = frameSized
Chris@285 105 (mat.columnSlice acc hop (mat.width acc));
Chris@285 106 added = mat.sum (frameSized (head fr)) extended;
Chris@285 107 (mat.columnSlice acc 0 hop) :: ola (tail fr) added;
Chris@285 108 fi;
Chris@285 109 mat.concat (Horizontal ()) (ola frames (mat.zeroSizeMatrix ())));
Chris@285 110
Chris@284 111 streamOverlapping rate framesize hop frames =
Chris@284 112 (var remaining = frames;
Chris@284 113 var buffered = mat.zeroSizeMatrix ();
Chris@284 114 var position = 0;
Chris@285 115 w = win.windowFunction (Hann ()) [ Symmetric true ] framesize;
Chris@284 116 channels = mat.height (head frames); // so we don't need to keep a head ptr
Chris@284 117 filt.delayedBy (- (framesize - hop))
Chris@282 118 {
Chris@282 119 get position () = position,
Chris@282 120 get channels () = channels,
Chris@282 121 get sampleRate () = rate,
Chris@284 122 get finished? () = empty? remaining and mat.empty? buffered,
Chris@284 123 get available () =
Chris@284 124 // Don't take length of frames: we don't want to lose
Chris@284 125 // laziness. If the caller cares that much, they can
Chris@284 126 // measure frames themselves
Chris@284 127 if empty? remaining then
Chris@284 128 Known (mat.width buffered)
Chris@284 129 else
Chris@284 130 Unknown ()
Chris@284 131 fi,
Chris@284 132 read count =
Chris@284 133 (framesFor samples acc =
Chris@284 134 if samples <= 0 or empty? remaining then
Chris@284 135 acc
Chris@284 136 else
Chris@284 137 this = mat.newMatrix (RowMajor ())
Chris@284 138 (map (bf.multiply w) (mat.asRows (head remaining)));
Chris@284 139 remaining := tail remaining;
Chris@284 140 framesFor (samples - hop) (acc ++ [this])
Chris@284 141 fi;
Chris@285 142 source = overlapAdd channels framesize hop
Chris@285 143 (framesFor count [buffered]);
Chris@285 144 // println "source = \(source)";
Chris@284 145 toReturn = mat.columnSlice source 0 count;
Chris@284 146 buffered := mat.columnSlice source count (mat.width source);
Chris@284 147 toReturn),
Chris@282 148 close = \(),
Chris@284 149 });
Chris@284 150
Chris@282 151 //!!! doc: convert frames back to a stream
Chris@282 152 streamed rate { framesize, hop } frames =
Chris@282 153 if framesize == hop then
Chris@282 154 streamContiguous rate framesize frames
Chris@282 155 else
Chris@284 156 streamOverlapping rate framesize hop frames
Chris@282 157 fi;
Chris@282 158
Chris@158 159 monoFrames params stream =
Chris@158 160 map ch.mixedDown (frames params stream);
Chris@158 161
Chris@49 162 windowedFrames { framesize, hop, window } stream =
Chris@49 163 (win = window framesize;
Chris@158 164 map (bf.multiply win) (monoFrames { framesize, hop } stream));
Chris@49 165
Chris@49 166 frequencyDomainFrames { framesize, hop } stream =
Chris@49 167 (f = fft.realForward framesize;
Chris@49 168 map f (windowedFrames { framesize, hop, window = win.hann } stream));
Chris@23 169
Chris@11 170 {
Chris@30 171 frames,
Chris@158 172 monoFrames,
Chris@49 173 windowedFrames,
Chris@49 174 frequencyDomainFrames,
Chris@49 175
Chris@49 176 framesOfFile parameters filename =
Chris@146 177 frames parameters (af.open filename),
Chris@49 178
Chris@158 179 monoFramesOfFile parameters filename =
Chris@158 180 monoFrames parameters (af.open filename),
Chris@158 181
Chris@49 182 windowedFramesOfFile parameters filename =
Chris@146 183 windowedFrames parameters (af.open filename),
Chris@49 184
Chris@49 185 frequencyDomainFramesOfFile parameters filename =
Chris@146 186 frequencyDomainFrames parameters (af.open filename),
Chris@284 187
Chris@285 188 overlapAdd,
Chris@285 189
Chris@284 190 streamed,
Chris@11 191 }
Chris@8 192