forked from pichenettes/avril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_output.h
executable file
·102 lines (87 loc) · 3.06 KB
/
audio_output.h
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
// Copyright 2009 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Audio output. Supports PWM (through a PwmOutput object) and DAC (through a
// Dac object, for example the one defined in mcp492x.h).
#ifndef AVRLIB_AUDIO_OUTPUT_H_
#define AVRLIB_AUDIO_OUTPUT_H_
#include "avrlib/base.h"
#include "avrlib/avrlib.h"
#include "avrlib/ring_buffer.h"
namespace avrlib {
enum UnderrunPolicy {
EMIT_CLICK = 0,
HOLD_SAMPLE = 1
};
template<typename OutputPort,
uint8_t buffer_size_ = 32,
uint8_t block_size = 16,
UnderrunPolicy underrun_policy = HOLD_SAMPLE>
class AudioOutput {
public:
AudioOutput() { }
enum {
buffer_size = buffer_size_,
data_size = OutputPort::data_size,
};
typedef AudioOutput<OutputPort, buffer_size_, block_size, underrun_policy> Me;
typedef typename DataTypeForSize<data_size>::Type Value;
typedef RingBuffer<Me> OutputBuffer;
static inline void Init() {
OutputPort::Init();
}
static inline void Write(Value v) { while (!writable()); Overwrite(v); }
static inline void Overwrite(Value v) { OutputBuffer::Overwrite(v); }
static inline uint8_t writable() { return OutputBuffer::writable(); }
static inline uint8_t writable_block() {
return OutputBuffer::writable() >= block_size;
}
static inline uint8_t NonBlockingWrite(Value v) {
if (!writable()) {
return 0;
}
Overwrite(v);
return 1;
}
static inline void DiscardSample() {
OutputBuffer::ImmediateRead();
}
// Called from data emission interrupt.
static inline void EmitSample() {
if (OutputBuffer::readable()) {
OutputPort::Write(Value(OutputBuffer::ImmediateRead()));
} else {
++num_glitches_;
if (underrun_policy == EMIT_CLICK) {
// Introduces clicks to allow underruns to be easily detected.
OutputPort::Write(0);
}
}
}
static inline uint8_t num_glitches() { return num_glitches_; }
static inline void ResetGlitchCounter() { num_glitches_ = 0; }
private:
static uint8_t num_glitches_;
DISALLOW_COPY_AND_ASSIGN(AudioOutput);
};
/* static */
template<typename OutputPort, uint8_t buffer_size_, uint8_t block_size,
UnderrunPolicy underrun_policy>
uint8_t AudioOutput<OutputPort, buffer_size_, block_size,
underrun_policy>::num_glitches_ = 0;
} // namespace avrlib
#endif // AVRLIB_AUDIO_OUTPUT_H_