-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More robust command extractor state machine (#187)
* ADC bias injection - removes clipping - makes louder * ADC auto DC removal 1) deals with ESP32 ADC reference voltage variation 2) 16 bit i2s transfers * DRA module retries. * Android: Switch AudioTrack to the AudioFormat.ENCODING_PCM_16BIT As some devices have broken PCM8 support * More robust command extractor state machine Old one was loosing data * Cleanup * Fixed merge confilict * signed pcm8 on wire
- Loading branch information
Showing
3 changed files
with
83 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
android-src/KV4PHT/app/src/main/java/com/vagell/kv4pht/radio/RxStreamParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.vagell.kv4pht.radio; | ||
|
||
import static com.vagell.kv4pht.radio.RadioAudioService.COMMAND_DELIMITER; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.function.BiConsumer; | ||
|
||
public class RxStreamParser { | ||
|
||
private int matchedDelimiterTokens = 0; | ||
private byte command; | ||
private byte commandParamLen; | ||
private final ByteArrayOutputStream commandParams = new ByteArrayOutputStream(); | ||
private final ByteArrayOutputStream lookaheadBuffer = new ByteArrayOutputStream(); | ||
|
||
private final BiConsumer<Byte, byte[]> onCommand; | ||
|
||
public RxStreamParser(BiConsumer<Byte, byte[]> onCommand) { | ||
this.onCommand = onCommand; | ||
} | ||
|
||
public byte[] extractAudioAndHandleCommands(byte[] newData) { | ||
ByteArrayOutputStream audioOut = new ByteArrayOutputStream(); | ||
for (byte b : newData) { | ||
lookaheadBuffer.write(b); | ||
if (matchedDelimiterTokens < COMMAND_DELIMITER.length) { | ||
if (b == COMMAND_DELIMITER[matchedDelimiterTokens]) { | ||
matchedDelimiterTokens++; | ||
} else { | ||
flushLookaheadBuffer(audioOut); | ||
matchedDelimiterTokens = 0; | ||
} | ||
} else if (matchedDelimiterTokens == COMMAND_DELIMITER.length) { | ||
command = b; | ||
matchedDelimiterTokens++; | ||
} else if (matchedDelimiterTokens == COMMAND_DELIMITER.length + 1) { | ||
commandParamLen = b; | ||
commandParams.reset(); | ||
matchedDelimiterTokens++; | ||
} else { | ||
commandParams.write(b); | ||
matchedDelimiterTokens++; | ||
lookaheadBuffer.reset(); | ||
if (commandParams.size() == commandParamLen) { | ||
onCommand.accept(command, commandParams.toByteArray()); | ||
resetParser(audioOut); | ||
} | ||
} | ||
} | ||
return audioOut.toByteArray(); | ||
} | ||
|
||
private void flushLookaheadBuffer(ByteArrayOutputStream audioOut) { | ||
byte[] buffer = lookaheadBuffer.toByteArray(); | ||
audioOut.write(buffer, 0, buffer.length); | ||
lookaheadBuffer.reset(); | ||
} | ||
|
||
private void resetParser(ByteArrayOutputStream audioOut) { | ||
flushLookaheadBuffer(audioOut); | ||
matchedDelimiterTokens = 0; | ||
commandParams.reset(); | ||
commandParamLen = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters