Mixing dynamic amount of samples

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

Hi guys,

I'm quite new to audio programming and music theorie and it's not the thing which is my focus on, but I think here are a lot of people who could help me out... My problem is quite simple. I'm programming a sound driver for my hobby operating system. I managed to write a AC97 driver and now the time has come to mix diffrent sounds/samples in my kernel.

I'm looking for a simple formula to add up a dynamic amount of samples with the smallest distortion. How would you implement this? I hope my question is not to stupid for this place... I hope this is even the right place for such questions ;)

I would like to avoid floating point operations as good as possible. I could also look ahead about 1024 samples 43 ms of delay before a sound is played seems to be ok.

Thank you for your patience and for your help :)

Post

You just add the signals. Nothing else needs to be done. If you need to scale the levels this should be done manually with a mixer control of some sort.

Automatic gain control is always an annoying and rarely useful feature. It is often something beginners will assume is required, without realizing that the sum of two uncorrelated signals is between +3db and +6db. There is no way to predict or even to measure reliably which is the case, and so the only practical solution is to allow manual user control of levels. This is why the audio interface has a level control and why level for each stream being mixed can be manually controlled.
Free plug-ins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.

Post

Mirimaro wrote:Hi guys,

I'm quite new to audio programming and music theorie and it's not the thing which is my focus on, but I think here are a lot of people who could help me out... My problem is quite simple. I'm programming a sound driver for my hobby operating system. I managed to write a AC97 driver and now the time has come to mix diffrent sounds/samples in my kernel.

I'm looking for a simple formula to add up a dynamic amount of samples with the smallest distortion. How would you implement this? I hope my question is not to stupid for this place... I hope this is even the right place for such questions ;)

I would like to avoid floating point operations as good as possible. I could also look ahead about 1024 samples 43 ms of delay before a sound is played seems to be ok.

Thank you for your patience and for your help :)

Code: Select all

#define MAX_AUDIO_BLOCK 512
short app_buffer[MAX_AUDIO_BLOCK * 2]; // *2 for stereo
int   mix_buffer[MAX_AUDIO_BLOCK * 2];

int nb_callbacks;
MY_CALLBACK_TYPE callbacks[MAX_NB_APPS];
int app_gain [MAX_NB_APPS];

#define TOTAL_GAIN_SHIFT 10

void mix(int nb_samples, short *out_buffer)
{
	memset(mix_buffer, 0, 4*nb_samples*2);

	for(int i=0; i<nb_callbacks; i++)
	{
		callbacks[i](nb_samples, app_buffer);
		for(int j=0; j<nb_samples; j++)
		{
			mix_buffer[j*2  ] += app_buffer[j*2  ] * app_gain[i];
			mix_buffer[j*2+1] += app_buffer[j*2+1] * app_gain[i];
		}
	}

	for(int j=0; j<nb_samples; j++)
	{
		int temp_l = mix_buffer[j*2  ] >> TOTAL_GAIN_SHIFT;
		int temp_r = mix_buffer[j*2+1] >> TOTAL_GAIN_SHIFT;

		if(temp_l > 32767)
			temp_l = 32767;
		else if(temp_l < -32768)
			temp_l = -32768;

		if(temp_r > 32767)
			temp_r = 32767;
		else if(temp_r < -32768)
			temp_r = -32768;

		out_buffer[j*2  ] = (short)temp_l;
		out_buffer[j*2+1] = (short)temp_r;
	}	
}

Post Reply

Return to “DSP and Plugin Development”