annotate yetilab/stream/framer.yeti @ 296:ec6a36d88f57

Move complex tests to complex package
author Chris Cannam
date Fri, 31 May 2013 22:02:03 +0100
parents 9bc985b602b3
children e8000dbab96e
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@8 16
Chris@291 17 //!!! todo: synchronized for everything with state assignment
Chris@291 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@291 97 overlapAdd overlap frames =
Chris@285 98 (ola fr acc =
Chris@291 99 case fr of
Chris@291 100 first::rest:
Chris@291 101 (w = mat.width acc;
Chris@291 102 pre = mat.columnSlice acc 0 (w - overlap);
Chris@291 103 added = mat.sum first
Chris@291 104 (mat.resizedTo (mat.size first)
Chris@291 105 (mat.columnSlice acc (w - overlap) w));
Chris@291 106 pre :: ola rest added);
Chris@291 107 _:
Chris@291 108 [acc];
Chris@291 109 esac;
Chris@291 110 case frames of
Chris@291 111 first::rest:
Chris@291 112 mat.concat (Horizontal ()) (ola rest first);
Chris@291 113 _:
Chris@291 114 mat.zeroSizeMatrix ();
Chris@291 115 esac);
Chris@285 116
Chris@284 117 streamOverlapping rate framesize hop frames =
Chris@284 118 (var remaining = frames;
Chris@284 119 var buffered = mat.zeroSizeMatrix ();
Chris@284 120 var position = 0;
Chris@294 121
Chris@288 122 factor = hop / (framesize/2);
Chris@290 123 w = bf.scaled factor (win.hann framesize); // periodic window, not symmetric
Chris@284 124 channels = mat.height (head frames); // so we don't need to keep a head ptr
Chris@294 125
Chris@291 126 syncd = synchronized remaining;
Chris@294 127
Chris@294 128 finished' () = syncd \(empty? remaining and mat.empty? buffered);
Chris@294 129
Chris@294 130 read' count =
Chris@294 131 (framesFor samples acc =
Chris@294 132 if samples <= 0 or empty? remaining then
Chris@294 133 acc
Chris@294 134 else
Chris@294 135 this = mat.resizedTo { columns = framesize, rows = channels }
Chris@294 136 (mat.newMatrix (RowMajor ())
Chris@294 137 (map (bf.multiply w) (mat.asRows (head remaining))));
Chris@294 138 remaining := tail remaining;
Chris@294 139 framesFor (samples - hop) (acc ++ [this])
Chris@294 140 fi;
Chris@294 141 source = overlapAdd (framesize - hop)
Chris@294 142 (framesFor count [buffered]);
Chris@294 143 buffered := mat.columnSlice source count (mat.width source);
Chris@294 144 mat.columnSlice source 0 count);
Chris@294 145
Chris@294 146 // lose initial padding
Chris@294 147 \() (read' (framesize - hop));
Chris@294 148
Chris@294 149 {
Chris@294 150 get position () = syncd \(position),
Chris@294 151 get channels () = channels,
Chris@294 152 get sampleRate () = rate,
Chris@294 153 get finished? () = finished' (),
Chris@294 154 get available () = if finished' () then Known 0 else Unknown () fi,
Chris@294 155 read count = syncd
Chris@294 156 \(data = read' count;
Chris@294 157 position := position + mat.width data;
Chris@294 158 data),
Chris@294 159 close = \(),
Chris@294 160 });
Chris@284 161
Chris@282 162 //!!! doc: convert frames back to a stream
Chris@282 163 streamed rate { framesize, hop } frames =
Chris@282 164 if framesize == hop then
Chris@282 165 streamContiguous rate framesize frames
Chris@282 166 else
Chris@284 167 streamOverlapping rate framesize hop frames
Chris@282 168 fi;
Chris@282 169
Chris@158 170 monoFrames params stream =
Chris@158 171 map ch.mixedDown (frames params stream);
Chris@158 172
Chris@49 173 windowedFrames { framesize, hop, window } stream =
Chris@49 174 (win = window framesize;
Chris@158 175 map (bf.multiply win) (monoFrames { framesize, hop } stream));
Chris@49 176
Chris@294 177 frequencyDomainFrames parameters stream =
Chris@294 178 (f = fft.realForward parameters.framesize;
Chris@294 179 map f (windowedFrames parameters stream));
Chris@23 180
Chris@11 181 {
Chris@30 182 frames,
Chris@158 183 monoFrames,
Chris@49 184 windowedFrames,
Chris@49 185 frequencyDomainFrames,
Chris@49 186
Chris@49 187 framesOfFile parameters filename =
Chris@146 188 frames parameters (af.open filename),
Chris@49 189
Chris@158 190 monoFramesOfFile parameters filename =
Chris@158 191 monoFrames parameters (af.open filename),
Chris@158 192
Chris@49 193 windowedFramesOfFile parameters filename =
Chris@146 194 windowedFrames parameters (af.open filename),
Chris@49 195
Chris@49 196 frequencyDomainFramesOfFile parameters filename =
Chris@146 197 frequencyDomainFrames parameters (af.open filename),
Chris@284 198
Chris@285 199 overlapAdd,
Chris@285 200
Chris@284 201 streamed,
Chris@11 202 }
Chris@8 203