Fast way to mix 2 signed 8 bit values (int8_t)

I have this little synthesizer oscillator thing which can read one of several wavetables and output to a PWM pin. it works fine, but i would like to mix 2 of the wave tables together by a third amount

it happens inside an ISR so it has to be quick, and even mixing the 2 values equally

outputvalue = (outputvalue1 + outputvalue2)/2;

takes too long. and starts causing missed notes as the ISR starts taking up too much time for the rest of the program to do much.
(it recieves MIDI and generates an envelope and LFO values)

What i would like to have is the outputvalue mixed between the value of outputvalue1 and outputvalue2; with a third value "mix" determining the proportion of each (can be any type, whatever would work the best)

I was wondering if there might be some bitwise trickery that would work much faster.

Best regards
DeFex

Well, the obvious one is ">>1" instead of "/2", but it is so obvious that the compiler probably already does it.
Can't you mix it in the background, i.e. a one-off, instead of on-the-fly?

If i only wanted a half way value i could do that but i would like to vary the ratio of the 2 values with a mix value. it can not be pre computed

the amount of the "mix" value may also be changed on the fly by a knob, LFO, or envelope generator.

outputvalue = (outputvalue1>>1)+ (outputvalue2>>1);

should be faster at the expense of being slightly more noisy, try it and see if it works.

Thank you I will try it. it will at least give me 122 different waveforms instead of 12, (not including when they are both set to the same wavetable)

i would still like to actually vary the level of them though. (one gos up while the other gos down)

it sounds great to modulate that.

never mind, looking at the code some more, i think i know what to do :smiley:
by the way the actual sound generating part of the oscillator is based on this

http://adrianfreed.com/content/arduino-sketch-high-frequency-precision-sine-wave-tone-sound-synthesis

Ok that has kind of worked, i have even got it mixing by varying amounts, unfortunately because they are signed bytes the ">>1" does not work properly because the leftmost sign bit is always kept when that operation is done.

I will change all my wavetables to uint8_t and convert them to int8_t at the very end.

edit:
It worked! thanks :wink: it has no problem reading 2 wavetables at once and adding them

now if i can modulate the phase of the second one i can actually get a detuned second oscillator for extra phat! :sunglasses:

even got the unison working, and since then i can have both osc playing a different pitch for octaves, etc.

now i just need a little help with the prescaler, i would like to double the frequency of the PWM

ill ask in a seperate thread though.