📄 musicin.c
字号:
if(info->copyright) printf(">>> Copyrighted material\n");
else printf(">>> Material not copyrighted\n");
printf("Is this the original? (y/<n>): ");
gets(t);
if (*t == 'y' || *t == 'Y') info->original = 1;
else info->original = 0;
if(info->original) printf(">>> Original material\n");
else printf(">>> Material not original\n");
printf("Do you wish to exit (last chance before encoding)? (y/<n>): ");
gets(t);
if (*t == 'y' || *t == 'Y') exit(0);
}
/************************************************************************
*
* parse_args
*
* PURPOSE: Sets encoding parameters to the specifications of the
* command line. Default settings are used for parameters
* not specified in the command line.
*
* SEMANTICS: The command line is parsed according to the following
* syntax:
*
* -l is followed by the layer number
* -m is followed by the mode
* -p is followed by the psychoacoustic model number
* -s is followed by the sampling rate
* -b is followed by the total bitrate, irrespective of the mode
* -d is followed by the emphasis flag
* -c is followed by the copyright/no_copyright flag
* -o is followed by the original/not_original flag
* -e is followed by the error_protection on/off flag
*
* If the input file is in AIFF format, the sampling frequency is read
* from the AIFF header.
*
* The input and output filenames are read into #inpath# and #outpath#.
*
************************************************************************/
void
parse_args(argc, argv, fr_ps, psy, num_samples, inPath, outPath)
int argc;
char **argv;
frame_params *fr_ps;
int *psy;
unsigned long *num_samples;
char inPath[MAX_NAME_SIZE];
char outPath[MAX_NAME_SIZE];
{
FLOAT srate;
int brate;
layer *info = fr_ps->header;
int err = 0, i = 0;
IFF_AIFF pcm_aiff_data;
long samplerate;
long soundPosition;
/* preset defaults */
inPath[0] = '\0'; outPath[0] = '\0';
info->lay = DFLT_LAY;
switch(DFLT_MOD) {
case 's': info->mode = MPG_MD_STEREO; info->mode_ext = 0; break;
case 'd': info->mode = MPG_MD_DUAL_CHANNEL; info->mode_ext=0; break;
case 'j': info->mode = MPG_MD_JOINT_STEREO; break;
case 'm': info->mode = MPG_MD_MONO; info->mode_ext = 0; break;
default:
fprintf(stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD);
abort();
}
*psy = DFLT_PSY;
if((info->sampling_frequency = SmpFrqIndex((long)(1000*DFLT_SFQ), &info->version)) < 0) {
fprintf(stderr, "%s: bad sfrq default %.2f\n", programName, DFLT_SFQ);
abort();
}
info->bitrate_index = 14;
brate = 0;
switch(DFLT_EMP) {
case 'n': info->emphasis = 0; break;
case '5': info->emphasis = 1; break;
case 'c': info->emphasis = 3; break;
default:
fprintf(stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP);
abort();
}
info->copyright = 0; info->original = 0; info->error_protection = FALSE;
/* process args */
while(++i<argc && err == 0) {
char c, *token, *arg, *nextArg;
int argUsed;
token = argv[i];
if(*token++ == '-') {
if(i+1 < argc) nextArg = argv[i+1];
else nextArg = "";
argUsed = 0;
while( (c = *token++) ) {
if(*token /* NumericQ(token) */) arg = token;
else arg = nextArg;
switch(c) {
case 'l': info->lay = atoi(arg); argUsed = 1;
if(info->lay<1 || info->lay>3) {
fprintf(stderr,"%s: -l layer must be 1, 2, or 3, not %s\n",
programName, arg);
err = 1;
}
break;
case 'm': argUsed = 1;
if (*arg == 's')
{ info->mode = MPG_MD_STEREO; info->mode_ext = 0; }
else if (*arg == 'd')
{ info->mode = MPG_MD_DUAL_CHANNEL; info->mode_ext=0; }
else if (*arg == 'j')
{ info->mode = MPG_MD_JOINT_STEREO; }
else if (*arg == 'm')
{ info->mode = MPG_MD_MONO; info->mode_ext = 0; }
else {
fprintf(stderr,"%s: -m mode must be s/d/j/m not %s\n",
programName, arg);
err = 1;
}
break;
case 'p': *psy = atoi(arg); argUsed = 1;
if(*psy<1 || *psy>2) {
fprintf(stderr,"%s: -p model must be 1 or 2, not %s\n",
programName, arg);
err = 1;
}
break;
case 's':
argUsed = 1;
srate = atof( arg );
/* samplerate = rint( 1000.0 * srate ); $A */
samplerate = (long) (( 1000.0 * srate ) + 0.5);
if( (info->sampling_frequency =
SmpFrqIndex((long) samplerate, &info->version)) < 0 )
err = 1;
break;
case 'b':
argUsed = 1;
brate = atoi(arg);
break;
case 'd': argUsed = 1;
if (*arg == 'n') info->emphasis = 0;
else if (*arg == '5') info->emphasis = 1;
else if (*arg == 'c') info->emphasis = 3;
else {
fprintf(stderr,"%s: -d emp must be n/5/c not %s\n",
programName, arg);
err = 1;
}
break;
case 'c': info->copyright = 1; break;
case 'o': info->original = 1; break;
case 'e': info->error_protection = TRUE; break;
default: fprintf(stderr,"%s: unrec option %c\n",
programName, c);
err = 1; break;
}
if(argUsed) {
if(arg == token) token = ""; /* no more from token */
else ++i; /* skip arg we used */
arg = ""; argUsed = 0;
}
}
}
else {
if(inPath[0] == '\0') strcpy(inPath, argv[i]);
else if(outPath[0] == '\0') strcpy(outPath, argv[i]);
else {
fprintf(stderr,"%s: excess arg %s\n", programName, argv[i]);
err = 1;
}
}
}
if(err || inPath[0] == '\0') usage(); /* never returns */
if(outPath[0] == '\0') {
#ifdef MS_DOS
/* replace old extension with new one, 1992-08-19, 1995-06-12 shn */
new_ext(inPath, DFLT_EXT, outPath);
#else
strcpy(outPath, inPath);
strcat(outPath, DFLT_EXT);
#endif
}
if ((musicin = fopen(inPath, "rb")) == NULL) {
printf("Could not find \"%s\".\n", inPath);
exit(1);
}
open_bit_stream_w(&bs, outPath, BUFFER_SIZE);
if ((soundPosition = aiff_read_headers(musicin, &pcm_aiff_data)) != -1) {
printf(">>> Using Audio IFF sound file headers\n");
aiff_check(inPath, &pcm_aiff_data, &info->version);
if (fseek(musicin, soundPosition, SEEK_SET) != 0) {
printf("Could not seek to PCM sound data in \"%s\".\n", inPath);
exit(1);
}
info->sampling_frequency = SmpFrqIndex((long)pcm_aiff_data.sampleRate, &info->version);
printf(">>> %f Hz sampling frequency selected\n",
pcm_aiff_data.sampleRate);
/* Determine number of samples in sound file */
#ifndef MS_DOS
*num_samples = pcm_aiff_data.numChannels *
pcm_aiff_data.numSampleFrames;
#else
*num_samples = (long)(pcm_aiff_data.numChannels) *
(long)(pcm_aiff_data.numSampleFrames);
#endif
if ( pcm_aiff_data.numChannels == 1 ) {
info->mode = MPG_MD_MONO;
info->mode_ext = 0;
}
}
else { /* Not using Audio IFF sound file headers. */
if (fseek(musicin, 0, SEEK_SET) != 0) {
printf("Could not seek to PCM sound data in \"%s\".\n", inPath);
exit(1);
}
/* Declare sound file to have "infinite" number of samples. */
*num_samples = MAX_U_32_NUM;
}
if ( brate == 0 )
brate = bitrate[info->version][info->lay-1][14];
if( (info->bitrate_index = BitrateIndex(info->lay, brate, info->version)) < 0) err=1;
if(err || inPath[0] == '\0') usage(); /* never returns */
}
/************************************************************************
*
* print_config
*
* PURPOSE: Prints the encoding parameters used
*
************************************************************************/
void print_config( frame_params *fr_ps, int *psy, char *inPath, char *outPath)
{
layer *info = fr_ps->header;
printf("Encoding configuration:\n");
printf("Algorithm=%s\n", version_names[info->version]);
if(info->mode != MPG_MD_JOINT_STEREO)
printf("Layer=%s mode=%s extn=%d psy model=%d\n",
layer_names[info->lay-1], mode_names[info->mode],
info->mode_ext, *psy);
else printf("Layer=%s mode=%s extn=data dependant psy model=%d\n",
layer_names[info->lay-1], mode_names[info->mode], *psy);
printf("samp frq=%.1f kHz total bitrate=%d kbps\n",
s_freq[info->version][info->sampling_frequency],
bitrate[info->version][info->lay-1][info->bitrate_index]);
printf("de-emph=%d c/right=%d orig=%d errprot=%s\n",
info->emphasis, info->copyright, info->original,
((info->error_protection) ? "on" : "off"));
printf("input file: '%s' output file: '%s'\n", inPath, outPath);
}
/************************************************************************
*
* main
*
* PURPOSE: MPEG I Encoder supporting layers 1 and 2, and 3, with
* psychoacoustic models 1 (MUSICAM) and 2 (AT&T)
*
* SEMANTICS: One overlapping frame of audio of up to 2 channels are
* processed at a time in the following order:
* (associated routines are in parentheses)
*
* 1. Filter sliding window of data to get 32 subband
* samples per channel.
* (window_subband,filter_subband)
*
* 2. If joint stereo mode, combine left and right channels
* for subbands above #jsbound#.
* (*_combine_LR)
*
* 3. Calculate scalefactors for the frame, and if layer 2,
* also calculate scalefactor select information.
* (*_scale_factor_calc)
*
* 4. Calculate psychoacoustic masking levels using selected
* psychoacoustic model.
* (*_Psycho_One, psycho_anal)
*
* 5. Perform iterative bit allocation for subbands with low
* mask_to_noise ratios using masking levels from step 4.
* (*_main_bit_allocation)
*
* 6. If error protection flag is active, add redundancy for
* error protection.
* (*_CRC_calc)
*
* 7. Pack bit allocation, scalefactors, and scalefactor select
* information (layer 2) onto bitstream.
* (*_encode_bit_alloc,*_encode_scale,II_transmission_pattern)
*
* 8. Quantize subbands and pack them into bitstream
* (*_subband_quantization, *_sample_encoding)
*
************************************************************************/
int frameNum=0;
void main(argc, argv)
int argc;
char **argv;
{
typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
SBS FAR *sb_sample;
L3SBS FAR *l3_sb_sample;
typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
JSBS FAR *j_sample;
typedef double IN[2][HAN_SIZE];
IN FAR *win_que;
typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
SUB FAR *subband;
frame_params fr_ps;
layer info;
char original_file_name[MAX_NAME_SIZE];
char encoded_file_name[MAX_NAME_SIZE];
short FAR **win_buf;
static short FAR buffer[2][1152];
static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
static double FAR ltmin[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
FLOAT snr32[32];
short sam[2][1344]; /* was [1056]; */
int whole_SpF, extra_slot = 0;
double avg_slots_per_frame, frac_SpF, slot_lag;
int model, stereo, error_protection;
static unsigned int crc;
int i, j, k, adb;
unsigned long bitsPerSlot, samplesPerFrame;
unsigned long frameBits, sentBits = 0;
unsigned long num_samples;
#ifdef MACINTOSH
argc = ccommand( &argv );
#endif
/* Most large variables are declared dynamically to ensure
compatibility with smaller machines */
sb_sample = (SBS FAR *) mem_alloc(sizeof(SBS), "sb_sample");
l3_sb_sample = (L3SBS FAR *) mem_alloc(sizeof(SBS), "l3_sb_sample");
j_sample = (JSBS FAR *) mem_alloc(sizeof(JSBS), "j_sample");
win_que = (IN FAR *) mem_alloc(sizeof(IN), "Win_que");
subband = (SUB FAR *) mem_alloc(sizeof(SUB),"subband");
win_buf = (short FAR **) mem_alloc(sizeof(short *)*2, "win_buf");
/* clear buffers */
memset((char *) buffer, 0, sizeof(buffer));
memset((char *) bit_alloc, 0, sizeof(bit_alloc));
memset((char *) scalar, 0, sizeof(scalar));
memset((char *) j_scale, 0, sizeof(j_scale));
memset((char *) scfsi, 0, sizeof(scfsi));
memset((char *) ltmin, 0, sizeof(ltmin));
memset((char *) lgmin, 0, sizeof(lgmin));
memset((char *) max_sc, 0, sizeof(max_sc));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -