int main(int argc, char **argv) { wave_t wave; time_t start_time, end_time; int16_t buffer[2][samp_per_frame]; shine_config_t config; shine_t s; long written; unsigned char *data; time(&start_time); /* Set the default MPEG encoding paramters - basically init the struct */ set_defaults(&config); if (!parse_command(argc, argv, &config)) { print_usage(); exit(1); } quiet = quiet || !strcmp(outfname, "-"); if (!quiet) print_name(); /* Open the input file and fill the config shine_wave_t header */ if (!wave_open(infname, &wave, &config, quiet)) error("Could not open WAVE file"); infile = wave.file; /* See if samplerate is valid */ if (L3_find_samplerate_index(config.wave.samplerate) < 0) error("Unsupported samplerate"); /* See if bitrate is valid */ if (L3_find_bitrate_index(config.mpeg.bitr) < 0) error("Unsupported bitrate"); /* open the output file */ if (!strcmp(outfname, "-")) outfile = stdout; else outfile = fopen(outfname, "wb"); if (!outfile) { fprintf(stderr, "Could not create \"%s\".\n", outfname); exit(1); } /* Set to stereo mode if wave data is stereo, mono otherwise. */ if (config.wave.channels > 1) config.mpeg.mode = STEREO; else config.mpeg.mode = MONO; /* Print some info about the file about to be created (optional) */ if (!quiet) check_config(&config); /* Initiate encoder */ s = L3_initialise(&config); /* All the magic happens here */ while (wave_get(buffer, &wave, &config)) { data = L3_encode_frame(s, buffer, &written); write_mp3(written, data, &config); } /* Flush and write remaining data. */ data = L3_flush(s, &written); write_mp3(written, data, &config); /* Close encoder. */ L3_close(s); /* Close the wave file (using the wav reader) */ wave_close(&wave); /* Close the MP3 file */ fclose(outfile); time(&end_time); end_time -= start_time; if (!quiet) printf("Finished in %02ld:%02ld:%02ld (%01.1fx realtime)\n", end_time / 3600, (end_time / 60) % 60, end_time % 60, (float)wave.duration / (float)end_time); return 0; }
MP3Stream::MP3Stream(StreamsManager *pStreamsManager, string name) : BaseAudioStream(pStreamsManager, ST_IN_AUDIO_MP3, name) { BaseAudioDevice *pAudioCapDevice= reinterpret_cast<BaseAudioDevice*> (HardwareManager::GetHardwareInstance(HT_MIC)); _pMP3streamCapabilities= new StreamCapabilities(); //pcmbuf = new uint8_t (PCM_PAGE_SIZE); //mp3 config L3_set_config_mpeg_defaults(&_mp3config.mpeg); //wave config _sampleRate = pAudioCapDevice->GetSampleRate(); _numOfChannel = pAudioCapDevice->GetNumberOfChannels(); _bitsPerSample = pAudioCapDevice->GetBitsPerSample(); _bitRate = _mp3config.mpeg.bitr;// //set wave type, TODO: removed _mp3config.wave.type = WAVE_RIFF_PCM; _mp3config.wave.channels = _numOfChannel; //mono _mp3config.wave.samplerate = _sampleRate; _mp3config.wave.bits = _bitsPerSample; //config sample rate index, and bit rate _mp3config.mpeg.samplerate_index = L3_find_samplerate_index(_sampleRate, _mp3config.mpeg.type); if ( _mp3config.mpeg.samplerate_index < 0) FATAL ("invalid sample rate in mp3stream ctor"); //TODO: should throw an execption _mp3config.mpeg.bitrate_index = L3_find_bitrate_index(_mp3config.mpeg.bitr, _mp3config.mpeg.type); if (_mp3config.mpeg.bitrate_index < 0) FATAL ("invliad bit rate in mpstream ctor"); open_bit_stream_w(&_bs, BITSSIZE); memset ((char*) &_side_info, 0, sizeof(L3_side_info_t)); L3_subband_initialise(); L3_mdct_initialise(); L3_loop_initialise(); _mp3config.mpeg.mode_gr = (_mp3config.mpeg.type==1)? 2: 1; _samplesInput = _mp3config.mpeg.samples_per_frame = (_mp3config.mpeg.type==1)? 1152:576; //config->mpeg.total_frames = (); _mp3config.mpeg.bits_per_slot = 8; _sideinfo_len = 32; if (_mp3config.mpeg.type==1) { if (_numOfChannel==1) _sideinfo_len += 136; else _sideinfo_len += 256; } else { //mpeg2 if (_numOfChannel==1) _sideinfo_len += 72; else _sideinfo_len += 136; } if (_mp3config.mpeg.crc) _sideinfo_len += 16; _avg_slots_per_frame = ((double)_samplesInput / ((double)_sampleRate/1000)) * ((double)_bitRate / (double)_mp3config.mpeg.bits_per_slot); _whole_slots_per_frame = (int)_avg_slots_per_frame; _frac_slots_per_frame = _avg_slots_per_frame - (double)_whole_slots_per_frame; _slot_lag = -_frac_slots_per_frame; if (_frac_slots_per_frame==0) _mp3config.mpeg.padding = 0; DEBUG ("mp3 stream info: _sideinfo_len:%d sampleInput:%d bitRate:%d sampleRate:%d avg slots per frame:%d", _sideinfo_len, _samplesInput, _bitRate, _sampleRate, _avg_slots_per_frame); GETTIMESTAMP(ats); mp3Duration=(1000.0/(double)_sampleRate)*(double)(_samplesInput/_numOfChannel); //m second _pMP3streamCapabilities->aac.InitAACCapability(_sampleRate, _numOfChannel, _bitsPerSample, (samplesInput/_numOfChannel), (_bitRate* 1000 *_numOfChannel), mp3Duration); //bitrate: 2 channel _pMP3streamCapabilities->audioCodecId = CODEC_AUDIO_MP3; #ifdef MP3STREAM_DEBUG if((pfOutputMP3=fopen("MICOut.mp3", "wb")) == NULL){ FATAL("Open output file 'MICOut.mp3' fail... !!\n"); exit(1); } if((pfPCM=fopen("MICOut.wav", "wb")) == NULL){ FATAL("Open output file 'MICOut.mp3' fail... !!\n"); exit(1); } DEBUG("Initial MP3Stream End..!!\n"); #endif }