-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio.cpp
184 lines (159 loc) · 4.34 KB
/
audio.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
/*
* author : Shuichi TAKANO
* since : Fri Aug 23 2019 3:6:13
*/
#include "audio.h"
#include <i2s.h>
#include <fpioa.h>
#include <algorithm>
#include <assert.h>
namespace
{
constexpr int SIZE = 4096;
constexpr int UNIT_SIZE = SIZE / 8;
struct Buffer
{
alignas(4) int16_t ring_[SIZE];
volatile int wp_ = 0;
volatile int rp_ = 0;
public:
void clear()
{
memset(ring_, 0, sizeof(ring_));
wp_ = UNIT_SIZE;
rp_ = 0;
}
int getFree() const
{
if (wp_ >= rp_)
{
return SIZE - wp_ + rp_ - 1;
}
else
{
return rp_ - wp_ - 1;
}
}
int getSize() const
{
if (wp_ >= rp_)
{
return wp_ - rp_;
}
else
{
return SIZE - rp_ + wp_;
}
}
void write(const int16_t *p, int size)
{
auto s = std::min(size, SIZE - wp_);
memcpy(&ring_[wp_], p, s * sizeof(int16_t));
p += s;
s = size - s;
memcpy(&ring_[0], p, s * sizeof(int16_t));
wp_ = (wp_ + size) & (SIZE - 1);
}
void writeMonoToStereo(const int16_t *p, int size)
{
assert((wp_ & 1) == 0);
auto copy = [](uint32_t *dst, const int16_t *&src, int n) {
while (n)
{
auto v = (uint16_t)*src++;
*dst++ = v | (v << 16);
--n;
}
};
size <<= 1;
auto s = std::min(size, SIZE - wp_);
copy((uint32_t *)&ring_[wp_], p, s >> 1);
s = size - s;
copy((uint32_t *)&ring_[0], p, s >> 1);
wp_ = (wp_ + size) & (SIZE - 1);
}
const int16_t *getReadPointer() const
{
return &ring_[rp_];
}
void advanceReadPointer(int size)
{
rp_ = (rp_ + size) & (SIZE - 1);
}
};
Buffer buffer_;
volatile bool active_ = false;
void play()
{
auto size = buffer_.getSize();
if (size >= UNIT_SIZE)
{
i2s_data_t data;
data.tx_channel = DMAC_CHANNEL0;
data.tx_buf = (uint32_t *)buffer_.getReadPointer();
data.tx_len = UNIT_SIZE / 2;
data.transfer_mode = I2S_SEND;
plic_interrupt_t irq;
irq.callback = [](void *) -> int {
buffer_.advanceReadPointer(UNIT_SIZE);
play();
return 0;
};
irq.priority = 2;
active_ = true;
// printf("dma %p, %zd\n", data.tx_buf, data.tx_len);
i2s_handle_data_dma(I2S_DEVICE_0, data, &irq);
}
else
{
active_ = false;
}
}
} // namespace
int getAudioBufferFree()
{
return buffer_.getFree() >> 1;
}
void writeAudioData(const int16_t *p, int count)
{
buffer_.writeMonoToStereo(p, count);
if (!active_)
{
play();
}
}
void initAudio(uint32_t freq, uint8_t amigo)
{
if (amigo)
{
fpioa_set_function(13, FUNC_I2S0_MCLK);
fpioa_set_function(21, FUNC_I2S0_SCLK);
fpioa_set_function(18, FUNC_I2S0_WS);
fpioa_set_function(35, FUNC_I2S0_IN_D0);
fpioa_set_function(34, FUNC_I2S0_OUT_D2);
}
else
{
fpioa_set_function(34, FUNC_I2S0_OUT_D0);
fpioa_set_function(35, FUNC_I2S0_SCLK);
fpioa_set_function(33, FUNC_I2S0_WS);
}
i2s_init(I2S_DEVICE_0, I2S_TRANSMITTER, 3);
i2s_tx_channel_config(I2S_DEVICE_0,
amigo ? I2S_CHANNEL_2 : I2S_CHANNEL_0,
RESOLUTION_16_BIT, SCLK_CYCLES_32,
TRIGGER_LEVEL_4,
amigo ? STANDARD_MODE : RIGHT_JUSTIFYING_MODE);
if (amigo)
{
i2s_rx_channel_config(I2S_DEVICE_0,
I2S_CHANNEL_0,
RESOLUTION_16_BIT, SCLK_CYCLES_32,
TRIGGER_LEVEL_4,
STANDARD_MODE);
}
i2s_set_sample_rate(I2S_DEVICE_0, freq);
i2s_set_dma_divide_16(I2S_DEVICE_0, true);
buffer_.clear();
play();
}