forked from GooyaBoard/gooya-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGooya.cpp
executable file
·384 lines (334 loc) · 10.9 KB
/
Gooya.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// Created by Evixar Inc.
// Copyright © 2018 Evixar Inc. All rights reserved.
//
#include "Gooya.h"
#include <stdio.h>
#include <soc/io_mux_reg.h>
#include <math.h>
//recorder
void GooyaRecorder::_inputcallback(int16_t* input, int8_t chs, int32_t frames, void* param)
{
GooyaRecorder* _this=(GooyaRecorder*)param;
_this->inputcallback(input,chs,frames);
}
void GooyaRecorder::inputcallback(int16_t* input, int8_t chs, int32_t frames)
{
int16_t* buff=&signal[pos*DMA_AUDIO_FRAMES*AUDIO_CHANNELS];
for (int i=0; i<DMA_AUDIO_FRAMES; i++){
for(int ch=0;ch<chs;ch++){
buff[chs*i] = input[chs*i+ch];
}
}
pos = (pos+1)%GOOYA_RECORD_BUFFER_COUNT;
buff=&signal[pos*DMA_AUDIO_FRAMES*AUDIO_CHANNELS];
if(xQueueSend(queue, &buff, 0)==errQUEUE_FULL)
Serial.println("over flow\n");
//else
// Serial.println("usual\n");
}
void GooyaRecorder::_recorder_task_function(void* param)
{
GooyaRecorder* _this=(GooyaRecorder*)param;
_this->recorder_task_function();
}
void GooyaRecorder::recorder_task_function()
{
int16_t* buff=0;
std::vector<int16_t> buff16;
std::vector<float> buff32;
if(!float32 && !stereo){
buff16.resize(DMA_AUDIO_FRAMES);
}
if(!float32 && stereo){
//do nothing
}
if(float32 && !stereo){
buff32.resize(DMA_AUDIO_FRAMES);
}
if(float32 && stereo){
buff32.resize(DMA_AUDIO_FRAMES*AUDIO_CHANNELS);
}
while(running)
{
if(xQueueReceive(queue, &buff, 0) && buff!=0){
if(!float32 && !stereo){
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
//record only left channel
buff16[i]=buff[AUDIO_CHANNELS*i];
}
file->write((const uint8_t*)buff16.data(),DMA_AUDIO_FRAMES*sizeof(int16_t));
}
if(!float32 && stereo){
file->write((const uint8_t*)buff,DMA_AUDIO_FRAMES*AUDIO_CHANNELS*sizeof(int16_t));
}
if(float32 && !stereo){
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
//record only left channel
buff32[i]=buff[AUDIO_CHANNELS*i]/32768.f;
}
file->write((const uint8_t*)buff32.data(),DMA_AUDIO_FRAMES*sizeof(int16_t));
}
if(float32 && stereo){
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
for(int ch=0;ch<AUDIO_CHANNELS;ch++){
buff32[AUDIO_CHANNELS*i+ch]=buff[AUDIO_CHANNELS*i+ch]/32768.f;
}
}
file->write((const uint8_t*)buff32.data(),DMA_AUDIO_FRAMES*AUDIO_CHANNELS*sizeof(float));
}
//file->flush();
//Serial.println("write\n");
}
else
{
//Serial.println("under flow\n");
vTaskDelay(1);
}
}
vTaskDelete(NULL);
}
GooyaRecorder::GooyaRecorder()
{
file=nullptr;
pos=0;
chunk_count=0;
queue=xQueueCreate(GOOYA_RECORD_BUFFER_COUNT,sizeof(int16_t*));
}
void GooyaRecorder::start(int devid, int sample_rate, File& file, bool _stereo, bool _float32)
{
codec.initialize((gpio_num_t)I2C_SDIN, (gpio_num_t)I2C_SCLK, (gpio_num_t)I2C_ADR);
codec.start();
if(devid==GOOYARECORDER_MICIN)
codec.setup(sample_rate,true,true);
else if(devid==GOOYARECORDER_LINEIN)
codec.setup(sample_rate,false,false);
i2s.initialize(sample_rate,GOOYA_CPU,GooyaRecorder::_inputcallback,nullptr,this);
this->file=&file;
pos=0;
stereo=_stereo;
float32=_float32;
running=true;
xTaskCreatePinnedToCore((TaskFunction_t)&_recorder_task_function, "recorder_task", 1024*2, this, 6, &recorder_task, GOOYA_CPU);
i2s.start();
}
void GooyaRecorder::stop()
{
running=false;
recorder_task=NULL;
i2s.stop();
codec.stop();
}
//player
void GooyaPlayer::_outputcallback(int16_t* output, int8_t chs, int32_t frames, void* param)
{
GooyaPlayer* _this=(GooyaPlayer*)param;
_this->outputcallback(output,chs,frames);
}
void GooyaPlayer::outputcallback(int16_t* output, int8_t chs, int32_t frames)
{
int16_t* buff=0;
if(xQueueReceive(queue, &buff, 0) && buff!=0){
for(int i=0;i<frames;i++){
for(int ch=0;ch<chs;ch++){
output[chs*i+ch]=buff[chs*i+ch];
}
}
}
}
void GooyaPlayer::_player_task_function(void* param)
{
GooyaPlayer* _this=(GooyaPlayer*)param;
_this->player_task_function();
}
void GooyaPlayer::player_task_function()
{
int pos=0;
std::vector<int16_t> buff16;
std::vector<float> buff32;
if(!float32 && !stereo){
buff16.resize(DMA_AUDIO_FRAMES);
}
if(!float32 && stereo){
//do nothing
}
if(float32 && !stereo){
buff32.resize(DMA_AUDIO_FRAMES);
}
if(float32 && stereo){
buff32.resize(DMA_AUDIO_FRAMES*AUDIO_CHANNELS);
}
while(running)
{
int16_t* buff=&signal[pos*DMA_AUDIO_FRAMES*AUDIO_CHANNELS];
if(!float32 && !stereo){
//read mono 16bit
file->read((uint8_t*)buff16.data(),DMA_AUDIO_FRAMES*sizeof(int16_t));
//conver to stereo
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
for(int ch=0;ch<AUDIO_CHANNELS;ch++){
buff[AUDIO_CHANNELS*i+ch]=buff16[i];
}
}
}
if(!float32 && stereo){
file->read((uint8_t*)buff,DMA_AUDIO_FRAMES*AUDIO_CHANNELS*sizeof(int16_t));
}
if(float32 && !stereo){
//read mono 32bit float
file->read((uint8_t*)buff32.data(),DMA_AUDIO_FRAMES*sizeof(float));
//conver to stereo
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
for(int ch=0;ch<AUDIO_CHANNELS;ch++){
buff[AUDIO_CHANNELS*i+ch]=buff32[i];
}
}
}
if(float32 && stereo){
//read stereo 32bit float
file->read((uint8_t*)buff32.data(),DMA_AUDIO_FRAMES*AUDIO_CHANNELS*sizeof(float));
//conver to stereo
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
for(int ch=0;ch<AUDIO_CHANNELS;ch++){
buff[AUDIO_CHANNELS*i+ch]=buff32[AUDIO_CHANNELS*i+ch]*32767.f;
}
}
}
pos = (pos+1)%GOOYA_PLAY_BUFFER_COUNT;
while( xQueueSend(queue, &buff, portMAX_DELAY) != pdPASS )
{
vTaskDelay(1);
}
}
vTaskDelete(NULL);
}
GooyaPlayer::GooyaPlayer()
{
file=nullptr;
//signal.resize(DMA_AUDIO_FRAMES*AUDIO_CHANNELS*GOOYA_PLAY_BUFFER_COUNT);
queue=xQueueCreate(GOOYA_PLAY_BUFFER_COUNT,sizeof(int16_t*));
}
void GooyaPlayer::start(int sample_rate, File& file, int headphonelevel, bool _stereo, bool _float32)
{
codec.initialize((gpio_num_t)I2C_SDIN, (gpio_num_t)I2C_SCLK, (gpio_num_t)I2C_ADR);
codec.start();
codec.setup(sample_rate,true,true,headphonelevel);
i2s.initialize(sample_rate,GOOYA_CPU,nullptr,GooyaPlayer::_outputcallback,this);
this->file=&file;
this->file->seek(0);
stereo=_stereo;
float32=_float32;
running=true;
xTaskCreatePinnedToCore((TaskFunction_t)&_player_task_function, "player_task", 1024*2, this, 5, &player_task, GOOYA_CPU);
vTaskDelay(500);//wait for expecting pre-read queue retension
i2s.start();
}
void GooyaPlayer::stop()
{
running=false;
player_task=NULL;
i2s.stop();
codec.stop();
}
// effect
void GooyaEffect::_inputcallback(int16_t* input, int8_t chs, int32_t frames, void* param)
{
GooyaEffect* _this=(GooyaEffect*)param;
_this->inputcallback(input,chs,frames);
}
void GooyaEffect::inputcallback(int16_t* input, int8_t chs, int32_t frames)
{
int16_t* buff=&signal[pos*DMA_AUDIO_FRAMES*AUDIO_CHANNELS];
for (int i=0; i<frames; i++) {
for(int ch=0;ch<chs;ch++){
buff[chs*i+ch] = input[chs*i+ch];
}
}
pos = (pos+1)%GOOYA_RECORD_BUFFER_COUNT;
buff=&signal[pos*DMA_AUDIO_FRAMES*AUDIO_CHANNELS];
if(xQueueSend(inputqueue, &buff, 0)==errQUEUE_FULL)
Serial.println("over flow\n");
}
void GooyaEffect::_outputcallback(int16_t* output, int8_t chs, int32_t frames, void* param)
{
GooyaEffect* _this=(GooyaEffect*)param;
_this->outputcallback(output,chs,frames);
}
void GooyaEffect::outputcallback(int16_t* output, int8_t chs, int32_t frames)
{
int16_t* buff=0;
if(xQueueReceive(outputqueue, &buff, 0) && buff!=0){
for(int i=0;i<frames;i++){
for(int ch=0;ch<chs;ch++){
output[chs*i+ch]=buff[chs*i+ch];
}
}
}
}
void GooyaEffect::_effect_task_function(void* param)
{
GooyaEffect* _this=(GooyaEffect*)param;
_this->effect_task_function();
}
void GooyaEffect::effect_task_function()
{
int16_t* buff=0;
float temp[AUDIO_CHANNELS*DMA_AUDIO_FRAMES];
while(running)
{
if(xQueueReceive(inputqueue, &buff, 0) && buff!=0){
//file->write((const uint8_t*)buff,GOOYA_RECORD_CHUNK_COUNT*DMA_AUDIO_FRAMES*AUDIO_CHANNELS*sizeof(int16_t));
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
for(int ch=0;ch<AUDIO_CHANNELS;ch++){
temp[AUDIO_CHANNELS*i+ch]=buff[AUDIO_CHANNELS*i+ch]/32768.f;
}
}
callback((const float*)temp,(float*)temp,AUDIO_CHANNELS,DMA_AUDIO_FRAMES);
for(int i=0;i<DMA_AUDIO_FRAMES;i++){
for(int ch=0;ch<AUDIO_CHANNELS;ch++){
buff[AUDIO_CHANNELS*i+ch]=temp[AUDIO_CHANNELS*i+ch]*32767.f;
}
}
if(xQueueSend(outputqueue, &buff, 0)==errQUEUE_FULL)
Serial.println("over flow\n");
//file->flush();
//Serial.println("write\n");
}
else
{
//Serial.println("under flow\n");
vTaskDelay(1);
}
}
vTaskDelete(NULL);
}
GooyaEffect::GooyaEffect()
{
pos=0;
chunk_count=0;
//signal.resize(DMA_AUDIO_FRAMES*AUDIO_CHANNELS*GOOYA_RECORD_BUFFER_COUNT);
inputqueue=xQueueCreate(GOOYA_RECORD_BUFFER_COUNT,sizeof(int16_t*));
outputqueue=xQueueCreate(GOOYA_RECORD_BUFFER_COUNT,sizeof(int16_t*));
}
void GooyaEffect::start(int devid, int sample_rate, GOOYA_AUDIOCALLBACK _callback, int headphonelevel)
{
codec.initialize((gpio_num_t)I2C_SDIN, (gpio_num_t)I2C_SCLK, (gpio_num_t)I2C_ADR);
codec.start();
if(devid==GOOYARECORDER_MICIN)
codec.setup(sample_rate,true,true,headphonelevel);
else if(devid==GOOYARECORDER_LINEIN)
codec.setup(sample_rate,false,false,headphonelevel);
i2s.initialize(sample_rate,GOOYA_CPU,GooyaEffect::_inputcallback,GooyaEffect::_outputcallback,this);
pos=0;
running=true;
callback=_callback;
xTaskCreatePinnedToCore((TaskFunction_t)&_effect_task_function, "effect_task", 1024*2, this, 6, &effect_task, GOOYA_CPU);
i2s.start();
}
void GooyaEffect::stop()
{
running=false;
effect_task=NULL;
i2s.stop();
codec.stop();
}