-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
182 lines (162 loc) · 5.49 KB
/
Copy pathmain.c
File metadata and controls
182 lines (162 loc) · 5.49 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
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
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXTENSION_SIZE 128
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define MAX(A, B) ((A) > (B) ? (A) : (B))
struct __attribute__((__packed__)) WavHeader {
// Master RIFF chunk
uint32_t file_type_bloc_id;
uint32_t file_size;
uint32_t file_format_id;
// Chunk describing the data format
uint32_t format_bloc_id;
uint32_t bloc_size;
uint16_t audio_format;
uint16_t nbr_channels;
uint32_t frequency;
uint32_t byte_per_sec;
uint16_t byte_per_bloc;
uint16_t bits_per_sample;
// Chunk containing the sampled data
uint32_t data_bloc_id;
uint32_t data_size;
};
struct Vec2 {
int x;
int y;
};
struct Vec2d {
double x;
double y;
};
struct Digit {
struct Vec2 table;
char ch;
};
struct Dft {
double re;
double im;
};
void get_file_extension(char *path, char *extension) {
char *extension_start = strrchr(path, '.');
char *dir_start = strrchr(path, '/');
if (!extension_start || (dir_start && dir_start > extension_start)) {
extension[0] = '\0';
return;
}
strncpy(extension, extension_start, strlen(extension_start));
}
int get_harmonic(int frequency, int resolution) { return frequency / resolution; }
double get_magnitude(struct Dft harmonic) { return sqrt(harmonic.re * harmonic.re + harmonic.im * harmonic.im); }
char get_digit(struct Digit *harmonics_to_digit, size_t count, int harmonic1, int harmonic2) {
for (int i = 0; i < count; i++) {
int x = harmonics_to_digit[i].table.x, y = harmonics_to_digit[i].table.y;
if (MIN(harmonic1, harmonic2) == MIN(x, y) && MAX(harmonic1, harmonic2) == MAX(x, y)) return harmonics_to_digit[i].ch;
}
return '\0';
}
struct Dft *calculate_dft(int16_t *data, size_t N, int *harmonics, size_t count) {
struct Dft *res = (struct Dft *)malloc(count * sizeof(struct Dft));
for (int i = 0; i < count; i++) {
int harmonic = harmonics[i];
double sum_x = 0, sum_y = 0;
for (int n = 0; n < N; n++) {
double angle = 2 * M_PI * harmonic * n / N;
sum_x += data[n] * cos(angle);
sum_y += data[n] * sin(angle);
}
sum_x /= N;
sum_y /= N;
res[i].re = sum_x;
res[i].im = sum_y;
}
return res;
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("You must specify the audio file\n");
return -1;
}
char *path = argv[1];
FILE *sound = fopen(path, "rb");
if (!sound) {
printf("Audio file not found\n");
return -1;
}
char extension[MAX_EXTENSION_SIZE];
get_file_extension(path, extension);
if (!strlen(extension) || strcmp(extension, ".wav")) {
printf("Only wav files are supported\n");
fclose(sound);
return -1;
}
struct WavHeader header;
fread(&header, sizeof(header), 1, sound);
// CONSTANTS
struct Digit frequencies_to_digit[] = {
{{697, 1209}, '1'}, {{770, 1209}, '4'}, {{852, 1209}, '7'}, {{941, 1209}, '*'}, {{697, 1336}, '2'}, {{770, 1336}, '5'},
{{852, 1336}, '8'}, {{941, 1336}, '0'}, {{697, 1477}, '3'}, {{770, 1477}, '6'}, {{852, 1477}, '9'}, {{941, 1477}, '#'},
{{697, 1633}, 'A'}, {{770, 1633}, 'B'}, {{852, 1633}, 'C'}, {{941, 1633}, 'D'},
};
static const size_t DIGITS_COUNT = sizeof(frequencies_to_digit) / sizeof(frequencies_to_digit[0]);
int frequencies[] = {697, 770, 852, 941, 1209, 1336, 1477, 1633};
static const size_t FREQUENCIES_COUNT = sizeof(frequencies) / sizeof(frequencies[0]);
const size_t SAMPLES_COUNT = header.data_size / (header.bits_per_sample / 8);
const int HARMONIC_DETECTION_MAGNITUDE = 5;
const int HARMONIC_RESOLUTION = 35;
const int N = header.frequency / HARMONIC_RESOLUTION;
const int SHIFT_INTERVAL = N / 3;
int harmonics[FREQUENCIES_COUNT];
for (int i = 0; i < FREQUENCIES_COUNT; i++) {
harmonics[i] = get_harmonic(frequencies[i], HARMONIC_RESOLUTION);
}
struct Digit harmonics_to_digit[DIGITS_COUNT];
for (int i = 0; i < DIGITS_COUNT; i++) {
harmonics_to_digit[i].table.x = get_harmonic(frequencies_to_digit[i].table.x, HARMONIC_RESOLUTION);
harmonics_to_digit[i].table.y = get_harmonic(frequencies_to_digit[i].table.y, HARMONIC_RESOLUTION);
harmonics_to_digit[i].ch = frequencies_to_digit[i].ch;
}
// Encoding
char last_digit = '\0';
int16_t samples[SAMPLES_COUNT];
fseek(sound, sizeof(struct WavHeader), SEEK_SET);
fread(samples, sizeof(int16_t), SAMPLES_COUNT, sound);
size_t curr = 0;
while (curr + N < SAMPLES_COUNT) {
struct Dft *dft = calculate_dft(samples + curr, N, harmonics, FREQUENCIES_COUNT);
struct Vec2d magnitudes[FREQUENCIES_COUNT];
for (int i = 0; i < FREQUENCIES_COUNT; i++) {
magnitudes[i].x = harmonics[i];
magnitudes[i].y = get_magnitude(dft[i]);
}
struct Vec2d magnitude1 = {0, -DBL_MAX};
struct Vec2d magnitude2 = {0, -DBL_MAX};
for (int i = 0; i < FREQUENCIES_COUNT; i++) {
if (magnitudes[i].y > magnitude1.y) {
magnitude2 = magnitude1;
magnitude1 = magnitudes[i];
} else if (magnitudes[i].y > magnitude2.y) {
magnitude2 = magnitudes[i];
}
}
if (magnitude1.y > HARMONIC_DETECTION_MAGNITUDE && magnitude2.y > HARMONIC_DETECTION_MAGNITUDE) {
char digit = get_digit(harmonics_to_digit, DIGITS_COUNT, magnitude1.x, magnitude2.x);
if (digit != '\0' && digit != last_digit) {
printf("%c", digit);
fflush(stdout);
last_digit = digit;
}
} else {
last_digit = '\0';
}
free(dft);
curr += SHIFT_INTERVAL;
}
printf("\n");
fclose(sound);
}