seeed-voicecard/ac108_plugin/pcm_ac108.c

537 lines
14 KiB
C
Raw Normal View History

2017-09-14 10:29:26 +02:00
//https://github.com/HazouPH/android_device_motorola_smi-plus/blob/48029b4afc307c73181b108a5b0155b9f20856ca/smi-modules/alsa-lib_module_voice/pcm_voice.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>
#include <alsa/pcm_plugin.h>
2017-09-30 10:00:07 +02:00
#include "ac108_help.h"
2017-09-14 10:29:26 +02:00
#include <math.h>
#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0]))
#define AC108_FRAME_SIZE 40960
2017-09-14 10:29:26 +02:00
struct ac108_t {
snd_pcm_ioplug_t io;
2017-09-30 10:00:07 +02:00
snd_pcm_t *pcm;
2017-09-14 10:29:26 +02:00
snd_pcm_hw_params_t *hw_params;
unsigned int last_size;
unsigned int ptr;
unsigned int latency; // Delay in usec
unsigned int bufferSize; // Size of sample buffer
};
2017-09-30 10:00:07 +02:00
static unsigned char capture_buf[AC108_FRAME_SIZE];
/* set up the fixed parameters of pcm PCM hw_parmas */
static int ac108_slave_hw_params_half(struct ac108_t *capture, unsigned int rate,snd_pcm_format_t format) {
2017-09-14 10:29:26 +02:00
int err;
2017-09-30 10:00:07 +02:00
snd_pcm_uframes_t bufferSize = capture->bufferSize;
unsigned int latency = capture->latency;
2017-09-14 10:29:26 +02:00
unsigned int buffer_time = 0;
unsigned int period_time = 0;
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_malloc(&capture->hw_params)) < 0) return err;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_any(capture->pcm, capture->hw_params)) < 0) {
SNDERR("Cannot get pcm hw_params");
2017-09-14 10:29:26 +02:00
goto out;
}
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_set_access(capture->pcm, capture->hw_params,
2017-09-14 10:29:26 +02:00
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
2017-09-30 10:00:07 +02:00
SNDERR("Cannot set pcm access RW_INTERLEAVED");
2017-09-14 10:29:26 +02:00
goto out;
}
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_set_channels(capture->pcm, capture->hw_params, 2)) < 0) {
SNDERR("Cannot set pcm channels 2");
2017-09-14 10:29:26 +02:00
goto out;
}
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_set_format(capture->pcm, capture->hw_params,
2017-09-14 10:29:26 +02:00
format)) < 0) {
2017-09-30 10:00:07 +02:00
SNDERR("Cannot set pcm format");
2017-09-14 10:29:26 +02:00
goto out;
}
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_set_rate(capture->pcm, capture->hw_params, rate, 0)) < 0) {
SNDERR("Cannot set pcm rate %d", rate);
2017-09-14 10:29:26 +02:00
goto out;
}
2017-09-30 10:00:07 +02:00
err = snd_pcm_hw_params_get_buffer_time_max(capture->hw_params,
2017-09-14 10:29:26 +02:00
&buffer_time, 0);
if (buffer_time > 80000)
buffer_time = 80000;
period_time = buffer_time / 4;
2017-09-30 10:00:07 +02:00
err = snd_pcm_hw_params_set_period_time_near(capture->pcm, capture->hw_params,
2017-09-14 10:29:26 +02:00
&period_time, 0);
if (err < 0) {
SNDERR("Unable to set_period_time_near");
2017-09-14 10:29:26 +02:00
goto out;
}
2017-09-30 10:00:07 +02:00
err = snd_pcm_hw_params_set_buffer_time_near(capture->pcm, capture->hw_params,
2017-09-14 10:29:26 +02:00
&buffer_time, 0);
if (err < 0) {
SNDERR("Unable to set_buffer_time_near");
2017-09-14 10:29:26 +02:00
goto out;
}
2017-09-30 10:00:07 +02:00
capture->bufferSize = bufferSize;
capture->latency = latency;
2017-09-14 10:29:26 +02:00
return 0;
out:
2017-09-30 10:00:07 +02:00
free(capture->hw_params);
capture->hw_params = NULL;
2017-09-14 10:29:26 +02:00
return err;
}
/*
2017-09-30 10:00:07 +02:00
* start and stop callbacks - just trigger pcm PCM
2017-09-14 10:29:26 +02:00
*/
static int ac108_start(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
if(!capture->pcm) {
SNDERR( "pcm is lost\n");
2017-09-30 10:00:07 +02:00
}
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
return snd_pcm_start(capture->pcm);
2017-09-14 10:29:26 +02:00
}
static int ac108_stop(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
return snd_pcm_drop(capture->pcm);
2017-09-14 10:29:26 +02:00
}
/*
* pointer callback
*
2017-09-30 10:00:07 +02:00
* Calculate the current position from the delay of pcm PCM
2017-09-14 10:29:26 +02:00
*/
static snd_pcm_sframes_t ac108_pointer(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
int size;
2017-09-30 10:00:07 +02:00
assert(capture);
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
size = snd_pcm_avail(capture->pcm);
if (size < 0)
return size;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
size = size/2;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
if (size > capture->last_size) {
capture->ptr += size - capture->last_size;
capture->ptr %= io->buffer_size;
}
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
//fprintf(stderr, "%s :%d %d %d %d %d %d\n", __func__,capture->ptr ,capture->last_size,size, io->buffer_size,io->appl_ptr, io->hw_ptr);
capture->last_size = size;
return capture->ptr;
2017-09-14 10:29:26 +02:00
}
/*
* transfer callback
*/
static snd_pcm_sframes_t ac108_transfer(snd_pcm_ioplug_t *io,
2017-09-30 10:00:07 +02:00
const snd_pcm_channel_area_t *dst_areas,
snd_pcm_uframes_t dst_offset,
2017-09-14 10:29:26 +02:00
snd_pcm_uframes_t size) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
int chn;
unsigned char *dst_samples[io->channels];
int dst_steps[io->channels];
int bps = snd_pcm_format_width(io->format) / 8; /* bytes per sample */
int i;
int count = 0;
int err = 0;
unsigned char *src_buf;
unsigned char src_data[4][4];
2017-09-30 10:00:07 +02:00
memset(capture_buf,0,AC108_FRAME_SIZE);
if(snd_pcm_avail(capture->pcm) > size*2){
if ((err = snd_pcm_readi (capture->pcm, capture_buf, size*2)) != size*2) {
SNDERR("read from audio interface failed %ld %d %s!\n",size,err,snd_strerror (err));
2017-09-30 10:00:07 +02:00
exit(EXIT_FAILURE);
size = 0 ;
}
}else{
size = 0;
}
#if 1
/* verify and prepare the contents of areas */
for (chn = 0; chn < io->channels; chn++) {
if ((dst_areas[chn].first % 8) != 0) {
SNDERR("dst_areas[%i].first == %i, aborting...\n", chn, dst_areas[chn].first);
2017-09-30 10:00:07 +02:00
exit(EXIT_FAILURE);
}
dst_samples[chn] = /*(signed short *)*/(((unsigned char *)dst_areas[chn].addr) + (dst_areas[chn].first / 8));
if ((dst_areas[chn].step % 16) != 0) {
SNDERR("dst_areas[%i].step == %i, aborting...\n", chn, dst_areas[chn].step);
2017-09-30 10:00:07 +02:00
exit(EXIT_FAILURE);
}
dst_steps[chn] = dst_areas[chn].step / 8;
dst_samples[chn] += dst_offset * dst_steps[chn];
}
#endif
// for(i = 0; i < size*2*bps;i++){
// fprintf(stderr,"%x ",capture_buf[i]);
// if(i%4 == 0)
// fprintf(stderr,"\n");
// }
//generate_sine(dst_areas, dst_offset,size, &count);
src_buf = capture_buf;
#if 1
while(count < size){
for(chn = 0; chn < 4; chn++){
for (i = 0; i < bps; i++){
src_data[chn][i] = src_buf[i];
}
src_buf += bps ;
}
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
for(chn = 0; chn < io->channels; chn++){
for (i = 0; i < bps; i++){
*(dst_samples[chn] + i) = src_data[chn][i];
//fprintf(stderr,"%x ",*(dst_samples[chn] + i));
}
//fprintf(stderr,"\n");
dst_samples[chn] += dst_steps[chn];
}
count++;
}
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
#endif
capture->last_size -= size;
2017-09-14 10:29:26 +02:00
return size;
}
/*
2017-09-30 10:00:07 +02:00
* poll-related callbacks - just pass to pcm PCM
2017-09-14 10:29:26 +02:00
*/
static int ac108_poll_descriptors_count(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
return snd_pcm_poll_descriptors_count(capture->pcm);
2017-09-14 10:29:26 +02:00
}
static int ac108_poll_descriptors(snd_pcm_ioplug_t *io, struct pollfd *pfd,
unsigned int space) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
return snd_pcm_poll_descriptors(capture->pcm, pfd, space);
2017-09-14 10:29:26 +02:00
}
static int ac108_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd,
unsigned int nfds, unsigned short *revents) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
return snd_pcm_poll_descriptors_revents(capture->pcm, pfd, nfds, revents);
2017-09-14 10:29:26 +02:00
}
/*
* close callback
*/
static int ac108_close(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
if (capture->pcm)
snd_pcm_close(capture->pcm);
2017-09-14 10:29:26 +02:00
return 0;
}
2017-09-30 10:00:07 +02:00
static int setSoftwareParams(struct ac108_t *capture) {
2017-09-14 10:29:26 +02:00
snd_pcm_sw_params_t *softwareParams;
int err;
snd_pcm_uframes_t bufferSize = 0;
snd_pcm_uframes_t periodSize = 0;
snd_pcm_uframes_t startThreshold, stopThreshold;
snd_pcm_sw_params_alloca(&softwareParams);
// Get the current software parameters
2017-09-30 10:00:07 +02:00
err = snd_pcm_sw_params_current(capture->pcm, softwareParams);
2017-09-14 10:29:26 +02:00
if (err < 0) {
SNDERR("Unable to get software parameters: %s", snd_strerror(err));
2017-09-14 10:29:26 +02:00
goto done;
}
// Configure ALSA to start the transfer when the buffer is almost full.
2017-09-30 10:00:07 +02:00
snd_pcm_get_params(capture->pcm, &bufferSize, &periodSize);
2017-09-14 10:29:26 +02:00
startThreshold = 1;
stopThreshold = bufferSize;
2017-09-30 10:00:07 +02:00
err = snd_pcm_sw_params_set_start_threshold(capture->pcm, softwareParams,
2017-09-14 10:29:26 +02:00
startThreshold);
if (err < 0) {
SNDERR("Unable to set start threshold to %lu frames: %s",
2017-09-14 10:29:26 +02:00
startThreshold, snd_strerror(err));
goto done;
}
2017-09-30 10:00:07 +02:00
err = snd_pcm_sw_params_set_stop_threshold(capture->pcm, softwareParams,
2017-09-14 10:29:26 +02:00
stopThreshold);
if (err < 0) {
SNDERR("Unable to set stop threshold to %lu frames: %s",
2017-09-14 10:29:26 +02:00
stopThreshold, snd_strerror(err));
goto done;
}
// Allow the transfer to start when at least periodSize samples can be
// processed.
2017-09-30 10:00:07 +02:00
err = snd_pcm_sw_params_set_avail_min(capture->pcm, softwareParams,
2017-09-14 10:29:26 +02:00
periodSize);
if (err < 0) {
SNDERR("Unable to configure available minimum to %lu: %s",
2017-09-14 10:29:26 +02:00
periodSize, snd_strerror(err));
goto done;
}
// Commit the software parameters back to the device.
2017-09-30 10:00:07 +02:00
err = snd_pcm_sw_params(capture->pcm, softwareParams);
if (err < 0)
SNDERR("Unable to configure software parameters: %s",snd_strerror(err));
2017-09-14 10:29:26 +02:00
return 0;
done:
snd_pcm_sw_params_free(softwareParams);
return err;
}
/*
* hw_params callback
*
2017-09-30 10:00:07 +02:00
* Set up pcm PCM according to the current parameters
2017-09-14 10:29:26 +02:00
*/
static int ac108_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
2017-09-14 10:29:26 +02:00
snd_pcm_uframes_t period_size;
snd_pcm_uframes_t buffer_size;
int err;
2017-09-30 10:00:07 +02:00
if (!capture->hw_params) {
err = ac108_slave_hw_params_half(capture, 2*io->rate,io->format);
2017-09-14 10:29:26 +02:00
if (err < 0) {
SNDERR("ac108_slave_hw_params_half error\n");
2017-09-14 10:29:26 +02:00
return err;
}
}
period_size = io->period_size;
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_set_period_size_near(capture->pcm, capture->hw_params,
2017-09-14 10:29:26 +02:00
&period_size, NULL)) < 0) {
2017-09-30 10:00:07 +02:00
SNDERR("Cannot set pcm period size %ld", period_size);
2017-09-14 10:29:26 +02:00
return err;
}
buffer_size = io->buffer_size;
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params_set_buffer_size_near(capture->pcm, capture->hw_params,
2017-09-14 10:29:26 +02:00
&buffer_size)) < 0) {
2017-09-30 10:00:07 +02:00
SNDERR("Cannot set pcm buffer size %ld", buffer_size);
2017-09-14 10:29:26 +02:00
return err;
}
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_hw_params(capture->pcm, capture->hw_params)) < 0) {
SNDERR("Cannot set pcm hw_params");
2017-09-14 10:29:26 +02:00
return err;
}
2017-09-30 10:00:07 +02:00
setSoftwareParams(capture);
2017-09-14 10:29:26 +02:00
return 0;
}
/*
* hw_free callback
*/
static int ac108_hw_free(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
free(capture->hw_params);
capture->hw_params = NULL;
return snd_pcm_hw_free(capture->pcm);
2017-09-14 10:29:26 +02:00
}
static int ac108_prepare(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
capture->ptr = 0;
capture->last_size =0;
return snd_pcm_prepare(capture->pcm);
2017-09-14 10:29:26 +02:00
}
static int ac108_drain(snd_pcm_ioplug_t *io) {
2017-09-30 10:00:07 +02:00
struct ac108_t *capture = io->private_data;
return snd_pcm_drain(capture->pcm);
2017-09-14 10:29:26 +02:00
}
#if 0
2017-09-14 10:29:26 +02:00
static int ac108_sw_params(snd_pcm_ioplug_t *io, snd_pcm_sw_params_t *params) {
return 0;
}
#endif
2017-09-14 10:29:26 +02:00
static int ac108_delay(snd_pcm_ioplug_t * io, snd_pcm_sframes_t * delayp){
return 0;
}
/*
* callback table
*/
static snd_pcm_ioplug_callback_t a108_ops = {
.start = ac108_start,
.stop = ac108_stop,
.pointer = ac108_pointer,
.transfer = ac108_transfer,
.poll_descriptors_count = ac108_poll_descriptors_count,
.poll_descriptors = ac108_poll_descriptors,
.poll_revents = ac108_poll_revents,
.close = ac108_close,
.hw_params = ac108_hw_params,
.hw_free = ac108_hw_free,
// .sw_params = ac108_sw_params,
.prepare = ac108_prepare,
.drain = ac108_drain,
.delay = ac108_delay,
};
2017-09-30 10:00:07 +02:00
static int ac108_set_hw_constraint(struct ac108_t *capture) {
2017-09-14 10:29:26 +02:00
static unsigned int accesses[] = {
2017-09-30 10:00:07 +02:00
SND_PCM_ACCESS_RW_INTERLEAVED
2017-09-14 10:29:26 +02:00
};
unsigned int formats[] = { SND_PCM_FORMAT_S32,
SND_PCM_FORMAT_S16 };
unsigned int rates[] = {
8000,
16000,
32000,
44100,
48000,
96000
};
2017-09-14 10:29:26 +02:00
int err;
2017-09-30 10:00:07 +02:00
err = snd_pcm_ioplug_set_param_list(&capture->io,
2017-09-14 10:29:26 +02:00
SND_PCM_IOPLUG_HW_ACCESS,
ARRAY_SIZE(accesses),
accesses);
2017-09-30 10:00:07 +02:00
if (err < 0){
SNDERR("ioplug cannot set ac108 hw access");
return err;
}
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
if ((err = snd_pcm_ioplug_set_param_list(&capture->io, SND_PCM_IOPLUG_HW_FORMAT,
2017-09-14 10:29:26 +02:00
ARRAY_SIZE(formats), formats)) < 0 ||
2017-09-30 10:00:07 +02:00
(err = snd_pcm_ioplug_set_param_minmax(&capture->io, SND_PCM_IOPLUG_HW_CHANNELS,
1, 4)) < 0 ||
(err = snd_pcm_ioplug_set_param_list(&capture->io, SND_PCM_IOPLUG_HW_RATE,
ARRAY_SIZE(rates), rates)) < 0)
{
2017-09-30 10:00:07 +02:00
SNDERR("ioplug cannot set ac108 format channel rate!");
return err;
}
err = snd_pcm_ioplug_set_param_minmax(&capture->io,SND_PCM_IOPLUG_HW_BUFFER_BYTES,
2017-09-14 10:29:26 +02:00
1, 4 * 1024 * 1024);
2017-09-30 10:00:07 +02:00
if (err < 0){
SNDERR("ioplug cannot set ac108 hw buffer bytes");
return err;
}
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
err = snd_pcm_ioplug_set_param_minmax(&capture->io,SND_PCM_IOPLUG_HW_PERIOD_BYTES,
2017-09-14 10:29:26 +02:00
128, 2 * 1024 * 1024);
2017-09-30 10:00:07 +02:00
if (err < 0) {
SNDERR("ioplug cannot set ac108 hw period bytes");
return err;
}
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
err = snd_pcm_ioplug_set_param_minmax(&capture->io, SND_PCM_IOPLUG_HW_PERIODS,3, 1024);
if (err < 0) {
SNDERR("ioplug cannot set ac108 hw periods");
return err;
}
2017-09-14 10:29:26 +02:00
return 0;
}
/*
* Main entry point
*/
SND_PCM_PLUGIN_DEFINE_FUNC(ac108) {
snd_config_iterator_t i, next;
int err;
const char *pcm_string = NULL;
2017-09-30 10:00:07 +02:00
struct ac108_t *capture;
2017-09-14 10:29:26 +02:00
int channels;
if (stream != SND_PCM_STREAM_CAPTURE) {
SNDERR("a108 is only for capture");
return -EINVAL;
}
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id;
if (snd_config_get_id(n, &id) < 0) continue;
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0) continue;
if (strcmp(id, "slavepcm") == 0) {
if (snd_config_get_string(n, &pcm_string) < 0) {
SNDERR("ac108 slavepcm must be a string");
return -EINVAL;
}
continue;
}
if (strcmp(id, "channels") == 0) {
long val;
if (snd_config_get_integer(n, &val) < 0) {
SNDERR("Invalid type for %s", id);
return -EINVAL;
}
channels = val;
if (channels != 2 && channels != 4 && channels != 6) {
SNDERR("channels must be 2, 4 or 6");
return -EINVAL;
}
continue;
}
}
2017-09-30 10:00:07 +02:00
capture = calloc(1, sizeof(*capture));
if (!capture) {
2017-09-14 10:29:26 +02:00
SNDERR("cannot allocate");
return -ENOMEM;
}
2017-09-30 10:00:07 +02:00
err = snd_pcm_open(&capture->pcm, pcm_string, stream, mode);
2017-09-14 10:29:26 +02:00
if (err < 0) goto error;
2017-09-30 10:00:07 +02:00
2017-09-14 10:29:26 +02:00
//SND_PCM_NONBLOCK
2017-09-30 10:00:07 +02:00
capture->io.version = SND_PCM_IOPLUG_VERSION;
capture->io.name = "AC108 decode Plugin";
capture->io.mmap_rw = 0;
capture->io.callback = &a108_ops;
capture->io.private_data = capture;
2017-09-14 10:29:26 +02:00
2017-09-30 10:00:07 +02:00
err = snd_pcm_ioplug_create(&capture->io, name, stream, mode);
2017-09-14 10:29:26 +02:00
if (err < 0) goto error;
2017-09-30 10:00:07 +02:00
if ((err = ac108_set_hw_constraint(capture)) < 0) {
snd_pcm_ioplug_delete(&capture->io);
2017-09-14 10:29:26 +02:00
return err;
}
2017-09-30 10:00:07 +02:00
*pcmp = capture->io.pcm;
2017-09-14 10:29:26 +02:00
return 0;
error:
2017-09-30 10:00:07 +02:00
if (capture->pcm) snd_pcm_close(capture->pcm);
free(capture);
2017-09-14 10:29:26 +02:00
return err;
}
SND_PCM_PLUGIN_SYMBOL(ac108);