|
| 1 | +// |
| 2 | +// Copyright(C) 2025 Simon Howard |
| 3 | +// |
| 4 | +// You can redistribute and/or modify this program under the terms of |
| 5 | +// the GNU General Public License version 2 as published by the Free |
| 6 | +// Software Foundation, or any later version. This program is |
| 7 | +// distributed WITHOUT ANY WARRANTY; without even the implied warranty |
| 8 | +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 9 | +// |
| 10 | + |
| 11 | +#include <assert.h> |
| 12 | +#include <ncurses.h> |
| 13 | + |
| 14 | +#include "common.h" |
| 15 | +#include "fs/vfile.h" |
| 16 | +#include "genmidi/genmidi.h" |
| 17 | + |
| 18 | +#define HEADER_VALUE "SBI\x1a" |
| 19 | + |
| 20 | +static const int sbi_field_mapping[] = { |
| 21 | + INSTR_M_AM_VIBRATO_EG, // 0 |
| 22 | + INSTR_C_AM_VIBRATO_EG, // 1 |
| 23 | + INSTR_M_KSL, // 2 ; _VOLUME is |ed in below |
| 24 | + INSTR_C_KSL, // 3 ; _VOLUME is |ed in below |
| 25 | + INSTR_M_ATTACK_DECAY, // 4 |
| 26 | + INSTR_C_ATTACK_DECAY, // 5 |
| 27 | + INSTR_M_SUSTAIN_RELEASE, // 6 |
| 28 | + INSTR_C_SUSTAIN_RELEASE, // 7 |
| 29 | + INSTR_M_WAVEFORM, // 8 |
| 30 | + INSTR_C_WAVEFORM, // 9 |
| 31 | + INSTR_FEEDBACK_FM, // 10 |
| 32 | +}; |
| 33 | + |
| 34 | +bool GENMIDI_WriteSBI(struct genmidi_instrument *instr, bool voice2, VFILE *out) |
| 35 | +{ |
| 36 | + uint8_t *voicedata; |
| 37 | + uint8_t buf[16]; |
| 38 | + int i; |
| 39 | + |
| 40 | + if (vfwrite(HEADER_VALUE, 4, 1, out) != 1 || |
| 41 | + vfwrite(instr->name, 32, 1, out) != 1) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + if (voice2) { |
| 46 | + voicedata = instr->voice2; |
| 47 | + } else { |
| 48 | + voicedata = instr->voice1; |
| 49 | + } |
| 50 | + |
| 51 | + memset(buf, 0, sizeof(buf)); |
| 52 | + for (i = 0; i < arrlen(sbi_field_mapping); ++i) { |
| 53 | + buf[i] = voicedata[sbi_field_mapping[i]]; |
| 54 | + } |
| 55 | + |
| 56 | + buf[2] |= voicedata[INSTR_M_VOLUME]; |
| 57 | + buf[3] |= voicedata[INSTR_C_VOLUME]; |
| 58 | + buf[12] = voicedata[INSTR_NOTE_OFFSET]; |
| 59 | + |
| 60 | + if ((instr->hdr.flags & GENMIDI_FLAG_FIXED) != 0) { |
| 61 | + buf[11] = 1; |
| 62 | + buf[13] = instr->hdr.fixed_note; |
| 63 | + } |
| 64 | + |
| 65 | + return vfwrite(buf, 16, 1, out) == 1; |
| 66 | +} |
0 commit comments