This repository has been archived by the owner on May 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuart-sound.c
172 lines (131 loc) · 4.77 KB
/
uart-sound.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
char *tty = "/dev/ttyUSB0";
int baudrate = 3000000;
typedef enum {
MODE_PDM = 0,
MODE_PWM32 = 1,
MODE_PWM64 = 2,
} encoding_mode_t;
encoding_mode_t mode;
int main(int argc, char *argv[])
{
if (argc < 5) {
printf("Usage: %s <audio.raw> </dev/ttyUART0> <baud rate> <mode>\n"
"Audio should use an appropriate sample rate, 16 bit mono signed\n",
"mode: 0 = MODE_PDM, sample rate = baud rate / 10 * 8 / 64\n"
" 1 = MODE_PWM32, sample rate = baud rate / 10 * 8 / 32\n"
" 2 = MODE_PWM64, sample rate = baud rate / 10 * 8 / 64\n",
argv[0]);
return -1;
}
FILE *fp = fopen(argv[1], "rb");
tty = argv[2];
baudrate = atol(argv[3]);
mode = atol(argv[4]);
fseek(fp, 0, SEEK_END);
int audio_buffer_bytes = ftell(fp);
int samples = audio_buffer_bytes / sizeof(int16_t);
fseek(fp, 0, SEEK_SET);
int16_t *audio_buffer = malloc(audio_buffer_bytes);
fread(audio_buffer, samples, sizeof(int16_t), fp);
printf("Read %d bytes from %s. Using %s @%d bps\n", samples, argv[1], tty, baudrate);
// Ugly, please don't judge.
char cmd[1024];
snprintf(cmd, 1023, "stty -F %s %d raw -clocal -echo icrnl", tty, baudrate);
puts(cmd);
system(cmd);
int usbdev = open(tty, O_RDWR);
int bits_per_transfer = 10; //
int buflen = 1024;
char buf[buflen];
uint32_t *buf32 = (uint32_t *) buf;
uint64_t *buf64 = (uint64_t *) buf;
/*
UART with 1 start bit and 1 stop bit sends data one byte at a time like this:
0 D0 D1 D2 D3 D4 D5 D6 D7 1
\ \__ Stop bit
\____________________________ Start bit
Data bitrate = (baudrate // 10) * 8
Baudrate: 2 000 000
Data bitrate: 1 600 000
Samplerate: 25 000
Bits per sample: 64 (64 + 8 implicit 1/0 transitions)
Bytes per sample: 8
*/
int i = 0;
int j = 0;
int k = 0;
uint32_t pdm = 0;
int samples_out = 0;
for (i = 0; i < samples; i++) {
int16_t sample = audio_buffer[i];
if (mode == MODE_PDM) {
// Encode as PDM
int k_iter = 8;
for (k = 0; k < k_iter; k++) {
uint8_t out = 0;
// int limit = 1 << 8;
int limit = 1 << 16;
// int limit = 1 << 31;
for (j = 0; j < 8; j++) {
if (pdm >= limit) {
pdm -= limit;
} else if (pdm <= -limit) {
pdm += limit;
}
// 9 bit
// pdm = (pdm & 0xff) + (((uint16_t)(sample + (1 << 15))) >> 8);
// 17 bit
pdm = (pdm & (limit - 1)) + ((uint16_t)(sample + (1 << 15)));
// 32 bit
// pdm = (pdm & (limit - 1)) + (((uint16_t)(sample + (1 << 15))) << 15);
int pdm_on = !!(pdm & limit);
// Store MSB of accumulator in LSB
out = (out >> 1) | (pdm_on << 7);
// Store MSB of accumulator in MSB
// out = (out << 1) | (pdm_on & 1);
}
// Byte ordering
// int buf_idx = samples_out + k_iter - k - 1;
int buf_idx = samples_out + k;
buf[buf_idx] = out;
if (buf_idx == buflen) {
write(usbdev, buf, buflen);
samples_out = 0;
}
}
samples_out += k_iter;
} else if (mode == MODE_PWM32) {
// Encode as 32 bit PWM - works ok at 1 000 000 baud
// 1000000 // 10 * 8 // 32 = 25000 Hz
// 32 bits = 4 bytes
uint8_t pwm = 16 + (sample >> 11); // [-32768, 32767] => [0, 31]
uint32_t out = (1ULL << pwm) - 1;
buf32[samples_out++] = out;
if (samples_out == buflen / sizeof(uint32_t)) {
write(usbdev, buf, buflen);
samples_out = 0;
}
} else if (mode == MODE_PWM64) {
// Encode as 64 bit PWM - works ok at 2 000 000 baud
// 2000000 // 10 * 8 // 64 = 25000 Hz
// 64 bits = 8 bytes
uint8_t pwm = 32 + (sample >> 10); // [-32768, 32767] => [0, 63]
uint64_t out = (1ULL << pwm) - 1;
buf64[samples_out++] = out;
if (samples_out == buflen / sizeof(uint64_t)) {
write(usbdev, buf, buflen);
samples_out = 0;
}
}
}
free(audio_buffer);
fclose(fp);
return 0;
}