-- AdpcmQuantize.mesa
-- L. Stewart, last updated: August 28, 1979 1:43 PM
DIRECTORY
AdpcmDefs: FROM "AdpcmDefs",
AdpcmPrivateDefs: FROM "AdpcmPrivateDefs",
AudioCompressDefs: FROM "AudioCompressDefs",
DisplayDefs: FROM "DisplayDefs",
IODefs: FROM "IODefs",
RamDefs: FROM "RamDefs",
SegmentDefs: FROM "SegmentDefs",
StreamDefs: FROM "StreamDefs",
WFDefs: FROM "WFDefs";
AdpcmQuantize: PROGRAM
IMPORTS AdpcmDefs, AdpcmPrivateDefs, DisplayDefs, IODefs, RamDefs, SegmentDefs, StreamDefs, WFDefs
EXPORTS AudioCompressDefs =
BEGIN OPEN AdpcmDefs, AdpcmPrivateDefs, IODefs, WFDefs;
sysDispPages: CARDINAL ← 40;
myAccess: StreamDefs.AccessOptions = StreamDefs.Write+StreamDefs.Append;
iFName: STRING ← [40];
oFName: STRING ← [40];
iFOpen, oFOpen: BOOLEAN ← FALSE;
iFH, oFH: SegmentDefs.FileHandle;
iS, oS: StreamDefs.DiskHandle;
pcmD: PcmHandle;
adpcmD: AdpcmHandle;
pcmBuf: PACKED ARRAY [0..512) OF [0..377B];
adpcmBuf: ARRAY [0..64) OF CARDINAL;
pTab: POINTER TO PredictorTable ← GetpTab[];
bTabs: POINTER TO BreakpointTable ← GetbTabs[];
qTabs: POINTER TO QuantizerTable ← GetqTabs[];
compressState, decompressState: AdpcmState;
InitFiles: PROCEDURE =
BEGIN OPEN IODefs, SegmentDefs, StreamDefs;
IF iFOpen THEN iS.destroy[iS];
IF oFOpen THEN oS.destroy[oS];
iFOpen ← oFOpen ← FALSE;
WF0["Input file name: "L];
[] ← ReadEditedString[iFName, EndOfString, TRUE];
iFH ← NewFile[ iFName, Read, OldFileOnly ];
iS ← CreateWordStream[iFH, Read];
iFOpen ← TRUE;
WF0["*nOutput file name: "L];
[] ← ReadEditedString[oFName, EndOfString, TRUE];
oFH ← NewFile[ oFName, myAccess, DefaultVersion ];
oS ← CreateWordStream[oFH, myAccess];
oFOpen ← TRUE;
WF0["*n"L];
END;
SetDisplay: PUBLIC PROCEDURE =
BEGIN
WF0["Display Pages? "L];
sysDispPages ← ReadNumber[sysDispPages,10];
WF0["*n"];
DisplayDefs.SetSystemDisplaySize[sysDispPages,(3*sysDispPages)/2];
END;
EndOfString: PUBLIC PROCEDURE[c: CHARACTER] RETURNS[b: BOOLEAN] =
BEGIN
SELECT c FROM
CR, TAB, LF, SP => b ← TRUE;
ENDCASE => b ← FALSE;
END;
YesOrNo: PUBLIC PROCEDURE[s: STRING] RETURNS[b: BOOLEAN] =
BEGIN
WF1["%s ", s];
SELECT ReadChar[] FROM
’Y, ’y, CR => b ← TRUE;
ENDCASE => b ← FALSE;
WF0[IF b THEN "Yes*n" ELSE "No*n"];
END;
PrintPredictorTable: PROCEDURE =
BEGIN
i, j: INTEGER;
WF0["Predictor Table*n"L];
i ← -128;
WHILE i<128 DO
WF1["%4d:",i];
FOR j IN [i..i+8) DO
WF1["%6d ",pTab↑[j]];
ENDLOOP;
WF0["*n"L];
i←i+8;
ENDLOOP;
END;
PrintQuantizerTables: PROCEDURE =
BEGIN
i: INTEGER;
WF0["Quantizer Tables"L];
WF0["*nHi Level: "L];
FOR i IN [0..NModes) DO
WF1["%3d :",qTabs[i].hi];
ENDLOOP;
WF0["*nBreakpts: "L];
FOR i IN [0..NModes) DO
WF1["%3d :",bTabs[i]];
ENDLOOP;
WF0["*nLo Level: "L];
FOR i IN [0..NModes) DO
WF1["%3d :",qTabs[i].lo];
ENDLOOP;
WF0["*n"L];
END;
SetPrediction: PROCEDURE =
BEGIN
i: INTEGER;
num: INTEGER ← 19;
den: INTEGER ← 20;
WF0["Set new prediction coefficient. (fraction)*n"];
WF0[" numerator: "];
num ← ReadNumber[num, 10];
WF0["*n denominator: "];
den ← ReadNumber[den, 10];
WF0["*n"];
-- fill in tables
FOR i IN Ex200 DO
pTab↑[i] ← ((i*num)/den); -- second guess of good value
ENDLOOP;
END;
SetAdaptation: PROCEDURE =
BEGIN
i: INTEGER;
-- fill in tables
WF0["Set Adaption tables*n"];
FOR i IN [0..NModes) DO
WF1[" mode %d, q.lo: ", i];
qTabs↑[i].lo ← ReadNumber[qTabs↑[i].lo, 10];
WF0[" break: "];
bTabs↑[i] ← ReadNumber[bTabs↑[i], 10];
WF0[" q.hi: "];
qTabs↑[i].hi ← ReadNumber[qTabs↑[i].hi, 10];
WF0["*n"];
ENDLOOP;
END;
CommonCompressionStuff: PROCEDURE =
BEGIN
InitFiles[];
AdpcmDefs.InitAdpcmState[@compressState];
AdpcmDefs.InitAdpcmState[@decompressState];
pcmD ← DESCRIPTOR[@pcmBuf,512];
adpcmD ← DESCRIPTOR[@adpcmBuf,64];
END;
ProcessFile: PROCEDURE =
BEGIN
icount, ocount: INTEGER;
more: BOOLEAN ← TRUE;
WF0["File ← Compress ← File:*n"];
CommonCompressionStuff[];
WHILE more DO
icount ← StreamDefs.ReadBlock[iS, @pcmBuf, 256];
IF icount#256 THEN more←FALSE;
compressState ← AdpcmDefs.CompressBlock[pcmD, adpcmD, icount*2, compressState];
decompressState ← AdpcmDefs.DeCompressBlock[pcmD, adpcmD, icount*2, decompressState];
ocount ← StreamDefs.WriteBlock[oS, @pcmBuf, icount];
IF icount#ocount THEN ERROR;
WriteChar[’.];
ENDLOOP;
WF0["*n"];
END;
CompressFile: PROCEDURE =
BEGIN
icount, ocount: INTEGER;
more: BOOLEAN ← TRUE;
WF0["Compress File ← File:*n"];
CommonCompressionStuff[];
WHILE more DO
icount ← StreamDefs.ReadBlock[iS, @pcmBuf, 256];
IF icount#256 THEN more←FALSE;
compressState ← AdpcmDefs.CompressBlock[pcmD, adpcmD, icount*2, compressState];
ocount ← StreamDefs.WriteBlock[oS, @adpcmBuf, icount/4];
IF icount/4#ocount THEN ERROR;
WriteChar[’.];
ENDLOOP;
WF0["*n"];
END;
DeCompressFile: PROCEDURE =
BEGIN
icount, ocount: INTEGER;
more: BOOLEAN ← TRUE;
WF0["File ← Compress File:*n"];
CommonCompressionStuff[];
WHILE more DO
icount ← StreamDefs.ReadBlock[iS, @adpcmBuf, 64];
IF icount#64 THEN more←FALSE;
decompressState ← AdpcmDefs.DeCompressBlock[pcmD, adpcmD, icount*8, decompressState];
ocount ← StreamDefs.WriteBlock[oS, @pcmBuf, icount*4];
IF icount*4#ocount THEN ERROR;
WriteChar[’.];
ENDLOOP;
WF0["*n"];
END;
LoadMicrocode: PROCEDURE =
BEGIN
s: STRING ← [40];
uc: RamDefs.MuImage;
WF0["Load microcode.*n microcode.br filename? "];
ReadID[s];
uc ← RamDefs.ReadPackedMuFile[s];
IF RamDefs.LoadRamAndBoot[uc, FALSE]#0 THEN WF0[" ...failed.*n"]
ELSE WF0[" ...OK.*n"];
RamDefs.ReleaseMuImage[uc];
END;
AdpcmMainLoop: PUBLIC PROCEDURE =
BEGIN
c: CHARACTER;
DO ENABLE Rubout => CONTINUE;
WF0[">"];
SELECT ReadChar[] FROM
’ => WF0["*n"L];
’. => WF0[".*n"L];
’- =>
BEGIN
WF0["-- "L];
UNTIL (c←ReadChar[])=CR DO WriteChar[c]; ENDLOOP;
WF0["*n"L];
END;
’I, ’i => BEGIN
WF0["Initialize Adpcm*n"L];
AdpcmDefs.InitAdpcm[];
END;
’L, ’l => BEGIN
LoadMicrocode[];
END;
’C, ’c => BEGIN
CompressFile[];
END;
’D, ’d => BEGIN
DeCompressFile[];
END;
’F, ’f => BEGIN
ProcessFile[];
END;
’M, ’m =>
BEGIN
WF0["Miscellaneous "L];
SELECT ReadChar[] FROM
’D, ’d => SetDisplay[];
’? => WF0[" Display.*n"L];
ENDCASE => WF0["???*n"L];
END;
’P, ’p =>
BEGIN
WF0["Print "L];
SELECT ReadChar[] FROM
’P, ’p => PrintPredictorTable[];
’Q, ’q => PrintQuantizerTables[];
’? => WF0[" Predictor Table. Quantizer Tables*n"L];
ENDCASE => WF0["???*n"L];
END;
’S, ’s =>
BEGIN
WF0["Set "L];
SELECT ReadChar[] FROM
’P, ’p => SetPrediction[];
’A, ’a => SetAdaptation[];
’? => BEGIN
WF0[" Predictor Table.*n"L];
WF0[" AdaptationTables.*n"L];
END;
ENDCASE => WF0["???*n"L];
END;
’Q, ’q => IF YesOrNo["Quit."L] THEN RETURN[];
’? => BEGIN
WF0["Set "];
WF0["Compress "];
WF0["DeCompress "];
WF0["Filter "];
WF0["Miscilaneous "];
WF0["Print "];
WF0["Quit "];
WF0["*n"];
END;
ENDCASE => WF0["???*n"L];
ENDLOOP;
END;
-- Main Line Code
END.