A2B: Add A2B Tx streaming support

A2B: Add A2BDecoder for streaming wave to A2B device
RADA2B: Add functions to configure settings
This commit is contained in:
Yasser Yassine
2023-03-08 18:22:14 +00:00
parent 539cfa511b
commit ddee1254a0
12 changed files with 1145 additions and 523 deletions
+25 -25
View File
@@ -140,6 +140,30 @@ bool Decoder::decode(std::shared_ptr<Message>& result, const std::shared_ptr<Pac
return true;
}
case Network::Type::A2B: {
result = HardwareA2BPacket::DecodeToMessage(packet->data);
if(!result) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return false; // A nullptr was returned, the packet was not long enough to decode
}
A2BMessage& msg = *static_cast<A2BMessage*>(result.get());
msg.network = packet->network;
return true;
}
case Network::Type::LIN: {
result = HardwareLINPacket::DecodeToMessage(packet->data);
if(!result) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return false; // A nullptr was returned, the packet was not long enough to decode
}
LINMessage& msg = *static_cast<LINMessage*>(result.get());
msg.network = packet->network;
return true;
}
case Network::Type::Internal: {
switch(packet->network.getNetID()) {
case Network::NetID::Reset_Status: {
@@ -352,33 +376,9 @@ bool Decoder::decode(std::shared_ptr<Message>& result, const std::shared_ptr<Pac
}
break;
}
case Network::Type::A2B: {
result = HardwareA2BPacket::DecodeToMessage(packet->data);
if(!result) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return false; // A nullptr was returned, the packet was not long enough to decode
}
A2BMessage& msg = *static_cast<A2BMessage*>(result.get());
msg.network = packet->network;
return true;
}
case Network::Type::LIN: {
result = HardwareLINPacket::DecodeToMessage(packet->data);
if(!result) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return false; // A nullptr was returned, the packet was not long enough to decode
}
LINMessage& msg = *static_cast<LINMessage*>(result.get());
msg.network = packet->network;
return true;
}
}
// For the moment other types of messages will automatically be decoded as raw messages
result = std::make_shared<RawMessage>(packet->network, packet->data);
return true;
}
}
@@ -0,0 +1,156 @@
#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;
}
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;
}
}
@@ -1,23 +1,26 @@
#include "icsneo/communication/message/callback/streamoutput/a2bwavoutput.h"
#include "icsneo/device/tree/rada2b/rada2b.h"
#include "icsneo/icsneocpp.h"
namespace icsneo
{
namespace icsneo {
void A2BWAVOutput::writeHeader(const std::shared_ptr<A2BMessage>& firstMsg) const {
WaveFileHeader header = WaveFileHeader(2 * firstMsg->getNumChannels(), wavSampleRate, firstMsg->getBitDepth());
header.write(stream);
streamStartPos = static_cast<uint32_t>(stream->tellp());
}
bool A2BWAVOutput::callIfMatch(const std::shared_ptr<Message>& message) const {
if(closed)
{
if(closed) {
return false;
}
if(message->type != Message::Type::Frame)
if(message->type != Message::Type::Frame) {
return false;
}
const auto& frame = std::static_pointer_cast<Frame>(message);
@@ -31,23 +34,21 @@ bool A2BWAVOutput::callIfMatch(const std::shared_ptr<Message>& message) const {
firstMessageFlag = false;
}
if(!writeSamples(a2bmsg, A2BMessage::A2BDirection::DownStream)) {
close();
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);
}
*/
if(!writeSamples(a2bmsg, A2BMessage::A2BDirection::UpStream)) {
close();
return false;
}
write((void*)a2bmsg->getAudioBuffer(), a2bmsg->getAudioBufferSize());
return true;
}
void A2BWAVOutput::close() const
{
void A2BWAVOutput::close() const {
if(closed) {
return;
}
@@ -65,36 +66,4 @@ void A2BWAVOutput::close() const
closed = true;
}
bool A2BWAVOutput::writeSamples(const std::shared_ptr<A2BMessage>& msg, A2BMessage::A2BDirection dir) const
{
uint8_t numChannels = msg->getNumChannels();
uint8_t channel = 0;
uint32_t frame = 0;
uint8_t bitDepth = msg->getBitDepth();
while(true) {
auto sample = msg->getSample(dir, channel, frame);
if(!sample) {
if(channel == 0) {
break;
}
return false;
}
uint32_t audioSample = sample.value() >> (32 - bitDepth);
write((void*)(&audioSample), A2BPCM_SAMPLE_SIZE);
channel = (channel + 1) % numChannels;
if(channel == 0) {
frame++;
}
}
return true;
}
}
+25 -124
View File
@@ -1,6 +1,6 @@
#include "icsneo/communication/packet/a2bpacket.h"
#include <cstring>
#include <vector>
namespace icsneo {
@@ -14,148 +14,49 @@ std::shared_ptr<Message> HardwareA2BPacket::DecodeToMessage(const std::vector<ui
{
return nullptr;
}
auto getSampleFromBytes = [](uint8_t bytesPerSample, const uint8_t *bytes) {
A2BPCMSample result = 0;
for(auto i = 0; i < bytesPerSample; i++) {
result |= static_cast<uint32_t>(bytes[i]) << (i * 8);
}
return result;
};
const HardwareA2BPacket *data = (const HardwareA2BPacket*)bytestream.data();
uint32_t totalPackedLength = static_cast<uint32_t>(bytestream.size()) - static_cast<uint32_t>(coreMiniMessageHeaderSize); // First 28 bytes are message header.
size_t totalPackedLength = static_cast<size_t>(bytestream.size()) - static_cast<size_t>(coreMiniMessageHeaderSize); // First 28 bytes are message header.
uint8_t bytesPerChannel = data->header.channelSize16 ? 2 : 4;
uint8_t numChannels = data->header.channelNum;
uint8_t bitDepth = data->header.channelSize16 ? A2BPCM_L16 : A2BPCM_L24;
std::shared_ptr<A2BMessage> msg = std::make_shared<A2BMessage>(
(uint8_t)data->header.channelNum,
data->header.channelSize16,
totalPackedLength
);
std::shared_ptr<A2BMessage> msg = std::make_shared<A2BMessage>(bitDepth, bytesPerChannel, numChannels);
msg->channelSize16 = data->header.channelSize16;
msg->monitor = data->header.monitor;
msg->txmsg = data->header.txmsg;
msg->errIndicator = data->header.errIndicator;
msg->syncFrame = data->header.syncFrame;
msg->rfu2 = data->header.rfu2;
const uint8_t *bytes = bytestream.data();
bytes+=coreMiniMessageHeaderSize;
uint8_t channel = 0;
for(uint32_t i = 0; i < totalPackedLength; i += 2 * static_cast<uint32_t>(bytesPerChannel), bytes += 2 * bytesPerChannel, channel = (channel + 1) % numChannels) {
msg->addSample(
getSampleFromBytes(bytesPerChannel, bytes),
A2BMessage::A2BDirection::DownStream,
channel
);
msg->addSample(
getSampleFromBytes(bytesPerChannel, bytes + bytesPerChannel),
A2BMessage::A2BDirection::UpStream,
channel
);
}
msg->setMonitorBit(data->header.monitor);
msg->setTxMsgBit(data->header.txmsg);
msg->setErrIndicatorBit(data->header.errIndicator);
msg->setSyncFrameBit(data->header.syncFrame);
msg->setRFU2(data->header.rfu2);
msg->setAudioBuffer(bytestream.begin() + coreMiniMessageHeaderSize, bytestream.end());
return msg;
}
bool HardwareA2BPacket::EncodeFromMessage(const A2BMessage& message, std::vector<uint8_t>& bytestream, const device_eventhandler_t& report) {
constexpr size_t a2btxMessageHeaderSize = 6;
if(message.getBytesPerSample() != 2 && message.getBytesPerSample() != 4) {
report(APIEvent::Type::MessageFormattingError, APIEvent::Severity::Error);
return false;
}
size_t sampleBytes = message.getNumSamples() * static_cast<size_t>(message.getBytesPerSample());
size_t totalSize = coreMiniMessageHeaderSize + sampleBytes;
size_t sampleBytes = message.getAudioBufferSize();
size_t totalSize = a2btxMessageHeaderSize + sampleBytes;
if(totalSize > a2bMessageMaxLength) {
report(APIEvent::Type::MessageMaxLengthExceeded, APIEvent::Severity::Error);
return false;
}
bytestream.reserve(totalSize);
bytestream.push_back(message.getNumChannels());
bytestream.push_back(message.channelSize16 ? 1 : 0);
bytestream.resize(totalSize, 0);
uint32_t offset = 0;
uint8_t a2b2Bits = 0;
if(message.monitor) {
a2b2Bits = a2b2Bits | 1;
}
bytestream[offset++] = 0;
bytestream[offset++] = 0;
bytestream[offset++] = (uint8_t)(sampleBytes & 0xFF);
bytestream[offset++] = (uint8_t)((sampleBytes >> 8) & 0xFF);
bytestream[offset++] = (uint8_t)((message.description >> 8) & 0xFF);
bytestream[offset++] = (uint8_t)(message.description & 0xFF);
if(message.txmsg) {
a2b2Bits = a2b2Bits | (1 << 1);
}
if(message.errIndicator) {
a2b2Bits = a2b2Bits | (1 << 2);
}
if(message.syncFrame) {
a2b2Bits = a2b2Bits | (1 << 3);
}
bytestream.push_back(a2b2Bits);
bytestream.push_back(0);
bytestream.push_back(static_cast<uint8_t>(message.rfu2));
bytestream.push_back(static_cast<uint8_t>(message.rfu2 >> 8));
for(size_t i = 0; i < (coreMiniMessageHeaderSize - a2bHeaderSize); i++)
bytestream.push_back(0);
uint8_t numChannels = message.getNumChannels();
uint8_t channel = 0;
uint32_t frame = 0;
auto writeSample = [&](A2BPCMSample&& sample) {
for(uint32_t i = 0; i < static_cast<uint32_t>(message.getBytesPerSample()); i++) {
bytestream.push_back(static_cast<uint8_t>((sample >> (i*8))));
}
};
while(true) {
auto dsSample = message.getSample(A2BMessage::A2BDirection::DownStream, channel, frame);
auto usSample = message.getSample(A2BMessage::A2BDirection::UpStream, channel, frame);
// Check if getSample failed for both downstream and upstream
if(!dsSample && !usSample) {
if(channel != 0) {
//Incomplete frame, the frame we are currently on does not contain all channel samples
report(APIEvent::Type::A2BMessageIncompleteFrame, APIEvent::Severity::Error);
return false;
}
// Since no samples have been written for the current frame yet and there are no more
// samples in both upstream and downstream, we can break and end parsing.
break;
}
// Since the first case failed, at least one of the streams still has samples.
// This case checks to see if the other stream does not have a sample.
else if(!dsSample || !usSample) {
// Report an error since we must have a one to one correspondence between upstream
// and downstream.
report(APIEvent::Type::A2BMessageIncompleteFrame, APIEvent::Severity::Error);
return false;
}
writeSample(std::move(dsSample.value()));
writeSample(std::move(usSample.value()));
channel = (channel + 1) % numChannels;
if(channel == 0)
frame++;
}
std::copy(message.data.begin(), message.data.end(), bytestream.begin() + offset);
return true;
}