#include <stdio.h>
#include <stdlib.h>
#include "m_main.h"
#include "s_main.h"
#include "dos/player.h"


long nosound=1;

cFile *sample=0;


long getvalue (char *s,long b, char c)
{
  char ta[32];
  char *t=&ta[0];
  while (*s)
    if (*s++==c)
    {
      while ( *s!=' ' && *s )
      {
         *t++=*s++;
      }
      *t=0;
      return (strtol( &ta[0], NULL, b ));
    }
  return -1;
}

void    s_init(void)
{
  char *cd;
  long a,i,d;

  if (nosound==1)
  {
    sample = m_loadfile("sfx/sample.raw");
    for (i=0;i<sample->len;i++)
        sample->data[i]+=128;


    cd=getenv("BLASTER");
    if (!cd)
    {
      nosound=-1;
      return;
    }


    a = getvalue (&cd[0], 16, 'A');
    i = getvalue (&cd[0], 10, 'I');
    d = getvalue (&cd[0], 10, 'D');


    printf ("%ld %ld %ld",a,i,d);
    if (init_player ( 1, a, i, d, 8, 0, 11*1024 ))
        nosound=-1;
  }
}


void    s_start(void) {

   if (nosound==1)
     start_player ( (void *)sample->data, sample->len );


    }

void    s_play(int mist) {

    }

void    s_shutdown(void) {

  if (nosound==1)
  {
    stop_player();
    clean_player ();
  }

  if (nosound)
    m_freefile(sample);

}