C Not able to set volume while having dsp-device opened

I'm puzzled why this code does not set the pcm-volume:

C++:
#include <sys/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>

int volume( const char* mixer, uint8_t left, uint8_t right )
{
    int vol = (left > 100? 100: left) |
              (right > 100? 100: right) << 8;

    int fd = ::open(mixer, O_WRONLY);
    if( fd < 0 ) {
        printf("Opening mixer %s failed with error %i\n", mixer, errno);
        return false;
    }

    if( ::ioctl( fd, SOUND_MIXER_WRITE_PCM, &vol )< 0 ) {
        printf("setting volume failed with error %i\n", errno);
        return false;
    }

    printf(" Setting %s to %d:%d\n", mixer, left, right);

    ::close( fd );

    return true;
}


int main( int argc, char **argv )
{
    const char* mixer = "/dev/mixer";
    int fd = open("/dev/dsp", O_WRONLY);
    if( fd < 0 ) {
        printf("Open not possible\n");
        return 1;
    } else printf("DSP opened: %i\n", fd);

    volume( mixer, 40, 40 );
    sleep( 3 );
    volume( mixer, 9, 9 );

    close(fd);
    printf("DSP closed\n");

    return 0;
}
The code works, when i do not open "/dev/dsp", or when i open another dsp, e.g. "/dev/dsp1". Then the volume gets adjusted. Why am i not able to alter the volume while having the corresponding dsp-device (/dev/mixer <-> /dev/dsp, /dev/mixer1 <-> /dev/dsp1 ) opened?


P.S.
Tested on FreeBSD-13p4 amd64
 
Back
Top