Thread: Port mixer from oss to alsa

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Port mixer from oss to alsa

    Hello
    I'm new in c programming and my requirement is as follows.
    I make a code for oss mixer and now when new systems, throwing the OSS, and there is no /dev/mixer I want to port my code to alsa but my skills are minimal.
    Any help would be welcome.

    Code:
    static int mixer_fd = -1, mixer_src = -1;
    static char *devices[] = SOUND_DEVICE_NAMES;
    
    int mixer_init(char *mixer_device, char *mixer_source)
    {
        int i;
        
        mixer_src = -1;
        
        for (i=0;i<SOUND_MIXER_NRDEVICES;i++)
            if (strcmp(mixer_source, devices[i]) == 0) 
                mixer_src = i;
    
        mixer_fd = open(mixer_device, O_RDWR);
    
        if (mixer_src < 0)    
            return -1;
                
        if (mixer_fd < 0)
            return 0;
        return 1;
    }
    
    char* mixer_get_sndcard_name(void)
    {
        mixer_info info;
        char *result = NULL;
    
        if (ioctl(mixer_fd, SOUND_MIXER_INFO, &info) == -1)
            return NULL;
    
        result = strdup(info.name);    
        
        return result;
    }
    
    char** mixer_get_rec_devices(void)
    {
        int i, o, devmask, res;
        char** result;
        
        if ((ioctl(mixer_fd, SOUND_MIXER_READ_RECMASK, &devmask)) == -1)
            return NULL;
        else
        {
            result = malloc(sizeof(char*)*SOUND_MIXER_NRDEVICES);
            o = 0;
            for (i=0;i<SOUND_MIXER_NRDEVICES;i++)
                {
                    res = (devmask >> i)%2;
                    if (res)
                    {
                        result[o] = malloc(strlen(devices[i])+1);
                        sprintf(result[o], "%s", devices[i]); 
                        o++;
                    }
                    result[o] = NULL;    
                }
        }
        return result;
    }
    
    int mixer_set_rec_device(void)
    {
        int devmask, recmask;
    
        if (mixer_fd <= 0)
            return 0;
    
        if (mixer_src < 0)
            return 0;
    
        if ((ioctl(mixer_fd, SOUND_MIXER_READ_RECMASK, &devmask)) == -1)
            return 0;
        
        recmask = 1 << mixer_src;
        if (!(recmask & devmask))
            return 0;
    
        if ((ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC, &recmask)) == -1)
            return 0;
    
        return 1;
    }            
    
    int mixer_close(void)
    {
        if (mixer_fd > 0)
            close(mixer_fd);
        
        return 1;
    }
            
    int mixer_set_volume(int volume)
    {
        int i_vol;
        if (mixer_fd<0)
            return -1;
    
        assert(volume <= 100);
        assert(volume >= 0);
    
    
        if (mixer_src<0) 
            return -1;
        
        i_vol = volume;  
        i_vol += volume << 8;
    
        if ((ioctl(mixer_fd, MIXER_WRITE(mixer_src), &i_vol)) < 0)
            return 0;
            
        return 1;
    }
    
    int mixer_get_volume(void)
    {
        int i_vol = 0, r, l, volume;
        
        if (mixer_fd<0)
            return -1;
    
        if (mixer_src<0) 
            return -1;
        
        if ((ioctl(mixer_fd, MIXER_READ(mixer_src), &i_vol)) < 0)
            return 0;
    
        r = i_vol >> 8;
        l = i_vol % (1 << 8);
        volume = (r + l)/2;
        
        assert((volume >= 0) && (volume <= 100));
        
        return volume;
    }
    Coexistance with OSS would be nice, so both could be
    used depending on users choice. Perhaps: if this first
    part of the mixer name is a valid device file, try OSS,
    otherwise try ALSA?
    Last edited by rudollf; 01-27-2013 at 05:16 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Read the API documentation:
    ALSA project - the C library reference: Main Page

    Also this should probably go to the Linux coding forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Play mp3 using libmad and alsa
    By tomas.havlas in forum Linux Programming
    Replies: 0
    Last Post: 10-21-2012, 11:25 AM
  2. ALSA programming
    By kot in forum Linux Programming
    Replies: 3
    Last Post: 12-29-2011, 05:54 PM
  3. New 2.6 kernel, ALSA problems
    By bludstayne in forum Tech Board
    Replies: 8
    Last Post: 02-23-2004, 09:58 PM
  4. ALSA sound problem in Linux
    By MathFan in forum Tech Board
    Replies: 0
    Last Post: 04-24-2003, 10:05 AM
  5. Programming with the ALSA API.
    By WebmasterMattD in forum Linux Programming
    Replies: 0
    Last Post: 01-24-2003, 06:54 PM