1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* TwoLAME: an optimized MPEG Audio Layer Two encoder
*
* Copyright (C) 2001-2004 Michael Cheng
* Copyright (C) 2004-2006 The TwoLAME Project
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <twolame.h>
#include "audio_wave.h"
#define MP2BUFSIZE (16384)
#define AUDIOBUFSIZE (9216)
static void usage(void)
{
printf("stwolame <input wavefile> <output mp2 file>\n");
exit(1);
}
int main(int argc, char **argv)
{
twolame_options *encodeOptions;
char *inputfilename = argv[1];
char *outputfilename = argv[2];
FILE *outfile;
short int *pcmaudio;
unsigned char *mp2buffer;
int num_samples = 0;
int mp2fill_size = 0;
int frames = 0;
wave_info_t *wave_info = NULL;
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, 192);
/* 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)) != 0) {
// Encode the audio!
mp2fill_size =
twolame_encode_buffer_interleaved(encodeOptions, pcmaudio, num_samples, 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);
}
/* vim:ts=4:sw=4:nowrap: */
|