<commit_before>#include "SDLMixer.hh"

#define MIN(a,b) (a<b)?a:b;

namespace RAGE
{
	namespace SDL
	{

		std::vector<const Sound*> Mixer::_channels;
		std::vector<int> Mixer::_channelscursor;
		std::vector<bool> Mixer::_activechannels;
		std::vector<bool> Mixer::_loopchannels;
		
		//class Mixer
		void Mixer::callback(void *userdata, Uint8 *stream, int len)
		{
			Uint8 *waveptr;
			int    waveleft;

			int i;
			//Going throw channels list to find out the active ones
			for (i = 0; i < _activechannels.size() && _activechannels[i]; i++)
			{
				//TODO : replace mixaudio with my own mix function, in case there is no SDL_mixer
				waveptr = _channels[i]->_buf + _channelscursor[i] ;
				waveleft = MIN(_channels[i]->_length - _channelscursor[i],len);//test end of sound buffer

				SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
				_channelscursor[i] += waveleft;
				if (_channelscursor[i] >= _channels[i]->_length)
				{
					_channelscursor[i]=0;
							
					//in case of loop
					if (_loopchannels[i])
					{
						stream += waveleft;
						len -= waveleft;
						waveleft = MIN(len,_channels[i]->_length);
						SDL_MixAudio(stream, _channels[i]->_buf, waveleft, SDL_MIX_MAXVOLUME);
						_channelscursor[i] += waveleft;
					}
					else
					{
						_activechannels[i] = false;
					}
					
				}
					
					//just insert the usual full chunk
//To debug, because SDL_MixAudio doesnt know all possible audio formats
// 					for (int j=0; j< len; j++)
// 					{
// 						stream[j] = waveptr[j];
// 					}
					
						
// 					Log << nl <<" Channel "<< i<<" after callback at " << _channelscursor[i] << " in [0 .. "<< _channels[i]->_length << "]";
			}

		}
		
		Mixer::Mixer(int frequency,unsigned short channels,unsigned short samples, bool SDLemulated) : _autoConvert(SDLemulated)
		{
#ifdef DEBUG
		Log << nl << "Mixer::Mixer(" << frequency << ", " << channels << ", " << samples << ", " << SDLemulated << ") called";
#endif


			SDL_AudioSpec* desired = new SDL_AudioSpec();<commit_msg>solved bug (again) that only allow the 1st channel to play<commit_after>#include "SDLMixer.hh"

#define MIN(a,b) (a<b)?a:b;

namespace RAGE
{
	namespace SDL
	{

		std::vector<const Sound*> Mixer::_channels;
		std::vector<int> Mixer::_channelscursor;
		std::vector<bool> Mixer::_activechannels;
		std::vector<bool> Mixer::_loopchannels;
		
		//class Mixer
		void Mixer::callback(void *userdata, Uint8 *stream, int len)
		{
			Uint8 *waveptr;
			int    waveleft;

			int i;
			//Going throw channels list to find out the active ones
			for (i = 0; i < _activechannels.size() ; i++)
			{
				if (_activechannels[i])
				{
					//TODO : replace mixaudio with my own mix function, in case there is no SDL_mixer
					waveptr = _channels[i]->_buf + _channelscursor[i] ;
					waveleft = MIN(_channels[i]->_length - _channelscursor[i],len);//test end of sound buffer

					SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
					_channelscursor[i] += waveleft;
					if (_channelscursor[i] >= _channels[i]->_length)
					{
						_channelscursor[i]=0;
								
						//in case of loop
						if (_loopchannels[i])
						{
							stream += waveleft;
							len -= waveleft;
							waveleft = MIN(len,_channels[i]->_length);
							SDL_MixAudio(stream, _channels[i]->_buf, waveleft, SDL_MIX_MAXVOLUME);
							_channelscursor[i] += waveleft;
						}
						else
						{
							_activechannels[i] = false;
						}
						
					}
				}
					
					//just insert the usual full chunk
//To debug, because SDL_MixAudio doesnt know all possible audio formats
// 					for (int j=0; j< len; j++)
// 					{
// 						stream[j] = waveptr[j];
// 					}
					
						
// 					Log << nl <<" Channel "<< i<<" after callback at " << _channelscursor[i] << " in [0 .. "<< _channels[i]->_length << "]";
			}

		}
		
		Mixer::Mixer(int frequency,unsigned short channels,unsigned short samples, bool SDLemulated) : _autoConvert(SDLemulated)
		{
#ifdef DEBUG
		Log << nl << "Mixer::Mixer(" << frequency << ", " << channels << ", " << samples << ", " << SDLemulated << ") called";
#endif


			SDL_AudioSpec* desired = new SDL_AudioSpec();
{'fraction_non_alphanumeric': 0.14791464597478177, 'fraction_numerical': 0.0031522793404461687, 'mean_word_length': 2.90625, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '619385', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1403, 'n_words': 338}