Mono Audio Input problem on Jetson TK1

There is two usb sound card(kinet) on my TK1, and I want to record mono audio input on the rt5639 with arecord,but it fail。

ubuntu@tegra-ubuntu:~$ arecord -Dplughw:1,0 -c1 -r44100 -fS16_LE test.wav
    Recording WAVE 'test.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono
    arecord: pcm_read:2031: read error: Input/output error

The list of sound cards like this:

ubuntu@tegra-ubuntu:~$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: tegrart5639 [tegra-rt5639], device 0: rt5639 PCM rt5639-aif1-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: tegrart5639 [tegra-rt5639], device 2: BT SCO PCM dit-hifi-2 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: tegrart5639 [tegra-rt5639], device 3: VOICE CALL PCM rt5639-aif1-3 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: tegrart5639 [tegra-rt5639], device 4: BT VOICE CALL PCM dit-hifi-4 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: tegrart5639 [tegra-rt5639], device 5: offload-pcm (*) []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: tegrart5639 [tegra-rt5639], device 7: offload-pcm-capture snd-soc-dummy-dai-7 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Device [PrimeSense Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

The list device names:

ubuntu@tegra-ubuntu:~$ arecord -L
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
default
    Playback/recording through the PulseAudio sound server
music
demixer
voice
aux_plug
music_and_voice
sysdefault:CARD=tegrart5639
    tegra-rt5639, 
    Default Audio Device
dmix:CARD=tegrart5639,DEV=0
    tegra-rt5639, 
    Direct sample mixing device
dmix:CARD=tegrart5639,DEV=2
    tegra-rt5639, 
    Direct sample mixing device
dmix:CARD=tegrart5639,DEV=3
    tegra-rt5639, 
    Direct sample mixing device
dmix:CARD=tegrart5639,DEV=4
    tegra-rt5639, 
    Direct sample mixing device
dmix:CARD=tegrart5639,DEV=5
    tegra-rt5639, 
    Direct sample mixing device
dmix:CARD=tegrart5639,DEV=7
    tegra-rt5639, 
    Direct sample mixing device
dsnoop:CARD=tegrart5639,DEV=0
    tegra-rt5639, 
    Direct sample snooping device
dsnoop:CARD=tegrart5639,DEV=2
    tegra-rt5639, 
    Direct sample snooping device
dsnoop:CARD=tegrart5639,DEV=3
    tegra-rt5639, 
    Direct sample snooping device
dsnoop:CARD=tegrart5639,DEV=4
    tegra-rt5639, 
    Direct sample snooping device
dsnoop:CARD=tegrart5639,DEV=5
    tegra-rt5639, 
    Direct sample snooping device
dsnoop:CARD=tegrart5639,DEV=7
    tegra-rt5639, 
    Direct sample snooping device
hw:CARD=tegrart5639,DEV=0
    tegra-rt5639, 
    Direct hardware device without any conversions
hw:CARD=tegrart5639,DEV=2
    tegra-rt5639, 
    Direct hardware device without any conversions
hw:CARD=tegrart5639,DEV=3
    tegra-rt5639, 
    Direct hardware device without any conversions
hw:CARD=tegrart5639,DEV=4
    tegra-rt5639, 
    Direct hardware device without any conversions
hw:CARD=tegrart5639,DEV=5
    tegra-rt5639, 
    Direct hardware device without any conversions
hw:CARD=tegrart5639,DEV=7
    tegra-rt5639, 
    Direct hardware device without any conversions
plughw:CARD=tegrart5639,DEV=0
    tegra-rt5639, 
    Hardware device with all software conversions
plughw:CARD=tegrart5639,DEV=2
    tegra-rt5639, 
    Hardware device with all software conversions
plughw:CARD=tegrart5639,DEV=3
    tegra-rt5639, 
    Hardware device with all software conversions
plughw:CARD=tegrart5639,DEV=4
    tegra-rt5639, 
    Hardware device with all software conversions
plughw:CARD=tegrart5639,DEV=5
    tegra-rt5639, 
    Hardware device with all software conversions
plughw:CARD=tegrart5639,DEV=7
    tegra-rt5639, 
    Hardware device with all software conversions
sysdefault:CARD=Device
    PrimeSense Device, USB Audio
    Default Audio Device
front:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    Front speakers
surround40:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    Direct sample mixing device
dsnoop:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    Direct sample snooping device
hw:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    Direct hardware device without any conversions
plughw:CARD=Device,DEV=0
    PrimeSense Device, USB Audio
    Hardware device with all software conversions

What the problem it is? ths

Here’s my notes…don’t know if this applies for you, I do not have any USB sound card.

SET AUDIO:
==========
amixer cset name="Stereo ADC MIXL ADC2 Switch" 0
amixer cset name="Stereo ADC MIXR ADC2 Switch" 0
amixer cset name="Int Mic Switch" 0
amixer cset name="ADC Capture Switch" 1
amixer cset name="RECMIXL BST1 Switch" 0
amixer cset name="RECMIXR BST1 Switch" 0
amixer cset name="RECMIXL BST2 Switch" 1
amixer cset name="RECMIXR BST2 Switch" 1
amixer cset name="Stereo ADC L1 Mux" "ADC"
amixer cset name="Stereo ADC R1 Mux" "ADC"
amixer cset name="Stereo ADC MIXL ADC1 Switch" 1
amixer cset name="Stereo ADC MIXR ADC1 Switch" 1
amixer cset name="Stereo ADC MIXL ADC2 Switch" 0
amixer cset name="Stereo ADC MIXR ADC2 Switch" 0
amixer cset name="IN1 Mode Control" "Single ended"
amixer cset name="IN2 Mode Control" "Single ended"
amixer cset name="Mic Jack Switch" 1


EXAMPLE:
arecord -t wav -c 1 -d 4 -v tmp.wav
aplay tmp.wav


DISPLAY AUDIO SETTINGS:
=======================
amixer cget name="Stereo ADC MIXL ADC2 Switch"
amixer cget name="Stereo ADC MIXR ADC2 Switch"
amixer cget name="Int Mic Switch"
amixer cget name="ADC Capture Switch"
amixer cget name="RECMIXL BST1 Switch"
amixer cget name="RECMIXR BST1 Switch"
amixer cget name="RECMIXL BST2 Switch"
amixer cget name="RECMIXR BST2 Switch"
amixer cget name="Stereo ADC L1 Mux"
amixer cget name="Stereo ADC R1 Mux"
amixer cget name="Stereo ADC MIXL AD1 Switch"
amixer cget name="Stereo ADC MIXR ADC1 Switch"
amixer cget name="Stereo ADC MIXL ADC2 Switch"
amixer cget name="Stereo ADC MIXR ADC2 Switch"
amixer cget name="IN1 Mode Control"
amixer cget name="IN2 Mode Control"
amixer cget name="Mic Jack Switch"

But how to program with asound?
I open the default device but ,the sound is not from rt5639 but noise.

static snd_pcm_t *handle;
static int g_rate =16000;

static int g_channels = 1;

static uint8_t *buffer = NULL;

static int frames = 32;
extern bool snd_read_end_flag;

int snd_init(void)
{
        snd_pcm_hw_params_t *params;
        snd_pcm_uframes_t frames;

        /* Open PCM device for recording (capture). *///plughw:1,0  --> must be fited with stereo -c 2  two channels
        int rc = snd_pcm_open(&handle,"default", SND_PCM_STREAM_CAPTURE, 0);
        
        if (rc < 0) {
            fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc));
            return -1;
        }
        
        /* Allocate a hardware parameters object. */
        snd_pcm_hw_params_alloca(&params);

        /* Fill it in with default values. */
        snd_pcm_hw_params_any(handle, params);

        /* Set the desired hardware parameters. */

        /* Interleaved mode */
        snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

        /* Signed 16-bit little-endian format */
        snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);

        /* channels  */
        snd_pcm_hw_params_set_channels(handle, params, g_channels);

        /* sampling rate */
        unsigned int val = g_rate;
        int dir;
        snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);

        /* Set period size to xx frames. */
        snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); 

        /* Write the parameters to the driver */
        rc = snd_pcm_hw_params(handle, params);   
        if (rc < 0) {
            fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));
            exit(1);
        }

        /* Use a buffer large enough to hold one period */
        snd_pcm_hw_params_get_period_size(params,  &frames, &dir);
        int size = frames ; /* 2 bytes/sample, 2 channels */
        buffer = (uint8_t *) malloc(size);

        return 0;
}

int snd_read(uint8_t *buf)
{
        TRACE();
        int rc = snd_pcm_readi(handle, buf, frames);
        fprintf(stderr, "rc = %d \n", rc);
        if (rc == -EPIPE) {
                /* EPIPE means overrun */
            fprintf(stderr, "overrun occurred\n");
            snd_pcm_prepare(handle);
        }
        else if (rc < 0) {
                fprintf(stderr, "error from read: %s\n", snd_strerror(rc));
        }
        else if(rc != (int)frames) {
                fprintf(stderr, "@ACap::Start()--short read, read %d frames\n", rc);
        }

return rc * 2;
}

extern int snd_count_read(uint8_t *buf, uint32_t len);
extern int snd_final(void);
extern int snd_init(void);

bool snd_read_end_flag = false;

int main(int argc, char **argv)
{
        int fd = open("m.raw", O_WRONLY|O_CREAT, 0666);
        int idx = 0, iCount;
        uint8_t buf[1024];

        snd_init();
        TRACE() ;

        while (idx < 400*1024)
        {
                iCount = snd_read(buf);
                if (iCount > 0)
                {
                       
                        idx += iCount;
                        write(fd, buf, iCount);
                }
                else {
                        printf("iCount = %d. \n", iCount);
                }
        }
     
        snd_final();
        close(fd);
        return 0;
}

I’ve always had issues with the way audio devices are listed and how to set it up to actually work. I can’t answer the asound programming question, but it is possible that your programming actually works and the mixer is not set properly. The settings I gave above are for a JTK1 without any external USB audio which I found through trial and error (including seeing what happens if I plug in or unplug a USB audio headset). Every time you plug something in or unplug it the “default” device might change (e.g., HDMI audio stops and headphones might start, but not both simultaneously).

I’d suggest looking at the amixer settings and seeing how they change with and without your USB audio devices. Change the USB audio to see how it changes with and without a microphone or speaker plugged in…the goal being to notice a change that tells you which setting really applies to which device.