<> <> <> DIRECTORY Commander, FS, IO, Real, RealFns, Rope, U255; LarkSineTableImpl: PROGRAM IMPORTS Commander, FS, IO, Real, RealFns, U255 = { OPEN U255; Main: Commander.CommandProc = TRUSTED { ENABLE ABORTED => GOTO Quit; out: IO.STREAM; name: Rope.ROPE; label: Rope.ROPE; res: BOOL; l: NAT; loss: REAL; out _ cmd.out; out.PutF["\nLarkSineTables running at %t\n", IO.time[]]; FOR i: NAT IN [0..7] DO l _ i*3; loss _ l; loss _ loss - 3.16; -- codec clipping level is at 3.16 dB name _ IO.PutFR["SineTable%02d.dsm", IO.int[l]]; label _ IO.PutFR["STab%02d", IO.int[l]]; out.PutF["Writing %g ... ", IO.rope[name]]; res _ WriteDSMFile[label: label, name: name, data: MakeSineTable[dBToLoss[l]]]; out.PutF[IF res THEN "OK.\n" ELSE "Failed!\n"]; ENDLOOP; out.PutF["\nLarkSineTables finished at %t\n", IO.time[]]; out.Flush[]; EXITS Quit => NULL; }; Pi: REAL = 3.1415926535; TwoPi: REAL = 2.0 * Pi; <> <
> <> MakeSineTable: PROC [loss: REAL] RETURNS [t: REF BTab] = { int: INTEGER; x: REAL; t _ NEW[BTab[256]]; FOR i: CARDINAL IN [0..256) DO x _ i; x _ x/256.0; -- fraction of circle x _ x * TwoPi; -- phase in radians x _ RealFns.Sin[x]; -- IN [-1..1] x _ x*loss; -- attenuate x _ x*32767; -- scale to left justified two's complement int _ Real.FixI[x]; -- Fix t[i] _ U255.Encode[int]; ENDLOOP; }; WriteDSMFile: PROC [label: Rope.ROPE, name: Rope.ROPE, data: REF BTab] RETURNS [BOOL] = { f: IO.STREAM; IF data.length MOD 2 # 0 THEN GOTO Fail; f _ FS.StreamOpen[fileName: name, accessOptions: $create ! FS.Error => TRUSTED {GOTO Fail}]; f.PutF["; %g\n; L. Stewart %t\n\n", IO.rope[name], IO.time[]]; f.PutF["\nC_CODE SEGMENT\n\n"]; f.PutF["_%g\tLABEL\tNEAR\n", IO.rope[label]]; FOR i: CARDINAL IN [0..data.length/2) DO f.PutF["\tDW\t0%02x%02xH\n", IO.card[data[i+i+1]], IO.card[data[i+i]]]; ENDLOOP; f.PutF["\n\nPUBLIC _%g\n\nC_CODE ENDS\n\tEND\n", IO.rope[label]]; f.Close[]; RETURN [TRUE]; EXITS Fail => RETURN [FALSE]; }; dBToLoss: PROC [dBloss: REAL] RETURNS [loss:REAL] = { RETURN [RealFns.Power[base: 10.0, exponent: -dBloss/20.0]]; }; Init: PROC = { Commander.Register["LarkSineTables", Main, "Make Lark Sinewave tables"]; }; <
> Init[]; }. <> <> <> <> <>