📄 setup.m
字号:
% MATLAB SIMULATION OF NSA FS-1016 CELP v3.2
% COPYRIGHT (C) 1995-99 ANDREAS SPANIAS AND TED PAINTER
%
% This Copyright applies only to this particular MATLAB implementation
% of the FS-1016 CELP coder. The MATLAB software is intended only for educational
% purposes. No other use is intended or authorized. This is not a public
% domain program and distribution to individuals or networks is strictly
% prohibited. Be aware that use of the standard in any form is goverened
% by rules of the US DoD. Therefore patents and royalties may apply to
% authors, companies, or committees associated with this standard, FS-1016. For
% questions regarding the MATLAB implementation please contact Andreas
% Spanias at (480) 965-1837. For questions on rules,
% royalties, or patents associated with the standard, please contact the DoD.
%
% ALL DERIVATIVE WORKS MUST INCLUDE THIS COPYRIGHT NOTICE.
%
% ******************************************************************
% SETUP
%
% PORTED TO MATLAB FROM CELP 3.2a C RELEASE
% 6-7-94
%
% ******************************************************************
%
% DESCRIPTION
%
% Open input and output speech files.
%
% DESIGN NOTES
%
% Computes total number of frames in the input file by seeking
% to file end, then rewinding.
%
% VARIABLES
%
% INPUTS
% none
%
% OUTPUTS
% fpi - Input file pointer
% fpo - File pointer, postfiltered output speech
% fpnpf - File pointer, non postfiltered output speech
% fphpf - File pointer, high-pass filtered output speech
%
% INTERNALS
% emsg - Error message returned by fopen
%
% GLOBALS
% guiFrames - Total number of frames in input file for gui display
% SimType - Simulation type - ANALYSIS/SYNTHESIS or SYNTHESIS only
% infile - Input filename
% outfile - Output filename, postfiltered
% npffile - Output filename, non postfiltered
% hpffile - Output filename, highpass filtered
%
% CONSTANTS
% LFRAME - Speech analysis frame size
% ANALYSIS - Simulation ANALYSIS only mode
%
% ******************************************************************
function [ fpi, fpo, fpnpf, fphpf ] = setup
% DECLARE GLOBAL VARIABLES FOR USER INTERFACE
global guiIfile guiOfile guiFrames CELPpath
% DECLARE GLOBAL CONSTANTS
global LFRAME
% DEFINE FILE CONSTANTS
WAVHDRSIZE = 44; % .WAV FILE HEADER SIZE (IN BYTES)
% CELPhdr = 'FS-1016 CELP 3.2a ,4.8';
CELPhdr = '';
% DISPLAY FILE SELECTION DIALOG
[ file, path ] = uigetfile( '*.wav', 'Select An Input Speech File' );
% OPEN FILE AND COMPUTE INPUT SPEECH FRAMES (TRAP CANCEL EXCEPTION)
if file ~= 0
% UPDATE SEARCH PATH FOR INPUT FILES
[ fpi, emsg ] = fopen( [path,file], 'r' );
fHdrOffset = WAVHDRSIZE;
guiIfile = file;
% READ WAVE FILE HEADER
rID = fread( fpi, 4, 'char' );
rLen = fread( fpi, 1, 'long' );
wId = fread( fpi, 4, 'char' );
ChunkID = fread( fpi, 4, 'char' );
fLen = fread( fpi, 1, 'long' );
wFormatTag = fread( fpi, 1, 'short' );
nChannels = fread( fpi, 1, 'short' );
nSamplesPerSec = fread( fpi, 1, 'long' );
nAvgBytesPerSec = fread( fpi, 1, 'long' );
nBlockAlign = fread( fpi, 1, 'short' );
FormatSpecific = fread( fpi, fLen-14, 'char' );
dId = fread( fpi, 4, 'char' );
dLen = fread( fpi, 1, 'long' );
% VERIFY VALID .WAV FILE HAS BEEN SELECTED
WORDSIZE = nBlockAlign;
FSAMP = nSamplesPerSec;
c = zeros( 9, 1 );
c(1) = strcmp( setstr( rID )', 'RIFF' );
c(2) = strcmp( setstr( wId )', 'WAVE' );
c(3) = strcmp( setstr( ChunkID )', 'fmt ' );
c(4) = strcmp( setstr( dId )', 'data' );
c(5) = (wFormatTag == 1);
c(6) = (nChannels == 1);
c(7) = (nAvgBytesPerSec == (FSAMP * WORDSIZE));
c(8) = WORDSIZE == 2;
c(9) = FSAMP == 8000;
if sum(c) ~= length(c)
if ~c(1) | ~c(2) | ~c(3) | ~c(4) | ~c(7)
error( 'Invalid .WAV file: File header corrupted!' );
elseif ~c(5)
error( 'Invalid .WAV file: Non-PCM data not supported!' );
elseif ~c(6)
error( 'Invalid .WAV file: Stereo not supported!' );
elseif ~c(8)
bs = str2num(fprintf('%d',8*WORDSIZE/nChannels));
error( ['Invalid .WAV file: CELP requires 16-bits per sample. Found', bs, '.' ] );
elseif ~c(9)
sr = str2num(fprintf('%d',FSAMP));
error( ['Invalid .WAV file: CELP requires 8 kHz sample rate. Found ', sr, '.'] );
end
end
else
error( 'CELP Simulation aborted!' );
end
% TRAP FILE OPEN EXCEPTIONS
if fpi == -1
fclose( 'all' );
error( emsg );
else
% DETERMINE FRAME COUNT
fseek( fpi, 0, 'eof' );
guiFrames = fix( ( ftell(fpi) - fHdrOffset ) / (WORDSIZE*LFRAME) );
fseek( fpi, fHdrOffset, 'bof' );
end
% OPEN OUTPUT FILE; TRAP EXCEPTIONS
[ file, path ] = uiputfile( '*.wav', 'Select An Output Speech File' );
cd(CELPpath);
% OPEN OUTPUT FILES AND GENERATE .WAV HEADERS (TRAP CANCEL EXCEPTION)
if file ~= 0
% UPDATE SEARCH PATH FOR INPUT FILES
[ fpo, emsg ] = fopen( [path,file], 'w' );
[ fpnpf, emsg ] = fopen( [path,'npf.wav'], 'w' );
[ fphpf, emsg ] = fopen( [path,'hpf.wav'], 'w' );
fHdrOffset = WAVHDRSIZE;
guiOfile = file;
% CREATE .WAV HEADERS
if ~wavhdr( fpo, guiFrames, LFRAME, CELPhdr )
error( 'Unable to create output .WAV file!');
end
if ~wavhdr( fpnpf, guiFrames, LFRAME, CELPhdr )
error( 'Unable to create non-postfiltered .WAV file!');
end
if ~wavhdr( fphpf, guiFrames, LFRAME, CELPhdr )
error( 'Unable to create high-pass filtered .WAV file!');
end
else
error( 'CELP Simulation aborted!' );
end
% TRAP FILE OPEN EXCEPTIONS
if fpo == -1
fclose( 'all' );
error( emsg );
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -