#include #include #include #include #include #include #include #include #include #include #include #include #include #include "hardware.h" #include "user_io.h" #include "spi.h" #include "cfg.h" #include "file_io.h" #include "menu.h" #include "audio.h" static uint8_t vol_att = 0; static uint8_t corevol_att = 0; unsigned long vol_set_timeout = 0; static int has_filter = 0; void send_volume() { get_volume(); get_core_volume(); if (!(vol_att & 0x10) && vol_att + corevol_att > 7) vol_att = 7 - corevol_att; spi_uio_cmd8(UIO_AUDVOL, vol_att + corevol_att); } int get_volume() { return vol_att & 0x17; } int get_core_volume() { corevol_att &= 7; if (corevol_att > 6) corevol_att = 6; return corevol_att; } void set_volume(int cmd) { vol_set_timeout = GetTimer(1000); vol_att &= 0x17; if (!cmd) vol_att ^= 0x10; else if (vol_att & 0x10) vol_att &= 0xF; else if (cmd < 0 && vol_att < 7) vol_att += 1; else if (cmd > 0 && vol_att > 0) vol_att -= 1; send_volume(); if (vol_att & 0x10) { Info("\x8d Mute", 1000); } else { char str[32]; memset(str, 0, sizeof(str)); sprintf(str, "\x8d "); char *bar = str + strlen(str); int vol = get_core_volume(); memset(bar, 0x8C, 8 - vol); memset(bar, 0x7f, 8 - vol - vol_att); Info(str, 1000); } } void set_core_volume(int cmd) { vol_set_timeout = GetTimer(1000); corevol_att &= 7; if (cmd < 0 && corevol_att < 6) corevol_att += 1; if (cmd > 0 && corevol_att > 0) corevol_att -= 1; send_volume(); } void save_volume() { if (vol_set_timeout && CheckTimer(vol_set_timeout)) { vol_set_timeout = 0; FileSaveConfig("Volume.dat", &vol_att, 1); static char cfg_name[128]; sprintf(cfg_name, "%s_volume.cfg", user_io_get_core_name_ex()); FileSaveConfig(cfg_name, &corevol_att, 1); } } static char filter_cfg_path[1024] = {}; static char filter_cfg[1024] = {}; static void setFilter() { fileTYPE f = {}; has_filter = spi_uio_cmd(UIO_SET_AFILTER); if (!has_filter || !filter_cfg[0]) return; sprintf(filter_cfg_path, AFILTER_DIR"/%s", filter_cfg + 1); if (filter_cfg[0] && FileOpen(&f, filter_cfg_path)) { char *buf = (char*)malloc(f.size + 1); if (buf) { memset(buf, 0, f.size + 1); int size; if ((size = FileReadAdv(&f, buf, f.size))) { int line = 0; spi_uio_cmd_cont(UIO_SET_AFILTER); char *end = buf + size; char *pos = buf; while (pos < end && line < 8) { char *st = pos; while ((pos < end) && *pos && (*pos != 10)) pos++; *pos = 0; while (*st == ' ' || *st == '\t' || *st == 13) st++; if (*st == '#' || *st == ';' || !*st) pos++; else { if (line == 0 || line == 2 || line == 3 || line == 4) { int val = 0; int n = sscanf(st, "%d", &val); printf("got %d values: %d\n", n, val); if (n == 1) { spi_w((uint16_t)val); if (line == 0) spi_w((uint16_t)(val >> 16)); line++; } } else if (line == 1) { double val = 0; int n = sscanf(st, "%lg", &val); printf("got %d values: %g\n", n, val); if (n == 1) { int64_t coeff = 0x8000000000 * val; printf(" -> converted to: %lld\n", coeff); spi_w((uint16_t)coeff); spi_w((uint16_t)(coeff >> 16)); spi_w((uint16_t)(coeff >> 32)); line++; } } else { double val = 0; int n = sscanf(st, "%lg", &val); printf("got %d values: %g\n", n, val); if (n == 1) { int32_t coeff = 0x200000 * val; printf(" -> converted to: %d\n", coeff); spi_w((uint16_t)coeff); spi_w((uint16_t)(coeff >> 16)); line++; } } } } DisableIO(); } free(buf); } } } void load_volume() { FileLoadConfig("Volume.dat", &vol_att, 1); if (!is_menu()) { static char cfg_name[128]; sprintf(cfg_name, "%s_volume.cfg", user_io_get_core_name_ex()); FileLoadConfig(cfg_name, &corevol_att, 1); } send_volume(); sprintf(filter_cfg_path, "%s_afilter.cfg", user_io_get_core_name_ex()); if (!FileLoadConfig(filter_cfg_path, &filter_cfg, sizeof(filter_cfg) - 1) || filter_cfg[0] > 1) { memset(filter_cfg, 0, sizeof(filter_cfg)); } setFilter(); } int audio_filter_en() { return has_filter ? filter_cfg[0] : -1; } char* audio_get_filter() { return filter_cfg + 1; } void audio_set_filter(char *name) { strcpy(filter_cfg + 1, name); sprintf(filter_cfg_path, "%s_afilter.cfg", user_io_get_core_name_ex()); FileSaveConfig(filter_cfg_path, &filter_cfg, sizeof(filter_cfg)); setFilter(); } void audio_set_filter_en(int n) { filter_cfg[0] = n ? 1 : 0; sprintf(filter_cfg_path, "%s_afilter.cfg", user_io_get_core_name_ex()); FileSaveConfig(filter_cfg_path, &filter_cfg, sizeof(filter_cfg)); setFilter(); }