From 5e8a6a8957f16e5ec6284a5c4fb728fbda09d63c Mon Sep 17 00:00:00 2001 From: marchingband Date: Sat, 11 Dec 2021 18:04:43 -0800 Subject: [PATCH] load each voice separate, delete overwirtten racks, allow delete notes --- src/file_system.c | 162 +++++++++++++++++++++++++++++----------------- src/file_system.h | 3 + src/server.cpp | 138 +++++++++++++++++++-------------------- 3 files changed, 172 insertions(+), 131 deletions(-) diff --git a/src/file_system.c b/src/file_system.c index c286a39..972377b 100644 --- a/src/file_system.c +++ b/src/file_system.c @@ -37,9 +37,6 @@ #define PIN_CONFIG_SIZE (NUM_PIN_CONFIGS * sizeof(struct pin_config_t)) #define PIN_CONFIG_BLOCKS (PIN_CONFIG_SIZE / SECTOR_SIZE + (PIN_CONFIG_SIZE % SECTOR_SIZE !=0)) -// #define NUM_VOICES 16 -// #define NUM_NOTES 128 - #define NUM_WAV_LUT_ENTRIES ( NUM_VOICES * NUM_NOTES ) #define WAV_LUT_START_BLOCK (PIN_CONFIG_START_BLOCK + PIN_CONFIG_BLOCKS) #define BYTES_PER_VOICE ( NUM_NOTES * sizeof(struct wav_file_t) ) @@ -535,9 +532,7 @@ void add_wav_to_file_system(char *name,int voice,int note,size_t start_block,siz strcpy(voice_data[note].name,name); ESP_ERROR_CHECK(emmc_write(voice_data,voice_start_block,BLOCKS_PER_VOICE)); read_wav_lut_from_disk(); - // read_rack_lut_from_disk(); free(voice_data); - // log_i("done"); } void add_rack_to_file_system(char *name, int voice, int note, size_t start_block, size_t size, int layer, const char *json) @@ -571,6 +566,7 @@ void add_rack_to_file_system(char *name, int voice, int note, size_t start_block int get_empty_rack(void){ for(int i=0;ivalueint >= 0) { updateRackConfig(note); + } + else if(cJSON_GetObjectItemCaseSensitive(note, "isRack")->valueint == -1) + { + if(voice_data[num_note].isRack > -1) + { + // this is a non-rack overwritting a former rack + clear_rack(voice_data[num_note].isRack); + voice_data[num_note].isRack = -1; + } + } + if(voice_data[num_note].empty == 0 && cJSON_GetObjectItemCaseSensitive(note, "empty")->valueint == 1) + { + // this is a note that needs deleted + log_i("delete voice %d note %d", num_voice, num_note); + voice_data[num_note].empty = 1; + voice_data[num_note].length = 0; + voice_data[num_note].start_block = 0; + voice_data[num_note].isRack = -1; + voice_data[num_note].play_back_mode = ONE_SHOT; + voice_data[num_note].retrigger_mode = RETRIGGER; + voice_data[num_note].note_off_meaning = HALT; + voice_data[num_note].response_curve = RESPONSE_ROOT_SQUARE; + voice_data[num_note].priority = 0; + char *blank = ""; + memcpy(voice_data[num_note].name, blank, 1); } num_note++; } @@ -1162,9 +1210,19 @@ void updateRackConfig(cJSON *note){ ESP_ERROR_CHECK(emmc_write(buf,RACK_DIRECTORY_START_BLOCK,RACK_DIRECTORY_BLOCKS)); feedLoopWDT(); free(buf); - // cJSON_Delete(point); - // cJSON_Delete(break_points); - // cJSON_Delete(rack); +} + +void clear_rack(int isRack) +{ + log_i("clearing a rack that is being overwritten by a non-rack"); + struct rack_file_t *buf = (struct rack_file_t *)ps_malloc(RACK_DIRECTORY_BLOCKS * SECTOR_SIZE); + if(buf == NULL){log_i("malloc rack_file_t buf failed");} + ESP_ERROR_CHECK(emmc_read(buf,RACK_DIRECTORY_START_BLOCK,RACK_DIRECTORY_BLOCKS)); + buf[isRack].free = 1; + buf[isRack].num_layers = 0; + ESP_ERROR_CHECK(emmc_write(buf,RACK_DIRECTORY_START_BLOCK,RACK_DIRECTORY_BLOCKS)); + feedLoopWDT(); + free(buf); } void updatePinConfig(cJSON *config){ @@ -1190,6 +1248,31 @@ void updatePinConfig(cJSON *config){ cJSON_Delete(json); } +// void deleteNote(int voice, int note) +// { +// struct wav_file_t *voice_data = (struct wav_file_t*)ps_malloc(BLOCKS_PER_VOICE * SECTOR_SIZE); +// if(voice_data==NULL){log_e("not enough ram to alloc voice_data");} + +// size_t voice_start_block = WAV_LUT_START_BLOCK + (BLOCKS_PER_VOICE * voice); +// ESP_ERROR_CHECK(emmc_read(voice_data, voice_start_block, BLOCKS_PER_VOICE)); +// voice_data[note].empty = 1; +// ESP_ERROR_CHECK(emmc_write(voice_data,voice_start_block,BLOCKS_PER_VOICE)); +// if(voice_data[note].isRack > -1) +// { +// //its a rack so clear the rack data too +// struct rack_file_t *rack_data = (struct rack_file_t *)ps_malloc(RACK_DIRECTORY_BLOCKS * SECTOR_SIZE); +// if(buf == NULL){log_i("malloc rack_file_t buf failed");} +// ESP_ERROR_CHECK(emmc_read(rack_data, RACK_DIRECTORY_START_BLOCK, RACK_DIRECTORY_BLOCKS)); +// rack_data[voice_data[note].isRack].num_layers = 0; +// rack_data[voice_data[note].isRack].free = 1; +// char *blank = ""; +// memcpy(rack_data[voice_data[note].isRack].name, blank, 1); +// ESP_ERROR_CHECK(emmc_write(buf,RACK_DIRECTORY_START_BLOCK,RACK_DIRECTORY_BLOCKS)); +// free(rack_data); +// }; +// free(voice_data); +// } + void log_pin_config(void) { for(int i=0;i<14;i++) @@ -1218,8 +1301,6 @@ void updateMetadata(cJSON *config){ metadata.wlog_verbosity = cJSON_GetObjectItemCaseSensitive(json, "wLogVerbosity")->valueint; memcpy(&metadata.ssid,cJSON_GetObjectItemCaseSensitive(json, "wifiNetworkName")->valuestring,20); memcpy(&metadata.passphrase,cJSON_GetObjectItemCaseSensitive(json, "wifiNetworkPassword")->valuestring,20); - // metadata.ssid = cJSON_GetObjectItemCaseSensitive(json, "wifiNetworkName")->valuestring; - // metadata.passphrase = cJSON_GetObjectItemCaseSensitive(json, "wifiNetworkPassword")->valuestring; write_metadata(metadata); wlog_i("gv:%d scsp:%d rmsp:%d wso:%d wlv:%d", metadata.global_volume, @@ -1277,42 +1358,5 @@ size_t getNumSectorsInEmmc(void) void getSector(size_t i, uint8_t *buf) { - // uint8_t *sector = (uint8_t*)ps_malloc(SECTOR_SIZE); emmc_read(buf, i, 1); - // return sector; -} - -/** - * file system starts empty. - * when browser starts up, it receives JSON of the file system. - * browser sorts the system into its order on disk. - * when a file is added, browser takes its info and finds a spot for the file - * (trying not to overwrite files slated for deletion in this pass) - * and modifys the file system accordingly. - * when user hits UPLOAD, the browser sends the files one at a time, passing the start block as a header. - * Waver writes the files to disk - * the browser receives comfirmation that each file was uploaded successfully. - * If all the files are successfully uploaded, then the browser sends the new filesystem as JSON. - * WAVER receives, and decodes the JSON into its array of array of wav_file_t structs. - * It writes this new fileSystem into memory at the location given by the system_metadata, - * then it changes a flag in the metadata to indicate the current file_system location, - * which will be one of 2 possile locations, - * which are fixed, dedicated memory blocks. It alternates back and forth each time. - * then it send an OK response to the browser, and the browser reports that the upload was completed. - * Now the browser marks the new file_system as the defacto one, and erases the old record. -*/ - -/** - * FOR MIDI START SIGS - * the midi task receives uart data, and parses the midi. - * it then sends the midi_event_t(plus bank) struct via queue to the filesystem task. - * the filesystem task finds the file to play, and sends the file info and the midi_event_t(plus bank) struct - * to the wav_player task, indicating that it should start playing that file. - * the wav_player task finds a free buffer and starts the playback, taking note of the midi_event_t (plus bank) struct, - * and keeping a record that assiciates that file, buffer, and struct. - * - * FOR MIDI STOP SIGS - * the midi task receives uart data, and parses the midi. - * it then sends the midi_event_t(plus bank) struct via queue directly to the wav_player task. ??? - * the wav_player task looks up the buffer that is running that file, and flags it to be cleared at the next pass. -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/file_system.h b/src/file_system.h index d4617ac..b9437c2 100644 --- a/src/file_system.h +++ b/src/file_system.h @@ -342,6 +342,9 @@ size_t getNumSectorsInEmmc(void); void getSector(size_t i, uint8_t *buf); void restore_emmc(uint8_t* source, size_t size); void close_restore_emmc(); +char *print_voice_json(int numVoice); +char *print_config_json(); +// void deleteNote(int voice, int note); #ifdef __cplusplus } diff --git a/src/server.cpp b/src/server.cpp index eb44c16..559e09c 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -22,7 +22,7 @@ #include "file_system.h" #include "server.h" -extern "C" char* print_fs_json(); +// extern "C" char* print_fs_json(); extern "C" size_t find_gap_in_file_system(size_t size); extern "C" esp_err_t write_wav_to_emmc(uint8_t* source, size_t block, size_t size); extern "C" esp_err_t close_wav_to_emmc(void); @@ -177,9 +177,7 @@ void handleUpdateVoiceConfig(AsyncWebServerRequest *request, uint8_t *data, size log_i("done updateVoiceConfig()"); free(voice_config_json); log_i("done free"); - // vTaskDelay(1000); feedLoopWDT(); - // log_i("request->send done"); //wav_player_resume(); } } @@ -379,54 +377,33 @@ void handleNewRecoveryFirmware(AsyncWebServerRequest *request, uint8_t *data, si } } -void handleNewGUI(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ - //once - if(index==0){ - log_i("handle new GUI"); - AsyncWebHeader* firmware_slot_string = request->getHeader("slot-index"); - sscanf(firmware_slot_string->value().c_str(), "%d", &f_firmware_slot); - AsyncWebHeader* gui_size_string = request->getHeader("gui-size"); - sscanf(gui_size_string->value().c_str(), "%d", &f_gui_size); - AsyncWebHeader* gui_name_string = request->getHeader("gui-name"); - log_i("strcpy : %s",gui_name_string->value().c_str()); - strcpy(&f_gui_name[0], gui_name_string->value().c_str()); - log_i("gui %s is %u bytes",f_gui_name,f_gui_size); - - struct website_t *w = get_website_slot(f_firmware_slot); - w->length = f_gui_size; - strcpy(w->name, gui_name_string->value().c_str()); - feedLoopWDT(); - } - //always - if((f_firmware_size > MAX_FIRMWARE_SIZE) || (f_gui_size > MAX_WEBSITE_SIZE)){ - feedLoopWDT(); - request->send(400, "text/plain", "FILES TOO LARGE"); - return; - } - - Serial.print("."); - write_website_to_emmc(f_firmware_slot, data, len); - f_bytes_read += len; - - //done - if(index + len == total){ +void handleVoiceJSON(AsyncWebServerRequest *request){ + int numVoice; + AsyncWebHeader* voice_string = request->getHeader("voice"); + sscanf(voice_string->value().c_str(), "%d", &numVoice); + char *json = print_voice_json(numVoice); + feedLoopWDT(); + size_t size = strlen(json); + AsyncWebServerResponse *response = request->beginResponse("text/html", size, [size,json](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { feedLoopWDT(); - close_website_to_emmc(f_firmware_slot); - log_i("done"); - log_i("wrote %u bytes",f_bytes_read); - f_bytes_read = 0; - request->send(200, "text/plain", "all done"); - } + size_t toWrite = min(size - index, maxLen); + memcpy(buffer, json + index, toWrite); + if(index + toWrite == size){ + free(json); + } + return toWrite; + }); + response->addHeader("size",String(size)); + request->send(response); } -void handleFsjson(AsyncWebServerRequest *request){ - // //wav_player_pause(); - log_i("print_fs_json()"); - char *json = print_fs_json(); - log_i("done print_fs_json()"); +void handleConfigJSON(AsyncWebServerRequest *request){ + log_i("print_config_json()"); + char *json = print_config_json(); + log_i("done print_config_json"); feedLoopWDT(); size_t size = strlen(json); - // wlog_e("fs size is %d",size); + log_i("config JSON size is %d",size); AsyncWebServerResponse *response = request->beginResponse("text/html", size, [size,json](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { feedLoopWDT(); size_t toWrite = min(size - index, maxLen); @@ -517,6 +494,19 @@ void handleMain(AsyncWebServerRequest *request){ }); } +// void handleDeleteNote(AsyncWebServerRequest *request){ +// int numVoice; +// int numNote; +// AsyncWebHeader* voice_string = request->getHeader("voice"); +// sscanf(voice_string->value().c_str(), "%d", &numVoice); +// AsyncWebHeader* note_string = request->getHeader("note"); +// sscanf(note_string->value().c_str(), "%d", &numNote); + +// deleteNote(numVoice, numNote); + +// request->send(200, "text/plain", "all done delete"); +// } + void handleRPC(AsyncWebServerRequest *request){ cJSON* json = cJSON_Parse(request->getHeader("json")->value().c_str()); on_rpc_in(json); @@ -533,16 +523,16 @@ void handleBootFromEmmc(AsyncWebServerRequest *request){ bootFromEmmc(index); } -void handleMulti(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ - if(!index){ - log_i("UploadStart: %s",filename.c_str()); - } - log_i("%s",filename.c_str()); - if(final){ - request->send(204); - log_i("done %s",filename.c_str()); - } -} +// void handleMulti(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ +// if(!index){ +// log_i("UploadStart: %s",filename.c_str()); +// } +// log_i("%s",filename.c_str()); +// if(final){ +// request->send(204); +// log_i("done %s",filename.c_str()); +// } +// } void _server_pause(){ ws.closeAll(); @@ -590,10 +580,22 @@ void server_begin() { handleEmmcGUI ); + // server.on( + // "/fsjson", + // HTTP_GET, + // handleFsjson + // ); + server.on( - "/fsjson", + "/voicejson", HTTP_GET, - handleFsjson + handleVoiceJSON + ); + + server.on( + "/configjson", + HTTP_GET, + handleConfigJSON ); server.on( @@ -666,26 +668,18 @@ void server_begin() { handleNewRecoveryFirmware ); - server.on( - "/addgui", - HTTP_POST, - [](AsyncWebServerRequest * request){request->send(204);}, - NULL, - handleNewGUI - ); - server.on( "/bootFromEmmc", HTTP_GET, handleBootFromEmmc ); - server.on( - "/multi", - HTTP_POST, - [](AsyncWebServerRequest *request){request->send(204);}, - handleMulti - ); + // server.on( + // "/multi", + // HTTP_POST, + // [](AsyncWebServerRequest *request){request->send(204);}, + // handleMulti + // ); server.on( "/addrack",