Device: Refactor A2B APIs

* Removes features in `A2BMessage` class to support API for reading 16, 24, and 32 bit samples from A2B channels
* Re-organizes WAV receiving and transmitting code and API
* Creates API for mapping message channels to WAV channels and vice versa for transmitting and receiving
* Fixes `icsneo::Network::NetID::ExtendedData` VnetID bug for `icsneo::ExtendedDataMessage` decoding
* Creates RAD-A2B sequence chart example
* Fixes coremini uploading for certain devices in EEPROM by introducing `icsneo::Device::supportsEraseMemory`
This commit is contained in:
Yasser Yassine
2024-03-12 12:06:49 +00:00
committed by Kyle Schwarz
parent 06f6861130
commit cb22e622b3
33 changed files with 1014 additions and 947 deletions
@@ -1,156 +0,0 @@
#include "icsneo/communication/message/callback/streamoutput/a2bdecoder.h"
#include <chrono>
#include "icsneo/icsneocpp.h"
namespace icsneo {
static constexpr uint8_t maxChannel = 255;
size_t A2BAudioChannelMap::getChannelIndex(Channel channel, A2BMessage::A2BDirection dir) const {
size_t output = (size_t)channel;
if(dir == A2BMessage::A2BDirection::Upstream) {
output++;
}
return output;
}
A2BAudioChannelMap::A2BAudioChannelMap(uint8_t tdm) {
rawMap.resize(2*tdm, maxChannel);
}
void A2BAudioChannelMap::set(Channel outChannel, A2BMessage::A2BDirection dir, Channel inChannel) {
auto index = getChannelIndex(outChannel, dir);
rawMap[index] = inChannel;
}
void A2BAudioChannelMap::setAll(Channel inChannel) {
std::fill(rawMap.begin(), rawMap.end(), inChannel);
}
Channel A2BAudioChannelMap::get(Channel outChannel, A2BMessage::A2BDirection dir) const {
auto index = getChannelIndex(outChannel, dir);
return rawMap[index];
}
size_t A2BAudioChannelMap::A2BAudioChannelMap::size() const {
return rawMap.size();
}
uint8_t A2BAudioChannelMap::getTDM() const {
return (uint8_t)(rawMap.size() / 2);
}
Channel& A2BAudioChannelMap::operator[](size_t idx) {
return rawMap[idx];
}
A2BAudioChannelMap::operator const std::vector<Channel>&() const {
return rawMap;
}
A2BDecoder::A2BDecoder(
std::unique_ptr<std::istream>&& streamOut,
bool chSize16,
const A2BAudioChannelMap& chMap
) : channelSize16(chSize16), channelMap(chMap) {
stream = std::move(streamOut);
tdm = chMap.getTDM();
initializeFromHeader();
}
A2BDecoder::A2BDecoder(
const char* filename,
bool chSize16,
const A2BAudioChannelMap& chMap
) : A2BDecoder(std::make_unique<std::ifstream>(filename, std::ios::binary), chSize16, chMap) { }
A2BDecoder::operator bool() const {
return initialized && stream->good() && !stream->eof();
}
void A2BDecoder::initializeFromHeader() {
WaveFileHeader header;
if(!stream->read((char*)&header, sizeof(header))) {
initialized = false;
return;
}
// Only allow 16 or 24 bit samples
if(header.bitsPerSample != 16 && header.bitsPerSample != 24) {
initialized = false;
return;
}
audioBytesPerSample = header.bitsPerSample == 16 ? 2 : 3;
channelsInWave = (uint8_t)header.numChannels;
size_t bytesPerSample = channelSize16 ? 2 : 4;
size_t frameSize = 2*tdm*bytesPerSample;
size_t frameSizeWave = (size_t)(channelsInWave) * (size_t)(audioBytesPerSample);
frame.resize(frameSize, 0);
frameWave.resize(frameSizeWave, 0);
initialized = true;
}
std::shared_ptr<A2BMessage> A2BDecoder::decode() {
if(!*(this)) {
return nullptr;
}
auto a2bMessagePtr = std::make_shared<icsneo::A2BMessage>(
tdm,
channelSize16,
2048
);
A2BMessage& a2bMessage = *a2bMessagePtr.get();
a2bMessage.setMonitorBit(false); // Probably not necessary
a2bMessage.setTxMsgBit(true);
a2bMessage.network = Network(Network::NetID::A2B2);
for(uint32_t frameIndex = 0; frameIndex < a2bMessage.getNumFrames(); frameIndex++) {
if(!stream->read((char*)frameWave.data(), frameWave.size())) {
break;
}
for(size_t icsChannel = 0; icsChannel < channelMap.size(); icsChannel++) {
if(channelMap[icsChannel] >= maxChannel) {
continue;
}
size_t wBegin = audioBytesPerSample * channelMap[icsChannel];
A2BPCMSample sample = 0;
uint8_t* sampBytes = (uint8_t*)&sample;
std::copy(frameWave.begin() + wBegin, frameWave.begin() + wBegin + audioBytesPerSample, sampBytes);
a2bMessage[frameIndex][icsChannel] = sample;
}
}
return a2bMessagePtr;
}
bool A2BDecoder::outputAll(std::shared_ptr<Device>& device) {
const auto& networks = device->getSupportedTXNetworks();
if(std::none_of(networks.begin(), networks.end(), [](const Network& net) { return net.getNetID() == Network::NetID::A2B2; })) {
return false;
}
while(*this) {
device->transmit(decode());
}
return true;
}
}
@@ -4,16 +4,100 @@
namespace icsneo {
void A2BWAVOutput::writeHeader(const std::shared_ptr<A2BMessage>& firstMsg) const {
A2BWAVOutput::A2BWAVOutput(
const char* filename,
const ChannelMap& channelMap,
PCMType bitDepth,
size_t numWAVChannels,
uint32_t sampleRate
)
: StreamOutput(filename), chMap(channelMap), wavSampleRate(sampleRate), numChannelsWAV(numWAVChannels) {
switch(bitDepth) {
case PCMType::L16:
bytesPerSampleWAV = 2;
break;
case PCMType::L24:
bytesPerSampleWAV = 3;
break;
case PCMType::L32:
bytesPerSampleWAV = 4;
break;
}
WaveFileHeader header = WaveFileHeader(2 * firstMsg->getNumChannels(), wavSampleRate, firstMsg->getBitDepth());
header.write(stream);
streamStartPos = static_cast<uint32_t>(stream->tellp());
if(initialize()) {
initialized = true;
}
}
A2BWAVOutput::A2BWAVOutput(
std::ostream& os,
const ChannelMap& channelMap,
PCMType bitDepth,
size_t numWAVChannels,
uint32_t sampleRate
)
: StreamOutput(os), chMap(channelMap), wavSampleRate(sampleRate), numChannelsWAV(numWAVChannels) {
switch(bitDepth) {
case PCMType::L16:
bytesPerSampleWAV = 2;
break;
case PCMType::L24:
bytesPerSampleWAV = 3;
break;
case PCMType::L32:
bytesPerSampleWAV = 4;
break;
}
if(initialize()) {
initialized = true;
}
}
A2BWAVOutput::~A2BWAVOutput() {
if(!closed) {
close();
}
}
bool A2BWAVOutput::initialize() {
static constexpr size_t maxWAVChannels = 256;
if(numChannelsWAV > maxWAVChannels) {
return false;
}
maxMessageChannel = 0;
// Check if the inputted channel map has invalid mappings and compute maxMessageChannel
for(auto [wavChannel, messageChannel] : chMap) {
maxMessageChannel = std::max<size_t>(maxMessageChannel, messageChannel);
if(wavChannel >= numChannelsWAV) {
return false;
}
}
WAVHeader header = WAVHeader(
static_cast<uint16_t>(chMap.size()),
wavSampleRate,
static_cast<uint16_t>(bytesPerSampleWAV * 8)
);
if(!stream->write(reinterpret_cast<const char*>(&header), sizeof(WAVHeader))) {
return false;
}
streamStartPos = static_cast<uint32_t>(stream->tellp());
wavBuffer = std::vector<uint8_t>(wavBufferSize, 0);
wavBufferOffset = 0;
return true;
}
bool A2BWAVOutput::callIfMatch(const std::shared_ptr<Message>& message) const {
if(!initialized) {
return false;
}
if(closed) {
return false;
}
@@ -22,28 +106,87 @@ bool A2BWAVOutput::callIfMatch(const std::shared_ptr<Message>& message) const {
return false;
}
const auto& frame = std::static_pointer_cast<Frame>(message);
const auto& frameMsg = std::dynamic_pointer_cast<Frame>(message);
if(frame->network.getType() != Network::Type::A2B)
if(!frameMsg) {
return false;
}
if(frameMsg->network.getType() != Network::Type::A2B)
return false;
const auto& a2bmsg = std::static_pointer_cast<A2BMessage>(frame);
const auto& a2bMsg = std::dynamic_pointer_cast<A2BMessage>(frameMsg);
if(firstMessageFlag) {
writeHeader(a2bmsg);
firstMessageFlag = false;
if(!a2bMsg) {
return false;
}
// Might need to readd this block of code later if sample alignment fix is necessary
/*
std::streamsize bps = (std::streamsize)a2bmsg->getBytesPerSample();
for(size_t i=0; i<a2bmsg->getNumSamples(); i++) {
A2BPCMSample samp = *(a2bmsg->getSample(i));
write((void*)&samp, bps);
}
*/
size_t frameSize = a2bMsg->getFrameSize();
size_t wavFrameSize = numChannelsWAV * bytesPerSampleWAV;
size_t bytesPerChannel = static_cast<size_t>(a2bMsg->getBytesPerChannel());
size_t numMessageChannels = 2 * a2bMsg->numChannels;
size_t numFrames = a2bMsg->getNumFrames();
write((void*)a2bmsg->getAudioBuffer(), a2bmsg->getAudioBufferSize());
const uint8_t* audioBuffer = a2bMsg->data.data();
if(maxMessageChannel >= numMessageChannels) {
// The max message channel in our channel map is larger than the number of channels in this message
// this is likely due to the user inputting incorrect settings
return false;
}
for(size_t frame = 0; frame < numFrames; frame++) {
// Check to see if we can read another frame in wavBuffer, otherwise write and clear the buffer
if(wavBufferOffset + wavFrameSize >= wavBufferSize) {
if(!writeCurrentBuffer()) {
return false;
}
}
for(size_t wavChannel = 0; wavChannel < numChannelsWAV; wavChannel++) {
if(auto iter = chMap.find(static_cast<uint8_t>(wavChannel)); iter != chMap.end()) {
auto messageChannel = iter->second;
size_t messageChannelOffset = messageChannel * bytesPerChannel + frameSize* frame;
// Samples in the WAV are little endian signed integers
// Samples in the message channels are little endian signed integers that are
// most significant bit aligned
if(a2bMsg->channelSize16) {
// In this case, the channel size will be less than or equal to the sample we are writing
// so we zero out any of the least significant bytes which won't be occupied by a sample byte
for(size_t zeroByte = 0; zeroByte < bytesPerSampleWAV - bytesPerChannel; zeroByte++) {
wavBuffer[wavBufferOffset++] = 0;
}
// Write the channel data in the most signifant bytes of the wav sample, this effectively
// writes a sample which is scaled up.
for(size_t channelByte = 0; channelByte < bytesPerChannel; channelByte++) {
wavBuffer[wavBufferOffset++] = audioBuffer[messageChannelOffset + channelByte];
}
} else {
// In this case, the channel size will be greater than or equal to the sample we are reading
// Align the wav sample with the most significant bytes of the channel
size_t channelByte = messageChannelOffset + (bytesPerChannel - bytesPerSampleWAV);
// Read the most significant bytes of the channel into the wavBuffer
for(size_t sampleByte = 0; sampleByte < bytesPerSampleWAV; sampleByte++, channelByte++) {
wavBuffer[wavBufferOffset++] = audioBuffer[channelByte];
}
}
} else {
// If this channel wasn't specified in the channel map, set a zero sample
for(
size_t sampleByte = 0;
sampleByte < bytesPerSampleWAV;
sampleByte++
) {
wavBuffer[wavBufferOffset++] = 0;
}
}
}
}
return true;
}
@@ -53,17 +196,39 @@ void A2BWAVOutput::close() const {
return;
}
if(!initialized) {
return;
}
// Write any left over data in the buffer
if(wavBufferOffset > 0) {
writeCurrentBuffer();
}
// Seek back in the output stream and write the WAV chunk sizes
uint32_t streamEndPos = static_cast<uint32_t>(stream->tellp());
uint32_t subChunk2Size = streamEndPos - streamStartPos;
uint32_t chunkSize = streamEndPos - 8;
stream->seekp(streamStartPos - 4);
write((void*)&subChunk2Size, 4);
stream->write(reinterpret_cast<const char*>(&subChunk2Size), 4);
stream->seekp(4, std::ios::beg);
write((void*)&chunkSize, 4);
stream->write(reinterpret_cast<const char*>(&chunkSize), 4);
closed = true;
}
bool A2BWAVOutput::writeCurrentBuffer() const {
if(!stream->write(reinterpret_cast<const char*>(wavBuffer.data()), wavBufferOffset)) {
return false;
}
wavBufferOffset = 0;
return true;
}
}