-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclame.c
More file actions
116 lines (100 loc) · 2.92 KB
/
clame.c
File metadata and controls
116 lines (100 loc) · 2.92 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <lame.h>
#define INBUFSIZE 4096
#define MP3BUFSIZE (int)(1.25 * INBUFSIZE) + 7200
/* Compile this with one of the following commands
*
* Under Linux:
* gcc -shared -I/usr/include/python2.4 -I/usr/include/lame \
* pylame1.c clame.c -lmp3lame -o pylame1.so
*
* adjust the -I locations based on the actual location of the lame include
* libraries on your system.
*
*
* Under windows, try the following:
* cl /LD /IC:\Python24\include /IC:\lame-3.96.1\include \
* pylame1.c clame.c \
* C:\Python24\libs\python24.lib \
* C:\lame-3.96.1\libmp3lame\Release\libmp3lame.lib \
* C:\lame-3.96.1\mpglib\Release\mpglib.lib
*
* again, adjusting the locations of your header files and libraries
* as appropraite.
*/
int encode(char *inpath, char *outpath) {
int status = 0;
lame_global_flags *gfp;
int ret_code;
FILE *infp;
FILE *outfp;
short *input_buffer;
int input_samples;
char *mp3_buffer;
int mp3_bytes;
/* Initialize the library. */
gfp = lame_init();
if (gfp == NULL) {
printf("lame_init returned NULL\n");
status = -1;
goto exit;
}
/* Set the encoding parameters. */
ret_code = lame_init_params(gfp);
if (ret_code < 0) {
printf("lame_init_params returned %d\n", ret_code);
status = -1;
goto close_lame;
}
/* Open our input and output files. */
infp = fopen(inpath, "rb");
outfp = fopen(outpath, "wb");
/* Allocate some buffers. */
input_buffer = (short*)malloc(INBUFSIZE*2);
mp3_buffer = (char*)malloc(MP3BUFSIZE);
/* Read from the input file, encode, and write to the output file. */
do {
input_samples = fread(input_buffer, 2, INBUFSIZE, infp);
if (input_samples > 0) {
mp3_bytes = lame_encode_buffer_interleaved(
gfp,
input_buffer,
input_samples / 2,
mp3_buffer,
MP3BUFSIZE
);
if (mp3_bytes < 0) {
printf("lame_encode_buffer_interleaved returned %d\n", mp3_bytes);
status = -1;
goto free_buffers;
} else if (mp3_bytes > 0) {
fwrite(mp3_buffer, 1, mp3_bytes, outfp);
}
}
} while (input_samples == INBUFSIZE);
/* Flush the encoder of any remaining bytes. */
mp3_bytes = lame_encode_flush(gfp, mp3_buffer, sizeof(mp3_buffer));
if (mp3_bytes > 0) {
printf("writing %d mp3 bytes\n", mp3_bytes);
fwrite(mp3_buffer, 1, mp3_bytes, outfp);
}
/* Clean up. */
free_buffers:
free(mp3_buffer);
free(input_buffer);
fclose(outfp);
fclose(infp);
close_lame:
lame_close(gfp);
exit:
return status;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("usage: clame rawinfile mp3outfile\n");
exit(1);
}
encode(argv[1], argv[2]);
return 0;
}