/
VarioSynthesiser.cpp
167 lines (135 loc) · 4.34 KB
/
VarioSynthesiser.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
/*
Copyright_License {
XCSoar Glide Computer - http://www.xcsoar.org/
Copyright (C) 2000-2013 The XCSoar Project
A detailed list of copyright holders can be found in the file "AUTHORS".
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
*/
#include "VarioSynthesiser.hpp"
//#include "Math/FastMath.h"
#include "Clamp.hpp"
#include <algorithm>
extern int beep_file;
/**
* The minimum and maximum vario range for the constants below [cm/s].
*/
static constexpr int min_vario = -500, max_vario = 500;
unsigned
VarioSynthesiser::VarioToFrequency(int ivario)
{
return ivario > 0
? (zero_frequency + (unsigned)ivario * (max_frequency - zero_frequency)
/ (unsigned)max_vario)
: (zero_frequency - (unsigned)(ivario * (int)(zero_frequency - min_frequency) / min_vario));
}
void
VarioSynthesiser::SetVario(unsigned sample_rate, fixed vario)
{
mutex.lock();
const int ivario = Clamp((int)(vario * 100), min_vario, max_vario);
if (dead_band_enabled && InDeadBand(ivario)) {
/* inside the "dead band" */
UnsafeSetSilence();
mutex.unlock();
return;
}
/* update the ToneSynthesiser base class */
SetTone(sample_rate, VarioToFrequency(ivario));
if (ivario > 0) {
/* while climbing, the vario sound gets interrupted by silence
periodically */
const unsigned period_ms = sample_rate
* (min_period_ms + (max_vario - ivario)
* (max_period_ms - min_period_ms) / max_vario)
/ 1000;
silence_count = period_ms / 3;
audible_count = period_ms - silence_count;
/* preserve the old "_remaining" values as much as possible, to
avoid chopping off the previous tone */
if (audible_remaining > audible_count)
audible_remaining = audible_count;
if (silence_remaining > silence_count)
silence_remaining = silence_count;
} else {
/* continuous tone while sinking */
audible_count = 1;
silence_count = 0;
}
mutex.unlock();
}
void
VarioSynthesiser::SetSilence()
{
mutex.lock();
UnsafeSetSilence();
mutex.unlock();
}
void
VarioSynthesiser::UnsafeSetSilence()
{
audible_count = 0;
silence_count = 1;
if (audible_remaining > 0)
/* quit the current period as early as possible; the method
Synthesise() will take care for finishing the current sine
wave to avoid clicking noise */
audible_remaining = 1;
silence_remaining = 0;
}
void
VarioSynthesiser::Synthesise(int16_t *buffer, size_t n)
{
mutex.lock();
assert(audible_count > 0 || silence_count > 0);
if (silence_count == 0 || beep_file >= 0) {
/* magic value for "continuous tone" */
ToneSynthesiser::Synthesise(buffer, n);
mutex.unlock();
return;
}
while (n > 0) {
if (audible_remaining > 0) {
/* generate a period of audible tone */
unsigned o = silence_count > 0
? std::min(n, audible_remaining)
: n;
ToneSynthesiser::Synthesise(buffer, o);
buffer += o;
n -= o;
audible_remaining -= o;
if (audible_remaining == 0 && silence_remaining > 0) {
/* finish the current sine wave to avoid clicking noise */
audible_remaining = ToZero();
if (audible_remaining == 0)
/* finished, we can now emit a period of silence */
Restart();
}
} else if (silence_remaining > 0) {
/* generate a period of silence (climbing) */
unsigned o = audible_count > 0
? std::min(n, silence_remaining)
: n;
/* the "silence" PCM sample value is zero */
std::fill(buffer, buffer + o, 0);
buffer += o;
n -= o;
silence_remaining -= o;
} else {
/* period finished, begin next one */
audible_remaining = audible_count;
silence_remaining = silence_count;
}
}
mutex.unlock();
}