-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.c
executable file
·243 lines (202 loc) · 6.22 KB
/
encode.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Attempts to decode speech data extracted from a North American phone.
*
* LICENSE: public domain.
* (c) 2015, Philippe Michaud-Boudreault
*/
#include "upd.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sndfile.h>
#include <signal.h>
extern const int upd7759_step[16][16];
extern const int upd7759_state_table[16];
/*
We know from the datasheet that this chip has an internal 9-bit DAC.
What the hell was NEC thinking, choosing such an odd bit-depth? In any
case, this is the first software-based encoder in existence to support
the chip. We can only suspect that the hardware encoder was made by
stoned wizards. Subsequently, we've only hired stoned wizards to
assist us in this undertaking.
*/
static struct {
char *inputFileName, *outputFileName;
uint8_t verbose;
} options = {0,};
typedef struct {
SNDFILE *fp;
SF_INFO info;
short *data;
uint64_t data_size;
} input_file_t;
typedef enum {
NONE = 0x0 << 3,
FIVEKHZ = 0x5f,
SIXKHZ = 0x59,
EIGHTKHZ = 0x53
} frequency_upd7759_t;
frequency_upd7759_t frequency_upd7759 = NONE;
static void cleanup();
static void kill_str(char *errstr, uint8_t exit_status) {
fputs("Sorry :(", stderr);
if (errstr)
fprintf(stderr, " -- %s", errstr);
fputc('\n', stderr);
cleanup();
exit(exit_status);
}
static void kill_errno(int error, uint8_t exit_status) {
if (!error)
kill_str(NULL, exit_status);
kill_str(strerror(error), exit_status);
}
#define kill(a) kill_errno(0, a)
static void process_arguments(int argc, char **argv) {
char c;
while ((c = getopt(argc, argv, "i:o:v")) != -1) {
if (c == 'i')
options.inputFileName = strdup(optarg);
else if (c == 'o')
options.outputFileName = strdup(optarg);
else if (c == 'v')
options.verbose = 1;
}
}
static void cleanup() {
if (options.inputFileName)
free(options.inputFileName);
if (options.outputFileName)
free(options.outputFileName);
}
// XXX: Stack memory allocated for returned object.
// MUST be free'd after it's done.
static input_file_t *open_input() {
input_file_t *input_file = calloc(1, sizeof(*input_file));
if (!input_file)
kill_errno(errno, EXIT_FAILURE);
if (options.inputFileName) {
input_file->fp = sf_open(options.inputFileName, SFM_READ, &input_file->info);
} else {
input_file->fp = sf_open_fd(STDIN_FILENO, SFM_READ, &input_file->info, 0);
}
if (!input_file->fp) {
char err[256] = {0,};
sf_error_str(input_file->fp, err, sizeof(err));
kill_str(err, EXIT_FAILURE);
}
return input_file;
}
static FILE *open_output() {
FILE *fp = NULL;
if (options.outputFileName) {
fp = fopen(options.outputFileName, "w");
} else {
fp = fdopen(STDOUT_FILENO, "w");
}
if (!fp)
kill_errno(errno, EXIT_FAILURE);
return fp;
}
static input_file_t *input_file = NULL;
static void read_pcm_file() {
sf_count_t ret = 0, pos = 0;
input_file = open_input();
if ((input_file->info.samplerate != 5000)
&& (input_file->info.samplerate != 6000)
&& (input_file->info.samplerate != 8000)) {
kill_str(
"Only sample rates of 5khz, 6khz, or 8kz are supported.",
EXIT_FAILURE
);
}
if (input_file->info.channels != 1) {
kill_str(
"Only single channel audio is supported.",
EXIT_FAILURE
);
}
if (!(input_file->info.format & SF_FORMAT_PCM_16)) {
kill_str(
"Audio data must be 16-bit PCM.",
EXIT_FAILURE
);
}
if (options.verbose) {
printf("Frames: %ld\n", (int64_t)input_file->info.frames);
printf("Sample Rate: %d\n", input_file->info.samplerate);
printf("Channels: %d\n", input_file->info.channels);
printf("Format: 0x%X\n", input_file->info.format);
printf("Sections: %d\n", input_file->info.sections);
printf("Seekable: %d\n", input_file->info.seekable);
}
input_file->data_size = (uint64_t)input_file->info.frames;
input_file->data = malloc(input_file->data_size * sizeof(*input_file->data));
sf_readf_short(input_file->fp, input_file->data, input_file->info.frames);
sf_close(input_file->fp);
free(input_file);
}
static uint8_t get_frequency(int samplerate) {
if (samplerate == 5000)
return FIVEKHZ;
if (samplerate == 6000)
return SIXKHZ;
if (samplerate == 8000)
return EIGHTKHZ;
return 0;
}
static void output_upd_file() {
FILE *o = open_output();
uint32_t i;
int state = 0, onesixty = 0;
char sample;
uint8_t output_sample = 0x0;
uint8_t freq = get_frequency(input_file->info.samplerate);
fwrite(&freq, sizeof(freq), 1, o);
for (i = 0; i < input_file->data_size; ++i) {
uint16_t sample2 = input_file->data[i];
sample = (sample2 >> 7);
if (state < 0) state = 0;
else if (state > 15) state = 15;
if (sample < 0) sample = 0;
else if (sample > 15) sample = 15;
// state %= 16;
// sample %= 16;
// state -= upd7759_state_table[sample];
state = (upd7759_state_table[sample] - state);
#ifdef DEBUG
fprintf(stderr, "State%.04x\n", state);
#endif
// sample -= upd7759_step[state][sample];
sample = (upd7759_step[state][sample] - sample);
#ifdef DEBUG
fprintf(stderr, "Sample%.02x\n", sample);
#endif
if (onesixty & 1) {
output_sample = (output_sample << 4) | (sample & 0x0f);
fwrite(&output_sample, sizeof(output_sample), 1, o);
} else {
output_sample = sample & 0x0f;
}
if (++onesixty == 256) {
onesixty = 0;
fwrite(&freq, sizeof(freq), 1, o);
}
}
if (onesixty & 1) { // purge remaining data
output_sample <<= 4;
fwrite(&output_sample, sizeof(output_sample), 1, o);
}
fclose(o);
}
int main(int argc, char *argv[]) {
process_arguments(argc, argv);
read_pcm_file();
output_upd_file();
cleanup();
return EXIT_SUCCESS;
}