add SampleBuffer and ErrorManager; WAV testing
parent
04e23e9ad7
commit
e080a26651
@ -0,0 +1,85 @@
|
||||
/*
|
||||
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* STM32L476xG memory setup.
|
||||
*/
|
||||
MEMORY
|
||||
{
|
||||
flash0 (rx) : org = 0x08000000, len = 1M
|
||||
flash1 (rx) : org = 0x00000000, len = 0
|
||||
flash2 (rx) : org = 0x00000000, len = 0
|
||||
flash3 (rx) : org = 0x00000000, len = 0
|
||||
flash4 (rx) : org = 0x00000000, len = 0
|
||||
flash5 (rx) : org = 0x00000000, len = 0
|
||||
flash6 (rx) : org = 0x00000000, len = 0
|
||||
flash7 (rx) : org = 0x00000000, len = 0
|
||||
ram0 (wx) : org = 0x20000000, len = 96k
|
||||
ram1 (wx) : org = 0x00000000, len = 0
|
||||
ram2 (wx) : org = 0x00000000, len = 0
|
||||
ram3 (wx) : org = 0x00000000, len = 0
|
||||
ram4 (wx) : org = 0x10000000, len = 0 /* Save 32k for ELF load */
|
||||
ram5 (wx) : org = 0x00000000, len = 0
|
||||
ram6 (wx) : org = 0x00000000, len = 0
|
||||
ram7 (wx) : org = 0x00000000, len = 0
|
||||
}
|
||||
|
||||
/* For each data/text section two region are defined, a virtual region
|
||||
and a load region (_LMA suffix).*/
|
||||
|
||||
/* Flash region to be used for exception vectors.*/
|
||||
REGION_ALIAS("VECTORS_FLASH", flash0);
|
||||
REGION_ALIAS("VECTORS_FLASH_LMA", flash0);
|
||||
|
||||
/* Flash region to be used for constructors and destructors.*/
|
||||
REGION_ALIAS("XTORS_FLASH", flash0);
|
||||
REGION_ALIAS("XTORS_FLASH_LMA", flash0);
|
||||
|
||||
/* Flash region to be used for code text.*/
|
||||
REGION_ALIAS("TEXT_FLASH", flash0);
|
||||
REGION_ALIAS("TEXT_FLASH_LMA", flash0);
|
||||
|
||||
/* Flash region to be used for read only data.*/
|
||||
REGION_ALIAS("RODATA_FLASH", flash0);
|
||||
REGION_ALIAS("RODATA_FLASH_LMA", flash0);
|
||||
|
||||
/* Flash region to be used for various.*/
|
||||
REGION_ALIAS("VARIOUS_FLASH", flash0);
|
||||
REGION_ALIAS("VARIOUS_FLASH_LMA", flash0);
|
||||
|
||||
/* Flash region to be used for RAM(n) initialization data.*/
|
||||
REGION_ALIAS("RAM_INIT_FLASH_LMA", flash0);
|
||||
|
||||
/* RAM region to be used for Main stack. This stack accommodates the processing
|
||||
of all exceptions and interrupts.*/
|
||||
REGION_ALIAS("MAIN_STACK_RAM", ram0);
|
||||
|
||||
/* RAM region to be used for the process stack. This is the stack used by
|
||||
the main() function.*/
|
||||
REGION_ALIAS("PROCESS_STACK_RAM", ram0);
|
||||
|
||||
/* RAM region to be used for data segment.*/
|
||||
REGION_ALIAS("DATA_RAM", ram0);
|
||||
REGION_ALIAS("DATA_RAM_LMA", flash0);
|
||||
|
||||
/* RAM region to be used for BSS segment.*/
|
||||
REGION_ALIAS("BSS_RAM", ram0);
|
||||
|
||||
/* RAM region to be used for the default heap.*/
|
||||
REGION_ALIAS("HEAP_RAM", ram0);
|
||||
|
||||
/* Generic rules inclusion.*/
|
||||
INCLUDE rules.ld
|
@ -0,0 +1,95 @@
|
||||
#ifndef WAV_HPP_
|
||||
#define WAV_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
||||
namespace wav
|
||||
{
|
||||
struct header {
|
||||
char riff[4]; // "RIFF"
|
||||
uint32_t filesize; // Total file size minus eight bytes
|
||||
char wave[4]; // "WAVE"
|
||||
|
||||
bool valid() const {
|
||||
return strncmp(riff, "RIFF", 4) == 0 && filesize > 8 && strncmp(wave, "WAVE", 4) == 0;
|
||||
}
|
||||
} __attribute__ ((packed));
|
||||
|
||||
struct format {
|
||||
char fmt_[4]; // "fmt "
|
||||
uint32_t size;
|
||||
uint16_t type;
|
||||
uint16_t channelcount;
|
||||
uint32_t samplerate;
|
||||
uint32_t byterate;
|
||||
uint16_t unused;
|
||||
uint16_t bps;
|
||||
|
||||
bool valid() const {
|
||||
return strncmp(fmt_, "fmt ", 4) == 0;
|
||||
}
|
||||
} __attribute__ ((packed));
|
||||
|
||||
struct data {
|
||||
char data[4]; // "data"
|
||||
uint32_t size;
|
||||
|
||||
bool valid() const {
|
||||
return strncmp(data, "data", 4) == 0;
|
||||
}
|
||||
} __attribute__ ((packed));
|
||||
|
||||
class clip {
|
||||
public:
|
||||
clip(const char *path) {
|
||||
std::ifstream file (path);
|
||||
if (!file.good())
|
||||
return;
|
||||
{
|
||||
header h;
|
||||
file.read(reinterpret_cast<char *>(&h), sizeof(header));
|
||||
if (!h.valid())
|
||||
return;
|
||||
}
|
||||
{
|
||||
format f;
|
||||
file.read(reinterpret_cast<char *>(&f), sizeof(format));
|
||||
if (!f.valid() || f.type != 1) // ensure PCM
|
||||
return;
|
||||
}
|
||||
{
|
||||
wav::data d;
|
||||
file.read(reinterpret_cast<char *>(&d), sizeof(wav::data));
|
||||
if (!d.valid())
|
||||
return;
|
||||
m_data = new char[d.size + 4096 - (d.size % 4096)];
|
||||
m_size = d.size;
|
||||
file.read(m_data, d.size);
|
||||
}
|
||||
}
|
||||
|
||||
bool valid() const {
|
||||
return m_data != nullptr && m_size > 0;
|
||||
}
|
||||
auto data() const {
|
||||
return m_data;
|
||||
}
|
||||
auto next(unsigned int chunksize = 3000) {
|
||||
if (m_pos == m_size) {
|
||||
m_pos = 0;
|
||||
}
|
||||
auto ret = m_data + m_pos;
|
||||
m_pos = std::min(m_pos + chunksize, m_size);
|
||||
return ret;
|
||||
}
|
||||
private:
|
||||
char *m_data = nullptr;
|
||||
uint32_t m_size = 0;
|
||||
uint32_t m_pos = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // WAV_HPP_
|
||||
|
@ -0,0 +1,56 @@
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 6000;
|
||||
|
||||
using Sample = uint16_t;
|
||||
|
||||
class SampleBuffer
|
||||
{
|
||||
public:
|
||||
void clear() {
|
||||
m_buffer.fill(0);
|
||||
}
|
||||
void modify(Sample *data, unsigned int srcsize) {
|
||||
auto size = srcsize < m_size ? srcsize : m_size;
|
||||
std::copy(data, data + size, m_buffer.data());
|
||||
m_modified = m_buffer.data();
|
||||
}
|
||||
void midmodify(Sample *data, unsigned int srcsize) {
|
||||
auto size = srcsize < m_size / 2 ? srcsize : m_size / 2;
|
||||
std::copy(data, data + size, middata());
|
||||
m_modified = middata();
|
||||
}
|
||||
|
||||
void setSize(unsigned int size) {
|
||||
m_size = size < MAX_SAMPLE_BUFFER_SIZE ? size : MAX_SAMPLE_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
Sample *data() /*const*/ {
|
||||
return m_buffer.data();
|
||||
}
|
||||
Sample *middata() /*const*/ {
|
||||
return m_buffer.data() + m_size / 2;
|
||||
}
|
||||
uint8_t *bytedata() /*const*/ {
|
||||
return reinterpret_cast<uint8_t *>(m_buffer.data());
|
||||
}
|
||||
|
||||
Sample *modified() {
|
||||
auto m = m_modified;
|
||||
m_modified = nullptr;
|
||||
return m;
|
||||
}
|
||||
unsigned int size() const {
|
||||
return m_size;
|
||||
}
|
||||
unsigned int bytesize() const {
|
||||
return m_size * sizeof(Sample);
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<Sample, MAX_SAMPLE_BUFFER_SIZE> m_buffer;
|
||||
unsigned int m_size = MAX_SAMPLE_BUFFER_SIZE;
|
||||
Sample *m_modified = nullptr;
|
||||
};
|
||||
|
@ -0,0 +1,38 @@
|
||||
#include <array>
|
||||
|
||||
constexpr unsigned int MAX_ERROR_QUEUE_SIZE = 8;
|
||||
|
||||
enum class Error : char
|
||||
{
|
||||
None = 0,
|
||||
BadParam,
|
||||
BadParamSize,
|
||||
BadUserCodeLoad,
|
||||
BadUserCodeSize,
|
||||
NotIdle,
|
||||
ConversionAborted
|
||||
};
|
||||
|
||||
class ErrorManager
|
||||
{
|
||||
public:
|
||||
void add(Error error) {
|
||||
if (m_index < m_queue.size())
|
||||
m_queue[m_index++] = error;
|
||||
}
|
||||
|
||||
bool assert(bool condition, Error error) {
|
||||
if (!condition)
|
||||
add(error);
|
||||
return condition;
|
||||
}
|
||||
|
||||
Error pop() {
|
||||
return m_index == 0 ? Error::None : m_queue[--m_index];
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<Error, MAX_ERROR_QUEUE_SIZE> m_queue;
|
||||
unsigned int m_index = 0;
|
||||
};
|
||||
|
Loading…
Reference in New Issue