-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathexample.c
More file actions
144 lines (120 loc) · 3.58 KB
/
Copy pathexample.c
File metadata and controls
144 lines (120 loc) · 3.58 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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "g711_table.h"
void print_usage(char *program_name)
{
printf("Usage: %s input_file CONVERSION output_file\n", program_name);
printf("Supported CONVERSIONs: pcm_alaw, pcm_ulaw, alaw_pcm, ulaw_pcm\n");
}
long get_file_size(FILE *f)
{
long file_size;
/* Go to end of file */
fseek(f, 0L, SEEK_END);
/* Get the number of bytes */
file_size = ftell(f);
/* reset the file position indicator to
the beginning of the file */
fseek(f, 0L, SEEK_SET);
return file_size;
}
char * allocate_buffer(long buffer_size)
{
char *buffer;
/* grab sufficient memory for the
buffer to hold the audio */
buffer = (char*)calloc(buffer_size, sizeof(char));
/* memory error */
if(buffer == NULL)
{
perror("Error while allocating memory for write buffer.\n");
exit(EXIT_FAILURE);
}
return buffer;
}
int main(int argc, char *argv[])
{
FILE *fRead, *fWrite;
char *bufferRead, *bufferWrite;
long bufferReadSize, bufferWriteSize;
size_t readed;
if(argc != 4)
{
printf("Incorrect parameter length.\n");
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
/* open an existing file for reading */
fRead = fopen(argv[1],"rb");
/* quit if the file does not exist */
if( fRead == NULL )
{
perror("Error while opening read file.\n");
exit(EXIT_FAILURE);
}
/* Get file size */
bufferReadSize = get_file_size(fRead);
/* grab sufficient memory for the
buffer to hold the audio */
bufferRead = allocate_buffer(bufferReadSize);
/* copy all the text into the buffer */
readed = fread(bufferRead, sizeof(char), bufferReadSize, fRead);
if (readed != bufferReadSize)
{
perror("Incorrect bytes readed\n");
exit(EXIT_FAILURE);
}
fclose(fRead);
/* Conversions */
if (strcmp(argv[2], "alaw_pcm") == 0)
{
alaw_pcm16_tableinit();
bufferWriteSize = bufferReadSize * 2;
bufferWrite = allocate_buffer(bufferWriteSize);
alaw_to_pcm16(bufferReadSize, bufferRead, bufferWrite);
}
else if (strcmp(argv[2], "ulaw_pcm") == 0)
{
ulaw_pcm16_tableinit();
bufferWriteSize = bufferReadSize * 2;
bufferWrite = allocate_buffer(bufferWriteSize);
ulaw_to_pcm16(bufferReadSize, bufferRead, bufferWrite);
}
else if (strcmp(argv[2], "pcm_alaw") == 0)
{
pcm16_alaw_tableinit();
bufferWriteSize = bufferReadSize / 2;
bufferWrite = allocate_buffer(bufferWriteSize);
pcm16_to_alaw(bufferReadSize, bufferRead, bufferWrite);
}
else if (strcmp(argv[2], "pcm_ulaw") == 0)
{
pcm16_ulaw_tableinit();
bufferWriteSize = bufferReadSize / 2;
bufferWrite = allocate_buffer(bufferWriteSize);
pcm16_to_ulaw(bufferReadSize, bufferRead, bufferWrite);
}
else
{
perror("Incorrect parameter.\n");
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
printf("Bytes read: %ld, Buffer Write: %ld\n", bufferReadSize, bufferWriteSize);
/* open a file for writing */
fWrite = fopen(argv[3], "wb");
/* quit if the file can not be opened */
if( fWrite == NULL )
{
perror("Error while opening the write file.\n");
exit(EXIT_FAILURE);
}
/* copy all the buffer into the file */
fwrite (bufferWrite , sizeof(char), bufferWriteSize, fWrite);
fclose (fWrite);
/* free the memory we used for the buffer */
free(bufferWrite);
free(bufferRead);
return 0;
}