I mix only two samples but I do if over and over for how many samples are playing while adding to the current sample. I add and then half each sample, so that the result is 50% mix of the two, but if either of the samples are zero, then I just select the other so that the volume doesn’t suffer is only one sample is being played.
Constant 127 is just as audible as constant 0. Amplitude is the absolute distance from whatever your wave oscillates around. You can make sound by oscillating between 0 and 10, in which case 5 would be the amplitude.
I’m more confused about this that I was when I started. It seems that what I’m doing works, but it’s wrong.
I tried detecting 127 as silence, but that resulted in distortion.
My above implementation is also not good enough, if multiple sounds are playing, the first ones the code adds will be quieter than the later ones as their volume will be adjusted more than once.
Because 127 can and will be used as a valid instant signal value, like any value of the range could be. You can’t use a single, instant value as a way to detect silence!
Silence = a whole, uninterrupted succession of the same value (like 127, but could actually be anything).
That makes things more complex of course. You might want to take the problem from another point of view - isn’t it silence when you play nothing?
Additionally, detecting the same value only twice isn’t a valid option either. That could happen frequently as well, because of how signals work. It’s especially true with low resolution output like 8bit one is.
If you really want to detect perfect silence, you have to calculate the lowest frequency that can be heard AND outputted by the pokitto.
Let’s say it’s 100hz: you’d have to check for 10ms (1/100 hz) of output for silence.
If no variation happens in those 10ms, then you won’t have any sound coming out.
What does it imply thought? Well you have to retain those 10ms somewhere in memory. At a sampling rate of 8Kb/s - 1KB/s, that means you’ll have to retain the last 10 bytes and check on them on a regular basis. At 16Kb/s, it’ll be 20 bytes, and so on. If you want to have a lower cut frequency, that’ll be more bytes too.