Dell XPS13/XPS15 white noise via audio

Just a quick post to make a copy of a script that I’d rather not lose. By default, the XPS15 has a fairly quiet white noise hiss when using headphones without any sound playing. Someone linked me to this script and I wanted to reproduce it here just in case I need it in future.

Save it as and run with sudo python

#!/usr/bin/env python

  import os
  import struct
  from fcntl import ioctl

  def __ioctl_val(val):
    # workaround for OverFlow bug in python 2.4
    if val & 0x80000000:
      return -((val^0xffffffff)+1)
    return val

  IOCTL_INFO = __ioctl_val(0x80dc4801)
  IOCTL_PVERSION = __ioctl_val(0x80044810)
  IOCTL_VERB_WRITE = __ioctl_val(0xc0084811)

  def set(nid, verb, param):
    verb = (nid << 24) | (verb << 8) | param
    res = ioctl(FD, IOCTL_VERB_WRITE, struct.pack('II', verb, 0))

  FD ="/dev/snd/hwC1D0", os.O_RDONLY)
  info = struct.pack('Ii64s80si64s', 0, 0, '', '', 0, '')
  res = ioctl(FD, IOCTL_INFO, info)
  name = struct.unpack('Ii64s80si64s', res)[3]
  if not name.startswith('HDA Codec'):
    raise IOError, "unknown HDA hwdep interface"
  res = ioctl(FD, IOCTL_PVERSION, struct.pack('I', 0))
  version = struct.unpack('I', res)
  if version < 0x00010000:    # 1.0.0
    raise IOError, "unknown HDA hwdep version"

  # initialization sequence starts here...

  set(0x0c, 0x300, 0x5180) # 0x0c035180 (SET_AMP_GAIN_MUTE)
  set(0x0c, 0x300, 0x6180) # 0x0c036180 (SET_AMP_GAIN_MUTE)