1 /* 2 * TwoLAME: an optimized MPEG Audio Layer Two encoder 3 * 4 * Copyright (C) 2001-2004 Michael Cheng 5 * Copyright (C) 2004-2018 The TwoLAME Project 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * 21 */ 22 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <twolame.h> 27 28 #include "audio_wave.h" 29 30 31 #define MP2BUFSIZE (16384) 32 #define AUDIOBUFSIZE (9216) 33 34 35 36 static void usage(void) 37 { 38 printf("stwolame <input wavefile> <output mp2 file>\n"); 39 exit(1); 40 } 41 42 43 int main(int argc, char **argv) 44 { 45 twolame_options *encodeOptions; 46 char *inputfilename = argv[1]; 47 char *outputfilename = argv[2]; 48 FILE *outfile; 49 short int *pcmaudio; 50 unsigned char *mp2buffer; 51 int num_samples = 0; 52 int mp2fill_size = 0; 53 int frames = 0; 54 wave_info_t *wave_info = NULL; 55 56 if (argc != 3) 57 usage(); 58 59 60 /* Allocate some space for the PCM audio data */ 61 if ((pcmaudio = (short *) calloc(AUDIOBUFSIZE, sizeof(short))) == NULL) { 62 fprintf(stderr, "pcmaudio alloc failed\n"); 63 exit(99); 64 } 65 66 /* Allocate some space for the encoded MP2 audio data */ 67 if ((mp2buffer = (unsigned char *) calloc(MP2BUFSIZE, sizeof(unsigned char))) == NULL) { 68 fprintf(stderr, "mp2buffer alloc failed\n"); 69 exit(99); 70 } 71 72 73 74 75 /* grab a set of default encode options */ 76 encodeOptions = twolame_init(); 77 printf("Using libtwolame version %s.\n", get_twolame_version()); 78 79 80 /* Open the wave file */ 81 if ((wave_info = wave_init(inputfilename)) == NULL) { 82 fprintf(stderr, "Not a recognised WAV file.\n"); 83 exit(99); 84 } 85 // Use sound file to over-ride preferences for 86 // mono/stereo and sampling-frequency 87 twolame_set_num_channels(encodeOptions, wave_info->channels); 88 if (wave_info->channels == 1) { 89 twolame_set_mode(encodeOptions, TWOLAME_MONO); 90 } else { 91 twolame_set_mode(encodeOptions, TWOLAME_STEREO); 92 } 93 94 95 /* Set the input and output sample rate to the same */ 96 twolame_set_in_samplerate(encodeOptions, wave_info->samplerate); 97 twolame_set_out_samplerate(encodeOptions, wave_info->samplerate); 98 99 /* Set the bitrate to 192 kbps */ 100 twolame_set_bitrate(encodeOptions, 192); 101 102 /* initialise twolame with this set of options */ 103 if (twolame_init_params(encodeOptions) != 0) { 104 fprintf(stderr, "Error: configuring libtwolame encoder failed.\n"); 105 exit(99); 106 } 107 108 /* Open the output file for the encoded MP2 data */ 109 if ((outfile = fopen(outputfilename, "wb")) == 0) { 110 fprintf(stderr, "error opening output file %s\n", outputfilename); 111 exit(99); 112 } 113 // Read num_samples of audio data *per channel* from the input file 114 while ((num_samples = wave_get_samples(wave_info, pcmaudio, AUDIOBUFSIZE)) != 0) { 115 116 // Encode the audio! 117 mp2fill_size = 118 twolame_encode_buffer_interleaved(encodeOptions, pcmaudio, num_samples, mp2buffer, 119 MP2BUFSIZE); 120 121 // Write the MPEG bitstream to the file 122 fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outfile); 123 124 // Display the number of MPEG audio frames we have encoded 125 frames += (num_samples / TWOLAME_SAMPLES_PER_FRAME); 126 printf("[%04i]\r", frames); 127 fflush(stdout); 128 } 129 130 /* flush any remaining audio. (don't send any new audio data) There should only ever be a max 131 of 1 frame on a flush. There may be zero frames if the audio data was an exact multiple of 132 1152 */ 133 mp2fill_size = twolame_encode_flush(encodeOptions, mp2buffer, MP2BUFSIZE); 134 fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outfile); 135 136 137 twolame_close(&encodeOptions); 138 free(pcmaudio); 139 140 printf("\nFinished nicely.\n"); 141 142 143 return (0); 144 } 145 146 /* vim:ts=4:sw=4:nowrap: */ 147