2 * mp3rtp command line frontend program
4 * initially contributed by Felix von Leitner
6 * Copyright (c) 2000 Mark Taylor
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 /* $Id: mp3rtp.c,v 1.36 2011/10/02 17:13:22 robert Exp $ */
27 /* Still under work ..., need a client for test, where can I get one? */
29 /* An audio player named Zinf (aka freeamp) can play rtp streams */
32 * experimental translation:
34 * gcc -I..\include -I..\libmp3lame -o mp3rtp mp3rtp.c ../libmp3lame/libmp3lame.a lametime.c get_audio.c ieeefloat.c timestatus.c parse.c rtp.c -lm
36 * wavrec -t 14400 -s 44100 -S /proc/self/fd/1 | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3
62 #include "timestatus.h"
63 #include "get_audio.h"
72 * Encode (via LAME) to mp3 with RTP streaming of the output.
74 * Author: Felix von Leitner <leitner@vim.org>
76 * mp3rtp ip[:port[:ttl]] [lame encoding options] infile outfile
79 * arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42:5004:2 -b 56 - /dev/null
80 * arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3
86 maxvalue(int Buffer[2][1152])
91 for (i = 0; i < 1152; i++) {
92 if (abs(Buffer[0][i]) > max)
93 max = abs(Buffer[0][i]);
94 if (abs(Buffer[1][i]) > max)
95 max = abs(Buffer[1][i]);
101 levelmessage(unsigned int maxv, int* maxx, int* tmpx)
103 char buff[] = "| . | . | . | . | . | . | . | . | . | . | \r";
104 int tmp = *tmpx, max = *maxx;
107 tmp = (maxv * 61 + 16384) / (32767 + 16384 / 61);
108 if (tmp > sizeof(buff) - 2)
109 tmp = sizeof(buff) - 2;
114 console_printf(buff);
121 /************************************************************************
125 * PURPOSE: MPEG-1,2 Layer III encoder with GPSYCHO
126 * psychoacoustic model.
128 ************************************************************************/
131 lame_main(lame_t gf, int argc, char **argv)
133 unsigned char mp3buffer[LAME_MAXMP3BUFFER];
134 char inPath[PATH_MAX + 1];
135 char outPath[PATH_MAX + 1];
138 int maxx = 0, tmpx = 0;
145 unsigned int port = 5004;
146 unsigned int ttl = 2;
150 console_printf("Encode (via LAME) to mp3 with RTP streaming of the output\n"
152 " mp3rtp ip[:port[:ttl]] [lame encoding options] infile outfile\n"
155 " arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42:5004:2 -b 56 - /dev/null\n"
156 " arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3\n"
161 switch (sscanf(argv[1], "%11[.0-9]:%u:%u%c", ip, &port, &ttl, &dummy)) {
167 error_printf("Illegal destination selector '%s', must be ip[:port[:ttl]]\n", argv[1]);
170 rtp_initialization();
171 if (rtp_socket(ip, port, ttl)) {
172 rtp_deinitialization();
173 error_printf("fatal error during initialization\n");
177 lame_set_errorf(gf, &frontend_errorf);
178 lame_set_debugf(gf, &frontend_debugf);
179 lame_set_msgf(gf, &frontend_msgf);
181 /* Remove the argumets that are rtp related, and then
182 * parse the command line arguments, setting various flags in the
183 * struct pointed to by 'gf'. If you want to parse your own arguments,
184 * or call libmp3lame from a program which uses a GUI to set arguments,
185 * skip this call and set the values of interest in the gf struct.
186 * (see lame.h for documentation about these parameters)
190 parse_args(gf, argc - 1, argv + 1, inPath, outPath, NULL, NULL);
192 /* open the output file. Filename parsed into gf.inPath */
193 if (0 == strcmp(outPath, "-")) {
194 lame_set_stream_binary_mode(outf = stdout);
197 if ((outf = lame_fopen(outPath, "wb+")) == NULL) {
198 rtp_deinitialization();
199 error_printf("Could not create \"%s\".\n", outPath);
205 /* open the wav/aiff/raw pcm or mp3 input file. This call will
206 * open the file with name gf.inFile, try to parse the headers and
207 * set gf.samplerate, gf.num_channels, gf.num_samples.
208 * if you want to do your own file input, skip this call and set
209 * these values yourself.
211 if (init_infile(gf, inPath) < 0) {
212 error_printf("Can't init infile '%s'\n", inPath);
217 /* Now that all the options are set, lame needs to analyze them and
218 * set some more options
220 ret = lame_init_params(gf);
223 display_bitrates(stderr);
224 rtp_deinitialization();
225 error_printf("fatal error during initialization\n");
229 lame_print_config(gf); /* print useful information about options being used */
231 if (global_ui_config.update_interval < 0.)
232 global_ui_config.update_interval = 2.;
234 /* encode until we hit EOF */
235 while ((wavsamples = get_audio(gf, Buffer)) > 0) { /* read in 'wavsamples' samples */
236 levelmessage(maxvalue(Buffer), &maxx, &tmpx);
237 mp3bytes = lame_encode_buffer_int(gf, /* encode the frame */
238 Buffer[0], Buffer[1], wavsamples,
239 mp3buffer, sizeof(mp3buffer));
240 rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */
241 fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */
244 mp3bytes = lame_encode_flush(gf, /* may return one or more mp3 frame */
245 mp3buffer, sizeof(mp3buffer));
246 rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */
247 fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */
249 lame_mp3_tags_fid(gf, outf); /* add VBR tags to mp3 file */
251 rtp_deinitialization();
253 close_infile(); /* close the sound input file */
257 /* end of mp3rtp.c */