📄 musicin.c
字号:
/**********************************************************************
* ISO MPEG Audio Subgroup Software Simulation Group (1996)
* ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
*
* $Id: musicin.c,v 1.2 1997/01/19 22:28:29 rowlands Exp $
*
* $Log: musicin.c,v $
* Revision 1.2 1997/01/19 22:28:29 rowlands
* Layer 3 bug fixes from Seymour Shlien
*
* Revision 1.1 1996/02/14 04:04:23 rowlands
* Initial revision
*
* Received from Mike Coleman
**********************************************************************/
/**********************************************************************
* date programmers comment *
* 3/01/91 Douglas Wong, start of version 1.1 records *
* Davis Pan *
* 3/06/91 Douglas Wong, rename: setup.h to endef.h *
* removed extraneous variables *
* 3/21/91 J.Georges Fritsch introduction of the bit-stream *
* package. This package allows you *
* to generate the bit-stream in a *
* binary or ascii format *
* 3/31/91 Bill Aspromonte replaced the read of the SB matrix *
* by an "code generated" one *
* 5/10/91 W. Joseph Carter Ported to Macintosh and Unix. *
* Incorporated Jean-Georges Fritsch's *
* "bitstream.c" package. *
* Modified to strictly adhere to *
* encoded bitstream specs, including *
* "Berlin changes". *
* Modified user interface dialog & code *
* to accept any input & output *
* filenames desired. Also added *
* de-emphasis prompt and final bail-out *
* opportunity before encoding. *
* Added AIFF PCM sound file reading *
* capability. *
* Modified PCM sound file handling to *
* process all incoming samples and fill *
* out last encoded frame with zeros *
* (silence) if needed. *
* Located and fixed numerous software *
* bugs and table data errors. *
* 27jun91 dpwe (Aware Inc) Used new frame_params struct. *
* Clear all automatic arrays. *
* Changed some variable names, *
* simplified some code. *
* Track number of bits actually sent. *
* Fixed padding slot, stereo bitrate *
* Added joint-stereo : scales L+R. *
* 6/12/91 Earle Jennings added fix for MS_DOS in obtain_param *
* 6/13/91 Earle Jennings added stack length adjustment before *
* main for MS_DOS *
* 7/10/91 Earle Jennings conversion of all float to FLOAT *
* port to MsDos from MacIntosh completed*
* 8/ 8/91 Jens Spille Change for MS-C6.00 *
* 8/22/91 Jens Spille new obtain_parameters() *
*10/ 1/91 S.I. Sudharsanan, Ported to IBM AIX platform. *
* Don H. Lee, *
* Peter W. Farrett *
*10/ 3/91 Don H. Lee implemented CRC-16 error protection *
* newly introduced functions are *
* I_CRC_calc, II_CRC_calc and encode_CRC*
* Additions and revisions are marked *
* with "dhl" for clarity *
*11/11/91 Katherine Wang Documentation of code. *
* (variables in documentation are *
* surround by the # symbol, and an '*'*
* denotes layer I or II versions) *
* 2/11/92 W. Joseph Carter Ported new code to Macintosh. Most *
* important fixes involved changing *
* 16-bit ints to long or unsigned in *
* bit alloc routines for quant of 65535 *
* and passing proper function args. *
* Removed "Other Joint Stereo" option *
* and made bitrate be total channel *
* bitrate, irrespective of the mode. *
* Fixed many small bugs & reorganized. *
* 2/25/92 Masahiro Iwadare made code cleaner and more consistent *
* 8/07/92 Mike Coleman make exit() codes return error status *
* made slight changes for portability *
*19 aug 92 Soren H. Nielsen Changed MS-DOS file name extensions. *
* 8/25/92 Shaun Astarabadi Replaced rint() function with explicit*
* rounding for portability with MSDOS. *
* 9/22/92 jddevine@aware.com Fixed _scale_factor_calc() calls. *
*10/19/92 Masahiro Iwadare added info->mode and info->mode_ext *
* updates for AIFF format files *
* 3/10/93 Kevin Peterson In parse_args, only set non default *
* bit rate if specified in arg list. *
* Use return value from aiff_read_hdrs *
* to fseek to start of sound data *
* 7/26/93 Davis Pan fixed bug in printing info->mode_ext *
* value for joint stereo condition *
* 8/27/93 Seymour Shlien, Fixes in Unix and MSDOS ports, *
* Daniel Lauzon, and *
* Bill Truerniet *
* 11/7/95 Soeren H. Nielsen LSF added. Bug fix in MSDOS ext. *
* 8/02/95 mc@fivebats.com Changed default bitrate selection so *
* it works with the new LSF stuff *
*10/01/95 mc@fivebats.com Added layer3 *
**********************************************************************/
#ifdef MS_DOS
#include <dos.h>
#endif
#include <stdlib.h>
#include "common.h"
#include "encoder.h"
#include "l3psy.h"
#include "mdct.h"
#include "loop.h"
#include "l3bitstream.h"
#include <assert.h>
/* Global variable definitions for "musicin.c" */
FILE *musicin;
Bit_stream_struc bs;
char *programName;
/* Implementations */
/************************************************************************
*
* obtain_parameters
*
* PURPOSE: Prompts for and reads user input for encoding parameters
*
* SEMANTICS: The parameters read are:
* - input and output filenames
* - sampling frequency (if AIFF file, will read from the AIFF file header)
* - layer number
* - mode (stereo, joint stereo, dual channel or mono)
* - psychoacoustic model (I or II)
* - total bitrate, irrespective of the mode
* - de-emphasis, error protection, copyright and original or copy flags
*
************************************************************************/
void
obtain_parameters(fr_ps,psy,num_samples,original_file_name,encoded_file_name)
frame_params *fr_ps;
int *psy;
unsigned long *num_samples;
char original_file_name[MAX_NAME_SIZE];
char encoded_file_name[MAX_NAME_SIZE];
{
int j;
long int freq;
int model, brt;
char t[50];
IFF_AIFF pcm_aiff_data;
layer *info = fr_ps->header;
long soundPosition;
#ifdef MS_DOS
char temp_str[MAX_NAME_SIZE];
#endif
do {
printf("Enter PCM input file name <required>: ");
gets(original_file_name);
if (original_file_name[0] == NULL_CHAR)
printf("PCM input file name is required.\n");
} while (original_file_name[0] == NULL_CHAR);
printf(">>> PCM input file name is: %s\n", original_file_name);
if ((musicin = fopen(original_file_name, "rb")) == NULL) {
printf("Could not find \"%s\".\n", original_file_name);
exit(1);
}
#ifdef MS_DOS
/* replace old extension with new one, 1992-08-19, 1995-06-12 shn */
new_ext(original_file_name, DFLT_EXT, temp_str);
printf("Enter MPEG encoded output file name <%s>: ",
temp_str);
#else
printf("Enter MPEG encoded output file name <%s%s>: ",
original_file_name, DFLT_EXT);
#endif
gets(encoded_file_name);
if (encoded_file_name[0] == NULL_CHAR) {
#ifdef MS_DOS
strcpy(encoded_file_name, temp_str);
#else
strcat(strcpy(encoded_file_name, original_file_name), DFLT_EXT);
#endif
}
printf(">>> MPEG encoded output file name is: %s\n", encoded_file_name);
open_bit_stream_w(&bs, encoded_file_name, BUFFER_SIZE);
if ((soundPosition = aiff_read_headers(musicin, &pcm_aiff_data)) != -1) {
printf(">>> Using Audio IFF sound file headers\n");
aiff_check(original_file_name, &pcm_aiff_data, &info->version);
if (fseek(musicin, soundPosition, SEEK_SET) != 0) {
printf("Could not seek to PCM sound data in \"%s\".\n",
original_file_name);
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
}
else { /* Not using Audio IFF sound file headers. */
printf("What is the sampling frequency? <44100>[Hz]: ");
gets(t);
freq = atol(t);
switch (freq) {
case 48000 : info->sampling_frequency = 1;
printf(">>> %ld Hz sampling freq selected\n", freq);
break;
case 44100 : info->sampling_frequency = 0;
printf(">>> %ld Hz sampling freq selected\n", freq);
break;
case 32000 : info->sampling_frequency = 2;
printf(">>> %ld Hz sampling freq selected\n", freq);
break;
case 24000 : info->sampling_frequency = 1;
info->version = MPEG_PHASE2_LSF;
printf(">>> %ld Hz sampling freq selected\n", freq);
break;
case 22050 : info->sampling_frequency = 0;
info->version = MPEG_PHASE2_LSF;
printf(">>> %ld Hz sampling freq selected\n", freq);
break;
case 16000 : info->sampling_frequency = 2;
info->version = MPEG_PHASE2_LSF;
printf(">>> %ld Hz sampling freq selected\n", freq);
break;
default: info->sampling_frequency = 0;
printf(">>> Default 44.1 kHz samp freq selected\n");
}
printf(">>> Encoding algorithm is %s\n", version_names[info->version]);
if (fseek(musicin, 0, SEEK_SET) != 0) {
printf("Could not seek to PCM sound data in \"%s\".\n",
original_file_name);
exit(1);
}
/* Declare sound file to have "infinite" number of samples. */
*num_samples = MAX_U_32_NUM;
}
printf("Which layer do you want to use?\n");
printf("Available: Layer (1), Layer (<2>), Layer (3): ");
gets(t);
switch(*t){
case '1': info->lay = 1; printf(">>> Using Layer %s\n",t); break;
case '2': info->lay = 2; printf(">>> Using Layer %s\n",t); break;
case '3': info->lay = 3; printf(">>> Using Layer %s\n",t); break;
default: info->lay = 2; printf(">>> Using default Layer 2\n"); break;
}
printf("Which mode do you want?\n");
printf("Available: (<s>)tereo, (j)oint stereo, ");
printf("(d)ual channel, s(i)ngle Channel: ");
gets(t);
switch(*t){
case 's':
case 'S':
info->mode = MPG_MD_STEREO; info->mode_ext = 0;
printf(">>> Using mode %s\n",t);
break;
case 'j':
case 'J':
info->mode = MPG_MD_JOINT_STEREO;
printf(">>> Using mode %s\n",t);
break;
case 'd':
case 'D':
info->mode = MPG_MD_DUAL_CHANNEL; info->mode_ext = 0;
printf(">>> Using mode %s\n",t);
break;
case 'i':
case 'I':
info->mode = MPG_MD_MONO; info->mode_ext = 0;
printf(">>> Using mode %s\n",t);
break;
default:
info->mode = MPG_MD_STEREO; info->mode_ext = 0;
printf(">>> Using default stereo mode\n");
break;
}
printf("Which psychoacoustic model do you want to use? <1>: ");
gets(t);
model = atoi(t);
if (model > 2 || model < 1) {
printf(">>> Default model 1 selected\n");
*psy = 1;
}
else {
*psy = model;
printf(">>> Using psychoacoustic model %d\n", model);
}
/* set default bitrate to highest allowed, which is index 14 */
brt = bitrate[info->version][info->lay-1][14];
printf( "What is the total bitrate? <%u>[kbps]: ", brt );
gets( t );
brt = atoi( t );
if ( brt == 0 )
j = 15;
else
j = 0;
while ( j < 15 )
{
if ( bitrate[info->version][info->lay-1][j] == brt )
break;
j++;
}
if ( j == 15 )
{
brt = bitrate[info->version][info->lay-1][14];
printf( ">>> Using default %u kbps\n", brt );
info->bitrate_index = 14;
}
else
{
info->bitrate_index = j;
printf( ">>> Bitrate = %d kbps\n", bitrate[info->version][info->lay-1][j] );
}
printf("What type of de-emphasis should the decoder use?\n");
printf("Available: (<n>)one, (5)0/15 microseconds, (c)citt j.17: ");
gets(t);
if (*t != 'n' && *t != '5' && *t != 'c') {
printf(">>> Using default no de-emphasis\n");
info->emphasis = 0;
}
else {
if (*t == 'n') info->emphasis = 0;
else if (*t == '5') info->emphasis = 1;
else if (*t == 'c') info->emphasis = 3;
printf(">>> Using de-emphasis %s\n",t);
}
/* Start 2. Part changes for CD Ver 3.2; jsp; 22-Aug-1991 */
printf("Do you want to set the private bit? (y/<n>): ");
gets(t);
if (*t == 'y' || *t == 'Y') info->extension = 1;
else info->extension = 0;
if(info->extension) printf(">>> Private bit set\n");
else printf(">>> Private bit not set\n");
/* End changes for CD Ver 3.2; jsp; 22-Aug-1991 */
printf("Do you want error protection? (y/<n>): ");
gets(t);
if (*t == 'y' || *t == 'Y') info->error_protection = TRUE;
else info->error_protection = FALSE;
if(info->error_protection) printf(">>> Error protection used\n");
else printf(">>> Error protection not used\n");
printf("Is the material copyrighted? (y/<n>): ");
gets(t);
if (*t == 'y' || *t == 'Y') info->copyright = 1;
else info->copyright = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -