ioctl to set mixer volume

Dennis dkorpel at gmail.com
Fri Apr 16 18:11:27 UTC 2021


On Friday, 16 April 2021 at 17:50:13 UTC, Alain De Vos wrote:
> The following very simple low level C-function simply sets the 
> mixer volume. How to convert this simple function to dlang ?
> ```
> void mixer_setlevel_stereo(int mixfd,int dev,int left,int right)
> {
> left+=256*right;
> #int_ioctl(int fd, unsigned long request, ...);
> ioctl(mixfd,
> ((unsigned long) (((0x80000000UL|0x40000000UL)) | 
> (((sizeof(int)) & ((1 << 13) - 1)) << 16) | ((('M')) << 8) | 
> ((dev)))),
> &left);
> }
> ```

You could change `sizeof(int)` to `int.sizeof` and `(unsigned 
long)` to `cast(uint)` in this case, but it seems you expanded 
the IOWR macro resulting in the ugly mess of bitwise operations.

I did a search for your function name and found what I think is 
the original C code:

```C
void mixer_setlevel_stereo(int mixfd,int dev,int left,int right)
{
   left+=256*right;
   ioctl(mixfd,MIXER_WRITE(dev),&left);
}
```

Looking at my `/usr/include/linux/soundcard.h:`

```
#define MIXER_WRITE(dev)		_SIOWR('M', dev, int)
```

And _SIOWR looks identical to IOWR, so I think this is a correct 
translation:

```D
import core.sys.posix.sys.ioctl: ioctl, _IOWR;

void mixer_setlevel_stereo(int mixfd, int dev, int left, int 
right)
{
	left += 256 * right;
	ioctl(mixfd, _IOWR!int('M', dev), &left);
}
```



More information about the Digitalmars-d-learn mailing list