xiphonics / picoTracker

BSD 3-Clause "New" or "Revised" License
213 stars 18 forks source link

Making it easier to add new Instrument types #126

Closed shockdesign closed 2 weeks ago

shockdesign commented 8 months ago

There are a few issues about adding new synths to picoTracker

Potentially a SID synth, etc and this issue - https://github.com/democloid/picoTracker/issues/36

I think we need a way to make Instruments easier to manage this.

Including on the Instrument screen a better way to switch what type of Instrument something is, etc... and how we define what options are available per Instrument.

i.e. Sample instruments have the sample name/file, volume, effect, etc?

Synth instruments would have specific variables as well..

Anyone with thoughts on this?

democloid commented 8 months ago

The issue of multiple instruments is tricky, we'll have to evaluate what we want to ultimately support as "official" instruments.

Main problem as with most things is memory, right now sample instruments use about 8.5kb of memory each, and MIDI 1.5kb IIRC. We use about 160Kb of memory for instruments. This is for number of instruments available, not necessarily used. To add more instruments we can either statically add them, find memory somewhere and add a number of whatever, or have dynamically loaded instruments.

I have a rough branch implementing the latter and it works pretty well but there are a few things that are tricky:

  1. How many instruments are supported? we can either use the max memory the biggest instrument uses, divide total instrument memory by that and support that. Right now that would be ~160Kb/8.5 = 18-19 instruments. (this would also leave out bigger instruments, my test with braids uses ~20Kb per instrument)
  2. Otherwise you have instrument memory slices of sorts and define how many slices does an instrument use. Say the slice is 2.5Kb, then sample insturments use 4 slices and MIDI use 1. You'd have 64 slices on 160Kb and can use 64 MIDI instruments, or 16 sample instruments. This ends up being less than today, total (as you can now run 16 and 16).
  3. Can just not use slices and just use actual memory, but...
  4. You need to ensure forward compatibility forever. You don't want to allow people to create a project with x amount of instruments of one type today that will not be supported in the future. If a sample instrument is 4 slices, then it has to be 4 slices always, so if it needs to use more memory for some feature, then we'd need to adjust either the slice size or optimize the instrument. With option 3 it becomes trickier, and with more instrument types even more as you need to support any combination.
  5. Furthermore, memory size of instruments may change with compiler versions and/or settings, so need to leave some slack.
maks commented 8 months ago

I've been meaning to get back to working on instrument memory usage as I think its very bloated at the moment and can get significant savings in this area. My back of envelope calcs say that ram usage per instrument excluding name & linked table should be less than 64 bytes! The current 8.5kB is just completely out there. Midi should be less than 16 bytes.

Given the above gutting the current implementation and fixing the instrument data structures should allow for large numbers of instruments, future synth instruments and still use less ram than the do currently.

To give some more context, I got this reply from one of the developers of Amy:

re: RAM. We should figure this out "for real", and we have an issue to track it set up, but back of the envelope is at 120 oscs and reverb and chorus on, on an arm64 machine, we alloc a total of 117KB. the sizeof(synthinfo) is 437 bytes. in practice, it looks like each oscillator takes about 512 bytes of RAM FM voices take 9 oscillators So assume 4.5KB or so per FM voice. But also know there's some default stuff getting alloced no matter what. i'd like to get that # number down but it works fine on the chips we target (esp32s, arm cortex 0s/rpi, NXP/teensy) (also, everything is malloced at runtime with a fixed AMY_OSCS setting, it's not allocating dynamically as you add new voices. You'll have to set AMY_OSCS yourself lower if you want to save ram)

So having full DX7 compatible FM instruments will only cost us 4.5KB per instrument, but just more basic 4-OP (like eg. Digitone) would only be around 2.5kB.

Note also that they are able to fit their whole engine including chorus & reverb fx into only 117kB!

maks commented 2 weeks ago

done as of #219