📄 recorder_trmp3.cpp
字号:
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <assert.h>#include <math.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include "recorder_trmp3.h"#define u8 unsigned char#define i8 char#define u32 unsigned int#define i32 int#define i16 short int#define NCHANNELS 0x02#define SAMPLERATE 44100#define SAMPLESIZE 0x10#define WRITE_BUFFER_SIZE 32000 #define SBLIMIT 32#define HAN_SIZE 512#define MAX_NAME_SIZE 81//#define INPATH "/hd/temp.wav"#define INPATH "/hd/temp.wav"//#define OUTPATH "/hd/21.mp3"/* ID related to WAVE file format */#define WAV_ID_RIFF 0x52494646 /* "RIFF" */#define WAV_ID_WAVE 0x57415645 /* "WAVE" */#define WAV_ID_FMT 0x666d7420 /* "fmt " */#define WAV_ID_DATA 0x64617461 /* "data" */#define DFLT_LAY 2 /* default encoding layer is II */#define DFLT_MOD 's' /* default mode is stereo */#define DFLT_PSY 1 /* default psych model is 1 */#define DFLT_SFQ 44.1 /* default input sampling rate is 44.1 kHz */#define DFLT_EMP 'n' /* default de-emphasis is none */#define DFLT_EXT ".mp3" /* default output file extension *//* MPEG Header Definitions - Mode Values */#define MPG_MD_STEREO 0#define MPG_MD_JOINT_STEREO 1#define MPG_MD_DUAL_CHANNEL 2#define MPG_MD_MONO 3#ifndef FALSE#define FALSE 0#endif#ifndef TRUE#define TRUE 1#endif/* Global variable definitions for "musicin.c" */FILE *musicin;FILE *music_out;i8 *programName;enum byte_order { order_unknown, order_bigEndian, order_littleEndian };enum byte_order NativeByteOrder = order_unknown;i32 Read16BitsLowHigh(FILE *fp){ i32 first, second, result; first = 0xff & getc(fp); second = 0xff & getc(fp); result = (second << 8) + first;/*#ifndef THINK_C42 if (result & 0x8000) result = result - 0x10000;#endif*/ /* THINK_C */ return(result);}i32 Read16BitsHighLow(FILE *fp){ i32 first, second, result; first = 0xff & getc(fp); second = 0xff & getc(fp); result = (first << 8) + second;/*#ifndef THINK_C42 if (result & 0x8000) result = result - 0x10000;#endif*/ /* THINK_C */ return(result);}i32 Read32BitsLowHigh(FILE *fp){ i32 first, second, result; first = 0xffff & Read16BitsLowHigh(fp); second = 0xffff & Read16BitsLowHigh(fp); result = (second << 16) + first;/*#ifdef CRAY if (result & 0x80000000) result = result - 0x100000000;#endif*/ /* CRAY */ return(result);}i32 Read32BitsHighLow(FILE *fp){ i32 first, second, result; first = 0xffff & Read16BitsHighLow(fp); second = 0xffff & Read16BitsHighLow(fp); result = (first << 16) + second;/*#ifdef CRAY if (result & 0x80000000) result = result - 0x100000000;#endif*/ return result;}/*i32 wave_read_headers( FILE *file_ptr,IFF_AIFF *aiff_ptr){ i32 chunk_length = Read32BitsHighLow(file_ptr); i32 sub_type; i32 sub_size; i32 loop = 2; i32 format, block_align, avg_bytes_per_sec; printf("the chunk_length is %x\n",chunk_length);// chunk_length = Read32BitsHighLow(file_ptr); if (Read32BitsHighLow(file_ptr) != WAV_ID_WAVE){ printf ("the file type is not wave\n"); return -1; }// chunk_length -= 8; while ( loop ) { loop --; sub_type = Read32BitsHighLow(file_ptr); printf("the sub_type is %x\n",sub_type); // chunk_length -= 4; switch ( sub_type ){ case WAV_ID_FMT: sub_size = Read32BitsLowHigh(file_ptr); printf("the sub_size is %x\n",sub_size); if (sub_size < 16) { printf("WAV_ID_FMT chunk too short!\nERROR\nEXIT\n"); return -1; } // chunk_length -= sub_size; format = Read16BitsLowHigh( file_ptr ); printf("the format is %x\n",format); if(format != 1){ printf("WAVE parser doesn't support this format\n"); return -1; // exit(0); } sub_size -= 2; aiff_ptr->numChannels = Read16BitsLowHigh( file_ptr ); printf("the numchannels is %x\n",aiff_ptr->numChannels); sub_size -= 2; aiff_ptr->sampleRate = Read32BitsLowHigh( file_ptr ); sub_size -= 4; // printf("the samplerate is %x\n",aiff_ptr->sampleRate); avg_bytes_per_sec = Read32BitsLowHigh(file_ptr);// printf("the avg_bytes_per_sec is %x\n",avg_bytes_per_sec); sub_size -= 4; printf("avg_bytes_per_sec = %x\n", avg_bytes_per_sec); block_align = Read16BitsLowHigh(file_ptr); sub_size -= 2; printf("block_align = %x\n", block_align); aiff_ptr->sampleSize = Read16BitsLowHigh( file_ptr ); sub_size -= 2; printf("sampleSize = %x\n", aiff_ptr->sampleSize); while ( sub_size > 0 ){ getc( file_ptr ); sub_size -= 1; } break; case WAV_ID_DATA: sub_size = Read32BitsLowHigh( file_ptr ); printf("sub_size is %x\n",sub_size); // aiff_ptr->numSampleFrames = sub_size / (aiff_ptr->numChannels * ((aiff_ptr->sampleSize+7) / 8)); return 1; default: sub_size = Read32BitsLowHigh(file_ptr); while ( sub_size > 0 ){ getc( file_ptr ); sub_size -= 1; }//end for while return -1; } } printf("the function wave_read_headers will return 1\n"); return 1;}*///i32 parse_args(i32 argc,i8 **argv,layer *info,u32 *num_samples,i8 inPath[MAX_NAME_SIZE],i8 outPath[MAX_NAME_SIZE])static i32 Init_layer(layer *info,char *filename1,char *filename){ i32 file_type; i32 brate; IFF_AIFF pcm_aiff_data; printf("\nparsing input arguments...\n"); 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:info->mode = MPG_MD_STEREO; info->mode_ext = 0; break; } if((info->sampling_frequency = SmpFrqIndex((long)(1000*DFLT_SFQ), &info->version)) < 0) { printf("info->sampling_frequency: bad sfrq default %d\n",__LINE__); return -1;//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: info->emphasis = 0; break; } info->copyright = 0; info->original = 0; info->error_protection = FALSE; info->lay = 3;//this may be 1,2,or 3 brate = 128;/* info->emphasis = 0;//this may be 0,1 or 3 info->copyright = 1; info->original = 1; info->error_protection = TRUE; */ if ((musicin = fopen(filename1, "rb")) == NULL) { printf("Could not find \"%s\".\n", filename1); return -1; } //music_out = fopen(OUTPATH, "wb"); music_out = fopen(filename, "wb"); if(music_out == NULL){ printf("Could not create \"%s\".\n", filename); fclose(musicin); return -1; }/* file_type = Read32BitsHighLow(musicin); if(file_type != 0x52494646){//"RIFF" fclose(musicin); fclose(music_out); return -1; }*/ file_type = 0x52494646;// if ((wave_read_headers(musicin, &pcm_aiff_data)) != -1) printf(">>> Using Audio WAVE sound file headers\n"); pcm_aiff_data.sampleRate = (double)SAMPLERATE; info->sampling_frequency = SmpFrqIndex((long)pcm_aiff_data.sampleRate, &info->version); printf("will printf the sample rate\n"); printf(">>>pcm_aiff_data.sampleRate%fHz 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;// printf("the num_samples is %d\n",*num_samples); pcm_aiff_data.numChannels = NCHANNELS;//0x02 if (pcm_aiff_data.numChannels == 1) { info->mode = MPG_MD_MONO; info->mode_ext = 0; } info->bitrate_index = getBitRateIndex(info->lay, brate, info->version); if(info->bitrate_index < 0) printf("info->bitrate_index < 0\n");// if(err || inPath[0] == '\0') // usage(); /* never returns */ printf("the parse_args function will return the file_type 0x%x\n",file_type); return file_type;}u32 read_samples(FILE *musicin, i16 sample_buffer[2304], u32 frame_size, i32 file_type){ u32 samples_read; if ((samples_read = fread(sample_buffer, sizeof(i16), frame_size, musicin)) == 0) { printf("Hit end of audio data %d\n", samples_read); return 0; } if (samples_read < frame_size && samples_read > 0) { printf("Insufficient PCM input for one frame - fillout with zeros\n"); for (; samples_read < frame_size; sample_buffer[samples_read++] = 0); }// printf("the read_samples function will return samples_read :0x%x\n",samples_read); return(samples_read);}u32 get_audio( FILE *musicin, i16 buffer[2*1152], i32 stereo, layer *info, i32 file_type ){ i32 j;// i16 insamp[2304]; u32 samples_read; i32 lay; lay = info->lay; //printf("lay=%d, version=%d\n", lay, info->version); if ( (lay == 3) && (info->version == 0) ) { if ( stereo == 2 ) { /* samples_read = read_samples( musicin, insamp, 1152, file_type); //printf("1,the stereo is 2 and the frame_size is 1152\n"); for ( j = 0; j < 576; j++ ) { buffer[0][j] = insamp[2 * j]; buffer[1][j] = insamp[2 * j + 1]; }*/ samples_read = read_samples( musicin, buffer, 1152, file_type); } else { /* samples_read = read_samples( musicin, insamp, 576, file_type); // printf("2,the stereo isnot 2 and the frame_size is 576\n"); for ( j = 0; j < 576; j++ ) { buffer[0][j] = insamp[j]; buffer[1][j] = 0; }*/ samples_read = read_samples( musicin, buffer, 576, file_type); } } else { /*if (lay == 1){ if(stereo == 2){ // layer 1, stereo samples_read = read_samples(musicin, insamp, 768, file_type); // printf("3,the lay is 1 the stereo is 2 and the frame_size is 768\n"); for(j=0;j<448;j++) { if(j<64) { buffer[0][j] = buffer[0][j+384]; buffer[1][j] = buffer[1][j+384]; } else { buffer[0][j] = insamp[2*j-128]; buffer[1][j] = insamp[2*j-127]; } } } else { // layer 1, mono samples_read = read_samples(musicin, insamp, 384, file_type); // printf("4,the lay is 1 and the stereo isnot 2 and the frame size is 384\n"); for(j=0;j<448;j++){ if(j<64) { buffer[0][j] = buffer[0][j+384]; buffer[1][j] = 0; } else { buffer[0][j] = insamp[j-64]; buffer[1][j] = 0; } } } } else {*/ if(stereo == 2){ /* layer 2 (or 3), stereo */ /* samples_read = read_samples(musicin, insamp, 2304, file_type); // printf("5,the lay isnot 1 and the stereo is 2 the frame_size is 2304\n"); for(j=0;j<1152;j++) { buffer[0][j] = insamp[2*j]; buffer[1][j] = insamp[2*j+1]; }*/ samples_read = read_samples( musicin, buffer, 2304, file_type); } else { /* layer 2 (or 3), mono */ /* samples_read = read_samples(musicin, insamp, 1152, file_type); // printf("6,the lay isnot 1 and the stereo isnot 3 the frame_size is 1152\n"); for(j=0;j<1152;j++){ buffer[0][j] = insamp[j]; buffer[1][j] = 0; }*/ samples_read = read_samples( musicin, buffer, 1152, file_type); }// } } return(samples_read);}layer *info1;u8 *f_buf;MP3CODEC *mp3codec;//int main(void)int InitEncodetoMp3(char *filename1,char *filename){ // char original_file_name[MAX_NAME_SIZE] = {"/hd/1.wav"}; // char encoded_file_name[MAX_NAME_SIZE] = {"/hd/test2.mp3"}; unsigned int num_samples; int file_type; int stereo; printf(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n"); info1 = (layer *)malloc(sizeof(layer)); if(info1 == NULL){ printf("malloc memory for info1 error\n"); return -1; } mp3codec = (MP3CODEC *)malloc(sizeof(MP3CODEC)); if(mp3codec == NULL){ printf("malloc memory for mp3codec error\n"); free(info1); return -1; } memset(info1, 0, sizeof(layer)); memset(mp3codec, 0, sizeof(MP3CODEC)); f_buf = (u8 *)malloc(WRITE_BUFFER_SIZE); if(f_buf == NULL){ printf("malloc memory for f_buf error\n"); free(info1); free(mp3codec); return -1; } printf("enter the parse_args\n"); file_type = Init_layer(info1,filename1,filename);//,&num_samples);//,original_file_name,encoded_file_name); return file_type;}int EncodetoMp3(int file_type){ int stereo; int result; int size; result = init_MP3codec(mp3codec, info1);//init_MP3code() have not init if(!result) { printf("Error! init_MP3codec failed\n"); free(info1); free(mp3codec); free(f_buf); return -1; } int i = 0,j = 0,k = 0,l = 0; stereo = mp3codec->fr_ps.stereo; printf("ente to loop get_audio\n"); while ( get_audio(musicin, mp3codec->buffer, stereo, info1, file_type) > 0 ) { //printf("to encoding frame %d\n", frameNum); size = MP3_encoding(f_buf, mp3codec); if(size){ // printf("the size is %d\n",size); fwrite(f_buf, 1, size, music_out); } //frameNum++; } size = deinit_MP3codec(mp3codec, f_buf); fwrite(f_buf, size, 1, music_out); return 0;}int deinitEncodetoMp3(){ if(fclose(music_out) != 0){ printf("Could not close the mp3 file.\n"); return -2; } if (fclose(musicin) != 0){ printf("Could not close the wav file.\n"); return -2; } free(info1); free(mp3codec); free(f_buf); printf("Encoding of wav file is finished\n");// remove(INPATH); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -