Dear all,
Our application is for DVR. So, I use the DM8168EVM(DVRRDK_01_06_00_11) to test the alsa audio channel.
I don't know How maps audio buffer.
anyone konws total single stream size and each channels offset size?
how can i save channel 0 audio stream and channel 1 audio stream ?
thanks all.
######### audio_capture.c My audio 0 channel capture code ############
<code>
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sched.h>
#define AUDIO_MAX_CHANNELS 2 ///< Max number of audio channel supported for recording / playback
#define AUDIO_SAMPLE_RATE_SUPPORTED 16000 //< Only 16KHz supported
#define AUDIO_SAMPLES_TO_READ_PER_CHANNEL (250) //< Ensure to align with hw param BUFFER_SIZE; only < 256 works right now
#define AUDIO_BUFFER_SIZE ((8192*2)>>2)
#define AUDIO_SAMPLE_LEN 2
#define AUDIO_PLAYBACK_SAMPLE_MULTIPLIER 64 /* For some reason, we have to read / feed this much data for playback */
/* Use the newer ALSA API */
//#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#define AUDIO_BUFFER_SUPPORT
int audio_capture()
{
long loops;
int rc;
int size, len;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir = 0;
snd_pcm_uframes_t frames;
char *buffer, *tmp_buffer;
int cap_fd;
int ii;
int audioRecordLen = 0;
/* Open PCM device for recording (capture). */
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));
exit(1);
}
cap_fd = open("sample.raw", O_CREAT | O_RDWR);
if(cap_fd < 0){
fprintf(stderr, "unable to open file: %s\n", "sample.raw");
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* 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);
snd_pcm_hw_params_set_channels(handle, params, AUDIO_MAX_CHANNELS);
/* 44100 bits/second sampling rate (CD quality) */
//val = 44100;
val = AUDIO_SAMPLE_RATE_SUPPORTED;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
if ((rc = snd_pcm_hw_params_set_buffer_size (handle, params, AUDIO_BUFFER_SIZE)) < 0)
{
fprintf( stderr, "cannot set buffer size \n");
}
/* 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);
}
loops = 500;
buffer = (char *) malloc(AUDIO_SAMPLES_TO_READ_PER_CHANNEL* AUDIO_SAMPLE_LEN * AUDIO_MAX_CHANNELS*AUDIO_PLAYBACK_SAMPLE_MULTIPLIER );
while(loops > 0){
loops--;
ii = 0;
audioRecordLen = 0;
tmp_buffer = buffer;
while(ii < AUDIO_PLAYBACK_SAMPLE_MULTIPLIER) { // 64 loop
rc = snd_pcm_readi(handle, tmp_buffer, AUDIO_SAMPLES_TO_READ_PER_CHANNEL);
if (rc == -EPIPE) {
/* EPIPE means overrun */
fprintf(stderr, "overrun occurred\n");
snd_pcm_prepare(handle);
continue;
} else if (rc <= 0) {
fprintf(stderr, "error from read: %s\n", snd_strerror(rc));
audioRecordLen = 0;
break;
}
tmp_buffer += rc * AUDIO_SAMPLE_LEN * AUDIO_MAX_CHANNELS;
audioRecordLen += rc;
ii++;
}
printf( "audio Record len %d bytes\n", audioRecordLen);
//rc = write(cap_fd, buffer, audioRecordLen);
rc = write(cap_fd, buffer, audioRecordLen/2);
//rc = write(cap_fd, buffer, audioRecordLen/4);
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
close(cap_fd);
return 0;
}
int main(int argc, char* argv[])
{
audio_capture();
}
</code>
######### audio_play.c My audio play code Usage: ./audio_play < sample.raw ############
#include <osa.h>
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sched.h>
#define AUDIO_MAX_CHANNELS 2 ///< Max number of audio channel supported for recording / playback
#define AUDIO_SAMPLE_RATE_SUPPORTED 16000 //< Only 16KHz supported
#define AUDIO_SAMPLES_TO_READ_PER_CHANNEL (250) //< Ensure to align with hw param BUFFER_SIZE; only < 256 works right now
#define AUDIO_BUFFER_SIZE ((8192*2)>>2)
#define AUDIO_SAMPLE_LEN 2
#define AUDIO_PLAYBACK_SAMPLE_MULTIPLIER 64 /* For some reason, we have to read / feed this much data for playback */
/* Use the newer ALSA API */
//#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
int audio_play()
{
long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir = 0;
snd_pcm_uframes_t frames;
char *buffer;
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* 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);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, AUDIO_MAX_CHANNELS);
/* 44100 bits/second sampling rate (CD quality) */
//val = 44100;
val = AUDIO_SAMPLE_RATE_SUPPORTED;
dir = 0; // must be zero
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
#if 1
{
int resample = 1;
snd_pcm_hw_params_set_rate_resample(handle, params, resample);
}
#endif
//frames = 32; // beep ... beep ... , it has problem
frames = 1000;
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);
//size = frames * 4; /* 2 bytes/sample, 2 channels */
size = frames * 2; /* 2 bytes/sample, 1 channels */
buffer = (char *) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
/* 5 seconds in microseconds divided by
* period time */
loops = 5000000 / val;
while (loops > 0) {
loops--;
rc = read(0, buffer, size);
if (rc == 0) {
fprintf(stderr, "end of file on input\n");
break;
} else if (rc != size) {
fprintf(stderr, "short read: read %d bytes\n", rc);
}
rc = snd_pcm_writei(handle, buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr, "error from writei: %s\n", snd_strerror(rc));
} else if (rc != (int)frames) {
fprintf(stderr, "short write, write %d frames\n", rc);
}
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return 0;
}
int main(int argc, char* argv[])
{
audio_play();
}