📄 main.c
字号:
/* main.c
* Command line interface.
*
* This fixed point version of shine is based on Gabriel Bouvigne's original
* source, version 0.1.2
*/
#include "types.h"
config_t config;
int cutoff;
int raw;
int mono_from_stereo;
int fd;
int mixerhander;
/*
* error:
* ------
*/
void error(char *s)
{
printf("[ERROR] %s\n",s);
exit(1);
}
/*
* set_defaults:
* -------------
*/
static void set_defaults()
{
config.mpeg.type = MPEG1;
config.mpeg.layr = LAYER_3;
config.mpeg.mode = MODE_DUAL_CHANNEL;
config.mpeg.bitr = 48;
config.mpeg.psyc = 0;
config.mpeg.emph = 0;
config.mpeg.crc = 0;
config.mpeg.ext = 0;
config.mpeg.mode_ext = 0;
config.mpeg.copyright = 0;
config.mpeg.original = 1;
config.mpeg.channels = 1;
config.mpeg.granules = 1;
cutoff = 418; /* 16KHz @ 44.1Ksps */
config.wave.samplerate = 44100;
}
/*
* find_samplerate_index:
* ----------------------
*/
static int find_samplerate_index(long freq)
{
static long sr[4][3] = {{11025, 12000, 8000}, /* mpeg 2.5 */
{ 0, 0, 0}, /* reserved */
{22050, 24000, 16000}, /* mpeg 2 */
{44100, 48000, 32000}}; /* mpeg 1 */
int i, j;
for(j=0; j<4; j++)
for(i=0; i<3; i++)
if((freq == sr[j][i]) && (j != 1))
{
config.mpeg.type = j;
return i;
}
error("Invalid samplerate");
return 0;
}
/*
* find_bitrate_index:
* -------------------
*/
static int find_bitrate_index(int bitr)
{
static long br[2][15] =
{{0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, /* mpeg 2/2.5 */
{0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}}; /* mpeg 1 */
int i;
for(i=1; i<15; i++)
if(bitr==br[config.mpeg.type & 1][i]) return i;
error("Invalid bitrate");
return 0;
}
int set_cutoff(void)
{
static int cutoff_tab[3][2][15] =
{
{ /* 44.1k, 22.05k, 11.025k */
{100,104,131,157,183,209,261,313,365,418,418,418,418,418,418}, /* stereo */
{183,209,261,313,365,418,418,418,418,418,418,418,418,418,418} /* mono */
},
{ /* 48k, 24k, 12k */
{100,104,131,157,183,209,261,313,384,384,384,384,384,384,384}, /* stereo */
{183,209,261,313,365,384,384,384,384,384,384,384,384,384,384} /* mono */
},
{ /* 32k, 16k, 8k */
{100,104,131,157,183,209,261,313,365,418,522,576,576,576,576}, /* stereo */
{183,209,261,313,365,418,522,576,576,576,576,576,576,576,576} /* mono */
}
};
return cutoff_tab[config.mpeg.samplerate_index]
[config.mpeg.mode == MODE_MONO]
[config.mpeg.bitrate_index];
}
/*
* check_config:
* -------------
*/
static void check_config()
{
static char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "mono" };
static char *layer_names[4] = { "", "III", "II", "I" };
static char *version_names[4] = { "MPEG 2.5", "", "MPEG 2", "MPEG 1" };
static char *psy_names[3] = { "none", "MUSICAM", "Shine" };
static char *demp_names[4] = { "none", "50/15us", "", "CITT" };
config.mpeg.samplerate_index = find_samplerate_index(config.wave.samplerate);
config.mpeg.bitrate_index = find_bitrate_index(config.mpeg.bitr);
cutoff = set_cutoff();
printf("%s layer %s, %s Psychoacoustic Model: %s\n",
version_names[config.mpeg.type],
layer_names[config.mpeg.layr],
mode_names[config.mpeg.mode],
psy_names[config.mpeg.psyc] );
printf("Bitrate=%d kbps ",config.mpeg.bitr );
printf("De-emphasis: %s %s %s\n",
demp_names[config.mpeg.emph],
(config.mpeg.original) ? "Original" : "",
(config.mpeg.copyright) ? "(C)" : "" );
}
/*
initial_buf()
*/
void initial_buf(void)
{
memset(buf, 0, sizeof(short)*samp_per_frame2);
}
int audio_initial(void)
{
int status;
int arg;
//int left = 100;
//int right = 100;
//int volume = (right << 8) + left;
fd = open("/dev/dsp", O_RDONLY);
if (fd < 0) {
error("open of /dev/dsp failed");
return -1;
}
arg = BITWIDTH;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if(status < 0 )
error("SOUND_PCM_WRITE_BITS fails\n");
arg = CHANNELS;
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if(status < 0 )
error("SOUND_PCM_WRITE_CHANNELS fails\n");
arg = RATE;
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1) {
error("SOUND_PCM_WRITE_WRITE ioctl failed");
}
/* mixerhander = open( "/dev/mixer", O_WRONLY );
if ( mixerhander >= 0 ) {
int devmask = SOUND_MIXER_MIC;
ioctl(mixerhander, SOUND_MIXER_WRITE_RECSRC, &devmask);
ioctl(mixerhander, SOUND_MIXER_WRITE_MIC, &volume );
close(mixerhander);
}
else
return -2;*/
return 1;
}
/*
wave_initial
*/
void wave_initial(void)
{
long length;
raw = 0;
mono_from_stereo = 1;
set_defaults();
//config.infile = "/home/src/shine8/ww.wav";
config.outfile = "/home/src/shine8/hello.mp3";
//if((config.wave.file = fopen(config.infile,"rb")) == NULL)
// error("Unable to open file");
config.mpeg.bitr = BITRATE;
config.wave.samplerate = RATE;
length = 1152;
config.wave.channels = 1;
config.wave.bits = 16;
config.wave.length = length/(2*config.wave.samplerate);
config.wave.total_samples = length/2;
config.mpeg.channels = 1;
config.mpeg.mode = MODE_MONO;
}
/*
free device
*/
void free_dev(void)
{
if(fd > 0)
close(fd);
}
/*
initial all
*/
int initial_all(void)
{
int i;
initial_buf();
wave_initial();
check_config();
if((i = audio_initial()) < 0 ) {
error("error in initial audio device");
return -1;
}
L3_compress_initial();
return 1;
}
/*
* main:
* -----
*/
int main(int argc, char **argv)
{
int k = 200;
int j;
time_t end_time;
printf("Shine v1.08 19/06/03\n");
time(&config.start_time);
if (initial_all() != 1) {
error("initial_all fails");
return -1;
}
//printf("hello\n");
while(k >= 0) {
j = read(fd, (char*)buf, sizeof(buf));
//if(j < 1152) {
//error("wrong buf");
// return -1;
// }
//printf("......\n");
L3_compress();
// printf("------\n");
k--;
}
//wave_initial();
// check_config(); /* prints mpeg (output) configuration */
//printf("Encoding \"%s\" to \"%s\"\n", config.infile, config.outfile);
// wave_close();
time(&end_time);
end_time -= config.start_time;
printf(" Finished in %2ld:%2ld:%2ld\n",
end_time/3600,(end_time/60)%60,end_time%60);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -