Difference between revisions of "Saa7134-alsa"

From LinuxTVWiki
Jump to: navigation, search
m (ALSA audio with other applications)
m (Record with mencoder: punctuation)
 
(20 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
Also see the [[Saa7134-alsa redundant]] article; whose info (if it is not redundant) should/will be merged (preferably by its authour so that proper user attribution is maintained for information additions) into this original existing article.
 +
 
=== How to use alsa or oss with saa7134 ===
 
=== How to use alsa or oss with saa7134 ===
  
Line 170: Line 172:
 
=== Record with mencoder ===
 
=== Record with mencoder ===
  
It may be simpler to get sound during recording than during playback. I get good sound using this, where $DEV is the number of the device node:
+
It may be simpler to get sound during recording than during playback. The example given below, in which "$DEV" is the number of the device node, is known to produce good sound:
  
 
  mencoder -tv driver=v4l2:device=/dev/video$DEV:fps=30000/1001:chanlist=us-bcast:\
 
  mencoder -tv driver=v4l2:device=/dev/video$DEV:fps=30000/1001:chanlist=us-bcast:\
Line 177: Line 179:
 
  -oac mp3lame -lameopts cbr:br=64 -endpos $TIM -o $DIR/$FIL.avi tv:// > /dev/null
 
  -oac mp3lame -lameopts cbr:br=64 -endpos $TIM -o $DIR/$FIL.avi tv:// > /dev/null
  
So the alsa device is simply called hw.1 -- not hw.1,0. The line is taken from the [[channel script]]. You may find you need to use hw.2 instead, depending on the audio hardware on your system.
+
So the alsa device is simply called hw.1 -- not hw.1,0. The line is taken from the [[V4L channel script]]. You may find you need to use hw.2 instead, depending on the audio hardware on your system.
  
 
Neither ffmpeg nor transcode as yet have ALSA support.
 
Neither ffmpeg nor transcode as yet have ALSA support.
  
 
Because so many applications still appear to have difficulties accessing ALSA's odd device enumeration, the OSS module for DMA sound continues to be useful. You can also use ALSA's OSS emulation with saa7134-alsa to get /dev/dspX devices for OSS-aware applications.
 
Because so many applications still appear to have difficulties accessing ALSA's odd device enumeration, the OSS module for DMA sound continues to be useful. You can also use ALSA's OSS emulation with saa7134-alsa to get /dev/dspX devices for OSS-aware applications.
 +
 +
=== Using with Mplayer, Mencoder with Gentoo default Pulseaudio and Alsa config ===
 +
 +
If you are using default SAA7134 Pulseaudio settings (Analog Stero Input with the video channel being captured), then you can try command line for composite signal capture:
 +
mplayer -tv driver=v4l2:device=/dev/video1:audiorate=32000:alsa:adevice=hw.1:immediatemode=0:input=1:amode=1:normid=7:width=720:height=576 -ao alsa -aspect 16:9 tv://
 +
Note: the line above may work in place of trying with arecord and aplay, as per the example below (arecord -D hw:1,0 -r 32000 -c 2 -f S16_LE | aplay -), which may not work as well as expected.
 +
 +
 +
For Mencoder, the following command line can be used:
 +
mencoder -tv driver=v4l2:device=/dev/video1:audiorate=32000:alsa:adevice=hw.1:input=1:amode=1:normid=7:width=720:height=576 -o filename.mpg -ovc lavc -oac copy -lavcopts vcodec=mpeg2video:vbitrate=1000:\
 +
keyint=5:aspect=16/9 -vf harddup -endpos 00:00:00 tv://
 +
This line has been successfully tested, recording a good few gigabytes, from a Virginmedia V+HD box.
 +
 +
These lines were created with the help of this very page and the Mplayer/Mencoder manual.
  
 
=== ALSA audio with other applications ===
 
=== ALSA audio with other applications ===
Line 220: Line 236:
  
 
  sox -q -c 2 -s -w -r 32000 -t ossdsp /dev/dsp1 -t ossdsp -w -r 32000 /dev/dsp
 
  sox -q -c 2 -s -w -r 32000 -t ossdsp /dev/dsp1 -t ossdsp -w -r 32000 /dev/dsp
 +
 +
=== Howto use Saa7134-alsa with Pulseaudio based-system ===
 +
 +
==== Redirect the sound with pulseaudio loopback module ====
 +
* If your Saa713x device is properly configured and still can get no sound with TVTime, you may to redirect the input source from your TV card to the output of your sound card. You must load the [http://pulseaudio.org/wiki/Modules#module-loopback loopback module] with the command:
 +
  $ pactl load-module module-loopback
 +
It will redirect the default input to the default output. The default input may not be the right one, you can change it in the 'Recording' tab with 'All Streams' displayed with Pulseaudio Volume Control (pavucontrol)
 +
[[File:Pavucontrol.png|thumb|400px|center|alt=Example of loopback module selection in Pulseaudio Volume Control|Example of loopback module selection in Pulseaudio Volume Control]]
 +
 +
* The sound should now works. To automate the process, we will edit /etc/pulse/default.pa
 +
 +
First we need to now what is the TV card source name in pulseaudio. You can know the PCI address of your card with
 +
$ lspci | grep SAA
 +
It will output something like that :
 +
04:08.0 Multimedia controller: Philips Semiconductors SAA7131/SAA7133/SAA7135 Video Broadcast Decoder (rev d1)
 +
'''04:08.0''' is the ''address'' of your card.
 +
 +
* Then, the following command
 +
$ LANG=C pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f 2
 +
will list all source detected by Pulseaudio.
 +
 +
For example, on my computer:
 +
alsa_input.pci-0000_04_08.0.analog-stereo
 +
alsa_output.pci-0000_00_10.1.analog-stereo.monitor
 +
alsa_input.pci-0000_00_10.1.analog-stereo
 +
 +
The source name to use is '''the one with the right PCI address'''.  Here the third one: alsa_input.pci-0000_'''04_08.0'''.analog-stereo
 +
 +
* Edit the file /etc/pulse/default.pa with your favorite text editor:
 +
# gedit /etc/pulse/default.pa
 +
Add the following line at the end of the file (replacing source with the one you found):
 +
load-module module-loopback source=alsa_input.pci-0000_04_08.0.analog-stereo
 +
 +
* Restart Pulseaudio
 +
$ pulseaudio -k
 +
 +
=== Sound distorted and high-pitched ===
 +
 +
* On some card, the sound is distorted, high-pitched as if it is accelerated. Some program will output buffer underrun in the terminal. Fixed for newer kernel: http://git.alsa-project.org/?p=alsa-kernel.git;a=commit;h=7b54360293c9d72b54e462d146c9376287d2f71b
 +
* To correct this problem we need to change the autodetected sampling rate of the TV card from to 32000Hz.
 +
* First we search for the current card parameters. Type :
 +
$ pactl list | grep device_id
 +
It will output sound card argument detected by udev :
 +
Argument: device_id="2" name="pci-0000_'''04_08.0'''" card_name="alsa_card.pci-0000_04_08.0" tsched=yes ignore_dB=no card_properties="module-udev-detect.discovered=1"
 +
Argument: device_id="0" name="pci-0000_00_10.1" card_name="alsa_card.pci-0000_00_10.1" tsched=yes ignore_dB=no card_properties="module-udev-detect.discovered=1"
 +
 +
As previously explained we will need arguments from the card with the right PCI address (here, the first line).
 +
 +
* Edit the file /etc/pulse/default.pa with your favorite text editor:
 +
# gedit /etc/pulse/default.pa
 +
 +
* Before the line
 +
### Automatically load driver modules depending on the hardware available
 +
add the following
 +
load-module module-alsa-card rate=32000 format=s16le $argument
 +
where $argument is the line previously found without the last argument card_properties="module-udev-detect.discovered=1"
 +
 +
For example,
 +
load-module module-alsa-card rate=32000 format=s16le device_id="2" name="pci-0000_04_08.0" card_name="alsa_card.pci-0000_04_08.0" tsched=yes ignore_dB=no
 +
 +
* Restart Pulseaudio
 +
$ pulseaudio -k
 +
[[Category:Drivers]]
 +
[[Category:Software]]

Latest revision as of 03:38, 4 January 2013

Also see the Saa7134-alsa redundant article; whose info (if it is not redundant) should/will be merged (preferably by its authour so that proper user attribution is maintained for information additions) into this original existing article.

How to use alsa or oss with saa7134

Starting with kernel 2.6.15, the module saa7134-alsa lets you use ALSA to get sound directly from your capture card. From 2.6.16 you can use this module with more than one capture card.

In kernel 2.6.16, the saa7134-oss module is also separated out, and the two modules are treated in much the same way; you can build both and switch between them, though both cannot be loaded at once.

Insmod parameters

Now that both alsa and oss DMA sound have been separated out as distinct modules, it is no longer necessary (though it is not forbidden) to include the insmod parameter "alsa=1" or "oss=1".

On the other hand, on the 2.6.16 kernel, it's not safe to use the parameter "disable_ir=1" to turn off the infrared system for the remote -- that caused an oops. This is fixed in 2.6.17.

In the case of more than one card, you can use something like this:

saa7134 card=2,2,2,2 tuner=43,43,43,43 video_nr=1,2,3,4 vbi_nr=1,2,3,4 radio_nr=1,2,3,4 

In dmesg, you should see this type of content murmuring:

input: saa7134 IR (LifeView FlyVIDEO30 as /class/input/input5
tuner 5-0061: chip found @ 0xc2 (saa7133[3])
tuner 5-0061: type set to 43 (Philips NTSC MK3 (FM1236MK3 or FM1236/F))
tuner 5-0063: chip found @ 0xc6 (saa7133[3])
saa7133[3]: i2c eeprom 00: 69 51 38 01 10 28 ff ff ff ff ff ff ff ff ff ff
saa7133[3]: i2c eeprom 10: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
saa7133[3]: i2c eeprom 20: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
saa7133[3]: i2c eeprom 30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
saa7133[3]: i2c eeprom 40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
saa7133[3]: i2c eeprom 50: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
saa7133[3]: i2c eeprom 60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
saa7133[3]: i2c eeprom 70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
saa7133[3]: registered device video4 [v4l2]
saa7133[3]: registered device vbi4
saa7133[3]: registered device radio4

To then load the new driver that lets you get sound off the PCI bus via ALSA or OSS, you would normally just issue "sudo modprobe saa7134-alsa" or "sudo modprobe saa7134-oss". You can also deliberately assign the enumeration; for instance, for four cards, do this for alsa:

sudo modprobe saa7134-alsa index=1,2,3,4

Again, dmesg approves:

saa7134 ALSA driver for DMA sound loaded
saa7133[0]/alsa: saa7133[0] at 0xf5006000 irq 19 registered as card 1
saa7133[1]/alsa: saa7133[1] at 0xf5007000 irq 20 registered as card 2
saa7133[2]/alsa: saa7133[2] at 0xf5004000 irq 21 registered as card 3
saa7133[3]/alsa: saa7133[3] at 0xf5005000 irq 22 registered as card 4 

If instead you use OSS, issue this to determine enumeration:

sudo modprobe saa7134-oss dsp_nr=1,2,3,4 mixer_nr=1,2,3,4

You should see this in dmesg:

saa7134 OSS driver for DMA sound loaded
saa7133[0]: registered device dsp1
saa7133[0]: registered device mixer1
saa7133[1]: registered device dsp2
saa7133[1]: registered device mixer2
saa7133[2]: registered device dsp3
saa7133[2]: registered device mixer3
saa7133[3]: registered device dsp4
saa7133[3]: registered device mixer4

Note that if you use saa7134-oss in Debian, and build your kernel with ALSA as modules, you may need to add saa7134 to the OSS-module-list to keep your OSS emulation under ALSA (cf. #359851):

echo saa7134 >> /usr/share/linux-sound-base/OSS-module-list

If OSS emulation is not working, some applications (such as VLC) that expect ALSA sound devices won't be able to find the legacy OSS devices created by saa7134-oss.

If your ALSA is built into the kernel rather than as modules, you won't need to worry about this.

Insert modules on boot

To insert the modules with these parameters on boot, you can create a file called /etc/modprobe.d/saa7134 with the following lines (adjust card and tuner numbers as required):

options saa7134 card=2,2,2,2 tuner=43,43,43,43 video_nr=1,2,3,4 vbi_nr=1,2,3,4 radio_nr=1,2,3,4 
install saa7134 /sbin/modprobe --ignore-install saa7134; /sbin/modprobe saa7134-alsa
options saa7134-alsa index=1,2,3,4

Your distribution may require you to put these lines into /etc/modprobe.conf; this is not a good idea in Debian, since the files in /etc/modprobe.d will then be ignored.

If instead you use OSS, these lines will load the modules for four cards:

options saa7134 card=2,2,2,2 tuner=43,43,43,43 video_nr=1,2,3,4 vbi_nr=1,2,3,4 radio_nr=1,2,3,4 
install saa7134 /sbin/modprobe --ignore-install saa7134; /sbin/modprobe saa7134-oss
options saa7134-oss dsp_nr=1,2,3,4 mixer_nr=1,2,3,4

Note that once you've entered this information inside /etc/modprobe.d, your subsequent manual insertions will also make use of this information. Scripts, for instance, need not repeat it.

These instructions are of course specific to kernels 2.6.16 and later, as earlier kernels do not have separate DMA modules.

For kernels before 2.6.16, DMA sound via OSS is activated simply by the insmod parameter "oss=1".

Set permissions

Make sure your users are added to the group "audio" to get access to them -- in Debian, if your user name is "tv", you would use

adduser tv audio

List your capture devices

To get a list of your alsa capture devices, issue

arecord -l

You should see your regular sound card and the capture card(s):

**** List of CAPTURE Hardware Devices ****
card 0: CK8S [NVidia CK8S], device 0: Intel ICH [NVidia CK8S]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: CK8S [NVidia CK8S], device 1: Intel ICH - MIC ADC [NVidia CK8S - MIC ADC]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: SAA7134 [SAA7134], device 0: SAA7134 PCM [SAA7134 PCM]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: SAA7134_1 [SAA7134], device 0: SAA7134 PCM [SAA7134 PCM]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 3: SAA7134_2 [SAA7134], device 0: SAA7134 PCM [SAA7134 PCM]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 4: SAA7134_3 [SAA7134], device 0: SAA7134 PCM [SAA7134 PCM]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Configure the cards to record

Set the cards to record on channel 1 and max volume, using for instance

alsamixergui -c 1

for each of the cards.

Watch with mplayer

You should now be ready to watch live tv with mplayer, using something like this:

These are for rf connections:

US broadcast or satellite:

mplayer tv://$channel -tv driver=v4l2:device=/dev/video0:chanlist=us-bcast:alsa:\
adevice=hw.1,0:amode=1:audiorate=32000:forceaudio:volume=100:immediatemode=0:norm=NTSC

If you're using a satellite box, $channel should probably be set to 3 or 4. If that doesn't work, try the US cable TV setup below. If you're getting your broadcast off the air, you can run the scantv utility to detect stations and set $channel to the local channel for your area.

US cable:

mplayer tv://"insert channel" -tv driver=v4l2:device=/dev/video0:chanlist=us-cable:alsa:\
adevice=hw.1,0:amode=1:audiorate=32000:forceaudio:volume=100:immediatemode=0:norm=NTSC

Here again you can run the scantv utility to detect the channels available from your provider, or you can use your provider's channel table.

Europe cable:

mplayer tv://"insert channel" -tv driver=v4l2:device=/dev/video0:chanlist=europe-west:alsa:\
adevice=hw.1,0:amode=1:audiorate=32000:forceaudio:volume=100:immediatemode=0:norm=PAL

Note the norm= and chanlist= parameters. Adjust as required for your location.

If "adevice=hw.1,0" doesn't work, try simply "adevice=hw.1". Some tuners require "adevice=hw.2" -- for instance, on a board with nforce 3 250/gb chipsets. If others experiance this with other boards, please update.

Record with mencoder

It may be simpler to get sound during recording than during playback. The example given below, in which "$DEV" is the number of the device node, is known to produce good sound:

mencoder -tv driver=v4l2:device=/dev/video$DEV:fps=30000/1001:chanlist=us-bcast:\
audiorate=32000:alsa:adevice=hw.$DEV:input=0:amode=1:normid=4:width=576:height=432 \
-ovc x264 -x264encopts threads=2:bitrate=500:bframes=2:subq=1:me=1:frameref=4:8x8dct \
-oac mp3lame -lameopts cbr:br=64 -endpos $TIM -o $DIR/$FIL.avi tv:// > /dev/null

So the alsa device is simply called hw.1 -- not hw.1,0. The line is taken from the V4L channel script. You may find you need to use hw.2 instead, depending on the audio hardware on your system.

Neither ffmpeg nor transcode as yet have ALSA support.

Because so many applications still appear to have difficulties accessing ALSA's odd device enumeration, the OSS module for DMA sound continues to be useful. You can also use ALSA's OSS emulation with saa7134-alsa to get /dev/dspX devices for OSS-aware applications.

Using with Mplayer, Mencoder with Gentoo default Pulseaudio and Alsa config

If you are using default SAA7134 Pulseaudio settings (Analog Stero Input with the video channel being captured), then you can try command line for composite signal capture:

mplayer -tv driver=v4l2:device=/dev/video1:audiorate=32000:alsa:adevice=hw.1:immediatemode=0:input=1:amode=1:normid=7:width=720:height=576 -ao alsa -aspect 16:9 tv://

Note: the line above may work in place of trying with arecord and aplay, as per the example below (arecord -D hw:1,0 -r 32000 -c 2 -f S16_LE | aplay -), which may not work as well as expected.


For Mencoder, the following command line can be used:

mencoder -tv driver=v4l2:device=/dev/video1:audiorate=32000:alsa:adevice=hw.1:input=1:amode=1:normid=7:width=720:height=576 -o filename.mpg -ovc lavc -oac copy -lavcopts vcodec=mpeg2video:vbitrate=1000:\
keyint=5:aspect=16/9 -vf harddup -endpos 00:00:00 tv://

This line has been successfully tested, recording a good few gigabytes, from a Virginmedia V+HD box.

These lines were created with the help of this very page and the Mplayer/Mencoder manual.

ALSA audio with other applications

To hear the audio through ALSA using tvtime (or other programs that don't support it directly), run the following command after starting tvtime:

arecord -D hw:1,0 -r 32000 -c 2 -f S16_LE | aplay -

It basically records at 32kHz Stereo from the SAA7134 ALSA source (hw:1,0 or change accordingly), and plays it through your default ALSA output. Look at the options from aplay to change your output.

There might be a delay between the video and the audio. To avoid it, specify a device for aplay (not the 'default' device):

arecord -D hw:1,0 -r 32000 -c 2 -f S16_LE | aplay -D surround41

In order to get full surround sound from the stereo TV audio output, edit your /etc/asound.conf or ~/.asoundrc file as described in ALSA FAQ028 (http://alsa.opensrc.org/FAQ028) and use this device:

arecord -D hw:1,0 -r 32000 -c 2 -f S16_LE | aplay -D ch51dup

I don't know how to use ALSA directly with xawtv, motv, kdtv, tvtime, or zapping; if you do, please add it in here!

If using arecord still causes a delay between the video and the audio, try using sox:

sox -r 32000 -w -t alsa hw:1,0 -t alsa hw:0,0

Recent versions of sox no longer accept "-w" option, so just omit it. You can also use "--buffer" option to improve A/V synchronization, as the default 8KB buffer causes audio to lag significantly. On my system 2KB buffer is optimal:

sox --buffer 2048 -r 32000 -t alsa hw:1,0 -t alsa hw:0,0

Sox might get you mono sound, while all the alsa solutions cause delays. A combination has been known to work:

arecord -D hw:1,0 -r 32000 -c 2 -f S16_LE | sox -q -c 2 -r 32000 -w -t wav - -t alsa hw:0,0

If sox doesn't have alsa support, you can try this instead:

arecord -D hw:1,0 -r 32000 -c 2 -f S16_LE | play -q -c 2 -r 32000 -w -t wav -

Alternatively you can use alsa's oss-emulation with sox, which seems to result in way better latency then all the above:

sox -q -c 2 -s -w -r 32000 -t ossdsp /dev/dsp1 -t ossdsp -w -r 32000 /dev/dsp

Howto use Saa7134-alsa with Pulseaudio based-system

Redirect the sound with pulseaudio loopback module

  • If your Saa713x device is properly configured and still can get no sound with TVTime, you may to redirect the input source from your TV card to the output of your sound card. You must load the loopback module with the command:
 $ pactl load-module module-loopback

It will redirect the default input to the default output. The default input may not be the right one, you can change it in the 'Recording' tab with 'All Streams' displayed with Pulseaudio Volume Control (pavucontrol)

Example of loopback module selection in Pulseaudio Volume Control
Example of loopback module selection in Pulseaudio Volume Control
  • The sound should now works. To automate the process, we will edit /etc/pulse/default.pa

First we need to now what is the TV card source name in pulseaudio. You can know the PCI address of your card with

$ lspci | grep SAA

It will output something like that :

04:08.0 Multimedia controller: Philips Semiconductors SAA7131/SAA7133/SAA7135 Video Broadcast Decoder (rev d1)

04:08.0 is the address of your card.

  • Then, the following command
$ LANG=C pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f 2

will list all source detected by Pulseaudio.

For example, on my computer:

alsa_input.pci-0000_04_08.0.analog-stereo
alsa_output.pci-0000_00_10.1.analog-stereo.monitor
alsa_input.pci-0000_00_10.1.analog-stereo

The source name to use is the one with the right PCI address. Here the third one: alsa_input.pci-0000_04_08.0.analog-stereo

  • Edit the file /etc/pulse/default.pa with your favorite text editor:
# gedit /etc/pulse/default.pa

Add the following line at the end of the file (replacing source with the one you found):

load-module module-loopback source=alsa_input.pci-0000_04_08.0.analog-stereo
  • Restart Pulseaudio
$ pulseaudio -k

Sound distorted and high-pitched

$ pactl list | grep device_id

It will output sound card argument detected by udev :

Argument: device_id="2" name="pci-0000_04_08.0" card_name="alsa_card.pci-0000_04_08.0" tsched=yes ignore_dB=no card_properties="module-udev-detect.discovered=1"
Argument: device_id="0" name="pci-0000_00_10.1" card_name="alsa_card.pci-0000_00_10.1" tsched=yes ignore_dB=no card_properties="module-udev-detect.discovered=1"

As previously explained we will need arguments from the card with the right PCI address (here, the first line).

  • Edit the file /etc/pulse/default.pa with your favorite text editor:
# gedit /etc/pulse/default.pa
  • Before the line
### Automatically load driver modules depending on the hardware available

add the following

load-module module-alsa-card rate=32000 format=s16le $argument

where $argument is the line previously found without the last argument card_properties="module-udev-detect.discovered=1"

For example,

load-module module-alsa-card rate=32000 format=s16le device_id="2" name="pci-0000_04_08.0" card_name="alsa_card.pci-0000_04_08.0" tsched=yes ignore_dB=no
  • Restart Pulseaudio
$ pulseaudio -k