diff --git a/Level_3/AACoder3.m b/Level_3/AACoder3.m index 827ad6b..e0b0260 100644 --- a/Level_3/AACoder3.m +++ b/Level_3/AACoder3.m @@ -130,7 +130,6 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) TL(band) = sumsqr(subFrameF) ./ SMRL(band); TR(band) = sumsqr(subFrameF) ./ SMRR(band); end - plot(TL); else TL(SHORT_WINDOW_NUMBER_OF_BANDS, 1) = 0; TR(SHORT_WINDOW_NUMBER_OF_BANDS, 1) = 0; @@ -176,6 +175,8 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) AACSeq3(i + 1).chr.stream = streamR; AACSeq3(i + 1).chl.codebook = huffcodebookL; AACSeq3(i + 1).chr.codebook = huffcodebookR; + + clearvars TL TR end save(fnameAACoded, 'AACSeq3'); diff --git a/Level_3/decoded3.wav b/Level_3/decoded3.wav index fca30cf..fb9dcf2 100644 Binary files a/Level_3/decoded3.wav and b/Level_3/decoded3.wav differ diff --git a/Level_3/demoAAC3.m b/Level_3/demoAAC3.m index 2a65ede..bf0e1f6 100644 --- a/Level_3/demoAAC3.m +++ b/Level_3/demoAAC3.m @@ -16,11 +16,26 @@ function [SNR, bitrate, compression] = demoAAC3(fNameIn, fNameOut, frameAACoded) % the bitrate after it AACSeq3 = AACoder3(fNameIn, frameAACoded); + + totalSize = 0; + for frameIndex = 1:length(AACSeq3) + totalSize = totalSize + 2; % frameType + totalSize = totalSize + 2 * 4; % TNS coefficients + totalSize = totalSize + length(AACSeq3(frameIndex).chl.G) * 4; + totalSize = totalSize + length(AACSeq3(frameIndex).chr.G) * 4; + totalSize = totalSize + length(AACSeq3(frameIndex).chl.sfc); + totalSize = totalSize + length(AACSeq3(frameIndex).chr.sfc); + totalSize = totalSize + length(AACSeq3(frameIndex).chl.stream); + totalSize = totalSize + length(AACSeq3(frameIndex).chr.stream); + end + decodedAudio = iAACoder3(AACSeq3, fNameOut); + bitrate = totalSize / (length(decodedAudio) / 48000); [audioData, ~] = audioread(fNameIn); + compression = (length(audioData) * 64) / totalSize; snr(audioData(1025:length(decodedAudio)-1024, 1), audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1)) snr(audioData(1025:length(decodedAudio)-1024, 2), audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, 2)) - SNR = snr(audioData(1025:length(decodedAudio)-1024, :), audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, :)); + SNR = snr(audioData(1025:length(decodedAudio)-1024, :), audioData(1025:length(decodedAudio)-1024, :) - decodedAudio(1025:end-1024, :)); end diff --git a/Level_3/encoded.mat b/Level_3/encoded.mat index c62b9bc..6892914 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 bb3fc5f..6ec492c 100644 --- a/Level_3/iAACoder3.m +++ b/Level_3/iAACoder3.m @@ -41,58 +41,47 @@ function x = iAACoder3(AACSeq3, fNameOut) decodedAudio(1024 * (length(AACSeq3) + 1), 2) = 0; % Initializes an array to hold both audio channels frameF(1024, 2) = 0; - + % Decodes audio file huffLUT = loadLUT(); for i = 0:length(AACSeq3) - 1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; - - if i < 2 - continue; - frameF(:, 1) = iTNS(AACSeq3(i + 1).chl.stream, ... - AACSeq3(i+1).frameType, ... - AACSeq3(i + 1).chl.TNScoeffs); - frameF(:, 2) = iTNS(AACSeq3(i + 1).chr.stream, ... - AACSeq3(i+1).frameType, ... - AACSeq3(i + 1).chr.TNScoeffs); - - frameT = iFilterbank(frameF, AACSeq3(i+1).frameType, AACSeq3(i+1).winType); + + 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 - 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, ... - AACSeq3(i + 1).chl.TNScoeffs); - frameF(:, 2) = iTNS(frameF(:, 2), ... - AACSeq3(i+1).frameType, ... - AACSeq3(i + 1).chr.TNScoeffs); - - frameT = iFilterbank(frameF, AACSeq3(i+1).frameType, AACSeq3(i+1).winType); + 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, ... + AACSeq3(i + 1).chl.TNScoeffs); + frameF(:, 2) = iTNS(frameF(:, 2), ... + AACSeq3(i+1).frameType, ... + AACSeq3(i + 1).chr.TNScoeffs); + + frameT = iFilterbank(frameF, AACSeq3(i+1).frameType, AACSeq3(i+1).winType); + + decodedAudio(currFrameStart:currFrameStop, :) = decodedAudio(currFrameStart:currFrameStop, :) + frameT; clearvars sfcDeHuffL sfcDeHuffR