📄 musicin.c
字号:
} music_out = fopen(outPath, "wb");
if(music_out == NULL)
{
printf("Could not create \"%s\".\n", outPath);
exit(1);
}
file_type = get_file_type(musicin); switch(file_type) { case FILE_AIFF: 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 */ *num_samples = pcm_aiff_data.numChannels * pcm_aiff_data.numSampleFrames; if ( pcm_aiff_data.numChannels == 1 ) { info->mode = MPG_MD_MONO; info->mode_ext = 0; } } break; case FILE_WAVE: if ((wave_read_headers(musicin, &pcm_aiff_data)) != -1) { printf(">>> Using Audio WAVE sound file headers\n"); 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 */ *num_samples = pcm_aiff_data.numChannels * pcm_aiff_data.numSampleFrames; if ( pcm_aiff_data.numChannels == 1 ) { info->mode = MPG_MD_MONO; info->mode_ext = 0; } } break; default: { /* Not using Audio IFF / WAVE 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; } break; }
info->bitrate_index = getBitRateIndex(info->lay, brate, info->version);
if(info->bitrate_index < 0)
err = 1;
if(err || inPath[0] == '\0')
usage(); /* never returns */ return file_type;}/************************************************************************** print_config** PURPOSE: Prints the encoding parameters used*************************************************************************/ void print_config( layer *info, i8 *inPath, i8 *outPath){ printf("Encoding configuration:\n"); printf("Algorithm=%s\n", getVersionName(info->version));
if(info->mode != MPG_MD_JOINT_STEREO) printf("Layer=%s mode=%s extn=%d\n", getLayerName(info->lay), getModeName(info->mode), info->mode_ext); else
printf("Layer=%s mode=%s extn=data dependant\n", getLayerName(info->lay), getModeName(info->mode));
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)*************************************************************************/i32 frameNum=0;
void main(i32 argc, i8 **argv){ i8 original_file_name[MAX_NAME_SIZE]; i8 encoded_file_name[MAX_NAME_SIZE]; u32 num_samples; i32 file_type;
i32 stereo;
i32 result;
layer *info;
u8 *f_buf;
MP3CODEC *mp3codec;
printf("MPEG-1 Audio Layer III Encoder, Ver 0.01\n"); programName = argv[0]; if(argc<=1) /* no command-line args */
{ printf("This MP3 encoder needs arguments\n>>> Example:-b 128 -s 441 -l 3 -m s -p 2 frer07_1.wav frer07_1.mp3");
}
// make sure all entries in structure layer(info) are valid !!! info = (layer *)malloc(sizeof(layer));
mp3codec = (MP3CODEC *)malloc(sizeof(MP3CODEC));
memset(info, 0, sizeof(layer));
memset(mp3codec, 0, sizeof(MP3CODEC));
file_type = parse_args(argc, argv, info, &num_samples, original_file_name, encoded_file_name);
print_config(info, original_file_name, encoded_file_name);
f_buf = (u8 *)malloc(WRITE_BUFFER_SIZE);
result = init_MP3codec(mp3codec, info);
if(!result)
{
printf("Error! init_MP3codec failed\n");
exit(1);
}
stereo = mp3codec->fr_ps.stereo;
// Start MP3 encoding while ( get_audio(musicin, mp3codec->buffer, num_samples, stereo, info, file_type) > 0 )
{
i32 size;
//printf("to encoding frame %d\n", frameNum);
size = MP3_encoding(f_buf, mp3codec);
if(size)
fwrite(f_buf, 1, size, music_out);
frameNum++; }
{
i32 size;
size = deinit_MP3codec(mp3codec, f_buf);
fwrite(f_buf, size, 1, music_out);
}
if(fclose(music_out) != 0){
printf("Could not close \"%s\".\n", encoded_file_name);
exit(2);
}; if (fclose(musicin) != 0){ printf("Could not close \"%s\".\n", original_file_name); exit(2); }
free(info);
free(mp3codec); printf("Encoding of \"%s\" is finished\n", original_file_name); printf("The MPEG encoded output file name is \"%s\"\n", encoded_file_name); } /************************************************************************** usage** PURPOSE: Writes command line syntax to the file specified by #stderr#*************************************************************************/void usage() /* print syntax & exit */{ fprintf(stderr, "usage: %s queries for all arguments, or\n", programName); fprintf(stderr, " %s [-l lay][-m mode][-s sfrq][-b br][-d emp]\n", programName); fprintf(stderr, " [-c][-o][-e] inputPCM [outBS]\n"); fprintf(stderr,"where\n"); fprintf(stderr," -l lay use layer <lay> coding (dflt %4u)\n",DFLT_LAY); fprintf(stderr," -m mode channel mode : s/d/j/m (dflt %4c)\n",DFLT_MOD); fprintf(stderr," -s sfrq input smpl rate in kHz (dflt %4.1f)\n",DFLT_SFQ); fprintf(stderr," -b br total bitrate in kbps (dflt highest)\n"); fprintf(stderr," -d emp de-emphasis n/5/c (dflt %4c)\n",DFLT_EMP); fprintf(stderr," -c mark as copyright\n"); fprintf(stderr," -o mark as original\n"); fprintf(stderr," -e add error protection\n"); fprintf(stderr," inputPCM input PCM sound file (standard or AIFF)\n"); fprintf(stderr," outBS output bit stream of encoded audio (dflt inName+%s)\n", DFLT_EXT); exit(1);}/************************************************************************** aiff_check** PURPOSE: Checks AIFF header information to make sure it is valid.* Exits if not.*************************************************************************/void aiff_check( i8 *file_name, IFF_AIFF *pcm_aiff_data, i32 *version){ if (pcm_aiff_data->sampleType != IFF_ID_SSND) { printf("Sound data is not PCM in \"%s\".\n", file_name); exit(1); } if(SmpFrqIndex((i32)pcm_aiff_data->sampleRate, version) < 0) { printf("in \"%s\".\n", file_name); exit(1); } if (pcm_aiff_data->sampleSize != sizeof(i16) * BITS_IN_A_BYTE) { printf("Sound data is not %d bits in \"%s\".\n", sizeof(i16) * BITS_IN_A_BYTE, file_name); exit(1); } if (pcm_aiff_data->numChannels != MONO && pcm_aiff_data->numChannels != STEREO) { printf("Sound data is not mono or stereo in \"%s\".\n", file_name); exit(1); } if (pcm_aiff_data->blkAlgn.blockSize != 0) { printf("Block size is not %d bytes in \"%s\".\n", 0, file_name); exit(1); } if (pcm_aiff_data->blkAlgn.offset != 0) { printf("Block offset is not %d bytes in \"%s\".\n", 0, file_name); exit(1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -