"Fossies" - the Fresh Open Source Software Archive

Member "alsa-oss-1.1.8/test/mixctl.h" (7 Jan 2019, 6105 Bytes) of package /linux/misc/alsa-oss-1.1.8.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // mixctl.h - MixCtl class provides control of audio mixer functions
    2 // 05/09/98  Release 1.0 Beta1
    3 // Copyright (C) 1998  Sam Hawker <shawkie@geocities.com>
    4 // This software comes with ABSOLUTELY NO WARRANTY
    5 // This software is free software, and you are welcome to redistribute it.
    6 
    7 // Although mixctl.h is an integral part of lmixer, it may also be distributed seperately.
    8 
    9 #include <stdio.h>
   10 #include <stdlib.h>
   11 #include <sys/ioctl.h>
   12 #include <sys/types.h>
   13 #include <sys/stat.h>
   14 #include <fcntl.h>
   15 #include <unistd.h>
   16 #ifdef __NetBSD__
   17 #include <soundcard.h>
   18 #endif
   19 #ifdef __FreeBSD__
   20 #include <machine/soundcard.h>
   21 #endif
   22 #ifdef __linux__
   23 #include <linux/soundcard.h>
   24 #endif
   25 #include <oss-redir.h>
   26 
   27 class MixCtl
   28 {
   29 public:
   30    MixCtl(char *dname){
   31       device=(char *)malloc(sizeof(char)*(strlen(dname)+1));
   32       strcpy(device,dname);
   33       if(mixfdopen=(mixfd=oss_mixer_open(device,O_RDONLY | O_NONBLOCK))!=-1){
   34          nrdevices=SOUND_MIXER_NRDEVICES;
   35          char *devnames[]=SOUND_DEVICE_NAMES;
   36          char *devlabels[]=SOUND_DEVICE_LABELS;
   37          if (oss_mixer_ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask)<0)
   38            fprintf(stderr, "SOUND_MIXER_READ_DEVMASK failed\n");
   39          if (oss_mixer_ioctl(mixfd, SOUND_MIXER_READ_STEREODEVS, &stmask)<0)
   40            fprintf(stderr, "SOUND_MIXER_READ_STEREODEVS failed\n");
   41          if (oss_mixer_ioctl(mixfd, SOUND_MIXER_READ_RECMASK, &recmask)<0)
   42            fprintf(stderr, "SOUND_MIXER_READ_RECMASK failed\n");
   43          if (oss_mixer_ioctl(mixfd, SOUND_MIXER_READ_CAPS, &caps)<0)
   44            fprintf(stderr, "SOUND_MIXER_READ_CAPS failed\n");
   45          mixdevs=(struct MixDev *)malloc(sizeof(struct MixDev)*nrdevices);
   46          int mixmask=1;
   47          for(int i=0;i<nrdevices;i++){
   48             mixdevs[i].support=devmask & mixmask;
   49             mixdevs[i].stereo=stmask & mixmask;
   50             mixdevs[i].records=recmask & mixmask;
   51             mixdevs[i].mask=mixmask;
   52             mixdevs[i].name=devnames[i];
   53             mixdevs[i].label=devlabels[i];
   54             mixmask*=2;
   55          }
   56          doStatus();
   57       }
   58    }
   59    ~MixCtl(){
   60       if(mixfdopen){
   61          if(mixdevs!=NULL)
   62             free(mixdevs);
   63          oss_mixer_close(mixfd);
   64       }
   65    }
   66    bool openOK(){
   67       return mixfdopen;
   68    }
   69    void doStatus(){
   70       oss_mixer_ioctl(mixfd, SOUND_MIXER_READ_RECSRC, &recsrc);
   71       for(int i=0;i<nrdevices;i++){
   72      if(mixdevs[i].support)
   73         oss_mixer_ioctl(mixfd, MIXER_READ(i), &mixdevs[i].value);
   74          mixdevs[i].recsrc=(recsrc & mixdevs[i].mask);
   75       }
   76    }
   77 
   78    // Return volume for a device, optionally reading it from device first.
   79    // Can be used as a way to avoid calling doStatus().
   80    int readVol(int dev, bool read){
   81       if(read)
   82          oss_mixer_ioctl(mixfd, MIXER_READ(dev), &mixdevs[dev].value);
   83       return mixdevs[dev].value/256;
   84    }
   85 
   86    // Return left and right componenets of volume for a device.
   87    // If you are lazy, you can call readVol to read from the device, then these
   88    // to get left and right values.
   89    int readLeft(int dev){
   90       return mixdevs[dev].value%256;
   91    }
   92    int readRight(int dev){
   93       return mixdevs[dev].value/256;
   94    }
   95 
   96    // Write volume to device. Use setVolume, setLeft and setRight first.
   97    void writeVol(int dev){
   98       oss_mixer_ioctl(mixfd, MIXER_WRITE(dev), &mixdevs[dev].value);
   99    }
  100 
  101    // Set volume (or left or right component) for a device. You must call writeVol to write it.
  102    void setVol(int dev, int value){
  103       mixdevs[dev].value=value;
  104    }
  105    void setBoth(int dev, int l, int r){
  106       mixdevs[dev].value=256*r+l;
  107    }
  108    void setLeft(int dev, int l){
  109       int r;
  110       if(mixdevs[dev].stereo)
  111          r=mixdevs[dev].value/256;
  112       else
  113          r=l;
  114       mixdevs[dev].value=256*r+l;
  115    }
  116    void setRight(int dev, int r){
  117       int l;
  118       if(mixdevs[dev].stereo)
  119          l=mixdevs[dev].value%256;
  120       else
  121          l=r;
  122       mixdevs[dev].value=256*r+l;
  123    }
  124 
  125    // Return record source value for a device, optionally reading it from device first.
  126    bool readRec(int dev, bool read){
  127       if(read){
  128      oss_mixer_ioctl(mixfd, SOUND_MIXER_READ_RECSRC, &recsrc);
  129          mixdevs[dev].recsrc=(recsrc & mixdevs[dev].mask);
  130       }
  131       return mixdevs[dev].recsrc;
  132    }
  133 
  134    // Write record source values to device. Use setRec first.
  135    void writeRec(){
  136       oss_mixer_ioctl(mixfd, SOUND_MIXER_WRITE_RECSRC, &recsrc);
  137    }
  138 
  139    // Make a device (not) a record source.
  140    void setRec(int dev, bool rec){
  141       if(rec){
  142          if(caps & SOUND_CAP_EXCL_INPUT)
  143             recsrc=mixdevs[dev].mask;
  144          else
  145             recsrc|=mixdevs[dev].mask;
  146       }
  147       else
  148          recsrc&=~mixdevs[dev].mask;
  149    }
  150 
  151    // Return various other info
  152    char *getDevName(){
  153       return device;
  154    }
  155    // Return the number of mixer devices..
  156    // note: check the validity of each with getSupport 
  157    // before using.
  158    int getNrDevices(){
  159       return nrdevices;
  160    }
  161    int getCapabilities(){
  162       return caps;
  163    }
  164    // Is this dev valid? 
  165    // Example: if (mixctl->getSupport(dev)) { "The channel is valid" }
  166    bool getSupport(int dev){
  167       return mixdevs[dev].support;
  168    }
  169    bool getStereo(int dev){
  170       return mixdevs[dev].stereo;
  171    }
  172    bool getRecords(int dev){
  173       return mixdevs[dev].records;
  174    }
  175    // Get the name of int dev.. 
  176    // example: printf("Channel %d's name is %s", channel, mixctl->getName(channel));
  177    char *getName(int dev){
  178       return mixdevs[dev].name;
  179    }
  180    char *getLabel(int dev){
  181       return mixdevs[dev].label;
  182    }
  183 
  184 private:
  185    int mixfd;
  186    int mixfdopen;
  187    char *device;
  188 
  189    struct MixDev{
  190       bool support;
  191       bool stereo;
  192       bool recsrc;
  193       bool records;
  194       char *name;
  195       char *label;
  196       int value;
  197       int mask;
  198    };
  199 
  200    int nrdevices;       // maximum number of devices
  201    int devmask;         // supported devices
  202    int stmask;          // stereo devices
  203    int recmask;         // devices which can be recorded from
  204    int caps;            // capabilities
  205    int recsrc;          // devices which are being recorded from
  206    struct MixDev *mixdevs;
  207 };