mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 09:28:40 +02:00
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:
committed by
Kyle Schwarz
parent
06f6861130
commit
cb22e622b3
@@ -14,33 +14,34 @@
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
struct WaveFileHeader {
|
||||
#pragma pack(push, 1)
|
||||
struct WAVHeader {
|
||||
|
||||
static constexpr uint32_t WAVE_CHUNK_ID = 0x46464952; // "RIFF"
|
||||
static constexpr uint32_t WAVE_FORMAT = 0x45564157; // "WAVE"
|
||||
static constexpr uint32_t WAVE_SUBCHUNK1_ID = 0x20746d66; // "fmt "
|
||||
static constexpr uint32_t WAVE_SUBCHUNK2_ID = 0x61746164; // "data"
|
||||
static constexpr uint16_t WAVE_SUBCHUNK1_SIZE = 16;
|
||||
static constexpr uint16_t WAVE_AUDIO_FORMAT_PCM = 1;
|
||||
static constexpr uint32_t WAVE_DEFAULT_SIZE = 0; // Default size for streamed wav
|
||||
static constexpr uint32_t WAV_CHUNK_ID = 0x46464952; // "RIFF"
|
||||
static constexpr uint32_t WAV_FORMAT = 0x45564157; // "WAV"
|
||||
static constexpr uint32_t WAV_SUBCHUNK1_ID = 0x20746d66; // "fmt "
|
||||
static constexpr uint32_t WAV_SUBCHUNK2_ID = 0x61746164; // "data"
|
||||
static constexpr uint16_t WAV_SUBCHUNK1_SIZE = 16;
|
||||
static constexpr uint16_t WAV_AUDIO_FORMAT_PCM = 1;
|
||||
static constexpr uint32_t WAV_DEFAULT_SIZE = 0; // Default size for streamed wav
|
||||
|
||||
uint32_t chunkId = WAVE_CHUNK_ID; // "RIFF"
|
||||
uint32_t chunkSize = WAVE_DEFAULT_SIZE; // number of bytes to follow
|
||||
uint32_t format = WAVE_FORMAT; // "WAVE"
|
||||
uint32_t subchunk1Id = WAVE_SUBCHUNK1_ID; // "fmt "
|
||||
uint32_t subchunk1Size = WAVE_SUBCHUNK1_SIZE; // number of bytes in *this* subchunk (always 16)
|
||||
uint16_t audioFormat = WAVE_AUDIO_FORMAT_PCM; // 1 for PCM
|
||||
uint32_t chunkId = WAV_CHUNK_ID; // "RIFF"
|
||||
uint32_t chunkSize = WAV_DEFAULT_SIZE; // number of bytes to follow
|
||||
uint32_t format = WAV_FORMAT; // "WAV"
|
||||
uint32_t subchunk1Id = WAV_SUBCHUNK1_ID; // "fmt "
|
||||
uint32_t subchunk1Size = WAV_SUBCHUNK1_SIZE; // number of bytes in *this* subchunk (always 16)
|
||||
uint16_t audioFormat = WAV_AUDIO_FORMAT_PCM; // 1 for PCM
|
||||
uint16_t numChannels; // number of channels
|
||||
uint32_t sampleRate; // sample rate in Hz
|
||||
uint32_t byteRate; // bytes per second of audio: sampleRate * numChannels * (bitsPerSample / 8)
|
||||
uint16_t blockAlign; // alignment of each block in bytes: numChannels * (bitsPerSample / 8)
|
||||
uint16_t bitsPerSample; // number of bits in each sample
|
||||
uint32_t subchunk2Id = WAVE_SUBCHUNK2_ID; // "data"
|
||||
uint32_t subchunk2Size = WAVE_DEFAULT_SIZE; // number of bytes to follow
|
||||
uint32_t subchunk2Id = WAV_SUBCHUNK2_ID; // "data"
|
||||
uint32_t subchunk2Size = WAV_DEFAULT_SIZE; // number of bytes to follow
|
||||
|
||||
WaveFileHeader() = default;
|
||||
WAVHeader() = default;
|
||||
|
||||
WaveFileHeader(uint16_t nChannels, uint32_t sRate, uint16_t bps, uint32_t nSamples = 0) {
|
||||
WAVHeader(uint16_t nChannels, uint32_t sRate, uint16_t bps, uint32_t nSamples = 0) {
|
||||
setHeader(nChannels, sRate, bps, nSamples);
|
||||
}
|
||||
|
||||
@@ -59,48 +60,82 @@ struct WaveFileHeader {
|
||||
subchunk2Size = numSamples * numChannels * (bitsPerSample / 8);
|
||||
chunkSize = subchunk2Size + 36;
|
||||
}
|
||||
};
|
||||
|
||||
void write(const std::unique_ptr<std::ostream>& stream) {
|
||||
#pragma pack(pop)
|
||||
|
||||
stream->write(reinterpret_cast<const char*>(&chunkId), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&chunkSize), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&format), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&subchunk1Id), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&subchunk1Size), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&audioFormat), 2);
|
||||
stream->write(reinterpret_cast<const char*>(&numChannels), 2);
|
||||
stream->write(reinterpret_cast<const char*>(&sampleRate), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&byteRate), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&blockAlign), 2);
|
||||
stream->write(reinterpret_cast<const char*>(&bitsPerSample), 2);
|
||||
stream->write(reinterpret_cast<const char*>(&subchunk2Id), 4);
|
||||
stream->write(reinterpret_cast<const char*>(&subchunk2Size), 4);
|
||||
class IWAVStream {
|
||||
private:
|
||||
std::unique_ptr<std::istream, std::function<void(std::istream*)>> stream;
|
||||
bool initialized = false;
|
||||
public:
|
||||
WAVHeader header;
|
||||
|
||||
IWAVStream(std::istream& WAVInput)
|
||||
: stream(&WAVInput, [](std::istream*){}) {
|
||||
|
||||
if(initialize()) {
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
IWAVStream(const char* filename)
|
||||
: stream(new std::ifstream(filename, std::ios::in | std::ios::binary), std::default_delete<std::istream>()) {
|
||||
|
||||
if(initialize()) {
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool initialize() {
|
||||
return !(!stream->read(reinterpret_cast<char*>(&header), sizeof(WAVHeader)));
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return initialized && stream && stream->good();
|
||||
}
|
||||
|
||||
bool read(char* into, std::streamsize num) {
|
||||
return !(!stream->read(into, num));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set stream immediately after WAV header
|
||||
*/
|
||||
void reset() {
|
||||
if(!(*this)) {
|
||||
return;
|
||||
}
|
||||
|
||||
stream->clear();
|
||||
stream->seekg(sizeof(icsneo::WAVHeader), std::ios::beg);
|
||||
}
|
||||
};
|
||||
|
||||
class StreamOutput : public MessageCallback {
|
||||
public:
|
||||
StreamOutput(std::unique_ptr<std::ostream>&& os, fn_messageCallback cb, std::shared_ptr<MessageFilter> f)
|
||||
: MessageCallback(cb, f), stream(std::move(os)) {}
|
||||
StreamOutput(std::ostream& os, fn_messageCallback cb, std::shared_ptr<MessageFilter> f)
|
||||
: MessageCallback(cb, f), stream(&os, [](std::ostream*){}) {}
|
||||
|
||||
StreamOutput(const char* filename, fn_messageCallback cb, std::shared_ptr<MessageFilter> f)
|
||||
: MessageCallback(cb, f) {
|
||||
stream = std::make_unique<std::ofstream>(filename, std::ios::binary);
|
||||
}
|
||||
:
|
||||
MessageCallback(cb, f),
|
||||
stream(
|
||||
new std::ofstream(filename, std::ios::binary),
|
||||
std::default_delete<std::ostream>()
|
||||
) {}
|
||||
|
||||
StreamOutput(const char* filename) : MessageCallback([](std::shared_ptr<Message> msg) {}) {
|
||||
stream = std::make_unique<std::ofstream>(filename, std::ios::binary);
|
||||
}
|
||||
StreamOutput(const char* filename) :
|
||||
MessageCallback([](std::shared_ptr<Message> msg) {}),
|
||||
stream(
|
||||
new std::ofstream(filename, std::ios::binary),
|
||||
std::default_delete<std::ostream>()
|
||||
) {}
|
||||
|
||||
StreamOutput(std::unique_ptr<std::ostream>&& os) : MessageCallback([](std::shared_ptr<Message> msg) {}), stream(std::move(os)) {}
|
||||
StreamOutput(std::ostream& os) : MessageCallback([](std::shared_ptr<Message> msg) {}), stream(&os, [](std::ostream*){}) {}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<std::ostream> stream;
|
||||
|
||||
void write(void* msg, std::streamsize size) const {
|
||||
stream->write(reinterpret_cast<const char*>(msg), size);
|
||||
}
|
||||
std::unique_ptr<std::ostream, std::function<void(std::ostream*)>> stream;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user