/**********************************************************************
This file is part of Crack dot Com's free source code release of Golgotha.
for information about compiling & licensing issues visit this URL
 If that doesn't help, contact Jonathan Clark at 
  golgotha_source@usa.net (Subject should have "GOLG" in it) 
***********************************************************************/

#include "error/error.hh"
#include "error/alert.hh"
#include "sound/linux/linux_sound.hh"
#include "loaders/wav_load.hh"
#include "string/string.hh"
#include "file/file.hh"

#include 
#include 
#include 
#include 
#include 

enum { LINUX_SOUND_BUFFER_BITS = 11 };
enum { LINUX_SOUND_SAMPLE_SPEED = 11025 };

// Thread State Enumeration
enum
{
  LINUX_SOUND_UNINITIALIZED,
  LINUX_SOUND_RUNNING,
  LINUX_SOUND_REQUEST_STOP,
  LINUX_SOUND_STOPPED,
};

static sw16 volume_table[I4_SOUND_VOLUME_LEVELS][256];
static sw32 mix_buffer[1<data = (w8*)info.data;
          snd->sample_rate = linux_sound_index(info.sample_rate);
          snd->size = linux_sound_index(info.size);
        }
        else
        {
          snd->size = 0;
          i4_alert(i4gets("bad_format"),200,&sounds[count]);
        }
      }
      else
        i4_alert(i4gets("bad_format"),200,&sounds[count]);
      delete fp;
    }
  }

  i4_free(sounds);
}

i4_voice_class *linux_sound_class::alloc(i4_sound_id sound_id, const sound_parameters& param)
{
  int i=0; 

  if (!sound[sound_id].data)
    return 0;

  while (idata[ w32(v.index) ] ];
          output_buffer[i] += volume_table[v.right_vol][ v.sound->data[ w32(v.index) ] ];
          v.index += v.increment;

          if (v.index >= v.sound->size)
          {
            if ( !v.looping && (v.complete == 0 || v.complete(&v)) )
            {
              v.sound = 0;
              break;
            }
            else
              while (v.index >= v.sound->size)
                v.index -= v.sound->size;
          }
        }
      }
    }
    for (i=0; i<1<<(LINUX_SOUND_BUFFER_BITS-2); i++)
    {
      sw32 val;

      val = output_buffer[i];
      val = (w16)((val<-32768) ? -32768 : ( (val>32767)? 32767 : val ));
      output_buffer[i] = val;
      val = mix_buffer[i];
      val = (w16)((val<-32768) ? -32768 : ( (val>32767)? 32767 : val ));
      output_buffer[i] |= val<<16;
    }
    write(linux_sound.fd, output_buffer, 1<