/* usage_short() Display the short usage information */ static void usage_short() { /* print a bit of info about the program */ fprintf(stderr, "TwoLAME version %s (%s)\n", get_twolame_version(), get_twolame_url()); fprintf(stderr, "MPEG Audio Layer II (MP2) encoder\n\n"); fprintf(stderr, "Usage: twolame [options] <infile> [outfile]\n\n"); fprintf(stderr, "Try \"twolame --help\" for more information.\n"); exit(ERR_NO_ENCODE); }
static struct encoder * twolame_encoder_init(const struct config_param *param, GError **error) { struct twolame_encoder *encoder; g_debug("libtwolame version %s", get_twolame_version()); encoder = g_new(struct twolame_encoder, 1); encoder_struct_init(&encoder->encoder, &twolame_encoder_plugin); /* load configuration from "param" */ if (!twolame_encoder_configure(encoder, param, error)) { /* configuration has failed, roll back and return error */ g_free(encoder); return NULL; } return &encoder->encoder; }
/* usage_long() Display the extended usage information */ static void usage_long() { fprintf(stdout, "TwoLAME version %s (%s)\n", get_twolame_version(), get_twolame_url()); fprintf(stdout, "MPEG Audio Layer II (MP2) encoder\n"); fprintf(stdout, "Usage: \n"); fprintf(stdout, "\ttwolame [options] <infile> [outfile]\n"); fprintf(stdout, "\n"); fprintf(stdout, "Both input and output filenames can be set to - to use stdin/stdout.\n"); fprintf(stdout, " <infile> input sound file (any format supported by libsndfile)\n"); fprintf(stdout, " <outfile> output bit stream of encoded audio\n"); fprintf(stdout, "\nInput Options\n"); fprintf(stdout, "\t-r, --raw-input input is raw 16-bit signed PCM audio\n"); fprintf(stdout, "\t-x, --byte-swap force byte-swapping of input\n"); fprintf(stdout, "\t-s, --samplerate srate sampling frequency of raw input (kHz)\n"); fprintf(stdout, "\t-N, --channels nch number of channels in raw input\n"); fprintf(stdout, "\t-g, --swap-channels swap channels of input file\n"); fprintf(stdout, "\t --scale value scale input (multiply PCM data)\n"); fprintf(stdout, "\t --scale-l value scale channel 0 (left) input\n"); fprintf(stdout, "\t --scale-r value scale channel 1 (right) input\n"); fprintf(stdout, "\nOutput Options\n"); fprintf(stdout, "\t-m, --mode mode (s)tereo, (j)oint, (m)ono or (a)uto\n"); fprintf(stdout, "\t-a, --downmix downmix from stereo to mono file for mono encoding\n"); fprintf(stdout, "\t-b, --bitrate br total bitrate in kbps (default 192 for 44.1kHz)\n"); fprintf(stdout, "\t-P, --psyc-mode psyc psychoacoustic model -1 to 3 (default 3)\n"); fprintf(stdout, "\t-v, --vbr enable VBR mode\n"); fprintf(stdout, "\t-V, --vbr-level lev enable VBR and set VBR level -50 to 50 (default 5)\n"); fprintf(stdout, "\t-B, --max-bitrate rate set the upper bitrate when in VBR mode\n"); fprintf(stdout, "\t-l, --ath lev ATH level (default 0)\n"); fprintf(stdout, "\t-q, --quick num only calculate psy model every num frames\n"); fprintf(stdout, "\t-S, --single-frame only encode a single frame of MPEG Audio\n"); fprintf(stdout, "\nMiscellaneous Options\n"); fprintf(stdout, "\t-c, --copyright mark as copyright\n"); fprintf(stdout, "\t-o, --non-original mark as non-original\n"); fprintf(stdout, "\t --original mark as original (default)\n"); fprintf(stdout, "\t-p, --protect enable CRC error protection\n"); fprintf(stdout, "\t-d, --padding force padding bit/frame on\n"); fprintf(stdout, "\t-R, --reserve-bits num set number of reserved bits in each frame\n"); fprintf(stdout, "\t-e, --deemphasis emp de-emphasis n/5/c (default: (n)one)\n"); fprintf(stdout, "\t-E, --energy turn on energy level extensions\n"); fprintf(stdout, "\nVerbosity Options\n"); fprintf(stdout, "\t-t, --talkativity num talkativity 0-10 (default is 2)\n"); fprintf(stdout, "\t --quiet same as --talkativity=0\n"); fprintf(stdout, "\t --brief same as --talkativity=1\n"); fprintf(stdout, "\t --verbose same as --talkativity=4\n"); fprintf(stdout, "\n"); fprintf(stdout, "\nAllowable bitrates for 32, 44.1 and 48kHz sample input (MPEG-1)\n"); fprintf(stdout, " 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384\n"); fprintf(stdout, "\nAllowable bitrates for 16, 22.05 and 24kHz sample input (MPEG-2)\n"); fprintf(stdout, " 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160\n"); fprintf(stdout, "\n"); exit(ERR_NO_ENCODE); }
int main(int argc, char **argv) { twolame_options *encodeOptions; char *inputfilename = argv[1]; char *outputfilename = argv[2]; FILE *outfile, *fpSrc = NULL; short int *pcmaudio; unsigned char *mp2buffer; int num_samples = 0; int mp2fill_size = 0; int frames = 0; wave_info_t *wave_info = NULL; int nReadSize = 0; if (argc != 3) usage(); /* Allocate some space for the PCM audio data */ if ((pcmaudio = (short *) calloc(AUDIOBUFSIZE, sizeof(short))) == NULL) { fprintf(stderr, "pcmaudio alloc failed\n"); exit(99); } /* Allocate some space for the encoded MP2 audio data */ if ((mp2buffer = (unsigned char *) calloc(MP2BUFSIZE, sizeof(unsigned char))) == NULL) { fprintf(stderr, "mp2buffer alloc failed\n"); exit(99); } /* grab a set of default encode options */ encodeOptions = twolame_init(); printf("Using libtwolame version %s.\n", get_twolame_version()); /* Open the wave file */ if ((wave_info = wave_init(inputfilename)) == NULL) { fprintf(stderr, "Not a recognised WAV file.\n"); exit(99); } // Use sound file to over-ride preferences for // mono/stereo and sampling-frequency twolame_set_num_channels(encodeOptions, wave_info->channels); if (wave_info->channels == 1) { twolame_set_mode(encodeOptions, TWOLAME_MONO); } else { twolame_set_mode(encodeOptions, TWOLAME_STEREO); } /* Set the input and output sample rate to the same */ twolame_set_in_samplerate(encodeOptions, wave_info->samplerate); twolame_set_out_samplerate(encodeOptions, wave_info->samplerate); /* Set the bitrate to 192 kbps */ twolame_set_bitrate(encodeOptions, 64); /* initialise twolame with this set of options */ if (twolame_init_params(encodeOptions) != 0) { fprintf(stderr, "Error: configuring libtwolame encoder failed.\n"); exit(99); } /* Open the output file for the encoded MP2 data */ if ((outfile = fopen(outputfilename, "wb")) == 0) { fprintf(stderr, "error opening output file %s\n", outputfilename); exit(99); } // Read num_samples of audio data *per channel* from the input file //while ((num_samples = wave_get_samples(wave_info, pcmaudio, AUDIOBUFSIZE/2)) != 0) { fpSrc = fopen( "a2002011001-e02.wav", "rb" ); while(1) { num_samples = fread( pcmaudio, sizeof( char ), AUDIOBUFSIZE/2, fpSrc ); // Encode the audio!2024*4 if( num_samples != AUDIOBUFSIZE/2 ) break; num_samples /= 2; mp2fill_size = twolame_encode_buffer_interleaved(encodeOptions, pcmaudio, num_samples/2, mp2buffer, MP2BUFSIZE); // Write the MPEG bitstream to the file fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outfile); // Display the number of MPEG audio frames we have encoded frames += (num_samples / TWOLAME_SAMPLES_PER_FRAME); printf("[%04i]\r", frames); fflush(stdout); } /* flush any remaining audio. (don't send any new audio data) There should only ever be a max of 1 frame on a flush. There may be zero frames if the audio data was an exact multiple of 1152 */ mp2fill_size = twolame_encode_flush(encodeOptions, mp2buffer, MP2BUFSIZE); fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outfile); twolame_close(&encodeOptions); free(pcmaudio); printf("\nFinished nicely.\n"); return (0); }
// Print the library version and // encoder parameter settings to STDERR void twolame_print_config(twolame_options *glopts) { FILE* fd = stderr; // Are we being silent ? if (glopts->verbosity<=0) return; // Are we being brief ? if (glopts->verbosity==1) { fprintf(fd, "LibTwoLame version %s (%s)\n", get_twolame_version(), get_twolame_url()); fprintf (fd, "Encoding as %dHz, ", twolame_get_out_samplerate(glopts)); fprintf (fd, "%d kbps, ", twolame_get_bitrate(glopts) ); if (twolame_get_VBR(glopts)) fprintf (fd, "VBR, " ); else fprintf (fd, "CBR, " ); fprintf (fd, "%s Layer II\n", twolame_get_version_name(glopts)); } else { fprintf (fd, "---------------------------------------------------------\n"); fprintf (fd, "LibTwoLame %s (%s)\n", get_twolame_version(), get_twolame_url()); fprintf (fd, "Input : %d Hz, %d channels\n", twolame_get_in_samplerate(glopts), twolame_get_num_channels(glopts)); fprintf (fd, "Output: %d Hz, %s\n", twolame_get_out_samplerate(glopts), twolame_get_mode_name(glopts)); fprintf (fd, "%d kbps ", twolame_get_bitrate(glopts) ); if (twolame_get_VBR(glopts)) fprintf (fd, "VBR " ); else fprintf (fd, "CBR " ); fprintf (fd, "%s Layer II ", twolame_get_version_name(glopts)); fprintf (fd, "psycho model=%d \n", twolame_get_psymodel(glopts)); fprintf (fd, "[De-emph:%s Copyright:%s Original:%s]\n", ((twolame_get_emphasis(glopts)) ? "On " : "Off"), ((twolame_get_copyright(glopts)) ? "Yes" : "No "), ((twolame_get_original(glopts)) ? "Yes" : "No ")); fprintf (fd, "[Padding:%s CRC:%s DAB:%s ]\n", ((twolame_get_padding(glopts)) ? "Normal" : "Off "), ((twolame_get_error_protection(glopts)) ? "On " : "Off"), ((twolame_get_DAB(glopts)) ? "On " : "Off")); if (glopts->verbosity>=3) { if (twolame_get_VBR(glopts)) { fprintf (fd, " - VBR Enabled. Using MNR boost of %f\n", twolame_get_VBR_level(glopts)); fprintf (fd, " - VBR bitrate index limits [%i -> %i]\n", glopts->lower_index, glopts->upper_index); } fprintf(fd," - ATH adjustment %f\n", twolame_get_ATH_level(glopts)); fprintf(fd," - Reserving %i Ancillary bits\n", twolame_get_num_ancillary_bits(glopts)); if (twolame_get_scale(glopts)!=1.0f) fprintf(fd," - Scaling audio by %f\n", twolame_get_scale(glopts)); if (twolame_get_scale_left(glopts)!=1.0f) fprintf(fd," - Scaling left channel by %f\n", twolame_get_scale_left(glopts)); if (twolame_get_scale_right(glopts)!=1.0f) fprintf(fd," - Scaling right channel by %f\n", twolame_get_scale_right(glopts)); //if (glopts->num_channels_in == 2 && glopts->num_channels_out == 1 ) { // fprintf(fd, " - Downmixing from stereo to mono.\n"); //} else if (glopts->num_channels_in == 1 && glopts->num_channels_out == 2 ) { // fprintf(fd, " - Upmixing from mono to stereo.\n"); //} } fprintf (fd, "---------------------------------------------------------\n"); } }