|
1
|
#include <pulse/simple.h>
|
|
2
|
#include <pulse/error.h>
|
|
3
|
#include <cmath>
|
|
4
|
#include <thread>
|
|
5
|
#include <atomic>
|
|
6
|
#include <iostream>
|
|
7
|
|
|
8
|
std::atomic<bool> running{true};
|
|
9
|
float gfreq=440;
|
|
10
|
#include <stdint.h>
|
|
11
|
|
|
12
|
#define REAL 0
|
|
13
|
|
|
14
|
typedef unsigned int divisor_t;
|
|
15
|
static_assert( sizeof(divisor_t) == 4 );
|
|
16
|
|
|
17
|
constexpr unsigned int div100( divisor_t x )
|
|
18
|
{
|
|
19
|
// Valid till 102400
|
|
20
|
return( ( ( x + 1 ) * 0xA3D7 ) >> 22 );
|
|
21
|
};
|
|
22
|
|
|
23
|
|
|
24
|
constexpr int div5( divisor_t x )
|
|
25
|
{
|
|
26
|
//return( ( ( x + 1 ) * 0x19999 ) >> 19 );
|
|
27
|
//return( ( ( x + 1 ) * 0xCCCC ) >> 18 );
|
|
28
|
return( ( ( x + 1 ) * 0x6666 ) >> 17 );
|
|
29
|
//return( ( ( x + 1 ) * 0x3333 ) >> 16 );
|
|
30
|
}
|
|
31
|
|
|
32
|
|
|
33
|
constexpr int div4( divisor_t x )
|
|
34
|
{
|
|
35
|
return( x >> 2 );
|
|
36
|
}
|
|
37
|
|
|
38
|
|
|
39
|
constexpr int div400( divisor_t x )
|
|
40
|
{
|
|
41
|
return( div100( div4( x ) ) );
|
|
42
|
}
|
|
43
|
|
|
44
|
constexpr int div7( divisor_t x )
|
|
45
|
{
|
|
46
|
return( ( ( x + 1 ) * 0x9249 ) >> 18 );
|
|
47
|
}
|
|
48
|
|
|
49
|
static_assert( 43690 / 7 == div7( 43690 ) );
|
|
50
|
|
|
51
|
constexpr int mod4( int x )
|
|
52
|
{
|
|
53
|
// return( x - ( div4(x) * 4 ) );
|
|
54
|
return( x % 4 );
|
|
55
|
}
|
|
56
|
|
|
57
|
static_assert( 43690 % 4 == mod4( 43690 ) );
|
|
58
|
|
|
59
|
constexpr int mod7( int x )
|
|
60
|
{
|
|
61
|
// return( x - ( div7(x) * 7 ) );
|
|
62
|
return( x % 7 );
|
|
63
|
}
|
|
64
|
|
|
65
|
static_assert( 43690 % 7 == mod7( 43690 ) );
|
|
66
|
|
|
67
|
constexpr int mod100( unsigned long long int x )
|
|
68
|
{
|
|
69
|
return( x - ( div100(x) * 100 ) );
|
|
70
|
//return( x % 100 );
|
|
71
|
}
|
|
72
|
|
|
73
|
static_assert( 43690 % 100 == mod100( 43690 ) );
|
|
74
|
|
|
75
|
constexpr unsigned int mod400( unsigned long long int x )
|
|
76
|
{
|
|
77
|
//return( x - ( div400(x) * 400 ) );
|
|
78
|
return( x % 400 );
|
|
79
|
}
|
|
80
|
|
|
81
|
static_assert( 43690 % 400 == mod400( 43690 ) );
|
|
82
|
|
|
83
|
// 0 = Sonntag, 1 = Montag, ... 6 = Samstag
|
|
84
|
constexpr int weekday(int y, int m, int d)
|
|
85
|
{
|
|
86
|
if (m < 3) {
|
|
87
|
m += 12;
|
|
88
|
y -= 1;
|
|
89
|
}
|
|
90
|
#if REAL || 0
|
|
91
|
int K = y % 100;
|
|
92
|
int J = y / 100;
|
|
93
|
int h = (d + (13 * (m + 1)) / 5 + K + K/4 + J/4 + 5*J) % 7;
|
|
94
|
|
|
95
|
// Zeller: 0=Samstag,1=Sonntag,... → 0=Sonntag
|
|
96
|
return (h + 6) % 7;
|
|
97
|
#else
|
|
98
|
int J = div100( y );
|
|
99
|
int K = y - ( J * 100 );
|
|
100
|
int h = mod7(d + div5(13 * (m + 1) ) + K + div4(K) + div4(J) + 5*J );
|
|
101
|
|
|
102
|
// Zeller: 0=Samstag,1=Sonntag,... → 0=Sonntag
|
|
103
|
return mod7( h + 6 );
|
|
104
|
#endif
|
|
105
|
}
|
|
106
|
|
|
107
|
constexpr static int last_sunday_of_month(int year, int month)
|
|
108
|
{
|
|
109
|
constexpr const uint8_t mdays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
|
|
110
|
int days = mdays[month-1];
|
|
111
|
|
|
112
|
// Schaltjahr
|
|
113
|
#if REAL
|
|
114
|
if (month == 2 && ((year%4==0 && year%100!=0) || (year%400==0)))
|
|
115
|
days = 29;
|
|
116
|
#else
|
|
117
|
if (month == 2 && (( mod4(year)==0 && mod100(year)!=0) || ( mod400( year ) == 0 ) ) )
|
|
118
|
days = 29;
|
|
119
|
#endif
|
|
120
|
|
|
121
|
for (int d = days; d >= days-6; --d) {
|
|
122
|
if (weekday(year, month, d) == 0)
|
|
123
|
return d;
|
|
124
|
}
|
|
125
|
return days; // Fallback
|
|
126
|
}
|
|
127
|
|
|
128
|
// 1 = Sommerzeit, 0 = Winterzeit (Europa)
|
|
129
|
constexpr int is_dst_europe(int year, int month, int day, int hour)
|
|
130
|
{
|
|
131
|
int start = last_sunday_of_month(year, 3); // letzter Sonntag im März
|
|
132
|
int end = last_sunday_of_month(year, 10); // letzter Sonntag im Oktober
|
|
133
|
|
|
134
|
if (month < 3) return 0;
|
|
135
|
if (month > 10) return 0;
|
|
136
|
if (month > 3 && month < 10) return 1;
|
|
137
|
|
|
138
|
if (month == 3) {
|
|
139
|
if (day < start) return 0;
|
|
140
|
if (day > start) return 1;
|
|
141
|
// Wechseltag: ab 02:00 Sommerzeit
|
|
142
|
return (hour >= 2);
|
|
143
|
}
|
|
144
|
|
|
145
|
if (month == 10) {
|
|
146
|
if (day < end) return 1;
|
|
147
|
if (day > end) return 0;
|
|
148
|
// Wechseltag: bis 03:00 Sommerzeit
|
|
149
|
return (hour < 3);
|
|
150
|
}
|
|
151
|
|
|
152
|
return 0;
|
|
153
|
}
|
|
154
|
|
|
155
|
// https://www.rechner.club/zeitumstellung/ende-sommerzeit-berechnen
|
|
156
|
// Geaendert 1996
|
|
157
|
static_assert( is_dst_europe( 1996, 3, 31, 0 ) == 0 );
|
|
158
|
static_assert( is_dst_europe( 1996, 3, 31, 1 ) == 0 );
|
|
159
|
static_assert( is_dst_europe( 1996, 3, 31, 2 ) == 1 );
|
|
160
|
static_assert( is_dst_europe( 1996, 3, 31, 3 ) == 1 );
|
|
161
|
static_assert( is_dst_europe( 1996, 10, 27, 2 ) == 1 );
|
|
162
|
static_assert( is_dst_europe( 1996, 10, 27, 3 ) == 0 );
|
|
163
|
static_assert( is_dst_europe( 1996, 10, 27, 4 ) == 0 );
|
|
164
|
|
|
165
|
static_assert( is_dst_europe( 2000, 3, 26, 0 ) == 0 );
|
|
166
|
static_assert( is_dst_europe( 2000, 3, 26, 1 ) == 0 );
|
|
167
|
static_assert( is_dst_europe( 2000, 3, 26, 2 ) == 1 );
|
|
168
|
static_assert( is_dst_europe( 2000, 3, 26, 3 ) == 1 );
|
|
169
|
static_assert( is_dst_europe( 2000, 3, 29, 4 ) == 1 );
|
|
170
|
static_assert( is_dst_europe( 2000, 10, 29, 2 ) == 1 );
|
|
171
|
static_assert( is_dst_europe( 2000, 10, 29, 3 ) == 0 );
|
|
172
|
static_assert( is_dst_europe( 2000, 10, 29, 4 ) == 0 );
|
|
173
|
|
|
174
|
static_assert( is_dst_europe( 2026, 3, 29, 0 ) == 0 );
|
|
175
|
static_assert( is_dst_europe( 2026, 3, 29, 1 ) == 0 );
|
|
176
|
static_assert( is_dst_europe( 2026, 3, 29, 2 ) == 1 );
|
|
177
|
static_assert( is_dst_europe( 2026, 3, 29, 3 ) == 1 );
|
|
178
|
static_assert( is_dst_europe( 2026, 3, 29, 4 ) == 1 );
|
|
179
|
static_assert( is_dst_europe( 2026, 3, 30, 4 ) == 1 );
|
|
180
|
static_assert( is_dst_europe( 2026, 10, 25, 2 ) == 1 );
|
|
181
|
static_assert( is_dst_europe( 2026, 10, 25, 3 ) == 0 );
|
|
182
|
static_assert( is_dst_europe( 2026, 10, 25, 4 ) == 0 );
|
|
183
|
static_assert( is_dst_europe( 2026, 10, 25, 5 ) == 0 );
|
|
184
|
|
|
185
|
static_assert( is_dst_europe( 2226, 3, 26, 0 ) == 0 );
|
|
186
|
static_assert( is_dst_europe( 2226, 3, 26, 1 ) == 0 );
|
|
187
|
static_assert( is_dst_europe( 2226, 3, 26, 2 ) == 1 );
|
|
188
|
static_assert( is_dst_europe( 2226, 3, 26, 3 ) == 1 );
|
|
189
|
static_assert( is_dst_europe( 2226, 3, 30, 4 ) == 1 );
|
|
190
|
static_assert( is_dst_europe( 2226, 10, 29, 2 ) == 1 );
|
|
191
|
static_assert( is_dst_europe( 2226, 10, 29, 3 ) == 0 );
|
|
192
|
static_assert( is_dst_europe( 2226, 10, 29, 4 ) == 0 );
|
|
193
|
|
|
194
|
static_assert( is_dst_europe( 3226, 3, 29, 0 ) == 0 );
|
|
195
|
static_assert( is_dst_europe( 3226, 3, 29, 1 ) == 0 );
|
|
196
|
static_assert( is_dst_europe( 3226, 3, 29, 2 ) == 1 );
|
|
197
|
static_assert( is_dst_europe( 3226, 3, 29, 3 ) == 1 );
|
|
198
|
static_assert( is_dst_europe( 3226, 3, 30, 4 ) == 1 );
|
|
199
|
static_assert( is_dst_europe( 3226, 10, 25, 2 ) == 1 );
|
|
200
|
static_assert( is_dst_europe( 3226, 10, 25, 3 ) == 0 );
|
|
201
|
static_assert( is_dst_europe( 3226, 10, 25, 4 ) == 0 );
|
|
202
|
|
|
203
|
typedef unsigned long long int ui;
|
|
204
|
|
|
205
|
int main() {
|
|
206
|
std::cout << "Starting tone in background...\n";
|
|
207
|
|
|
208
|
//printf("12345/5=%d\n", div5(-12345));
|
|
209
|
//return(0);
|
|
210
|
|
|
211
|
// 102400-1
|
|
212
|
// -(1<<16)
|
|
213
|
for(int i1=0; i1< (1<<16) + 4; i1++)
|
|
214
|
{
|
|
215
|
if( (i1/100) != div100(i1) )
|
|
216
|
{
|
|
217
|
printf( "Error 100: %d: %d; delta %d\n", i1, div100(i1), (i1/100) - div100(i1) );
|
|
218
|
}
|
|
219
|
if( (i1/5) != div5(i1) )
|
|
220
|
{
|
|
221
|
printf( "Error 5: %d: %d vs. %d; delta %d\n", i1, i1/5, div5(i1), (i1/5) - div5(i1) );
|
|
222
|
}
|
|
223
|
if( (i1/7) != div7(i1) )
|
|
224
|
{
|
|
225
|
printf( "Error 7: %d: %d vs. %d; delta %d\n", i1, i1/7, div7(i1), (i1/7) - div7(i1) );
|
|
226
|
}
|
|
227
|
|
|
228
|
if( (i1/400) != div400(i1) )
|
|
229
|
{
|
|
230
|
printf( "Error 400: %d: %d vs. %d; delta %d\n", i1, i1/400, div400(i1), (i1/400) - div400(i1) );
|
|
231
|
}
|
|
232
|
}
|
|
233
|
|
|
234
|
|
|
235
|
printf( "At least < 100pb\n" );
|
|
236
|
// find nearest
|
|
237
|
//for(ui i1=0; i1< 0xffffffff; i1++)
|
|
238
|
//ui i1=0xA3D7;
|
|
239
|
//ui divider=100ull;
|
|
240
|
ui divider=5ull;
|
|
241
|
|
|
242
|
for(ui i1=0; i1< 32; i1++)
|
|
243
|
{
|
|
244
|
ui m=(1ull << i1)/divider;
|
|
245
|
//m++;
|
|
246
|
ui r = ( 10240 * m ) >> i1;
|
|
247
|
ui d = (1ull << i1)-( m * divider);
|
|
248
|
ui pb = (d*1000ull*1000ull);
|
|
249
|
|
|
250
|
if(m)
|
|
251
|
pb/=m;
|
|
252
|
else
|
|
253
|
pb=-1;
|
|
254
|
|
|
255
|
//if( r == (10240/divider) )
|
|
256
|
{
|
|
257
|
printf("m=0x%llX; s=%d; r=%d; max=%d; fehler=%dpb\n", m, i1, r
|
|
258
|
, 0xFFFFFFFF / ( m ? m : 1 )
|
|
259
|
, pb);
|
|
260
|
// "102400/100=%d\n", r);
|
|
261
|
}
|
|
262
|
}
|
|
263
|
|
|
264
|
return(0);
|
|
265
|
}
|