int main(int argc, char **argv) { twolame_options *encopts = NULL; SNDFILE *inputfile = NULL; FILE *outputfile = NULL; short int *pcmaudio = NULL; int samples_read = 0; unsigned int frame_count = 0; unsigned char *mp2buffer = NULL; int mp2fill_size = 0; int audioReadSize = 0; // Allocate memory for the PCM audio data if ((pcmaudio = (short int *) calloc(AUDIOBUFSIZE, sizeof(short int))) == NULL) { fprintf(stderr, "Error: pcmaudio memory allocation failed\n"); exit(ERR_MEM_ALLOC); } // Allocate memory for the encoded MP2 audio data if ((mp2buffer = (unsigned char *) calloc(MP2BUFSIZE, sizeof(unsigned char))) == NULL) { fprintf(stderr, "Error: mp2buffer memory allocation failed\n"); exit(ERR_MEM_ALLOC); } // Initialise Encoder Options Structure encopts = twolame_init(); if (encopts == NULL) { fprintf(stderr, "Error: initializing libtwolame encoder failed.\n"); exit(ERR_MEM_ALLOC); } // Get options and parameters from the command line parse_args(argc, argv, encopts); // Open the input file inputfile = open_input_file( inputfilename ); twolame_set_num_channels( encopts, sfinfo.channels ); twolame_set_in_samplerate( encopts, sfinfo.samplerate ); // Open the output file outputfile = open_output_file( outputfilename ); // display file settings print_file_config( inputfile, twolame_get_verbosity(encopts) ); // initialise twolame with this set of options if (twolame_init_params( encopts ) != 0) { fprintf(stderr, "Error: configuring libtwolame encoder failed.\n"); exit(ERR_INVALID_PARAM); } // display encoder settings twolame_print_config( encopts ); // Only encode a single frame of mpeg audio ? if (single_frame_mode) audioReadSize = 1152; else audioReadSize = AUDIOBUFSIZE; // Now do the reading/encoding/writing while ((samples_read = sf_read_short( inputfile, pcmaudio, audioReadSize )) > 0) { int bytes_out = 0; // Force byte swapping if requested if (byteswap) { int i; for (i = 0; i<samples_read; i++) { short tmp = pcmaudio[i]; char *src = (char*)&tmp; char *dst = (char*)&pcmaudio[i]; dst[0] = src[1]; dst[1] = src[0]; } } // Calculate the number of samples we have (per channel) samples_read /= sfinfo.channels; // Do swapping of left and right channels if requested if (channelswap && sfinfo.channels == 2) { int i; for(i=0; i<samples_read; i++) { short tmp = pcmaudio[(2*i)]; pcmaudio[(2*i)] = pcmaudio[(2*i)+1]; pcmaudio[(2*i)+1] = tmp; } } // Encode the audio to MP2 mp2fill_size = twolame_encode_buffer_interleaved( encopts, pcmaudio, samples_read, mp2buffer, MP2BUFSIZE); // Stop if we don't have any bytes (probably don't have enough audio for a full frame of mpeg audio) if (mp2fill_size==0) break; if (mp2fill_size<0) { fprintf(stderr,"error while encoding audio: %d\n", mp2fill_size); exit(ERR_WRITING_OUTPUT); } // Write the encoded audio out bytes_out = fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outputfile); if (bytes_out<=0) { perror("error while writing to output file"); exit(ERR_WRITING_OUTPUT); } // Only single frame ? if (single_frame_mode) break; // Display Progress frame_count += (samples_read / 1152); if (twolame_get_verbosity(encopts)>0) { fprintf(stderr, "[%04i", frame_count); //fprintf(stderr, "/%04i", (int)(sfinfo.frames / sfinfo.channels / 1152)); fprintf(stderr, "]\r"); fflush(stderr); } } // // 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( encopts, mp2buffer, MP2BUFSIZE); if (mp2fill_size>0) { int bytes_out = fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outputfile); if (bytes_out<=0) { perror("error while writing to output file"); exit(ERR_WRITING_OUTPUT); } } if (twolame_get_verbosity(encopts)>1) { fprintf(stderr, "\nEncoding Finished.\n"); } // Close input and output files sf_close( inputfile ); fclose( outputfile ); // Close the libtwolame encoder twolame_close(&encopts); // Free up memory free(pcmaudio); free(mp2buffer); return (ERR_NO_ERROR); }
int main(int argc, char **argv) { twolame_options *encopts = NULL; audioin_t *inputfile = NULL; FILE *outputfile = NULL; short int *pcmaudio = NULL; unsigned int frame_count = 0; unsigned int total_frames = 0; unsigned int frame_len = 0; unsigned int total_bytes = 0; unsigned char *mp2buffer = NULL; int samples_read = 0; int mp2fill_size = 0; int audioReadSize = 0; // Allocate memory for the PCM audio data if ((pcmaudio = (short int *) calloc(AUDIO_BUF_SIZE, sizeof(short int))) == NULL) { fprintf(stderr, "Error: pcmaudio memory allocation failed\n"); exit(ERR_MEM_ALLOC); } // Allocate memory for the encoded MP2 audio data if ((mp2buffer = (unsigned char *) calloc(MP2_BUF_SIZE, sizeof(unsigned char))) == NULL) { fprintf(stderr, "Error: mp2buffer memory allocation failed\n"); exit(ERR_MEM_ALLOC); } // Initialise Encoder Options Structure encopts = twolame_init(); if (encopts == NULL) { fprintf(stderr, "Error: initializing libtwolame encoder failed.\n"); exit(ERR_MEM_ALLOC); } // Get options and parameters from the command line parse_args(argc, argv, encopts); // Display the filenames print_filenames(twolame_get_verbosity(encopts)); // Open the input file if (use_raw) { // use raw input handler inputfile = open_audioin_raw(inputfilename, &sfinfo, sample_size); } else { // use libsndfile inputfile = open_audioin_sndfile(inputfilename, &sfinfo); } // Display input information if (twolame_get_verbosity(encopts) > 1) { inputfile->print_info(inputfile); } // Use information from input file to configure libtwolame twolame_set_num_channels(encopts, sfinfo.channels); twolame_set_in_samplerate(encopts, sfinfo.samplerate); // Open the output file outputfile = open_output_file(outputfilename); // initialise twolame with this set of options if (twolame_init_params(encopts) != 0) { fprintf(stderr, "Error: configuring libtwolame encoder failed.\n"); exit(ERR_INVALID_PARAM); } // display encoder settings twolame_print_config(encopts); // Only encode a single frame of mpeg audio ? if (single_frame_mode) audioReadSize = TWOLAME_SAMPLES_PER_FRAME; else audioReadSize = AUDIO_BUF_SIZE; // Calculate the size and number of frames we are going to encode frame_len = twolame_get_framelength(encopts); if (sfinfo.frames) total_frames = sfinfo.frames / TWOLAME_SAMPLES_PER_FRAME; // Now do the reading/encoding/writing while ((samples_read = inputfile->read(inputfile, pcmaudio, audioReadSize)) > 0) { int bytes_out = 0; // Force byte swapping if requested if (byteswap) { int i; for (i = 0; i < samples_read; i++) { short tmp = pcmaudio[i]; char *src = (char *) &tmp; char *dst = (char *) &pcmaudio[i]; dst[0] = src[1]; dst[1] = src[0]; } } // Calculate the number of samples we have (per channel) samples_read /= sfinfo.channels; // Do swapping of left and right channels if requested if (channelswap && sfinfo.channels == 2) { int i; for (i = 0; i < samples_read; i++) { short tmp = pcmaudio[(2 * i)]; pcmaudio[(2 * i)] = pcmaudio[(2 * i) + 1]; pcmaudio[(2 * i) + 1] = tmp; } } // Encode the audio to MP2 mp2fill_size = twolame_encode_buffer_interleaved(encopts, pcmaudio, samples_read, mp2buffer, MP2_BUF_SIZE); // Stop if we don't have any bytes (probably don't have enough audio for a full frame of // mpeg audio) if (mp2fill_size == 0) break; if (mp2fill_size < 0) { fprintf(stderr, "error while encoding audio: %d\n", mp2fill_size); exit(ERR_ENCODING); } // Check that a whole number of frame was written // if (mp2fill_size % frame_len != 0) { // fprintf(stderr,"error while encoding audio: non-whole number of frames written\n"); // exit(ERR_ENCODING); // } // Write the encoded audio out bytes_out = fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outputfile); if (bytes_out != mp2fill_size) { perror("error while writing to output file"); exit(ERR_WRITING_OUTPUT); } total_bytes += bytes_out; // Only single frame ? if (single_frame_mode) break; // Display Progress frame_count += (mp2fill_size / frame_len); if (twolame_get_verbosity(encopts) > 0) { fprintf(stderr, "\rEncoding frame: %i", frame_count); if (total_frames) { fprintf(stderr, "/%i (%i%%)", total_frames, (frame_count * 100) / total_frames); } fflush(stderr); } } // Was there an error reading the audio? if (inputfile->error_str(inputfile)) { fprintf(stderr, "Error reading from input file: %s\n", inputfile->error_str(inputfile)); } // // 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(encopts, mp2buffer, MP2_BUF_SIZE); if (mp2fill_size > 0) { int bytes_out = fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outputfile); frame_count++; if (bytes_out <= 0) { perror("error while writing to output file"); exit(ERR_WRITING_OUTPUT); } total_bytes += bytes_out; } if (twolame_get_verbosity(encopts) > 1) { char *filesize = format_filesize_string(total_bytes); fprintf(stderr, "\nEncoding Finished.\n"); fprintf(stderr, "Total bytes written: %s.\n", filesize); free(filesize); } // Close input and output streams inputfile->close(inputfile); fclose(outputfile); // Close the libtwolame encoder twolame_close(&encopts); // Free up memory free(pcmaudio); free(mp2buffer); return (ERR_NO_ERROR); }