Showing posts with label STATIC. Show all posts
Showing posts with label STATIC. Show all posts

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

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