Skip to content

Commit 183bb0d

Browse files
committed
Whitespace + audio cli for WAV files
1 parent 2383849 commit 183bb0d

File tree

4 files changed

+128
-36
lines changed

4 files changed

+128
-36
lines changed

BoxCLI.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ void BoxCLI::begin() {
6161
cmdSay.addFlagArg("sing");
6262
cmdSay.addFlagArg("p/hoentic");
6363

64-
cmdPlay = cli.addCmd("play");
64+
cmdAudio = cli.addCmd("audio");
65+
cmdAudio.setDescription(" Play/Pause audio files");
66+
cmdAudio.addFlagArg("p/lay,pause");
67+
cmdAudio.addArg("f/ile/name", "");
6568
}
6669

6770
void BoxCLI::loop() {
@@ -98,8 +101,8 @@ void BoxCLI::parse() {
98101
execI2S();
99102
} else if (lastCmd == cmdSay) {
100103
execSay();
101-
} else if (lastCmd == cmdPlay) {
102-
execPlay();
104+
} else if (lastCmd == cmdAudio) {
105+
execAudio();
103106
}
104107
}
105108

@@ -374,8 +377,16 @@ void BoxCLI::execSay() {
374377
}
375378
}
376379

377-
void BoxCLI::execPlay() {
378-
Box.boxDAC.opusTest();
380+
void BoxCLI::execAudio() {
381+
Command c = lastCmd;
382+
383+
String filenameStr = c.getArg("file").getValue();
384+
385+
if (c.getArg("file").isSet() && filenameStr != "") {
386+
Box.boxDAC.playFile(filenameStr.c_str());
387+
} else if (c.getArg("play").isSet()) {
388+
Box.boxDAC.audioPlaying = !Box.boxDAC.audioPlaying;
389+
}
379390
}
380391

381392
unsigned long BoxCLI::parseNumber(String numberString) {

BoxCLI.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class BoxCLI : public EnhancedThread {
2424
Command cmdLoad;
2525
Command cmdI2S;
2626
Command cmdSay;
27-
Command cmdPlay;
27+
Command cmdAudio;
2828

2929
void parse();
3030
unsigned long
@@ -40,7 +40,7 @@ class BoxCLI : public EnhancedThread {
4040
execLoad(),
4141
execI2S(),
4242
execSay(),
43-
execPlay();
43+
execAudio();
4444
};
4545

4646
#endif

BoxDAC.cpp

+93-27
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void BoxDAC::begin() {
6060

6161
UDMAInit();
6262
UDMAChannelSelect(UDMA_CH5_I2S_TX, NULL);
63-
63+
6464
writeBuffer = audioBuffer.getBuffer(BoxAudioBufferTriple::BufferType::WRITE);
6565
writeBuffer->state = BoxAudioBufferTriple::BufferState::WRITING;
6666
audioBuffer.logState(writeBuffer);
@@ -91,7 +91,7 @@ void BoxDAC::begin() {
9191
(void *)I2S_TX_DMA_PORT,
9292
UDMA_DST_INC_NONE
9393
);
94-
94+
9595

9696
Log.info("I2S");
9797
MAP_I2SIntEnable(I2S_BASE, I2S_INT_XDATA);
@@ -141,24 +141,96 @@ void BoxDAC::opusTest() {
141141
}
142142
Log.info("Mp3 finished");
143143
*/
144+
//AudioGeneratorTonie *opus;
145+
AudioFileSourceFatFs *file;
146+
147+
//file = new AudioFileSourceFatFs("/gs-16b-2c-44100hz.opus");
148+
/*file = new AudioFileSourceFatFs("/CONTENT/6977960C/500304E0");
149+
opus = new AudioGeneratorTonie();
150+
opus->begin(file, audioOutput);
151+
while (opus->isRunning()) {
152+
if (!opus->loop())
153+
opus->stop();
154+
}
155+
free(file);
156+
free(opus);
157+
Log.info("Opus done");
158+
*/
159+
160+
playFile("/piano2.wav");
161+
//file = new AudioFileSourceFatFs("/LRMonoPhase4.wav");
162+
//file = new AudioFileSourceFatFs("/organfinale.wav");
163+
//file = new AudioFileSourceFatFs("/piano2.wav");
164+
144165
/*
145166
OpusDecoder* decoder;
146167
int error;
147168
int channels = 2;
148169
170+
Box.boxPower.feedSleepTimer();
171+
Log.info("Needed heap %ib", opus_decoder_get_size(channels));
149172
decoder = opus_decoder_create(16000, channels, &error);
150173
if (error != OPUS_OK) {
151174
Log.error("Could not create OPUS Decoder error=%i", error);
152175
return;
153176
}
154-
177+
Box.boxPower.feedSleepTimer();
155178
opus_decoder_destroy(decoder);
156179
157180
//opus_decode(decoder, data, len, pcmout, frameSize, 0);*/
158181
}
159182

160-
void BoxDAC::loop() {
161-
generateZeroAudio(25);
183+
void BoxDAC::loop() {
184+
loop(50);
185+
}
186+
void BoxDAC::loop(uint16_t timeoutMs) {
187+
if (audioPlaying) {
188+
if (!audioGenerator || !audioSource) {
189+
audioPlaying = false;
190+
return;
191+
}
192+
193+
BoxTimer timeout;
194+
timeout.setTimer(timeoutMs);
195+
196+
while (timeout.isRunning() && audioGenerator->isRunning()) {
197+
if (!audioGenerator->loop())
198+
audioGenerator->stop();
199+
timeout.tick();
200+
}
201+
if (!audioGenerator->isRunning())
202+
audioPlaying = false;
203+
} else {
204+
generateZeroAudio(timeoutMs);
205+
}
206+
}
207+
208+
bool BoxDAC::playFile(const char* path) {
209+
Log.info("Start playing file %s...", path);
210+
211+
if (audioGenerator && audioGenerator->isRunning()) {
212+
audioGenerator->stop();
213+
free(audioGenerator);
214+
}
215+
if (audioSource && audioSource->isOpen()) {
216+
audioSource->close();
217+
free(audioSource);
218+
}
219+
220+
audioPlaying = false;
221+
return _playWAV(path);
222+
}
223+
bool BoxDAC::_playWAV(const char* path) {
224+
audioGenerator = new AudioGeneratorWAV();
225+
audioSource = new AudioFileSourceFatFs(path);
226+
227+
if (!audioGenerator->begin(audioSource, audioOutput)) {
228+
Log.error("Could not play wav?!");
229+
return false;
230+
}
231+
Log.info("WAV file loaded...");
232+
audioPlaying = true;
233+
return true;
162234
}
163235

164236
void BoxDAC::generateFrequency(uint32_t frequency, uint16_t timeoutMs) {
@@ -167,15 +239,15 @@ void BoxDAC::generateFrequency(uint32_t frequency, uint16_t timeoutMs) {
167239
timeout.setTimer(timeoutMs);
168240

169241
while (timeout.isRunning()) {
170-
while(writeBuffer->position<writeBuffer->size && timeout.isRunning()) {
242+
while (writeBuffer->position<writeBuffer->size && timeout.isRunning()) {
171243
if (count % halfWavelength == 0)
172244
sample = -1 * sample; // invert the sample every half wavelength count multiple to generate square wave
173-
if (count % (2*halfWavelength) == 0)
245+
if (count % (2*halfWavelength) == 0)
174246
count = 0;
175-
247+
176248
writeBuffer->buffer[writeBuffer->position++] = sample;
177249
writeBuffer->buffer[writeBuffer->position++] = sample;
178-
250+
179251
count++;
180252
i2sElmCount++;
181253
timeout.tick();
@@ -196,7 +268,7 @@ void BoxDAC::generateZeroAudio(uint16_t timeoutMs) {
196268
timeout.setTimer(timeoutMs);
197269

198270
while (timeout.isRunning()) {
199-
while(writeBuffer->position<writeBuffer->size) {
271+
while (writeBuffer->position<writeBuffer->size) {
200272
writeBuffer->buffer[writeBuffer->position++] = 0;
201273
writeBuffer->buffer[writeBuffer->position++] = 0;
202274
}
@@ -216,7 +288,7 @@ void BoxDAC::dmaPingPingComplete() {
216288
MAP_I2SIntClear(I2S_BASE, I2S_INT_XDMA);
217289

218290
unsigned long intStatus = MAP_uDMAIntStatus();
219-
291+
220292
dmaIRQcount++;
221293
if (intStatus & 0x20) { //TX IRQ I2S_INT_XDMA?
222294
unsigned long channelModePri = MAP_uDMAChannelModeGet(UDMA_CH5_I2S_TX | UDMA_PRI_SELECT);
@@ -335,11 +407,11 @@ void BoxDAC::beepRaw(uint16_t sin, uint16_t cos, uint32_t length, uint8_t volume
335407

336408
send(ADDR_P0_SERIAL::BEEP_R_GEN, 0x80);
337409
send(ADDR_P0_SERIAL::BEEP_L_GEN, 0x80|(volume&0x3F)); //enable beep generator with right channel volume,
338-
410+
339411
//send(ADDR_P0_SERIAL::DAC_NDAC_VAL, 0x84); //power up NDAC divider - Page 41 (but makes glitches?!)
340412

341413
send(ADDR_P0_SERIAL::DAC_VOL_CTRL, 0x00); //unmute DACs optional
342-
414+
343415
}
344416
void BoxDAC::beepMidi(uint8_t midiId, uint16_t lengthMs, bool async) {
345417
//TODO Check boundaries!
@@ -371,7 +443,7 @@ void BoxDAC::beepMidi(uint8_t midiId, uint16_t lengthMs, bool async) {
371443
int32_t samples_opt = samplerate*(cycles)*100/freq/2;
372444

373445
//int32_t samples = lengthMs * samplerate / 1000; //check length
374-
//Log.info("samplerate=%i, lengthMs=%i, freq=%i, sin=%i, cos=%i", samplerate, lengthMs, freq, sin, cos);
446+
Log.info("samplerate=%i, lengthMs=%i, freq=%i, sin=%i, cos=%i", samplerate, lengthMs, freq, sin, cos);
375447
//Log.info("samples=%i, cycles=%i, samples_opt=%i", samples, cycles, samples_opt);
376448

377449
beepRaw(sin, cos, samples_opt);
@@ -393,13 +465,13 @@ void BoxDAC::samSay(const char *text, enum ESP8266SAM::SAMVoice voice, uint8_t s
393465
ESP8266SAM* sam = new ESP8266SAM();
394466

395467
sam->SetVoice(voice);
396-
if (speed > 0)
468+
if (speed > 0)
397469
sam->SetSpeed(speed);
398-
if (pitch > 0)
470+
if (pitch > 0)
399471
sam->SetSpeed(pitch);
400-
if (throat > 0)
472+
if (throat > 0)
401473
sam->SetSpeed(throat);
402-
if (mouth > 0)
474+
if (mouth > 0)
403475
sam->SetSpeed(mouth);
404476
sam->SetSingMode(sing);
405477
sam->SetPhonetic(phoentic);
@@ -529,7 +601,7 @@ void BoxDAC::initDACI2C() {
529601
//Extracted from logic analyzer capture of box
530602
send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
531603
send(ADDR_P0_SERIAL::SOFTWARE_RESET, 0x01); //Self-clearing software reset for control register
532-
604+
533605
send(ADDR_P0_SERIAL::CLOCKGEN_MUX, 0x07); //0000:reserved, 01:PLL_CLKIN=BCLK, 11:CODEC_CLKIN=PLL_CLK
534606
send(ADDR_P0_SERIAL::PLL_J_VAL, 0x20); //00:reserved, 100000:PLL multiplier J=32 (0x20)
535607
send(ADDR_P0_SERIAL::PLL_D_VAL_MSB, 0x00); //00:reserved, 000000:fraktional multiplier D-value = 0
@@ -541,7 +613,7 @@ void BoxDAC::initDACI2C() {
541613
send(ADDR_P0_SERIAL::DAC_DOSR_VAL_LSB, 0x00); //00000000:DAC OSR LSB
542614

543615
delay(10); //w PLL Start-Up
544-
616+
545617
send(ADDR_P0_SERIAL::CODEC_IF_CTRL1, 0x00); //00:Codec IF=I2S, 00: Codec IF WL=16 bits, 0:BCLK=Input, 0:WCKL=Output, 0:reserved // w IF statt INT
546618
send(ADDR_P0_SERIAL::DAC_PROC_BLOCK_SEL, 0x19); //000:reserved, 11001:DAC signal-processing block PRB_P25
547619

@@ -560,12 +632,9 @@ void BoxDAC::initDACI2C() {
560632
send(ADDR_P0_SERIAL::INT1_CTRL_REG, 0x80); //1:Headset-insertion detect IRQ INT1, 0:Button-press detect, ...., 0=INT1 is only one pulse 2ms
561633
send(ADDR_P0_SERIAL::GPIO1_INOUT_CTRL, 0x14); //XX:reserved, 0101:GPIO1=INT1 output, X=GPIO1 input buffer value, GPIO1 Output=X
562634

563-
564-
565635
//send(0x2E); Excel 161
566636

567637
// PAUSE 0,2s
568-
569638
//read 0x18 addr
570639

571640

@@ -585,8 +654,7 @@ void BoxDAC::initDACI2C() {
585654
send(ADDR_P1_DAC_OUT::SPK_DRIVER, 0x00); // SPK driver is muted
586655

587656
send(ADDR_P1_DAC_OUT::SPK_DRIVER, 0x04); // an TEST gehört hier nicht hin
588-
589-
657+
590658
//PAUSE 50ms
591659
delay(50); //w Ramp
592660

@@ -599,7 +667,6 @@ void BoxDAC::initDACI2C() {
599667
//PAUSE 50ms
600668
delay(50); //w Ramp
601669

602-
603670
send(ADDR_P1_DAC_OUT::HPL_DRIVER, 0x06); // HPL driver 0dB, not muted
604671
send(ADDR_P1_DAC_OUT::HPR_DRIVER, 0x06); // HPR drvier 0dB, not muted
605672
//send(ADDR_P1_DAC_OUT::HP_DRIVERS, 0xC2); // Falscher Wert must 1
@@ -621,7 +688,6 @@ void BoxDAC::initDACI2C() {
621688
send(ADDR_P0_SERIAL::DAC_DATA_PATH_SETUP, 0xF1); // DAC power on, Left=left, Right=Right, DAC Softstep SPEAKER MONO
622689
send(ADDR_P0_SERIAL::DAC_VOL_L_CTRL, 0xDC);
623690
send(ADDR_P0_SERIAL::DAC_VOL_R_CTRL, 0xDC);
624-
625691
//Excel 219
626692
// Extract END
627693
send(ADDR::PAGE_CONTROL, PAGE::DAC_OUT_VOL);

BoxDAC.h

+17-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44
#include "BaseHeader.h"
55
#include <EnhancedThread.h>
66

7+
#include "AudioCustomLogger.h"
8+
79
#include "BoxAudioBufferTriple.h"
810
#include "AudioOutputCC3200I2S.h"
911
#include <ESP8266SAM.h>
12+
#include "AudioFileSourceFatFs.h"
13+
#include "AudioGeneratorTonie.h"
14+
#include <AudioGenerator.h>
15+
#include <AudioGeneratorWAV.h>
16+
17+
//#include "libopus/opus.h"
1018

11-
class BoxDAC : public EnhancedThread {
19+
class BoxDAC : public EnhancedThread {
1220
public:
1321
void
1422
begin(),
15-
loop();
23+
loop(),
24+
loop(uint16_t timeoutMs);
1625

1726
void opusTest();
1827

@@ -54,6 +63,12 @@ class BoxDAC : public EnhancedThread {
5463
BoxAudioBufferTriple::BufferStruct* writeBuffer;
5564

5665
AudioOutputCC3200I2S* audioOutput;
66+
AudioFileSource* audioSource;
67+
AudioGenerator* audioGenerator;
68+
bool audioPlaying = false;
69+
70+
bool playFile(const char* path);
71+
bool _playWAV(const char* path);
5772

5873
const static uint8_t VOL_MIN = 0xB0+0x7F; //0xB0=-40.0dB /min allowed value 0x81=-63.5dB
5974
const static uint8_t VOL_MAX = 0x0A+0x7F; //0x0A=+04.0dB /max allowed value 0x30=+24.0dB

0 commit comments

Comments
 (0)