Showing posts with label Raspberry Pi. Show all posts
Showing posts with label Raspberry Pi. Show all posts

Wednesday, February 1, 2017

NFS on RPI2

Just a quick note - following the instructions I've put up elsewhere here, the only thing to add for an RPI is that rpc.statd needs to be started for the network mount to work.  Run this before the sudo mount -a command:
sudo service rpcbind restart



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.

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 ...