If the samples are normalized to the same level and stored in a signed format, so that the "zero level" is 0 or 0.0 , the answer is quite simple:
S_C = (S_A / 2) - (S_B / 2);
for each sample S_A and S_B in and B.
If you use unsigned values โโfor samples, you will need to do more work: first, you need to convert them to a value with a sign with a zero center (for example, if you have 16-bit unsigned samples, subtract 32768 from each), then apply the formula and then convert them back to unsigned format. Be careful with overflow - here is an example of how to do conversions for the above 16-bit patterns:
#define PCM_16U_ZERO 32768 short pcm_16u_to_16s(unsigned short u) { return (u < PCM_16U_ZERO) ? (short)u - PCM_16U_ZERO : (short)(u - PCM_16U_ZERO); } unsigned short pcm_16s_to_16u(short s) { return (unsigned short)s + PCM_16U_ZERO; }
source share