Skip to content

Commit

Permalink
zeroAudio gen, fixed cli vars, freq gen
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Jul 21, 2020
1 parent 9a68fe9 commit dc76e75
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
33 changes: 18 additions & 15 deletions BoxCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void BoxCLI::begin() {
cmdLoad = cli.addCmd("load");
cmdLoad.setDescription(" Shows the current load of all threads");
cmdLoad.addArg("n/ame", "");
cmdLoad.addArg("p/ointer", 0);
cmdLoad.addArg("p/ointer", "0");
cmdLoad.addFlagArg("r/eset");

cmdHelp = cli.addSingleArgumentCommand("help");
Expand All @@ -48,16 +48,16 @@ void BoxCLI::begin() {
cmdI2S.setDescription(" I2S debug information");
cmdI2S.addFlagArg("l/og");
cmdI2S.addArg("t/est", 0);
cmdI2S.addArg("f/requency", 0);
cmdI2S.addArg("f/requency", "440");

cmdSay = cli.addCmd("say");
cmdSay.setDescription(" Generate speech with SAM");
cmdSay.addPosArg("t/ext");
cmdSay.addArg("v/oice", 0);
cmdSay.addArg("s/peed", 0);
cmdSay.addArg("p/itch", 0);
cmdSay.addArg("t/hroat", 0);
cmdSay.addArg("m/outh", 0);
cmdSay.addArg("v/oice", "0");
cmdSay.addArg("s/peed", "0");
cmdSay.addArg("p/itch", "0");
cmdSay.addArg("t/hroat", "0");
cmdSay.addArg("m/outh", "0");
cmdSay.addFlagArg("sing");
cmdSay.addFlagArg("p/hoentic");
}
Expand Down Expand Up @@ -322,18 +322,21 @@ void BoxCLI::execLoad() {
void BoxCLI::execI2S() {
Command c = lastCmd;
unsigned long freq = parseNumber(c.getArg("frequency").getValue());
unsigned long test = parseNumber(c.getArg("test").getValue());
unsigned long testTime = parseNumber(c.getArg("test").getValue());
if (c.getArg("log").isSet()) {
Box.boxDAC.audioBuffer.logState();
Box.boxDAC.logDmaIrqChanges();
}
if (freq > 0) {
Log.info("Set test frequency=%i", freq);
Box.boxDAC.frequency = freq;
}
if (test > 0) {
Log.info("Run test for %ims", test);
Box.delayTask(test);
if (testTime > 0 && freq > 0) {
Log.info("Run frequency test for %ims with %iHz", testTime, freq);
BoxTimer timer;
timer.setTimer(testTime);
while (timer.isRunning()) {
Box.boxDAC.generateFrequency(freq, timer.getTimeTillEnd());
timer.tick();
}
Box.boxDAC.audioOutput->flush();

if (c.getArg("log").isSet()) {
Box.boxDAC.audioBuffer.logState();
Box.boxDAC.logDmaIrqChanges();
Expand Down
35 changes: 26 additions & 9 deletions BoxDAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ void dma_irq() {

void BoxDAC::begin() {
Log.info("Initialize DAC...");
audioBuffer.init();/*
audioBuffer.logState();
fillBuffer(25);
fillBuffer(25);
fillBuffer(25);*/
audioBuffer.init();
audioBuffer.logState();

MAP_PinTypeI2S(PIN_50, PIN_MODE_4); //I2S Data0 (DIN)
Expand Down Expand Up @@ -127,12 +123,12 @@ void BoxDAC::begin() {
}

void BoxDAC::loop() {
fillBuffer(25);
generateZeroAudio(25);
}

void BoxDAC::fillBuffer(uint16_t timeoutMs) {
void BoxDAC::generateFrequency(uint32_t frequency, uint16_t timeoutMs) {
BoxTimer timeout;
uint32_t halfWavelength = (sampleRate / frequency) / 2;
uint32_t halfWavelength = (audioOutput->GetRate() / frequency) / 2;
timeout.setTimer(timeoutMs);

while (timeout.isRunning()) {
Expand Down Expand Up @@ -160,6 +156,27 @@ void BoxDAC::fillBuffer(uint16_t timeoutMs) {
}
}

void BoxDAC::generateZeroAudio(uint16_t timeoutMs) {
BoxTimer timeout;
timeout.setTimer(timeoutMs);

while (timeout.isRunning()) {
while(writeBuffer->position<writeBuffer->size) {
writeBuffer->buffer[writeBuffer->position++] = 0;
writeBuffer->buffer[writeBuffer->position++] = 0;
}
timeout.tick();
if (writeBuffer->position >= writeBuffer->size) {
if (audioBuffer.flip(BoxAudioBufferTriple::BufferType::WRITE)) {
writeBuffer = audioBuffer.getBuffer(BoxAudioBufferTriple::BufferType::WRITE);
writeBuffer->state = BoxAudioBufferTriple::BufferState::WRITING;
continue;
}
}
break;
}
}

void BoxDAC::dmaPingPingComplete() {
MAP_I2SIntClear(I2S_BASE, I2S_INT_XDMA);

Expand Down Expand Up @@ -354,7 +371,7 @@ void BoxDAC::samSay(const char *text, enum ESP8266SAM::SAMVoice voice, uint8_t s
sam->Say(audioOutput, text);
audioOutput->flush();
delete sam;

audioOutput->SetRate(samplerate);
}

Expand Down
6 changes: 3 additions & 3 deletions BoxDAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class BoxDAC : public EnhancedThread {
begin(),
loop();

void fillBuffer(uint16_t timeoutMs);
void generateZeroAudio(uint16_t timeoutMs);

void generateFrequency(uint32_t frequency, uint16_t timeoutMs);

void beepTest();
void beep();
Expand All @@ -41,9 +43,7 @@ class BoxDAC : public EnhancedThread {

void logDmaIrqChanges();

uint32_t frequency = 1; // frequency of square wave in Hz
const int16_t amplitude = 500; // amplitude of square wave
const uint32_t sampleRate = 16000; // sample rate in Hz
int16_t sample = amplitude; // current sample value
uint32_t count = 0;
unsigned long i2sElmCount = 0;
Expand Down
2 changes: 1 addition & 1 deletion Hackiebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void Hackiebox::delayTask(uint16_t millis) {
}
}
void Hackiebox::delayTaskWork(uint16_t millis) {
boxDAC.fillBuffer(millis);
boxDAC.generateZeroAudio(millis);
}

void Hackiebox::loop() {
Expand Down

0 comments on commit dc76e75

Please sign in to comment.