Ambika firmware with avr-g++ 9.1

Hello again! I have made some discoveries:

  • I believe I have found the problem with the Voxelito patch, and it is actually from the RenderVowel() function in (so any use of the vowel wave should sound bad). The problematic line is
    uint8_t x = S16ClipS8(4 * result) + 128;
    Specifically, the (optimised) S16ClipS8() function in op.h doesn’t work properly. You should replace it with the unoptimised one: return x < -128 ? -128 : (x > 127 ? 127 : x);. The RenderVowel() function is the only time ever in the Ambika code that the S16ClipS8 function is called, which I think is why it was hard to pick this one up.
    My Voxelito patch sounds good now, but could you try this change out in your code and see if it fixes the problem?
  • As for the RenderCz* functions: interestingly, they have all in common that they use ReadSample(), which is used nowhere else in the code. I wonder if manually inlining the ReadSample() function by hand - i.e. replacing calls to it with the corresponding ResourcesManager::Lookup call might improve things? Anyway, I used the UPDATE_PHASE_MORE_REGISTERS trick that you suggested, and the ‘Badtaste’ patch sounds good, so no real worries here.

So at this point I’d say that the new voicecard firmware is sounding just like the stock one! I’ll upload a hex file here in case anyone wants to test. (36.3 KB)

Hi @machfour, nice you’re still on it!

I replaced S16ClipS8 with the unoptimized version, but unfortunately I still get a garble sound :thinking:, with -O2…

[EDIT] I tried without the clipping function: uint8_t x = result + 128; The sound has a lower level, as expected, but is still garbled. S16ClipS8 might be faulty, but there might be another culprit…

I actually replaced the entire call because it seemed simpler to calculate the value directly. Here is how the end of that function looks in my code now:

// (-32, 32) -> (0, 255)
//uint8_t x = S16ClipS8(4 * result) + 128;
uint8_t x;
if (result <= -32) {
    x = 0;
} else if (result >= 32) {
    x = 255;
} else {
    x = U8(result + 32) << 2u; // U8(x) is just static_cast<uint8_t>(x)
*buffer++ = x;
*buffer++ = x;
size--; // second decrement

Let me know if this works or doesn’t work. By the way, I am compiling with -O2 as well.

If the sound is “crackling” it may simply be a matter of cpu overload due to insufficient optimization since vowel is one of the most computationally demanding algorithms. (Sorry for pointing out the “bleeding obvious”.)

Great work in this thread!

still no luck.
Now that’s strange, if you succeeded, there is no reason I cannot. Here are the g++ options I have:

/usr/local/bin/avr-g++ -c -mmcu=atmega328p -I. -DF_CPU=20000000 -D__PROG_TYPES_COMPAT__ -fdata-sections -ffunction-sections -fshort-enums -fno-move-loop-invariants -DDISABLE_DEFAULT_UART_RX_ISR -DDISABLE_WAVETABLE_LFOS -Wall -Wextra -Winline -pedantic -g -Wno-deprecated-declarations -Wno-attributes -O2 --param max-inline-insns-size=31    -DATMEGA328P -DSERIAL_RX_0 -mcall-prologues -fno-exceptions voicecard/ -o build/ambika_voicecard/oscillator.o

with this avr-g++:
gcc version 9.3.0 (Homebrew AVR GCC 9.3.0)

yes, the question is which optimization that was applied with 4.3, is not anymore with 9.x?
The fix with UPDATE_PHASE_MORE_REGISTERS suggests that the use of registers(?) instead of object fields is improving things. Vowel also seems to heavily use tables. So, it may be related to the way the code accesses memory with 9.x, or with recent AVR-related headers (pgm_read_word maybe?).
These are all speculations of course.
First I should find out why machfour’s code works for him and not for me…

It was not really ‘crackling’, which I believe I did hear from the CZ* functions, before I added UPDATE_PHASE_MORE_REGISTERS. It was more like noticeable distortion, which varied in quality when the oscillator parameter was changed.
Hm, it after some testing, it seems I wrongly accused the S16ClipS8 function of not working as intended. But I think it does rely on implementation defined behaviour… Just for a sanity check, could you please check that the firmware I uploaded before sounds fine on the Voxelito patch?

Here are my compilation flags, I have changed them a bit. By the way, I am also using 9.3.0.

/usr/bin/avr-g++ -c -g -O2 -std=c++2a -I. -mmcu=atmega328p -Wall -Wextra -pedantic -Wno-narrowing -Winline  -ffreestanding -fno-common -fshort-enums -fno-move-loop-invariants -nostdlib -fverbose-asm -fdata-sections -ffunction-sections -flto -mrelax -mcall-prologues -DF_CPU=20000000 -DDISABLE_DEFAULT_UART_RX_ISR -DDISABLE_WAVETABLE_LFOS  -DATMEGA328P -DSERIAL_RX_0  -fno-exceptions -fno-non-call-exceptions -fno-use-cxa-atexit -fno-rtti 

Removing the irrelevant flags, we get

/usr/bin/avr-g++ -O2 -std=c++2a -Winline -ffreestanding -fno-common -fshort-enums -fno-move-loop-invariants -nostdlib -fverbose-asm -fdata-sections -ffunction-sections -flto -mrelax -mcall-prologues -fno-exceptions -fno-non-call-exceptions -fno-use-cxa-atexit -fno-rtti

The most significant differences I can see are that i used -mrelax and -flto which may improve things for you. See if that helps. Otherwise, you are welcome to dig around / ask questions about my (significantly refactored) code to see if anything else works for you.

(unfortunately,) my ambika is not dismantled anymore, which means my only way to upload a voicecard firmware is to use the SD card method with a .bin: can you send the .bin instead please?

I don’t have a bin file in my build directory… it’s just made by running avr-objcopy -I ihex -O binary ambika_voicecard.hex ambika_voicecard.bin… right?

the .hex and .bin file are made from .elf.
can you try make bin maybe?

The command was make build/ambika_voicecard/ambika_voicecard.bin, but I checked using diff and it’s exactly the same output as if you run the command I gave before. Anyway, here’s the bin file. (24.4 KB)

sorry I misread your previous answer :man_facepalming: you were right about the command.
and I confirm that your firmware is working! :grinning:

1 Like

Awesome! Well as you said, at least we now know that it’s possible to get it working :laughing:

I’m currently working on some refactoring of the controller. Hopefully I haven’t introduced any bugs…

now the question is: what exactly made your firmware work?

Also, I find it too bad that we need to resort to UPDATE_PHASE_MORE_REGISTERS: either the genuine firmware was very close to the max capacity of the proc and any change would have exhibit garbled sound, or avr-gcc 9.x produces less efficient code, which is a disappointment… Without better debug tools (and better skills for me), it is difficult to understand what happens here.

(Other related news: the gcc crew has obsoleted support for avr in the next version. There is a bounty to help port the avr backend to the new gcc backend engine, but the current amount seems far from enough…)

Yeah, the fact that we’re working in an embedded system context makes it really hard to get a good understanding of what’s going on at runtime. I guess it’s possible to use a simulator by developing software dummies for the necessary hardware components… but that’s a lot more work than it’s worth now.

Are they really deprecating AVR support in GCC 10??? That is so sad :disappointed: I knew there was a lack of GCC developers from the AVR backend but I hoped it would pick up somehow.

Update: I have the controller firmware also working now, and somehow down to 50908 bytes! I’m not sure what happened here, it seemed to just shrink magically after some refactoring :smile:

If anyone wants to test out my firmware, I will post .bin files below (and .elf, for the curious). It shouldn’t be functionally any different from the official v1.0, but just for fun I bumped the version number to 1.1, because I have spent way too many hours on this, and I feel like it’s worth celebrating a bit :stuck_out_tongue_closed_eyes: Of course, please let me know if you find any problems! (146.1 KB)


Thats great news! Will you put this on GitHub? I still have some additions I’d like to make for integrating Ambika better with DAWs but its impossible working with a couple of bytes free. If this is glitch free, it would be just what the doctor ordered.

It’s on Github! I have been experimenting with some things (e.g. FM waveforms, polyblep) so I’m not sure if it’s truly glitch free. But you could try to go back a couple of commits and see what I’ve changed/undo the changes.

I haven’t been working on this recently but if I get time I can try and fix it properly.

Very cool. Do you remember what version of the toolchain did you use to compile to 50908 bytes?

Hey MachFour … brilliant… could you do a quick .bin build for those not AVR tooled up… I also have 2 polivoks in my Ambika … Its a simple character swap to make the rotarys go the right way … thnks :] Dan

If you’re not trying to modify the source code, there isn’t really any difference between the firmware I posted and the official one you can download. But if you still want to try out my firmware, the zip file is here in this thread, just scroll up a few posts. It has a .bin file in it.