-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBELL202encoder.h
More file actions
77 lines (59 loc) · 1.46 KB
/
Copy pathBELL202encoder.h
File metadata and controls
77 lines (59 loc) · 1.46 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
#ifndef BELL202ENCODER_H
#define BELL202ENCODER_H
class BELL202encoder
{
private:
bool state;
double phase;
public:
int baud;
int sampleRate;
unsigned int Freq[2];
double angularSpeed[2];
BELL202encoder()
{
state=0;
phase=0;
baud=600;
sampleRate=48000;
Freq[0]=2200;
Freq[1]=1200;
angularSpeed[0] = 1.0/((double)sampleRate/Freq[0]);
angularSpeed[1] = 1.0/((double)sampleRate/Freq[1]);
}
unsigned int generate(short *buff, bool value)
{
if ( !value ) state^=1;
unsigned int sampleSize = sampleRate/baud;
for (int i=0; i<sampleSize; i++)
{
// buff[i] = sin( 2.0*M_PI*Freq[state] * (i) / (float) sampleRate )*32767.0;
buff[i] = sin( 2.0*M_PI* (i*angularSpeed[state] + phase) )*32767.0;
}
phase += (double)sampleSize*angularSpeed[state];
phase -= (int) phase;
// cout<<"DEBUG PHASE: "<<phase<<"|"<<(sampleSize-1)*angularSpeed[state]<<endl<<flush;
// for (int i=0; i<sampleSize/10; i++)
// {
// buff[i] *= (double) i/(sampleSize/10.0);
// buff[sampleSize-i-1] *= (double) i/(sampleSize/10.0);
// }
return sampleSize;
}
unsigned int encode(char *data, unsigned int length, short *result)
{
unsigned int offset=0;
for (unsigned int i=0; i<length; i++)
{
// cout<<(unsigned int) byte%256<<endl;
for (int j=7; j>=0; j--)
{
offset+=generate( &result[offset], data[i]&(1<<j) );
// cout<<"DEBUG ENCODING: "<<(bool) (data[i]&(1<<j)) <<endl<<flush;
}
}
state=0;
return offset;
}
};
#endif