Convert .wav to .mp3 and set id3 tags

Today, I’ve been listening to a set on Soundcloud that only provides .wav downloads. I wanted to save a copy so that I can sync it to my phone for when I don’t have internet connectivity. The first thing to do, was to download all the tracks as .wav. I did this by clicking download next to each song in the set

Once I had all the songs, it was time to convert them to .mp3 and set their metadata. First, we need to grab a couple of dependencies.

sudo apt-get install lame mp3info

First, convert from .wav to .mp3. We make high quality files (-V 1) and use the default bitrate (128kbps)

for f in *.wav; do lame -V 1 "$f" "${f%.wav}.mp3"; done

Once we have our .mp3 files, we need to set our metadata. This is fairly straightforward use of the mp3info utility. A nice little trick is that we set the song title to be the same as the filename, minus “.mp3″ by using ${i%.mp3}.

# `-t` is the song title
# `-l` is the album
# `-a` is the artist
# `-g` is the genre number (See http://axon.cs.byu.edu/~adam/gatheredinfo/organizedtables/musicgenrelist.php)
for i in *.mp3; do mp3info -t "${i%.mp3}" -l "2014 World Championships" -a "League of Legends" -y "2014" -g 36 "$i"; done

Job done. I downloaded an album as .wav, converted to .mp3 and set all the metadata all on the command line.

golang: Get last character of a string

A very quick one here. I needed to get the last character of a string in golang, but thinking like a PHP developer I was looking for an equivalent to substr($var, -1);.

As it turns out, you actually do it using slices

newVal := val[len(val)-1:]

This says “start at string length, minus 1 and give me everything to the end”

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 white-noise-fix.py and run with sudo python white-noise-fix.py.

#!/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 = os.open("/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)

os.close(FD)