Grabación audio

Imagen de jlcabral
0 puntos

Buenas tardes.

Amig@s Ubunteros que mas?

Actualmente me encuentro trabajando en un programa de adquisición y reproducción de audio, el siguiente código funciona perfectamente, pero, cuando le cambio la frecuencia de muestreo o le cambio el número del tamaño de lectura por muestra de 8 a 16, el programa ya no me reproduce igual, realmente no se porque sucede eso, según tengo entendido la frecuencia de muestreo sería un factor independiente, a menos que tenga problemas con Nyquist. EN fin, realmente necesito bajarle a la frecuencia de muestreo y aumentar el tamaño de la lectura por muestra, les agradezco.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/*
* parrot.c
* Program to illustrate /dev/dsp device
* Records several seconds of sound, then echoes it back.
* Runs until Control-C is pressed.
*/

#include
#include
#include
#include
#include
#include
#include

#define LENGTH 5 /* how many seconds of speech to store */
#define RATE 8000 /* the sampling rate */
#define SIZE 8 /* sample size: 8 or 16 bits */
#define CHANNELS 1 /* 1 = mono 2 = stereo */

/* this buffer holds the digitized audio */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main()
{
int fd,ofd; /* sound device file descriptor */
int arg; /* argument for ioctl calls */
int status; /* return status of system calls */

/* open sound device */
fd = open("/dev/dsp", O_RDONLY);
ofd =open("/dev/dsp", O_WRONLY);
if (fd < 0) {
perror("open of /dev/dsp failed");
exit(1);
}

/* set sampling parameters */
arg = SIZE; /* sample size */
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if (arg != SIZE)
perror("unable to set sample size");

arg = CHANNELS; /* mono or stereo */
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
if (arg != CHANNELS)
perror("unable to set number of channels");

arg = RATE; /* sampling rate */
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");

while (1) { /* loop until Control-C */
printf("Say something>> Buffer: %d\n",sizeof(buf));
status = read(fd, buf, sizeof(buf)); /* record some sound */
if (status != sizeof(buf))
perror("read wrong number of bytes");
printf("You said:\n");
status = write(ofd, buf, sizeof(buf)); /* play it back */
if (status != sizeof(buf))
perror("wrote wrong number of bytes");
/* wait for playback to complete before recording again */
status = ioctl(fd, SOUND_PCM_SYNC, 0);
if (status == -1)
perror("SOUND_PCM_SYNC ioctl failed");
}
close(fd);
close(ofd);

}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Imagen de jlcabral
+1
0
-1

""""""""""""""""""""""""""""""""""""""""""""""""""""

#include
#include
#include
#include
#include
#include
#include

#define LENGTH 5 /* how many seconds of speech to store */
#define RATE 8000 /* the sampling rate */
#define SIZE 8 /* sample size: 8 or 16 bits */
#define CHANNELS 1 /* 1 = mono 2 = stereo */

/* this buffer holds the digitized audio */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main()
{
int fd,ofd; /* sound device file descriptor */
int arg; /* argument for ioctl calls */
int status; /* return status of system calls */

/* open sound device */
fd = open("/dev/dsp", O_RDONLY);
ofd =open("/dev/dsp", O_WRONLY);
if (fd < 0) {
perror("open of /dev/dsp failed");
exit(1);
}

/* set sampling parameters */
arg = SIZE; /* sample size */
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if (arg != SIZE)
perror("unable to set sample size");

arg = CHANNELS; /* mono or stereo */
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
if (arg != CHANNELS)
perror("unable to set number of channels");

arg = RATE; /* sampling rate */
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");

while (1) { /* loop until Control-C */
printf("Say something>> Buffer: %d\n",sizeof(buf));
status = read(fd, buf, sizeof(buf)); /* record some sound */
if (status != sizeof(buf))
perror("read wrong number of bytes");
printf("You said:\n");
status = write(ofd, buf, sizeof(buf)); /* play it back */
if (status != sizeof(buf))
perror("wrote wrong number of bytes");
/* wait for playback to complete before recording again */
status = ioctl(fd, SOUND_PCM_SYNC, 0);
if (status == -1)
perror("SOUND_PCM_SYNC ioctl failed");
}
close(fd);
close(ofd);

}

""""""""""""""""""""""""""""""""""""""""""""""""""""

+1
0
-1

Jose Luis Cabra