Wednesday, November 30, 2016

Raspberry Pi Zero USB Audio on Raspbian Jessie (method 2)

Well, this is the second way to skin the cat.  Note that arecord ignores what went into the recording section of the config file specified below.  See the execution section for actually making a record or playing something.

Followed the instructions of OP in this post, more or less (as detailed below).  Note the extra information provided by the top post at this link.

For some reason, the space on the RPi's SD card filled up completely after following those instructions and messing with the results.  Not even enough space to run ls on a big directory.  Something I'd expect if I'd run arecord instead of aplay, and left it going indefinitely.

Log into the pi.
ssh pi@raspberrypi
Install a dependency (don't worry, it's small).
sudo apt-get install libasound2-plugins
Edit the ALSA config file, but first backup the original.
cp ~/.asoundrc ~/.asoundrc.bak
sudo nano ~/.asoundrc
Add the following to the file.
pcm.!default {
              type asym
              playback.pcm "defaultplayback"
              capture.pcm "defaultrec"
                hint{   show on
                   description "default play and rec koko"
                }
      }

      pcm.defaultrec {
              type plug
              slave {
                  pcm "hw:1,0"
                  rate 48000 
                  channels 2
                  format S16_LE
              }
                hint{   show on
                   description "default rec koko"
                }

      }

  pcm.defaultplayback{
          type rate
          slave.pcm mix1
          slave.rate 48000 

          #Intel(R) Core(TM)2 Duo CPU     E7500  @ 2.93GHz:

            #converter "samplerate_best"     # perfect: 16%cpu, maybe overkill
            #converter "samplerate_medium"   # almost perfect: 6%cpu
            #converter "samplerate"          # good: 4%cpu, definitely usable
            #converter "samplerate_linear"   # bad: 2%cpu, way better than default wine resampler
            #converter "samplerate_order"    # very bad: 2%cpu, like the default wine resampler

          converter "samplerate"

            hint{   show on
                   description "default play koko"
            }
  }

    pcm.mix1 {
            type dmix
            ipc_key 1024
            slave {
                pcm "hw:1,0"
                rate 48000
                periods 128
                period_time 0
                period_size 1024 # must be power of 2
                buffer_size 65536

            }
    }

Execution

To play a file (of the proper format):
aplay file.wav
To record a file:
arecord -f cd file.wav
Read the aplay or arecord manual to see what's up with the -f cd thing.  Essentially, it's specifying the format the audio should be in.
man arecord

Tuesday, November 22, 2016

Raspberry Pi Zero USB Audio on Raspbian Jessie (method 1)

I'm surprised how much of a pain it is to get a USB audio controller set up without a GUI.  I'm trying to use the RPi Zero, which doesn't have any audio hardware - not even a pin-based PWN situation.  Anything has to be done with extra hardware - either USB or custom analog with an ADC (analog-digital converter).

Note that extra static on the line seems to be related to the bitrate and frequency of the recording. If one has a lot of static, try another. The static on playback and when nothing is playing seems to be from the line - it's on a USB hub that also runs the powerful TP-Link wifi adapter.  Since USB is already directly connected to the RPi's GND, there isn't really anything else that can be done about the static.  Hence, custom hardware as the alternative.

There is more than one way to skin a cat; this is the first method, which I think I prefer.  The other method is here.

Overall, setting up audio on Linux and Raspbian is an overly complex process that is definitely not user-friendly.  I'm a user, and this was not a positive experience to sort out.

ID your sound card devices

Figure out what the card, device and subdevice numbers are.  This command makes it pretty straightforward.
cat /proc/asound/modules

Change default sound card to USB audio

ALSA configuration file has moved (source) - edit to reflect a new default device setting.  You want the default sound card to be the same number as your USB card (as found above).  Open the following file:
sudo nano /usr/share/alsa/alsa.conf
Look for the following two lines, and change the trailing 0's to match the number of your USB sound card.
defaults.ctl.card 1 # was 0
defaults.pcm.card 1 # was 0
Note: This is the same file previously located at
/etc/modprobe.d/alsa-base.conf
For some reason it was moved, and it's unfortunate that many of the tutorials dealing with USB audio are old enough to still refer to the old file location.

Allow USB audio to be set as default

Close that file.  There is another file which overrides the /etc/modprobe.d/ files and sets all USB cards with a negative (never default) index. (source - not sure about the overwriting thing, though, as the /etc/-based file doesn't exist.) Open it and comment out the relevant line.
sudo nano /lib/modprobe.d/aliases.conf
Comment out (put a # in front of) this line:
options snd-usb-audio index=-2
The file does say it doesn't need to be modified, but that's to prevent "unusual" cards from being set as default - which is exactly what we want.

Set USB as the default audio device

Open the user-specific alsa config file, back it up, and replace the contents.  (source and source)
cp ~/.asoundrc ~/.asoundrc.bak
sudo nano ~/.asoundrc
Replace everything in the file with the following. (alternative: use the existing format, and change the 0's to 1's)
pcm.!default plughw:Device
ctl.!default plughw:Device

pcm.!default {
    type hw
    card 1
}

ctl.!default {
    type hw
    card 1
}
The backup is located at ~/.asoundrc.bak.  If you're curious about that weird plughw string, run
aplay -L
to see a few examples of it. (this is a more complete version of the aplay -l command used above)

Test the results

A command to run a built-in sound file that tests the left and right channels. Disclaimer: it doesn't work for me.  I had to create a very custom file with specific formats in order to get a result.  (TODO later)
aplay /usr/share/sounds/alsa/Front_Center.wav

What didn't work

Tracking failures, for later.

Changing hardware defaults in aliases.conf

(source) Open and edit:
sudo nano /lib/modprobe.d/aliases.conf
Change and edit these lines.  One exists already, the second should be added directly below it.
options snd-usb-audio index=0
options snd_bcm2835 index=1
That way, all the problems are short-circuited from the beginning.

What happened: made the change, rebooted and ran aplay -l to see what was up: the USB device wasn't even listed.  Apparently, that lower-level adjustment was not pleasing to the ALSA gods.  A better magic is required.

Sources

I'm going to leave these here once I'm done as a permanent record in case anything goes wrong in the future.  They're good sources.

Extra Stuff

Other ways to check what audio devices are available

You can also use this to see what USB devices are connected:
lsusb
After running the following command, look for the entry that refers to your USB audio device.
aplay -l
In my case, the entry showed card 1, device 0, and only one subdevice, numbered 0 as the USB audio device I wanted.  See below for my output.  (This is a RPi Zero with a USB audio device connected)
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: EarMicrophone [USB Ear-Microphone], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
And this one gives a more complete version than the lowercase option.


aplay -L

Speed up the Raspberry Pi

Normally I'd just bookmark this, but the link has already died once and I had to dig it up from alternate development trees.  Current source: https://github.com/autostatic/scripts/blob/rpi/jackstart.  And this is the rather fascinating page that lead me to all this.

(note: I'm not sure this is compatible with the latest Linux kernel, and it's not been tested. YMMV.)

#!/bin/bash

## Stop the ntp service
sudo service ntp stop

## Stop the triggerhappy service
sudo service triggerhappy stop

## Stop the dbus service. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
sudo service dbus stop

## Stop the console-kit-daemon service. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
sudo killall console-kit-daemon

## Stop the polkitd service. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
sudo killall polkitd

## Only needed when Jack2 is compiled with D-Bus support (Jack2 in the AutoStatic RPi audio repo is compiled without D-Bus support)
#export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket

## Remount /dev/shm to prevent memory allocation errors
sudo mount -o remount,size=128M /dev/shm

## Kill the usespace gnome virtual filesystem daemon. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
killall gvfsd

## Kill the userspace D-Bus daemon. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
killall dbus-daemon

## Kill the userspace dbus-launch daemon. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
killall dbus-launch

## Uncomment if you'd like to disable the network adapter completely
#echo -n “1-1.1:1.0” | sudo tee /sys/bus/usb/drivers/smsc95xx/unbind
## In case the above line doesn't work try the following
#echo -n “1-1.1” | sudo tee /sys/bus/usb/drivers/usb/unbind

## Set the CPU scaling governor to performance
echo -n performance | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

## And finally start JACK
jackd -P70 -p16 -t2000 -d alsa -dhw:UA25 -p 128 -n 3 -r 44100 -s &

exit
If you don't mind killing the networking, you can add these to the script as well:

sudo service ifplugd stop
sudo killall ifplugd
sudo service networking stop



Running GUI programs over ssh command line

I've never run into this before, and it's really cool.
ssh -X pi@raspberrypi
This allows the remote computer to use the local computer's x server to run the gui for the remote program.  Sooo, I can use Mathematica on my Mint 18 laptop through an ssh connection to my RPi 2.  Very cool.




Monday, November 14, 2016

Setting up Raspberry Pi Zero in Headless Mode

Download the raspbian image, dd it to the sd card, unmount and remount it on the computer.

Figure out the  name of your micro sd card (I'm going to assume that it's sdb) - look for the device with a total size similar to what yours is labeled as.
lsblk
Download the image here.  Normally I'd give you a wget command, but it's a lot faster doing it on the browser.  Grab the "Raspbian Jessie with Pixel" version...I don't trust the Lite to have everything needed for future projects.

Extract the file (right-click and select either 'extract' or 'open with archive manager') and put it in the Downloads folder.  If you rename it to raspbian.img after extracting, then you can cut and paste the following command. Note that you should change 'foo' to the device name you identified above, e.g., of=/dev/sdb.  I'm keeping it as of=/dev/foo to prevent accidents.
sudo dd bs=4M if=/home/$USER/Downloads/raspbian.img of=/dev/foo status=progress
Remove the sd card from the computer and plug it back in.  That seems to allow the low-level processes to reassess the contents of the card after everything on it was changed.

Resize the partition in gparted if you're planning on doing anything that involves a significant amount of storage.  The Raspbian image doesn't leave a lot of free space, and your card probably has extra room on it.  Install gparted below, then run and resize (that's a GUI operation, so no walkthrough - sorry.  Also, I've been doing from nearly the beginning of my Linux career and don't need any reminders on how to do it).
sudo apt-get install gparted
Set up the networking - you'll need to tell the pi what the wifi password is (Source).  cd into the primary partition of the sd card - you'll find it as the folder named with a long string of meaningless text if you do:
ls /media/$USER
cd into that folder (represented by me here with x's and dashes) and then go several folders deeper to edit the wifi config file.  The whole thing is done below.
sudo nano /media/$USER/xxxx-xxxx-xxxxx/etc/wpa_supplicant/wpa_supplicant.conf
Add this to the end of the file, where "foo" is the name of your wifi network (literally, what shows up when you're choosing a network to connect to - it is case-sensitive) and "bar" is the wifi password. Both should be in quotation marks.
network={
  ssid="foo"
  psk="bar"
  proto=RSN
  key_mgmt=WPA-PSK
  pairwise=CCMP
  auth_alg=OPEN
}
Save and exit by pressing CTRL-O, ENTER and CTRL-X.

Unmount, stick the ssd in the pi, connect a wifi adapter and plug in the power.  Most of the time you don't even need to know the IP of the pi, as you can use the computer name to ssh in:
ssh pi@raspberrypi
Note the password is
raspberry

A few extra commands to be aware of:

Note that to ssh into a Mint 18 distro from the pi, you need to install openssh-server on Mint first.
sudo apt-get install openssh-server
If you know the user and computer names that you want to ssh into, you can use them instead of hard-to-keep-track-of IP addresses.  If everything is on the local network (LAN).

Copy files with scp:
scp user@computer:desired.file ~/path/to/containing/folder
And on an *ahem* completely unrelated note, found a few more dependencies that sphinxtrain needed in the install script.  I'll update vmc when I get a chance.

Wednesday, November 9, 2016

Basic filesharing server on OpenBSD (updated for 6.0)

For the record.  I assume that the other OpenBSD pages on this site have already been implemented.

Install samba:
pkg_add samba
Create a folder to use as a shared location.  According to the official docs, that's what /srv is for.  Since I use this a lot, I'm not going to bother with any further file structure.
mkdir -p /srv/
chmod 777 /srv/
Modify the samba configuration file for a basic all-permissive shared folder.  My threat model assumes that if someone is connected to the network, they're friendly.  Since there's several Win7 computers on the network, I had to get around the authentication requests.

Cut & paste stuff doesn't really work on the command line, so here's what needs to be added at the bottom of the /etc/samba/smb.conf. src.
nano /etc/samba/smb.conf
[SRV]
    path = /srv/
    public = yes
    only guest = yes
    writable = yes
    printable = no
    guest ok = yes
    read only = no
    map to guest = bad user
After finishing the smb.conf edits, use the rc.conf.local file to start the samba share with the machine.  You can use the echo command to make the adjustments.  Note: rc.conf.local doesn't actually exist in the default OpenBSD...but it is acknowledged.  src.
echo '
smbd_flags="-D"
nmbd_flags="-D"
' >> /etc/rc.conf.local
rcctl enable samba
rcctl restart samba
That should do it.

Open a USB drive on OpenBSD

For the record.

Find out the device name of the usb with (e.g., sd1):
sysctl hw.disknames
Then find out the name of the partition you want to mount with (e.g., i):
disklabel sd0
Then create a mount point (a folder that is linked to the partition you're mounting):
mkdir /mnt/foo
Then mount the partition at that location (e.g., sd1i):
mount /dev/sd1i /mnt/foo
To remove your flash drive, run:
umount /mnt/foo

Setting up OpenBSD (updated for 6.0)

A few notes on how I set up my OpenBSD installation.  This will be an ongoing compilation.

Installation

Had to do this with a USB connected CD drive.  Followed the instructions for a flash drive, but the installation itself didn't want to play ball.  I forget the exact scenario; it was confusing.

Wireless

I'm on an Acer Aspire One from a long time ago - I believe it's a D250 model.  It uses the athn0 wifi driver.  Set it up by putting this into your /etc/hostname.ath0 file (copy the whole thing into the command prompt and run it):
echo "
nwid 'foo'
wpakey 'bar'
dhcp 
" > /etc/hostname.ath0
Replace the text as required with your own information...specifically, the stuff that says foo and bar.  :)  After that's added, run:
sh /etc/netstart
...because it won't start automatically.  Don't know why.  I used this link to figure out how to get it running.

Ethernet

If you have ethernet access, internet is somewhat simpler.  Find out what the ethernet device name is:
ifconfig
Mine is fxp0.  Using DHCP makes things easy.  All this command does is put dhcp in the device's config file.
echo dhcp > /etc/hostname.fxp0
Reboot, and you should be online.  Any problems, visit http://www.openbsd.org/faq/faq6.html#Setup
and http://www.openbsd.org/faq/faq6.html#DHCP.

Package Installation

See http://www.openbsd.org/faq/faq15.html#Intro for an excellent explanation of how all this works.

Setting up the Package Mirror

Being able to install packages is always nice.  On OpenBSD, you have to specify the mirror you want to search from and download from manually.  You can set this variable after startup every time, or put it in your .profile.  I used the MIT mirror; it's not going anywhere anytime soon. [edit: ok, it did go.  They didn't keep the 5.9 mirror once 6.0 came out; here's the link to the 6.0 packages.]
vi ./.profile
Now add (I stuck it in the middle of the file):
export PKG_PATH=http://mirrors.mit.edu/pub/OpenBSD/$(uname -r)/packages/$(uname -m)/
Except that in my case this didn't work.  OpenBSD read that as
mirrors.mit.edu/pub/OpenBSD/OpenBSD/packages/i386/
which doesn't make sense.  Instead, I had to do
export PKG_PATH=http://mirrors.mit.edu/pub/OpenBSD/6.0/packages/$(uname -m)/
which destroys flexibility when I upgrade to 6.0 (whenever that comes out). Changed for compatibility with 6.0.

Installing a Package

Now I can do
pkg_add python-2.7.11
to install python 2.7 - but to get that full package name, I have to do CTRL-F in the mirror webpage and figure out what's available.  I'm pretty sure there's a way to search that on the command line, but I haven't figured it out yet.  If only the package name, and not the exact version number, is known, then just use that.  The following successfully installs nano.
pkg_add nano

Turning the Computer Off

Restarting

Restarting is simple: 
reboot

Shutting Down

You'd think this would be simple, eh?  Linux works with a straightforward
shutdown now
but that eventually brings you right back to the shell on my computer's OpenBSD installation.  I have to use 
halt
Though, and I haven't tried this yet, something like
shutdown -h now
is also supposed to work.  This thread has more details.

Final post here

I'm switching over to github pages .  The continuation of this blog (with archives included) is at umhau.github.io .  By the way, the ...