-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
241 lines (192 loc) · 7.83 KB
/
main.cpp
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
// psk_orange
// A homebrew psk31 codec
// by John howard, [email protected], August 2017
// MIT license
// for additional info see:
// https://en.wikipedia.org/wiki/PSK31
// http://aintel.bi.ehu.es/psk31.html
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cmath>
#include <deque>
#include <vector>
#include <functional>
#include <cstring>
#include "PSKOrange.h"
using namespace std;
//////////////////////////
// int16 LE file writing
void write_s16LE(ostream &stream, const float &value)
{
int16_t s16 = (uint16_t)((float)INT16_MAX * value);
stream.write(reinterpret_cast<const char*>(&s16), sizeof(int16_t));
}
float read_s16LE(istream &stream)
{
int16_t s16;
stream.read(reinterpret_cast<char*>(&s16), sizeof(int16_t));
return (float)s16 / (float)INT16_MAX;
}
//////////////////////////
struct MathUtil
{
typedef float Valuetype;
typedef float Angletype;
static constexpr float PI = (float)M_PI;
static constexpr float PI_2 = (float)M_PI_2;
// Approximates atan2(y, x) with maximum error of 0.1620 degrees
// https://stackoverflow.com/a/14100975/968363 - with dbz and nan fix and renormalization to (-pi,+pi)
static float atan2( float y, float x )
{
static const uint32_t sign_mask = 0x80000000;
static const float b = 0.596227f;
// Extract the sign bits
uint32_t ux_s = sign_mask & (uint32_t &)x;
uint32_t uy_s = sign_mask & (uint32_t &)y;
// Determine the quadrant offset
auto q = (float)( ( ~ux_s & uy_s ) >> 29 | ux_s >> 30 );
// Calculate the arctangent in the first quadrant
float bxy_a = fabsf( b * x * y );
float num = bxy_a + y * y;
float atan_1q = num / (x * x + bxy_a + num + .0001f); // .0001 to fix dbz and nan issue
// Translate it to the proper quadrant
uint32_t uatan_2q = (ux_s ^ uy_s) | (uint32_t &)atan_1q;
return (q + (float &)uatan_2q) * PI_2 - PI; // [0,4) to [0,2pi) to (-pi,+pi)
}
static float sqrt(float in) { return ::sqrt(in); }
static float abs(float in) { return ::fabsf(in); }
static float max(float a, float b) { return ::max(a,b); }
static float sin(float in) { return ::sin(in); }
};
//////////////////////////
int main()
{
srand48(int(time(NULL)));
const int element_cycles = 10;
const int carrier_cycles = 3 * element_cycles;
const int freq = 500;
const int samplerate = 22050;
const char* szAudio = "[email protected]";
#if defined(TARGET_TESTENCODE)
const char* message = "Hello World!";
cout << "encode message \"" << message << "\" to " << szAudio << std::endl;
cout << "play audio with: /usr/bin/aplay -f cd -r " << samplerate << " " << szAudio<< std::endl;
std::ostream &console = cout; // lower cout into local context for use in lambda
// synthesis
ofstream outFile;
outFile.open(szAudio, ios::binary);
PSKOrange<element_cycles,carrier_cycles,freq,samplerate,MathUtil>::ElementEncoder encoder;
encoder.encode(
[&outFile](float v) { write_s16LE( outFile, v ); },
[&message,&console](void) -> bool*
{
static uint i = 0; // todo: bit count might overflow for long messages
static bool bit = false;
if(message[i/8] == 0) { return nullptr; }
// encode smallest bits first = big endian = network order
bit = (bool)(message[i/8] & (1 << i%8));
console << bit << (++i%8 == 0 ? " " : "" ) << std::flush;
return &bit;
}
);
outFile.close();
cout << std::endl;
#elif defined(TARGET_DUMPDECODE)
cout << "read " << szAudio << " and print the decoded pattern as big endian bytes" << std::endl;
std::ostream &console = cout; // lower cout into local context for use in lambda
// read and decode
ifstream inFile;
inFile.open(szAudio, ios::binary);
PSKOrange<element_cycles,carrier_cycles,freq,samplerate,MathUtil>::ElementDecoder decoder;
decoder.decode(
[&inFile](void) -> float*
{
static float sample; // simple hack that allows us to signal end-of-stream
return inFile.eof() ? nullptr : &(sample = read_s16LE(inFile));
},
[&console](bool bit) -> void
{
static uint i = 0; // todo: bit count might overflow for long messages
// decode smallest bits first = big endian = network order
console << bit << (++i%8 == 0 ? " " : "" ) << std::flush;
},
[&console](uint bit) -> void { /*console << "(E" << bit << ')';*/ } // todo: fix
);
inFile.close();
cout << std::endl;
#elif defined(TARGET_DEBUGDECODE)
cout << "read " << szAudio << " and generate binary file details.bin for debugging" << std::endl;
ifstream inFile;
inFile.open(szAudio, ios::binary);
ofstream detailsFile;
detailsFile.open("details.bin", ios::binary);
PSKOrange<element_cycles,carrier_cycles,freq,samplerate,MathUtil>::ElementDecoder decoder;
while(!inFile.eof())
{
float sample = read_s16LE(inFile);
decoder.process(sample);
write_s16LE(detailsFile, sample);
write_s16LE(detailsFile, decoder.inversionDetector.phase * M_1_PI);
write_s16LE(detailsFile, decoder.channelA.phase * M_1_PI);
write_s16LE(detailsFile, decoder.channelA.iIntegrator);
write_s16LE(detailsFile, decoder.channelA.qIntegrator);
write_s16LE(detailsFile, decoder.channelA.squelch);
write_s16LE(detailsFile, decoder.channelB.phase * M_1_PI);
write_s16LE(detailsFile, decoder.channelB.iIntegrator);
write_s16LE(detailsFile, decoder.channelB.qIntegrator);
write_s16LE(detailsFile, decoder.channelB.squelch);
}
inFile.close();
detailsFile.close();
#elif defined(TARGET_ENCODE)
cout << "encode stdin to " << szAudio << std::endl;
cout << "example: echo -n \"Hello World!\" | encode ; dumpdecode" << std::endl;
std::istream &console = cin; // lower into local context for use in lambda
// synthesis
ofstream outFile;
outFile.open(szAudio, ios::binary);
PSKOrange<element_cycles,carrier_cycles,freq,samplerate,MathUtil>::ElementEncoder encoder;
encoder.encode(
[&outFile](float v) { write_s16LE( outFile, v ); },
[&console](void) -> bool*
{
static uint i = 0; // todo: bit count might overflow for long messages
static bool bit = false;
static char byte = 0;
if (i%8 == 0)
if(!console.get(byte)) return nullptr;
// encode smallest bits first = big endian = network order
bit = (bool)(byte & (1 << i++%8));
return &bit;
}
);
outFile.close();
#elif defined(TARGET_DECODE)
cout << "read " << szAudio << " and print the decoded stream" << std::endl;
std::ostream &console = cout; // lower cout into local context for use in lambda
// read and decode
ifstream inFile;
inFile.open(szAudio, ios::binary);
PSKOrange<element_cycles,carrier_cycles,freq,samplerate,MathUtil>::ElementDecoder decoder;
decoder.decode(
[&inFile](void) -> float*
{
static float sample; // simple hack that allows us to signal end-of-stream
return inFile.eof() ? nullptr : &(sample = read_s16LE(inFile));
},
[&console](bool bit) -> void
{
static uint i = 0; // todo: bit count might overflow for long messages
static unsigned char byte = 0;
// when decoding smallest bits come first = big endian = network order
byte = (byte >> 1) | (bit?0b10000000:0);
if (++i%8 == 0)
console << byte << std::flush;
},
[&console](uint bit) -> void { /*console << "(E" << bit << ')';*/ } // todo: fix
);
inFile.close();
#endif
return 0;
}