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;
}
Regards Filip