Channels swapping order randomly when running loopback-test

Moderator: flatmax

Post Reply
Sommer
Posts: 1
Joined: Wed May 16, 2018 3:29 pm

Channels swapping order randomly when running loopback-test

Post by Sommer » Wed May 16, 2018 4:06 pm

Hi
I am using the octo sound card for a project at my university. I am far from an expert in using Rasberry Pi, and have run into some problems using the sound card for real time application.
Whenever I run the following loopback-script (Most of this I did not write myself..) , where I basicly pass the input vector straight to the output, the channels sometimes swap position in between different runs. I believe this is true for both the input and the output channels. The swapping however seems to also be done in "pairs", such that Ch0 and Ch1 are linked, Ch2 and Ch3 are linked and so on...

The channel swapping has also been observed when using jaaa -A.

I have on the other hand succeeded recording with arecord at 2 channels, 16bit LE, 48kHz into a raw file, and afterwards running a script to convert the raw data into txt, and here the channels are consistent.

In other words I have my application up and running, however it does not work in real time due to channels swapping randomly. The code I'm using for the simple loopback prototype is written below:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
      
#define BUF_BYTES 128*8 //

int main (int argc, char *argv[]) {
    int err;
    unsigned char buf[BUF_BYTES];
    unsigned char newBuf[BUF_BYTES];
    
    snd_pcm_t *playback_handle;
    snd_pcm_t *capture_handle;
    
    char* device = "default";
    if (argc > 1) device = argv[1];
    
    unsigned int rate = 48000;
    unsigned int nchannels = 8;
    snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
    
    unsigned int buf_frames = BUF_BYTES / nchannels / 2;     
	int format_bits = snd_pcm_format_width(format);  
	unsigned int maxval = (1 << (format_bits - 1)) - 1;

    if ((err = snd_pcm_open (&playback_handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
        fprintf (stderr, "cannot open audio device %s (%s)\n", device, snd_strerror (err)); exit (1);
    }
    
    if ((err = snd_pcm_set_params(playback_handle, format,  SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, 500000)) < 0) {   /* 0.5sec */
        fprintf(stderr, "Playback open error: %s\n", snd_strerror(err)); exit(1);
    }
    
    if ((err = snd_pcm_open (&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
        fprintf (stderr, "cannot open audio device %s (%s)\n", device, snd_strerror (err)); exit (1);
    }
    
    if ((err = snd_pcm_set_params(capture_handle, format,  SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, 500000)) < 0) {   /* 0.5sec */
        fprintf(stderr, "capture open error: %s\n", snd_strerror(err)); exit(1);
    }

snd_pcm_prepare(capture_handle);

    while(1) {
        if ((err = snd_pcm_readi (capture_handle, buf, buf_frames)) != buf_frames) { // Load input into 'buf'
            fprintf (stderr, "read from audio interface failed (%s)\n %d", snd_strerror (err), count); exit (1);
        }

	// My Code Starts Here
	
	// My Code Ends Here

	if ((err = snd_pcm_writei (playback_handle, buf, buf_frames)) != buf_frames) { // Send 'buf' directly to output buffer
            fprintf (stderr, "write to audio interface failed (%s)\n %d", snd_strerror (err), count); exit (1);
        }
   }

    fprintf (stderr, "close handles\n");
    snd_pcm_close (playback_handle);
    snd_pcm_close (capture_handle);
    
    return 0;
}
Thank you in advance

Regards Filip

zeroke
Posts: 1
Joined: Tue Jul 24, 2018 4:28 pm

Re: Channels swapping order randomly when running loopback-test

Post by zeroke » Tue Jul 24, 2018 5:18 pm

Hi

I have the similar behavior.
In my environment, it is as follows

 abnormal: capData[chCount * loopIndex] = capData[chCount * loopIndex + 1] = -1,
 normal: capData is any value. If set capture channel count 8,
capData[chCount * loopIndex - 2] = capData[chCount * loopIndex - 1] = -1

, where capData is capture baffuer.
My source code is almost same, the difference is that I use snd_pcm_link().
"/etc/asound.conf" is set the same way with reference to "viewtopic.php?f=9&t=1222".

Thank you in advance.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests