Lines 33-39
Link Here
|
33 |
#include <sys/stat.h> |
33 |
#include <sys/stat.h> |
34 |
#include <fcntl.h> |
34 |
#include <fcntl.h> |
35 |
|
35 |
|
36 |
#include <lame/lame.h> |
36 |
#include <dlfcn.h> |
37 |
|
37 |
|
38 |
#include "util.h" |
38 |
#include "util.h" |
39 |
#include "Toc.h" |
39 |
#include "Toc.h" |
Lines 47-52
Link Here
|
47 |
static int CREATE_ALBUM_DIRECTORY = 0; |
47 |
static int CREATE_ALBUM_DIRECTORY = 0; |
48 |
static std::string TARGET_DIRECTORY; |
48 |
static std::string TARGET_DIRECTORY; |
49 |
|
49 |
|
|
|
50 |
#define STEREO 0 |
51 |
#define LAME_MAXMP3BUFFER 16384 |
52 |
|
53 |
typedef void *lame_global_flags; |
54 |
static lame_global_flags *(*dl_lame_init)(void); |
55 |
typedef void lame_msgfun(const char *fmt, va_list args); |
56 |
static void (*dl_lame_set_msgf)(lame_global_flags *lf, lame_msgfun *f); |
57 |
static void (*dl_lame_set_debugf)(lame_global_flags *lf, lame_msgfun *f); |
58 |
static void (*dl_lame_set_errorf)(lame_global_flags *lf, lame_msgfun *f); |
59 |
static void (*dl_lame_set_in_samplerate)(lame_global_flags *lf, int rate); |
60 |
static void (*dl_lame_set_num_channels)(lame_global_flags *lf, int chans); |
61 |
static void (*dl_lame_set_quality)(lame_global_flags *lf, int q); |
62 |
static void (*dl_lame_set_mode)(lame_global_flags *lf, int mode); |
63 |
static void (*dl_lame_set_brate)(lame_global_flags *lf, int bitrate); |
64 |
static int (*dl_lame_encode_buffer)(lame_global_flags *lf, short int *leftSamples, short int *rightSamples, int samples, unsigned char *mp3buffer, int bufsize); |
65 |
static int (*dl_lame_encode_flush_nogap)(lame_global_flags *lf, unsigned char *mp3buffer, int bufsize); |
66 |
static void (*dl_lame_mp3_tags_fid)(lame_global_flags *lf, FILE *fp); |
67 |
static int (*dl_lame_init_params)(lame_global_flags *lf); |
68 |
static void (*dl_lame_print_config)(lame_global_flags *lf); |
69 |
static void (*dl_lame_print_internals)(lame_global_flags *lf); |
70 |
static int (*dl_lame_init_bitstream)(lame_global_flags *lf); |
71 |
static void (*dl_lame_close)(lame_global_flags *lf); |
72 |
static char *(*dl_get_lame_version)(void); |
73 |
static int (*dl_bitrate_table)[3][16]; |
74 |
static void (*dl_id3tag_init)(lame_global_flags *lf); |
75 |
static void (*dl_id3tag_add_v2)(lame_global_flags *lf); |
76 |
static void (*dl_id3tag_set_title)(lame_global_flags *lf, const char *title); |
77 |
static void (*dl_id3tag_set_artist)(lame_global_flags *lf, const char *artist); |
78 |
static void (*dl_id3tag_set_album)(lame_global_flags *lf, const char *album); |
79 |
static void (*dl_id3tag_set_track)(lame_global_flags *lf, const char *track); |
80 |
|
81 |
static int load_lame(void) { |
82 |
static int loaded=0; |
83 |
void *library; |
84 |
|
85 |
if (loaded) return 0; |
86 |
|
87 |
library = dlopen("libmp3lame.so", RTLD_NOW); |
88 |
|
89 |
if (!(dl_lame_init = (lame_global_flags *(*)(void))dlsym(library, "lame_init"))) |
90 |
return -1; |
91 |
if (!(dl_lame_set_msgf = (void (*)(lame_global_flags *, lame_msgfun *))dlsym(library, "lame_set_msgf"))) |
92 |
return -1; |
93 |
if (!(dl_lame_set_debugf = (void (*)(lame_global_flags *, lame_msgfun *))dlsym(library, "lame_set_debugf"))) |
94 |
return -1; |
95 |
if (!(dl_lame_set_errorf = (void (*)(lame_global_flags *, lame_msgfun *))dlsym(library, "lame_set_errorf"))) |
96 |
return -1; |
97 |
if (!(dl_lame_set_in_samplerate = (void (*)(lame_global_flags *, int))dlsym(library, "lame_set_in_samplerate"))) |
98 |
return -1; |
99 |
if (!(dl_lame_set_num_channels = (void (*)(lame_global_flags *, int))dlsym(library, "lame_set_num_channels"))) |
100 |
return -1; |
101 |
if (!(dl_lame_set_quality = (void (*)(lame_global_flags *, int))dlsym(library, "lame_set_quality"))) |
102 |
return -1; |
103 |
if (!(dl_lame_set_mode = (void (*)(lame_global_flags *, int))dlsym(library, "lame_set_mode"))) |
104 |
return -1; |
105 |
if (!(dl_lame_set_brate = (void (*)(lame_global_flags *, int))dlsym(library, "lame_set_brate"))) |
106 |
return -1; |
107 |
if (!(dl_lame_encode_buffer = (int (*)(lame_global_flags *, short int *, short int *, int, unsigned char *, int))dlsym(library, "lame_encode_buffer"))) |
108 |
return -1; |
109 |
if (!(dl_lame_encode_flush_nogap = (int (*)(lame_global_flags *, unsigned char *, int))dlsym(library, "lame_encode_flush_nogap"))) |
110 |
return -1; |
111 |
if (!(dl_lame_mp3_tags_fid = (void (*)(lame_global_flags *, FILE *))dlsym(library, "lame_mp3_tags_fid"))) |
112 |
return -1; |
113 |
if (!(dl_lame_init_params = (int (*)(lame_global_flags *))dlsym(library, "lame_init_params"))) |
114 |
return -1; |
115 |
if (!(dl_lame_print_config = (void (*)(lame_global_flags *))dlsym(library, "lame_print_config"))) |
116 |
return -1; |
117 |
if (!(dl_lame_print_internals = (void (*)(lame_global_flags *))dlsym(library, "lame_print_internals"))) |
118 |
return -1; |
119 |
if (!(dl_lame_init_bitstream = (int (*)(lame_global_flags *))dlsym(library, "lame_init_bitstream"))) |
120 |
return -1; |
121 |
if (!(dl_lame_close = (void (*)(lame_global_flags *))dlsym(library, "lame_close"))) |
122 |
return -1; |
123 |
if (!(dl_get_lame_version = (char * (*)(void))dlsym(library, "get_lame_version"))) |
124 |
return -1; |
125 |
if (!(dl_bitrate_table = (int (*)[3][16])dlsym(library, "bitrate_table"))) |
126 |
return -1; |
127 |
if (!(dl_id3tag_init = (void (*)(lame_global_flags *))dlsym(library, "id3tag_init"))) |
128 |
return -1; |
129 |
if (!(dl_id3tag_add_v2 = (void (*)(lame_global_flags *lf))dlsym(library, "id3tag_add_v2"))) |
130 |
return -1; |
131 |
if (!(dl_id3tag_set_title = (void (*)(lame_global_flags *, const char *))dlsym(library, "id3tag_set_title"))) |
132 |
return -1; |
133 |
if (!(dl_id3tag_set_artist = (void (*)(lame_global_flags *, const char *))dlsym(library, "id3tag_set_artist"))) |
134 |
return -1; |
135 |
if (!(dl_id3tag_set_album = (void (*)(lame_global_flags *, const char *))dlsym(library, "id3tag_set_album"))) |
136 |
return -1; |
137 |
if (!(dl_id3tag_set_track = (void (*)(lame_global_flags *, const char *))dlsym(library, "id3tag_set_track"))) |
138 |
return -1; |
139 |
|
140 |
loaded++; |
141 |
return 0; |
142 |
} |
50 |
|
143 |
|
51 |
void message_args(int level, int addNewLine, const char *fmt, va_list args) |
144 |
void message_args(int level, int addNewLine, const char *fmt, va_list args) |
52 |
{ |
145 |
{ |
Lines 141-150
Link Here
|
141 |
|
234 |
|
142 |
message(0, ""); |
235 |
message(0, ""); |
143 |
|
236 |
|
144 |
message(0, "LAME encoder version: %s", get_lame_version()); |
237 |
message(0, "LAME encoder version: %s", dl_get_lame_version()); |
145 |
message(0, "Supported bit rates: "); |
238 |
message(0, "Supported bit rates: "); |
146 |
for (int i = 0; i < 16 && bitrate_table[1][i] >= 0; i++) { |
239 |
for (int i = 0; i < 16 && (*dl_bitrate_table)[1][i] >= 0; i++) { |
147 |
message(0, "%d ", bitrate_table[1][i]); |
240 |
message(0, "%d ", (*dl_bitrate_table)[1][i]); |
148 |
} |
241 |
} |
149 |
message(0, ""); |
242 |
message(0, ""); |
150 |
} |
243 |
} |
Lines 238-245
Link Here
|
238 |
lame_global_flags *lf; |
331 |
lame_global_flags *lf; |
239 |
int bitrateOk = 0; |
332 |
int bitrateOk = 0; |
240 |
|
333 |
|
241 |
for (int i = 0; bitrate_table[1][i] >= 0 && !bitrateOk; i++) { |
334 |
for (int i = 0; (*dl_bitrate_table)[1][i] >= 0 && !bitrateOk; i++) { |
242 |
if (bitrate == bitrate_table[1][i]) |
335 |
if (bitrate == (*dl_bitrate_table)[1][i]) |
243 |
bitrateOk = 1; |
336 |
bitrateOk = 1; |
244 |
} |
337 |
} |
245 |
|
338 |
|
Lines 248-270
Link Here
|
248 |
return NULL; |
341 |
return NULL; |
249 |
} |
342 |
} |
250 |
|
343 |
|
251 |
if ((lf = lame_init()) == NULL) { |
344 |
if ((lf = dl_lame_init()) == NULL) { |
252 |
return NULL; |
345 |
return NULL; |
253 |
} |
346 |
} |
254 |
|
347 |
|
255 |
lame_set_msgf(lf, lame_message); |
348 |
dl_lame_set_msgf(lf, lame_message); |
256 |
lame_set_debugf(lf, lame_message); |
349 |
dl_lame_set_debugf(lf, lame_message); |
257 |
lame_set_errorf(lf, lame_error_message); |
350 |
dl_lame_set_errorf(lf, lame_error_message); |
258 |
|
351 |
|
259 |
lame_set_in_samplerate(lf, 44100); |
352 |
dl_lame_set_in_samplerate(lf, 44100); |
260 |
|
353 |
|
261 |
lame_set_num_channels(lf, 2); |
354 |
dl_lame_set_num_channels(lf, 2); |
262 |
|
355 |
|
263 |
lame_set_quality(lf, 2); |
356 |
dl_lame_set_quality(lf, 2); |
264 |
|
357 |
|
265 |
lame_set_mode(lf, STEREO); |
358 |
dl_lame_set_mode(lf, STEREO); |
266 |
|
359 |
|
267 |
lame_set_brate(lf, bitrate); |
360 |
dl_lame_set_brate(lf, bitrate); |
268 |
|
361 |
|
269 |
//lame_set_VBR(lf, vbr_abr); |
362 |
//lame_set_VBR(lf, vbr_abr); |
270 |
|
363 |
|
Lines 287-308
Link Here
|
287 |
{ |
380 |
{ |
288 |
char buf[100]; |
381 |
char buf[100]; |
289 |
|
382 |
|
290 |
id3tag_init(lf); |
383 |
dl_id3tag_init(lf); |
291 |
|
384 |
|
292 |
id3tag_add_v2(lf); |
385 |
dl_id3tag_add_v2(lf); |
293 |
|
386 |
|
294 |
if (!title.empty()) |
387 |
if (!title.empty()) |
295 |
id3tag_set_title(lf, title.c_str()); |
388 |
dl_id3tag_set_title(lf, title.c_str()); |
296 |
|
389 |
|
297 |
if (!artist.empty()) |
390 |
if (!artist.empty()) |
298 |
id3tag_set_artist(lf, artist.c_str()); |
391 |
dl_id3tag_set_artist(lf, artist.c_str()); |
299 |
|
392 |
|
300 |
if (!album.empty()) |
393 |
if (!album.empty()) |
301 |
id3tag_set_album(lf, album.c_str()); |
394 |
dl_id3tag_set_album(lf, album.c_str()); |
302 |
|
395 |
|
303 |
if (tracknr > 0 && tracknr <= 255) { |
396 |
if (tracknr > 0 && tracknr <= 255) { |
304 |
sprintf(buf, "%d", tracknr); |
397 |
sprintf(buf, "%d", tracknr); |
305 |
id3tag_set_track(lf, buf); |
398 |
dl_id3tag_set_track(lf, buf); |
306 |
} |
399 |
} |
307 |
} |
400 |
} |
308 |
|
401 |
|
Lines 346-352
Link Here
|
346 |
rightSamples[i] = audioData[i].right(); |
439 |
rightSamples[i] = audioData[i].right(); |
347 |
} |
440 |
} |
348 |
|
441 |
|
349 |
int count = lame_encode_buffer(lf, leftSamples, rightSamples, |
442 |
int count = dl_lame_encode_buffer(lf, leftSamples, rightSamples, |
350 |
SAMPLES_PER_BLOCK, mp3buffer, |
443 |
SAMPLES_PER_BLOCK, mp3buffer, |
351 |
sizeof(mp3buffer)); |
444 |
sizeof(mp3buffer)); |
352 |
|
445 |
|
Lines 368-374
Link Here
|
368 |
} |
461 |
} |
369 |
|
462 |
|
370 |
if (ret != 0) { |
463 |
if (ret != 0) { |
371 |
int count = lame_encode_flush_nogap(lf, mp3buffer, sizeof(mp3buffer)); |
464 |
int count = dl_lame_encode_flush_nogap(lf, mp3buffer, sizeof(mp3buffer)); |
372 |
|
465 |
|
373 |
if (count > 0) { |
466 |
if (count > 0) { |
374 |
if (fullWrite(fd, mp3buffer, count) != count) { |
467 |
if (fullWrite(fd, mp3buffer, count) != count) { |
Lines 387-393
Link Here
|
387 |
FILE *fp = fopen(fileName.c_str(), "a+"); |
480 |
FILE *fp = fopen(fileName.c_str(), "a+"); |
388 |
|
481 |
|
389 |
if (fp != NULL) { |
482 |
if (fp != NULL) { |
390 |
lame_mp3_tags_fid(lf, fp); |
483 |
dl_lame_mp3_tags_fid(lf, fp); |
391 |
fclose(fp); |
484 |
fclose(fp); |
392 |
} |
485 |
} |
393 |
else { |
486 |
else { |
Lines 449-454
Link Here
|
449 |
|
542 |
|
450 |
PRGNAME = *argv; |
543 |
PRGNAME = *argv; |
451 |
|
544 |
|
|
|
545 |
if (load_lame()) |
546 |
message(-10, "Cannot load libmp3lame.so"); |
547 |
|
452 |
switch (parseCommandLine(argc, argv, &tocFile, &bitrate)) { |
548 |
switch (parseCommandLine(argc, argv, &tocFile, &bitrate)) { |
453 |
case 0: |
549 |
case 0: |
454 |
printUsage(); |
550 |
printUsage(); |
Lines 600-616
Link Here
|
600 |
set_id3_tags(lf, trackNr, title, performer, album); |
696 |
set_id3_tags(lf, trackNr, title, performer, album); |
601 |
|
697 |
|
602 |
if (firstEncodedTrack) { |
698 |
if (firstEncodedTrack) { |
603 |
if (lame_init_params(lf) < 0) { |
699 |
if (dl_lame_init_params(lf) < 0) { |
604 |
message(-2, "Setting of lame parameters failed"); |
700 |
message(-2, "Setting of lame parameters failed"); |
605 |
err = 1; |
701 |
err = 1; |
606 |
break; |
702 |
break; |
607 |
} |
703 |
} |
608 |
message(1, "Lame encoder settings:"); |
704 |
message(1, "Lame encoder settings:"); |
609 |
lame_print_config(lf); |
705 |
dl_lame_print_config(lf); |
610 |
message(1, "Selected bit rate: %d kbit/s", bitrate); |
706 |
message(1, "Selected bit rate: %d kbit/s", bitrate); |
611 |
|
707 |
|
612 |
if (VERBOSE >= 2) |
708 |
if (VERBOSE >= 2) |
613 |
lame_print_internals(lf); |
709 |
dl_lame_print_internals(lf); |
614 |
|
710 |
|
615 |
message(1, ""); |
711 |
message(1, ""); |
616 |
|
712 |
|
Lines 621-627
Link Here
|
621 |
firstEncodedTrack = 0; |
717 |
firstEncodedTrack = 0; |
622 |
} |
718 |
} |
623 |
else { |
719 |
else { |
624 |
if (lame_init_bitstream(lf) != 0) { |
720 |
if (dl_lame_init_bitstream(lf) != 0) { |
625 |
message(-2, "Cannot initialize bit stream."); |
721 |
message(-2, "Cannot initialize bit stream."); |
626 |
err = 1; |
722 |
err = 1; |
627 |
break; |
723 |
break; |
Lines 648-654
Link Here
|
648 |
nextTrack = titr.next(nstart, nend); |
744 |
nextTrack = titr.next(nstart, nend); |
649 |
} |
745 |
} |
650 |
|
746 |
|
651 |
lame_close(lf); |
747 |
dl_lame_close(lf); |
652 |
|
748 |
|
653 |
exit(err); |
749 |
exit(err); |
654 |
} |
750 |
} |