-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransport.c
112 lines (101 loc) · 2.67 KB
/
transport.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
#include <stdio.h>
#include <stdlib.h>
#define LEADER_LENGTH 3000
#define HZ 44100
double onePeriod(int frequency, double inRemainder)
{
int numBytes = HZ / (2 * frequency);
double remainder = inRemainder + (double)(((double)(HZ / (double)(2 * frequency))-numBytes)/20);
unsigned char value = 1;
int i;
for (i = 0; i < numBytes; i++) {
putchar(value);
}
if (remainder > 1.0) {
putchar(value);
}
value = -1;
for (i = 0; i < numBytes; i++) {
putchar(value);
}
if (remainder > 1.0) {
putchar(value);
remainder -= 1.0;
}
return remainder;
}
void leaderTone(int duration)
{
for (int i = 0; i < duration; i++) {
onePeriod(770, 0.0);
}
}
void silence()
{
for (int i = 0; i < 15000; i++) {
putchar(0);
}
}
double encodeByte(unsigned char data, double inRemainder)
{
double remainder = inRemainder;
for (int j = 0; j < 8; j++) {
if ((data & 0x00000080) == 0x00000080) {
// It's a 1
remainder = onePeriod(1000, remainder);
//Log.print(false,"1");
}
else {
// It's a 0
remainder = onePeriod(2000, remainder);
//Log.print(false,"0");
}
data = (unsigned char) ((data << 1) & 0x000000FFL);
//Log.println(false,"BytesToWav.encodeByte() remainder after bit: "+remainder);
}
//Log.println(false,"BytesToWav.encodeByte() returning remainder: "+remainder);
return remainder;
}
unsigned char encodeData(unsigned char *myFile, int length,
unsigned char checksum)
{
int i;
double remainder = 0.0;
for (i = 0; i < length; i++) {
checksum = (unsigned char) (myFile[i] ^ checksum);
remainder = encodeByte(myFile[i], remainder);
}
return checksum;
}
int main(int argc, char *argv[])
{
unsigned char *myFile;
if (argc != 2) {
printf("usage: %s <file>\n", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "rb");
if (!fp) {
perror("fopen");
return 1;
}
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
rewind(fp);
myFile = (unsigned char *)malloc(length);
if (fread(myFile, 1, length, fp) != length) {
perror("fread");
return 1;
}
fprintf(stderr,"Length: %d\n", length);
fprintf(stderr, "Load at: %.4x..%.4x\n", 0x800,0x800+length-1);
fprintf(stderr,"Press enter when ready...");
getchar();
unsigned char checksum = -1;
leaderTone(LEADER_LENGTH);
onePeriod(2200, 0.0);
checksum = encodeData(myFile, length, checksum);
encodeByte(checksum, 0.0);
onePeriod(200, 0.0);
silence();
}