Chris@8: Chris@8: module framer; Chris@8: Chris@25: /** Chris@25: * Framer expresses a stream (or a file) as a lazy list of (possibly Chris@25: * overlapping) frames of mono data. Chris@25: */ Chris@25: Chris@11: vec = load fvector; Chris@20: block = load block; Chris@8: af = load audiofile; Chris@8: Chris@32: blockList framesize stream = Chris@14: if stream.finished? then Chris@14: (stream.close (); [] ); Chris@13: else Chris@32: block.resizedTo framesize (stream.readMono framesize) Chris@32: :. \(blockList framesize stream); Chris@13: fi; Chris@13: Chris@32: overlappingBlockList size hop stream valid buffer Chris@23: is number -> number -> 'a -> number -> ~double[] -> 'b = Chris@29: ( Chris@29: b = stream.readMono hop; Chris@24: obtained = block.length b; Chris@23: samples = block.unblock b; Chris@23: Chris@32: // Retain framesize - hop samples from old buffer, add hop samples Chris@29: // (zero-padded if necessary) just read Chris@29: buffer = vec.concat Chris@35: [vec.rangeOf buffer hop (size-hop), Chris@35: vec.resizedTo hop samples]; Chris@23: Chris@23: // Number of "valid" elements (not tail-end zero-padding) left in buffer Chris@24: remaining = valid - (hop - obtained); Chris@23: Chris@23: if remaining <= 0 then Chris@23: stream.close (); Chris@23: []; Chris@12: else Chris@29: v = block.block buffer; Chris@32: v :. \(overlappingBlockList size hop stream remaining buffer); Chris@23: fi); Chris@14: Chris@32: frames { framesize, hop } stream = Chris@32: if framesize == hop then Chris@32: blockList framesize stream Chris@30: else Chris@32: overlappingBlockList framesize hop stream Chris@32: framesize (vec.zeros framesize); Chris@30: fi; Chris@14: Chris@32: framesOfFile { framesize, hop } filename = Chris@32: if framesize == hop then Chris@32: blockList framesize (af.open filename) Chris@30: else Chris@32: overlappingBlockList framesize hop (af.open filename) Chris@32: framesize (vec.zeros framesize); Chris@30: fi; Chris@23: Chris@11: { Chris@30: frames, Chris@30: framesOfFile, Chris@11: } Chris@8: