/* 
    sound for Bt848 frame grabber driver

    Copyright (C) 1996,97 Marcus Metzler (mocm@thp.uni-koeln.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * sound.cc 
 */

#include <stream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/soundcard.h>

#include "sound.h"
                 
int mixer_device;
int devmask,recmask,recsrc,stereodevs,caps;

char *device_labels[] = SOUND_DEVICE_LABELS;
char *device_names[] = SOUND_DEVICE_NAMES;

int locked_channels, muted_channels;

lr_vol volume_list[SOUND_MIXER_NRDEVICES];
lr_vol m;

void sound_init ()
{
  
  mixer_device=open("/dev/mixer", O_RDWR, 0);
  
  if (mixer_device<0) {
    cerr << "unable to open mixer !" << endl;
  } else {
    /* get mixer information */
    if (	(
		 ioctl(mixer_device, SOUND_MIXER_READ_RECSRC, &recsrc) |
		 ioctl(mixer_device, SOUND_MIXER_READ_RECMASK, &recmask) |
		 ioctl(mixer_device, SOUND_MIXER_READ_STEREODEVS, &stereodevs)|
		 ioctl(mixer_device, SOUND_MIXER_READ_DEVMASK, &devmask) |
		 ioctl(mixer_device, SOUND_MIXER_READ_CAPS, &caps)
		 )<0) {
      cerr << "Can't initialize sound mixer !" << endl;
    } else {
      cerr << "Sound mixer initialized !" << endl;
      for (int i=0;i<SOUND_MIXER_NRDEVICES;i++) {
	if (devmask & (1<<i)) {
	  /* read current volume */
	  ioctl(mixer_device, MIXER_READ(i), &(volume_list[i]));
	}
	close(mixer_device);  
      }
    }
  } 
  

}
  
void set_master_vol(int l, int r=-1){
  
  if (devmask & 1) {
    mixer_device=open("/dev/mixer", O_RDWR, 0);
    volume_list[0].l=l;
    if (r<0) volume_list[0].r=l;
    else volume_list[0].r=r;
     
    ioctl(mixer_device, MIXER_WRITE(0), &(volume_list[0]));
    close(mixer_device);
  }
}

void get_master_vol(int &l, int lr=LEFT){
  
  if (devmask & 1) {
    if (lr==LEFT) l=volume_list[0].l;
    else l=volume_list[0].r;
  } else {
    if (lr==LEFT) l=0;
    else l=0;    
  }
  
}




