diff --git a/Level_2/TNS.m b/Level_2/TNS.m index 866417e..c76a274 100644 --- a/Level_2/TNS.m +++ b/Level_2/TNS.m @@ -60,7 +60,8 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType) [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); % Quantizes these coefficients - quantizedLinPredCoeff = round(linPredCoeff, COEF_RES); + quantizedLinPredCoeff = quantiz(linPredCoeff(2:end), -0.7:0.1:0.7); + quantizedLinPredCoeff = [1 -0.75 + 0.1 * quantizedLinPredCoeff]; % Filters MDCT coefficients if ~isstable(1, quantizedLinPredCoeff) @@ -102,10 +103,11 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType) % Calculates the linear prediction coefficients [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); - + % Quantizes these coefficients - quantizedLinPredCoeff = round(linPredCoeff, COEF_RES); - + quantizedLinPredCoeff = quantiz(linPredCoeff(2:end), -0.7:0.1:0.7); + quantizedLinPredCoeff = [1 -0.75 + 0.1 * quantizedLinPredCoeff]; + % Filters MDCT coefficients if ~isstable(1, quantizedLinPredCoeff) error('TNS, l[79]: Inverse filter not stable!'); diff --git a/Level_3/AACoder3.m b/Level_3/AACoder3.m index efe52c1..2c191e9 100644 --- a/Level_3/AACoder3.m +++ b/Level_3/AACoder3.m @@ -79,6 +79,7 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) GR = 0; sfcL = 0; sfcR = 0; + continue; else prev1FrameStart = (i - 1) * 1024 + 1; prev1FrameStop = prev1FrameStart + 2047; @@ -100,16 +101,20 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) TL = 0; TR = 0; - streamL = SL; - huffcodebookL = 0; - streamR = SR; - huffcodebookR = 0; -% [streamL, huffcodebookL] = encodeHuff(SL, huffLUT); -% [streamR, huffcodebookR] = encodeHuff(SR, huffLUT); -% [sfcL, ~] = encodeHuff(sfcL, huffLUT, 12); -% [sfcR, ~] = encodeHuff(sfcR, huffLUT, 12); - + [streamL, huffcodebookL] = encodeHuff(SL, huffLUT); + [streamR, huffcodebookR] = encodeHuff(SR, huffLUT); + + if strcmp(frameTypes(i+1), 'ESH') + [sfcHuffL, ~] = encodeHuff(reshape(sfcL(2:end, :), 1, [])', ... + huffLUT, 12); + [sfcHuffR, ~] = encodeHuff(reshape(sfcR(2:end, :), 1, [])', ... + huffLUT, 12); + else + [sfcHuffL, ~] = encodeHuff(sfcL(2:end), huffLUT, 12); + [sfcHuffR, ~] = encodeHuff(sfcR(2:end), huffLUT, 12); + end + AACSeq3(i + 1).frameType = frameTypes(i + 1); AACSeq3(i + 1).winType = WINDOW_TYPE; AACSeq3(i + 1).chl.TNScoeffs = TNScoeffsL; @@ -118,8 +123,8 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) AACSeq3(i + 1).chr.T = TR; AACSeq3(i + 1).chl.G = GL; AACSeq3(i + 1).chr.G = GR; - AACSeq3(i + 1).chl.sfc = sfcL; - AACSeq3(i + 1).chr.sfc = sfcR; + AACSeq3(i + 1).chl.sfc = sfcHuffL; + AACSeq3(i + 1).chr.sfc = sfcHuffR; AACSeq3(i + 1).chl.stream = streamL; AACSeq3(i + 1).chr.stream = streamR; AACSeq3(i + 1).chl.codebook = huffcodebookL; diff --git a/Level_3/decoded3.wav b/Level_3/decoded3.wav index 5e5ad7d..b2e9296 100644 Binary files a/Level_3/decoded3.wav and b/Level_3/decoded3.wav differ diff --git a/Level_3/encoded.mat b/Level_3/encoded.mat index a075ad3..5936d84 100644 Binary files a/Level_3/encoded.mat and b/Level_3/encoded.mat differ diff --git a/Level_3/iAACoder3.m b/Level_3/iAACoder3.m index df53e1f..bb3fc5f 100644 --- a/Level_3/iAACoder3.m +++ b/Level_3/iAACoder3.m @@ -49,6 +49,7 @@ function x = iAACoder3(AACSeq3, fNameOut) currFrameStop = currFrameStart + 2047; if i < 2 + continue; frameF(:, 1) = iTNS(AACSeq3(i + 1).chl.stream, ... AACSeq3(i+1).frameType, ... AACSeq3(i + 1).chl.TNScoeffs); @@ -58,22 +59,29 @@ function x = iAACoder3(AACSeq3, fNameOut) frameT = iFilterbank(frameF, AACSeq3(i+1).frameType, AACSeq3(i+1).winType); else - % SL = decodeHuff(AACSeq3(i + 1).chl.stream, ... - % AACSeq3(i + 1).chl.codebook, huffLUT); - % SR = decodeHuff(AACSeq3(i + 1).chr.stream, ... - % AACSeq3(i + 1).chr.codebook, huffLUT); - % - % sfcL = decodeHuff(AACSeq3(i + 1).chl.sfc, 12, huffLUT); - % sfcR = decodeHuff(AACSeq3(i + 1).chr.sfc, 12, huffLUT); - - frameF(:, 1) = iAACquantizer(AACSeq3(i + 1).chl.stream, ... - AACSeq3(i + 1).chl.sfc, ... - AACSeq3(i + 1).chl.G, ... - AACSeq3(i+1).frameType); - frameF(:, 2) = iAACquantizer(AACSeq3(i + 1).chr.stream, ... - AACSeq3(i + 1).chr.sfc, ... - AACSeq3(i + 1).chr.G, ... - AACSeq3(i+1).frameType); + SL = decodeHuff(AACSeq3(i + 1).chl.stream, ... + AACSeq3(i + 1).chl.codebook, huffLUT); + SR = decodeHuff(AACSeq3(i + 1).chr.stream, ... + AACSeq3(i + 1).chr.codebook, huffLUT); + + sfcL = decodeHuff(AACSeq3(i + 1).chl.sfc, 12, huffLUT)'; + sfcR = decodeHuff(AACSeq3(i + 1).chr.sfc, 12, huffLUT)'; + if strcmp(AACSeq3(i+1).frameType, 'ESH') + sfcDeHuffL(42, 8) = 0; + sfcDeHuffR(42, 8) = 0; + sfcDeHuffL(2:end, :) = reshape(sfcL, [41 8]); + sfcDeHuffR(2:end, :) = reshape(sfcR, [41 8]); + sfcDeHuffL(1, :) = AACSeq3(i+1).chl.G; + sfcDeHuffR(1, :) = AACSeq3(i+1).chr.G; + else + sfcDeHuffL = [AACSeq3(i+1).chl.G; sfcL]; + sfcDeHuffR = [AACSeq3(i+1).chr.G; sfcR]; + end + + frameF(:, 1) = iAACquantizer(SL, sfcDeHuffL, ... + AACSeq3(i + 1).chl.G, AACSeq3(i+1).frameType); + frameF(:, 2) = iAACquantizer(SR, sfcDeHuffR, ... + AACSeq3(i + 1).chr.G, AACSeq3(i+1).frameType); frameF(:, 1) = iTNS(frameF(:, 1), ... AACSeq3(i+1).frameType, ... @@ -86,6 +94,8 @@ function x = iAACoder3(AACSeq3, fNameOut) end decodedAudio(currFrameStart:currFrameStop, :) = decodedAudio(currFrameStart:currFrameStop, :) + frameT; + + clearvars sfcDeHuffL sfcDeHuffR end audiowrite(fNameOut, decodedAudio, 48000);