Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions hvcc/generators/c2daisy/templates/HeavyDaisy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Heavy_{{patch_name}}.h"
#include "Heavy_{{patch_name}}.hpp"
#include "HeavyDaisy_{{patch_name}}.hpp"
#include <string>
#include "fatfs.h"

#define SAMPLE_RATE {{samplerate}}.f

Expand Down Expand Up @@ -35,6 +37,12 @@
#define MIDI_OUT_FIFO_SIZE 128
{% endif %}

#define DSY_TEXT __attribute__((section(".text")))

#define HV_HASH_SND_WRITE 0x74140F5F
#define HV_HASH_SND_READ 0xEB5BD581
#define HV_HASH_SND_READ_RES 0x280AFD69

{% for k, v in display_params.items() %}
#define HV_HASH_{{k|upper}} {{v}}
{% endfor %}
Expand All @@ -59,12 +67,27 @@ FIFO<uint8_t, MIDI_OUT_FIFO_SIZE> midi_tx_fifo;
{% endif %}
// int midiOutCount;
// uint8_t* midiOutData;

/** SDMMC Configuration */
SdmmcHandler sdmmc;
/** FatFS Interface for libDaisy */
DSY_TEXT FatFSInterface fsi;
/** Global File object */
DSY_TEXT FIL file;
const int FILE_BUF_SIZE = 1024;
DSY_TEXT float file_buf[FILE_BUF_SIZE];

bool sndfile_action;
uint32_t sndHash;
const HvMessage *sndMsg;

void CallbackWriteIn(Heavy_{{patch_name}}* hv);
void LoopWriteIn(Heavy_{{patch_name}}* hv);
void CallbackWriteOut();
void LoopWriteOut();
void PostProcess();
void Display();
void sndFileOperator(uint32_t sendHash, const HvMessage *m);

{% if output_parameters|length > 0 %}
constexpr int DaisyNumOutputParameters = {{output_parameters|length}};
Expand Down Expand Up @@ -244,6 +267,36 @@ int main(void)
{% endif %}
hv->setSendHook(sendHook);

/** Initialize the SDMMC Hardware
* For this example we'll use:
* Medium (25MHz), 4-bit, w/out power save settings
*/
SdmmcHandler::Config sd_cfg;
sd_cfg.Defaults();
sd_cfg.speed = SdmmcHandler::Speed::STANDARD;
sd_cfg.width = SdmmcHandler::BusWidth::BITS_1;
sdmmc.Init(sd_cfg);

/** Setup our interface to the FatFS middleware */
FatFSInterface::Config fsi_config;
fsi_config.media = FatFSInterface::Config::MEDIA_SD;
fsi.Init(fsi_config);

/** Get the reference to the FATFS Filesystem for use in mounting the hardware. */
FATFS& fs = fsi.GetSDFileSystem();

/** mount the filesystem to the root directory
* fsi.GetSDPath can be used when mounting multiple filesystems on different media
*/
auto mounted = f_mount(&fs, "/", 1);

if(mounted != FR_OK)
{
hardware.som.PrintLine("Failed to mount filesystem");
} else {
hardware.som.PrintLine("Filesystem mounted");
}

for(;;)
{
{% if debug_printing %}
Expand Down Expand Up @@ -305,6 +358,12 @@ int main(void)
}
}
{% endif %}

if (sndfile_action)
{
sndFileOperator(sndHash, sndMsg);
sndfile_action = false;
}
}
}

Expand Down Expand Up @@ -480,6 +539,8 @@ void HandleDisplayParams(uint32_t sendHash, const HvMessage *m)
}
{% endif %}

{% include 'sndFileOperator.cpp' %}

/** Receives messages from PD and writes them to the appropriate
* index in the `output_data` array, to be written later.
*/
Expand All @@ -500,6 +561,16 @@ static void sendHook(HeavyContextInterface *c, const char *receiverName, uint32_
{% if display_params|length > 0 %}
HandleDisplayParams(receiverHash, m);
{% endif %}
switch (receiverHash)
{
case HV_HASH_SND_WRITE:
case HV_HASH_SND_READ:
case HV_HASH_SND_READ_RES:
sndHash = receiverHash;
sndMsg = m;
sndfile_action = true;
break;
}
}

{% if debug_printing is sameas true %}
Expand Down
2 changes: 2 additions & 0 deletions hvcc/generators/c2daisy/templates/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ APP_TYPE = {{bootloader}}
LDFLAGS += -u _printf_float
{% endif %}

USE_FATFS=1

# Project Source
C_SOURCES = $(wildcard *.c)
CPP_SOURCES = $(wildcard *.cpp)
Expand Down
94 changes: 94 additions & 0 deletions hvcc/generators/c2daisy/templates/sndFileOperator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
void sndFileOperator(uint32_t sendHash, const HvMessage *m)
{
switch (sendHash) {
case HV_HASH_SND_READ: // __hv_snd_read
{
float sndID = hv_msg_getFloat(m, 0);
const char *fileName = hv_msg_getSymbol(m, 1);
const char *tableName = hv_msg_getSymbol(m, 2);

char sndRecInfo[32];
char sndRecSamples[32];
char *bufInfo = sndRecInfo;
char *bufSamples = sndRecSamples;

snprintf(bufInfo, 31, "%d__hv_snd_info", (int) sndID);
snprintf(bufSamples, 31, "%d__hv_snd_samples", (int) sndID);

hv_uint32_t tableHash = hv_string_to_hash(tableName);
float *table = hv->getBufferForTable(tableHash);
int tableSize = hv->getLengthForTable(tableHash);

auto sta = f_open(&file, fileName, (FA_OPEN_EXISTING | FA_READ));

if (sta != FR_OK) {
hardware.som.PrintLine("Failed to open file");
}

FileReader reader(&file);
WavParser parser;

if(!parser.parse(reader))
{
hardware.som.PrintLine("Error parsing file: %s", fileName);
}
const auto& info = parser.info();
static int w_start = parser.dataOffset() * 8;
static int data_size = parser.dataSize();

hardware.som.PrintLine("Data start: %d", w_start);
hardware.som.PrintLine("Data size: %d", data_size);
hardware.som.PrintLine("Buffer size: %d", FILE_BUF_SIZE);
hardware.som.PrintLine("Buffer size: %d", sizeof(file_buf));

UINT br = 0;
int chunk = 0;

f_lseek(&file, parser.dataOffset());

int to_read = tableSize * sizeof(float);

while( to_read > 0 ) {
FRESULT fres = f_read(&file, file_buf, 512, &br);
if (fres != FR_OK) {
hardware.som.PrintLine("Error reading file: %s", fileName);
} else {

int values = br / sizeof(float);

// hardware.som.PrintLine("Values: %d", values);

for (int i = 0; i < values; i++) {
if (chunk < tableSize) {
table[chunk] = file_buf[i];
chunk++;
} else {
break;
}
}
to_read -= br;
}
}


hv->sendMessageToReceiverV(
hv_string_to_hash(sndRecInfo), 0, "ffffs",
(float) info.sampleRate, // sample rate
44.0, // header size
1.0, // channels
(float) info.bitsPerSample, // bytes per sample
"l" // endianness
);

hv->sendFloatToReceiver(
hv_string_to_hash(sndRecSamples),
float(tableSize)
);

f_close(&file);

break;
}
default: break;
}
}
Loading