Terratec under Linux

From: Laura (laucriado_at_hotmail.com)
Date: 03/26/04


Date: 26 Mar 2004 10:17:09 -0800

Hi,

I'm working with a terratec EWS88MT audio card under linux (debian)
and I'm using alsa drivers.

I have proved it with aplay, arecord commands, and also with brutefir
library, and works fine.

But I have written an easy application to get samples of a wav file
and I have tried to play them with the soundcard. I have take it from
the
ALSA 0.9.0 HOWTO (http://www.suse.de/~mana/alsa090_howto.html#sect02).
I can't change anything of the setup. For example, EWS88MT has 10
channels, I tried to the select only one, but this is no possible. I
received a setting channel error. That happens too with rate, format
and so on.

Someone knows how to change all these parameters?

Thanks in advance,
Lau.

This is a copy of the program:

#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <alsa/asoundlib.h>

/* Handle for the PCM device */
snd_pcm_t *pcm_handle;

/* Playback stream */
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;

/* This structure contains information about */
/* the hardware and can be used to specify the */
/* configuration to be used for the PCM stream. */
snd_pcm_hw_params_t *hwparams;

/* Name of the PCM device, like plughw:0,0 */
/* The first number is the number of the soundcard, */
/* the second number is the number of the device. */
char *pcm_name;

main(int argc, char *argv[]){

int rate = 44100; /* Sample rate */
int exact_rate; /* Sample rate returned by */
                  /* snd_pcm_hw_params_set_rate_near */
int dir; /* exact_rate == rate --> dir = 0 */
                  /* exact_rate < rate --> dir = -1 */
                  /* exact_rate > rate --> dir = 1 */
int periods = 2; /* Number of periods */
int periodsize = 8192; /* Periodsize (bytes) */

int pcmreturn, l1, l2;
short s1, s2;
int frames;
int channels = 2;
snd_pcm_format_t format;
int valor = 1;
int posibilidades;

char n_senal[200];
FILE *faudio;
double *data; //almacenamos la entrada
int i=0, num=0, salir=0, count=0;

/* Init pcm_name. Of course, later you */
/* will make this configurable ;-) */
//pcm_name = strdup("plughw:0,0");
pcm_name = "hw:0,0");

/* Allocate the snd_pcm_hw_params_t structure on the stack. */
snd_pcm_hw_params_alloca(&hwparams);

/* Open PCM. The last parameter of this function is the mode. */
/* If this is set to 0, the standard mode is used. Possible */
/* other values are SND_PCM_NONBLOCK and SND_PCM_ASYNC. */
/* If SND_PCM_NONBLOCK is used, read / write access to the */
/* PCM device will return immediately. If SND_PCM_ASYNC is */
/* specified, SIGIO will be emitted whenever a period has */
/* been completely processed by the soundcard. */
if (snd_pcm_open(&pcm_handle, pcm_name, stream, 0) < 0) {
   fprintf(stderr, "Error opening PCM device %s\n", pcm_name);
   return(-1);
}

/* Init hwparams with full configuration space */
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
   fprintf(stderr, "Can not configure this PCM device.\n");
   return(-1);
}

/* Set access type. This can be either */
/* SND_PCM_ACCESS_RW_INTERLEAVED or */
/* SND_PCM_ACCESS_RW_NONINTERLEAVED. */
/* There are also access types for MMAPed */
/* access, but this is beyond the scope */
/* of this introduction. */
if (snd_pcm_hw_params_set_access(pcm_handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
        fprintf(stderr, "Error setting access.\n");
        return(-1);
}

/* Set sample format */
//snd_pcm_hw_params_get_format(hwparams,&posibilidades);
if (snd_pcm_hw_params_set_format(pcm_handle, hwparams, posibilidades)
< 0) {//SND_PCM_FORMAT_S16_LE) < 0) {
        fprintf(stderr, "Error setting format.\n");
        return(-1);
}

/* Set sample rate. If the exact rate is not supported */
/* by the hardware, use nearest possible rate. */
exact_rate = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams,
&rate, &dir);
if (dir != 0) {
        fprintf(stderr, "The rate %d Hz is not supported by your hardware.\n
        ==> Using %d Hz instead.\n", rate, exact_rate);
}

/* Set number of channels */
//snd_pcm_hw_params_get_channels(hwparams,&channels);
if (snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels) <
0) {
  fprintf(stderr, "Error setting channels.\n");
  return(-1);
}

/* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(pcm_handle, hwparams, periods, 0) <
0) {
   fprintf(stderr, "Error setting periods.\n");
   return(-1);
}

/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if (snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams,
(periodsize * periods)>>2) < 0) {
  fprintf(stderr, "Error setting buffersize.\n");
  return(-1);
}

/* Apply HW parameter settings to */
/* PCM device and prepare device */
if (snd_pcm_hw_params(pcm_handle, hwparams) < 0) {
  fprintf(stderr, "Error setting HW params.\n");
  return(-1);
}

/* Write num_frames frames from buffer data to */
/* the PCM device pointed to by pcm_handle. */
/* Returns the number of frames actually written. */
//snd_pcm_sframes_t snd_pcm_writei(pcm_handle, data, num_frames);

/* Write num_frames frames from buffer data to */
/* the PCM device pointed to by pcm_handle. */
/* Returns the number of frames actually written. */
//snd_pcm_sframes_t snd_pcm_writen(pcm_handle, data, num_frames);

printf("Introduce la direcci&#65533;n y nombre del fichero donde se
encuentra la se&#65533;al: ");
scanf("%s",n_senal);
if ((faudio=fopen(n_senal,"r"))==NULL)
{
        printf("No se puede abrir el archivo %s.\n",n_senal);
        exit(1);
}

data = calloc(periodsize,sizeof(double));
frames = periodsize / (channels*2 );

snd_pcm_writei(pcm_handle, data, frames);

do
{
        if ((num=fread(data, sizeof(double),periodsize, faudio))!=periodsize)
        {
                if (feof(faudio))
                {
                        printf("Final de archivo.\n");
                        salir=1; //es el &#65533;ltimo segmento, despu&#65533;s saldremos
del bucle
                        for(i=num; i<periodsize; i++)
                        {
                                data[i]=0.;
                        }
                }
                else
                {
                        printf("Error de lectura en el archivo %s.\n",n_senal);
                }
        }
        while ((pcmreturn = snd_pcm_writei(pcm_handle, data, frames)) < 0)
        {
                        snd_pcm_prepare(pcm_handle);
                        fprintf(stderr, "<<<<<<<<<<<<<<< Buffer Underrun %d
>>>>>>>>>>>>>>>\n",count);
        }
        count++;
} while (salir==0);

free(data);
fclose(faudio);
/* Stop PCM device and drop pending frames */
//snd_pcm_drop(pcm_handle);

/* Stop PCM device after pending frames have been played */
snd_pcm_drain(pcm_handle);

}



Relevant Pages