📄 main.cpp
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "i2s.h"#include "spii2s_dma.h"#include "dac.h"#include "mpegdec/mpegdec.h"#include "mpegdec/em85xx.h"#include "./libmp3enc/libmp3enc.h"#include "audio_in.h"#define DFLT_LAY 3 /* 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 32//44.1 /* default input sampling rate is 44.1 kHz */#define DFLT_EMP 'n' /* default de-emphasis is none */#define DFLT_EXT ".mpg" /* default output file extension */#define FILETYPE_ENCODE 'TEXT'#define CREATOR_ENCODE 'MpgD'#define DELT_BRATE 128#define WRITE_BUFFER_SIZE 1024*32//32000 // must be enough to store one frame compress data#define RECEIVE_REMAIN_SIZE 1024*256 // bit_writer will never check fullness for this buffer#define READ_16BIT_DATA 2/* The following functions are in the file "musicin.c" */void parse_args();void print_config();void usage();void aiff_check();/* Implementations */i32 init_MP3codec(MP3CODEC *mp3codec, layer *input);i32 MP3_encoding(u8 *f_buf, MP3CODEC *mp3codec);i32 deinit_MP3codec(MP3CODEC *mp3codec, u8 *buf);//************************************************************************#if 1static void debug_break (void){}#define ASSERT(exp) ((void)((exp)?1:(printf ("***ASSERT failed: line %d, file %s\n", __LINE__,__FILE__), debug_break(), 0)))#define DEBUGMSG(cond,printf_exp) ((void)((cond)?(printf printf_exp),1:0))#else#define ASSERT(exp)#define DEBUGMSG(cond,printf_exp)#endif#define CAPTURE_BUFFER_LENGTH (1024*256)static RMuint32 capturebuffer[CAPTURE_BUFFER_LENGTH/4];static MpegDecoder *pMpegDecoder = 0;u8 *f_buf;layer *info;FILE *music_out;static MP3CODEC *mp3codec;i32 frameNum=0;i32 stereo, size; i32 result;int first =1, LastTime = 0, lastlength =0,lastlast =0, RecAgainLength =0,RecAgainRemain =0, Last_Remain_Flag =0;int Last_Remain_Size = 0 ,Current_Remain_Size = 0;int nleav =0;static RMuint8 *Reptr; u32 num_samples;void parse_args( i32 argc, i8 **argv, layer *info, u32 *num_samples){ i32 file_type; i32 srate; i32 brate; i32 err = 0, i = 0; IFF_AIFF pcm_aiff_data; long samplerate; long soundPosition; printf("\nparsing input arguments...\n"); /* 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(); } if((info->sampling_frequency = SmpFrqIndex((long)(1000*DFLT_SFQ), &info->version)) < 0) { fprintf(stderr, " bad sfrq default %.2f\n", 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; brate = DELT_BRATE;//atoi(arg); //break; 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", *num_samples = MAX_U_32_NUM; info->bitrate_index = getBitRateIndex(info->lay, brate, info->version);}void Mp3Encode(RMuint8 *pcmBuffer,int nSize){ int LoopCouter, size, CurrentLength; int frame_size = 2304; CurrentLength =nSize; RMuint8 *ptemp = (RMuint8 *) pcmBuffer; RMuint8 *HandleBuffer =(RMuint8 *) pcmBuffer; printf("21 to encoding frame %d\n", nSize); Last_Remain_Size = Current_Remain_Size; if(lastlast != 0){ nSize = nSize + lastlast + lastlength; LoopCouter =nSize / (frame_size * READ_16BIT_DATA); Current_Remain_Size = nSize % (frame_size * READ_16BIT_DATA); printf("212 1 to encoding frame %d\n", nSize); } else{ nSize = nSize + Last_Remain_Size; printf("212 to encoding frame %d\n", nSize); Current_Remain_Size = nSize % (frame_size * READ_16BIT_DATA); LoopCouter =nSize / (frame_size * READ_16BIT_DATA); } if(Current_Remain_Size == 0){ //Reptr =NULL; printf("4 here \n"); } if(LastTime == 1){ if(LoopCouter !=0){ if(lastlast != 0){ printf("3 n=0 here everylength %d , %d\n", lastlength, lastlast); memcpy((Reptr + lastlast+ lastlength),ptemp,LoopCouter * frame_size* READ_16BIT_DATA -lastlast -lastlength); Reptr[LoopCouter * frame_size * READ_16BIT_DATA +1] ='\0'; HandleBuffer = Reptr; Last_Remain_Flag =1; lastlast =0; lastlength = 0; } else{ printf("31 here %d ,%x\n", Last_Remain_Size, Reptr); memcpy((Reptr + Last_Remain_Size),ptemp,LoopCouter * frame_size * READ_16BIT_DATA - Last_Remain_Size ); Reptr[LoopCouter * frame_size * READ_16BIT_DATA +1] ='\0'; HandleBuffer = Reptr; printf("312 here \n"); } } if(LoopCouter == 0) { printf("3 here everylength %d, %d \n", CurrentLength, Last_Remain_Size); memcpy((Reptr + Last_Remain_Size),ptemp,CurrentLength); Current_Remain_Size = 0; lastlast = Last_Remain_Size; lastlength = CurrentLength; } } if(Current_Remain_Size != 0 && LoopCouter != 0 && Last_Remain_Flag ==0){ memcpy(Reptr, ptemp + (LoopCouter * frame_size * READ_16BIT_DATA - Last_Remain_Size), Current_Remain_Size); printf("31 2222222 here %x\n", Reptr); LastTime = 1; } if(Last_Remain_Flag !=0){ memcpy(Reptr, ptemp + (LoopCouter * frame_size * READ_16BIT_DATA - lastlast -lastlength), Current_Remain_Size); Last_Remain_Flag =0; printf("in flag\\\\\\\\\\\\\\\\\\n"); } printf("ENDCODE START:length = %d,nleav = %d!!!\n",LoopCouter,Current_Remain_Size); // Start MP3 encoding while ( LoopCouter) { i32 size; get_audio((i16 *)HandleBuffer, mp3codec->buffer, num_samples, stereo, info, nSize); HandleBuffer =HandleBuffer + frame_size * READ_16BIT_DATA; LoopCouter--; size = MP3_encoding(f_buf, mp3codec); if(size) fwrite(f_buf, 1, size, music_out); frameNum++; }}int main (int argc, char *argv[]){ char c, dummy[64]; int addr, data; printf("MPEG-1 Audio Layer III Encoder, Ver 0.01\n");// make sure all entries in structure layer(info) are valid !!! info = (layer *)malloc(sizeof(layer)); memset(info, 0, sizeof(layer)); mp3codec = (MP3CODEC *)malloc(sizeof(MP3CODEC)); memset(mp3codec, 0, sizeof(MP3CODEC)); f_buf = (u8 *)malloc(WRITE_BUFFER_SIZE); memset(f_buf,0,WRITE_BUFFER_SIZE); Reptr = (RMuint8 *)malloc(RECEIVE_REMAIN_SIZE); memset(Reptr,0,RECEIVE_REMAIN_SIZE); parse_args(argc, argv, info, &num_samples); result = init_MP3codec(mp3codec, info); if(!result) { printf("Error! init_MP3codec failed\n"); exit(1); } stereo = mp3codec->fr_ps.stereo; printf ("i2s input test\n"); printf ("==============\n"); // create an instance of the mpeg decoder pMpegDecoder = (MpegDecoder *) new MpegDecoder; ASSERT (pMpegDecoder); pMpegDecoder->Init (); decoderproperty Dp; RMuint32 bitspersample = 16; Dp.PropSet = DECODER_SET; Dp.PropId = edecAudioDacBitsPerSample; Dp.PropTypeLength = sizeof (RMuint32); Dp.pValue = &bitspersample; ioctl (pMpegDecoder->m_handle, REALMAGICHWL_IOCTL_DECODER_SET_PROPERTY, &Dp); PCM_PARAMETERS pcmparams; pcmparams.NumberOfBitsPerSample = 16; pcmparams.NumberOfChannels = 2; pcmparams.SamplesPerSecond = 32000; pMpegDecoder->SetPCMParameters (&pcmparams); pMpegDecoder->Play (); // initialize the wolfson WM8731 dac dac_write (0x00, 0x17); // L line-in volume dac_write (0x02, 0x17); // R line-in volume dac_write (0x08, 0x00); // select line-in as input dac_write (0x0c, 0x0a); // power on dac_write (0x0e, 0x01); // non-i2s mode - left justified dac_write (0x12, 0x01); // active // allow user to change any dac parameters while (1) { printf ("> "); gets (dummy); if (dummy[0] == 'q') break; switch (dummy[0]) { case 'w': sscanf (dummy, "%c %x %x", &c, &addr, &data); printf ("write 0x%x to 0x%x\n", data, addr); dac_write (addr, data); break; case 'r': sscanf (dummy, "%c %x", &c, &addr); data = dac_read (addr); printf ("read 0x%x from 0x%x\n", data, addr); break; default: printf ("unknown command\n"); break; } } FILE *f = fopen ("/hd1/out.pcm", "wb"); music_out = fopen("/hd1/out.mp3", "wb"); //////////////////////// // program the i2s block *I2S_CTRL = 0x02; printf ("*I2S_CTRL = 0x02\n"); *I2S_PROG_LEN = 0x80 | 16; printf ("*I2S_PROG_LEN = 0x80 | 16\n"); // program the i2s dma block *SPII2S_DMA_CNTRL = 0x2; printf ("*SPII2S_DMA_CNTRL = 0x2\n"); *SPII2S_DMA_BASE_ADD = (int)capturebuffer; printf ("*SPII2S_DMA_BASE_ADD = (int)capturebuffer\n"); *SPII2S_DMA_SIZE = CAPTURE_BUFFER_LENGTH; printf ("*SPII2S_DMA_SIZE = 1024*1024\n"); *SPII2S_DMA_RD_PTR = 0; printf ("*SPII2S_DMA_RD_PTR = 0\n"); RMuint8 *p = (RMuint8 *)capturebuffer; int t = 0, n, wr, rd, x1, x2, s; *SPII2S_DMA_CNTRL = 0x1 | 0x8; printf ("*SPII2S_DMA_CNTRL = 0x1 | 0x8\n"); // after programming the dma engine, you can begin the transfer ... // 0x01 = enable i2s receiving // 0x80 = non i2s audiomode *I2S_CTRL = 0x01 | 0x80; printf ("*I2S_CTRL = 0x01 | 0x80\n"); // captured data is in INTEL format (LSB,MSB) while (1) { s = *SPII2S_DMA_INT_POLL; if (s & 1) { printf ("overflow error.\n"); fflush (stdout); break; } if (s & 4) { printf ("buffer half full ...\n"); fflush (stdout); } else continue; wr = *SPII2S_DMA_WR_PTR; rd = *SPII2S_DMA_RD_PTR; // save data n = wr - rd; n = n / 4 * 4; ASSERT (n); if (n < 0) { n += CAPTURE_BUFFER_LENGTH; x1 = CAPTURE_BUFFER_LENGTH - rd; x2 = wr; ASSERT (n == (x1+x2)); Mp3Encode(p+rd, x1); fwrite (p + rd, 1, x1, f); Mp3Encode(p+rd+x1, x2); //fwrite (p + rd + x1, 1, x2, f); fwrite (p, 1, x2, f); } else { Mp3Encode(p+rd, n); fwrite (p + rd, 1, n, f); } rd += n; if (rd >= CAPTURE_BUFFER_LENGTH) { rd -= CAPTURE_BUFFER_LENGTH; fflush (stdout); } *SPII2S_DMA_RD_PTR = rd; t += n; if (t > 1024 * 1024 * 4) break; } { i32 size; printf("Could not close\n"); size = deinit_MP3codec(mp3codec, f_buf); fwrite(f_buf, size, 1, music_out); } fclose (f); fclose(music_out); bitspersample = 24; Dp.PropSet = DECODER_SET; Dp.PropId = edecAudioDacBitsPerSample; Dp.PropTypeLength = sizeof (RMuint32); Dp.pValue = &bitspersample; ioctl (pMpegDecoder->m_handle, REALMAGICHWL_IOCTL_DECODER_SET_PROPERTY, &Dp); free(info); free(mp3codec); free(Reptr); delete pMpegDecoder; // reset i2s block *I2S_CTRL = 0x02; // reset i2s dma block *SPII2S_DMA_CNTRL = 0x2; printf ("\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -