hosted pcm-alsa improvements

* xduoo x3ii/x20:  Better line out support
 * less granular volume settings (too many steps before)
 * Better handling of swiching sample rates
 * Log actual sample rate in debug menu

Most credit goes to Roman Stolyarov
Additional integration [re]work by myself

Change-Id: I63af3740678cf2ed3170f61534e1029c81826bb6
This commit is contained in:
Solomon Peachy 2020-09-30 22:12:35 -04:00
parent 6459fa0765
commit e43726df2c
15 changed files with 218 additions and 91 deletions

View File

@ -2525,7 +2525,7 @@ static const struct {
{ "Screendump", dbg_screendump },
#endif
{ "Skin Engine RAM usage", dbg_skin_engine },
#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || (defined(SONY_NWZ_LINUX) && !defined(SIMULATOR))
#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(SONY_NWZ_LINUX) || defined(AGPTEK_ROCKER) || defined(XDUOO_X3II) || defined(XDUOO_X20) && !defined(SIMULATOR)
{ "View HW info", dbg_hw_info },
#endif
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)

View File

@ -44,6 +44,7 @@
#include "storage.h"
#include "misc.h"
#include "settings.h"
#include "audiohw.h"
#ifdef HAVE_TAGCACHE
#include "tagcache.h"
@ -3850,6 +3851,10 @@ static void audio_change_frequency_callback(unsigned short id, void *data)
static bool starting_playback = false;
struct mp3entry *id3;
#ifdef AUDIOHW_HAVE_SET_OUTPUT
audiohw_set_output();
#endif
switch (id)
{
case PLAYBACK_EVENT_START_PLAYBACK:

View File

@ -29,6 +29,9 @@
static int fd_hw;
static long int vol_l_hw = 255;
static long int vol_r_hw = 255;
static void hw_open(void)
{
fd_hw = open("/dev/snd/controlC0", O_RDWR);
@ -41,19 +44,32 @@ static void hw_close(void)
close(fd_hw);
}
void audiohw_mute(int mute)
{
if(mute)
{
long int ps0 = 0;
alsa_controls_set_ints("Output Port Switch", 1, &ps0);
}
else
{
long int ps2 = 2;
alsa_controls_set_ints("Output Port Switch", 1, &ps2);
}
}
void audiohw_preinit(void)
{
long int hp = 2;
alsa_controls_init();
hw_open();
/* Output port switch set to Headphones */
alsa_controls_set_ints("Output Port Switch", 1, &hp);
}
void audiohw_postinit(void)
{
long int hp = 2;
/* Output port switch set to Headphones */
alsa_controls_set_ints("Output Port Switch", 1, &hp);
}
void audiohw_close(void)
@ -69,8 +85,8 @@ void audiohw_set_frequency(int fsel)
void audiohw_set_volume(int vol_l, int vol_r)
{
long int vol_l_hw = -vol_l/5;
long int vol_r_hw = -vol_r/5;
vol_l_hw = -vol_l/5;
vol_r_hw = -vol_r/5;
alsa_controls_set_ints("Left Playback Volume", 1, &vol_l_hw);
alsa_controls_set_ints("Right Playback Volume", 1, &vol_r_hw);

View File

@ -29,9 +29,14 @@
#include "panic.h"
#include "sysfs.h"
#include "alsa-controls.h"
#include "pcm-alsa.h"
static int fd_hw;
static long int vol_l_hw = 255;
static long int vol_r_hw = 255;
static long int last_ps = 0;
static void hw_open(void)
{
fd_hw = open("/dev/snd/controlC0", O_RDWR);
@ -44,44 +49,69 @@ static void hw_close(void)
close(fd_hw);
}
void audiohw_preinit(void)
void audiohw_mute(int mute)
{
alsa_controls_init();
hw_open();
if(mute)
{
#if defined(XDUOO_X3II)
alsa_controls_set_bool("AK4490 Soft Mute", true);
#endif
#if defined(XDUOO_X20)
long int ps0 = (last_ps > 1) ? 1 : 2;
alsa_controls_set_ints("Output Port Switch", 1, &ps0);
#endif
}
else
{
#if defined(XDUOO_X3II)
alsa_controls_set_bool("AK4490 Soft Mute", false);
#endif
#if defined(XDUOO_X20)
alsa_controls_set_ints("Output Port Switch", 1, &last_ps);
#endif
}
}
void audiohw_postinit(void)
void audiohw_set_output(void)
{
long int ps = 2; // headset
int status = 0;
const char * const sysfs_lo_switch = "/sys/class/switch/lineout/state";
const char * const sysfs_hs_switch = "/sys/class/switch/headset/state";
#ifdef XDUOO_X20
#if defined(XDUOO_X20)
const char * const sysfs_bal_switch = "/sys/class/switch/balance/state";
#endif
#if defined(XDUOO_X3II)
alsa_controls_set_bool("AK4490 Soft Mute", true);
#endif
sysfs_get_int(sysfs_lo_switch, &status);
if (status) ps = 1; // lineout
sysfs_get_int(sysfs_hs_switch, &status);
if (status) ps = 2; // headset
#ifdef XDUOO_X20
#if defined(XDUOO_X20)
sysfs_get_int(sysfs_bal_switch, &status);
if (status) ps = 3; // balance
#endif
/* Output port switch */
alsa_controls_set_ints("Output Port Switch", 1, &ps);
if (last_ps != ps)
{
/* Output port switch */
last_ps = ps;
alsa_controls_set_ints("Output Port Switch", 1, &last_ps);
}
}
#if defined(XDUOO_X3II)
alsa_controls_set_bool("AK4490 Soft Mute", false);
#endif
void audiohw_preinit(void)
{
alsa_controls_init();
hw_open();
}
void audiohw_postinit(void)
{
audiohw_set_output();
}
void audiohw_close(void)
@ -97,24 +127,24 @@ void audiohw_set_frequency(int fsel)
void audiohw_set_volume(int vol_l, int vol_r)
{
long int vol_l_hw = -vol_l/5;
long int vol_r_hw = -vol_r/5;
vol_l_hw = -vol_l/5;
vol_r_hw = -vol_r/5;
alsa_controls_set_ints("Left Playback Volume", 1, &vol_l_hw);
alsa_controls_set_ints("Right Playback Volume", 1, &vol_r_hw);
}
void audiohw_set_filter_roll_off(int value)
{
/* 0 = fast (sharp);
1 = slow;
2 = fast2
3 = slow2
4 = NOS ? */
long int value_hw = value;
/* 0 = Sharp;
1 = Slow;
2 = Short Sharp
3 = Short Slow */
#if defined(XDUOO_X3II)
long int value_hw = value;
alsa_controls_set_ints("AK4490 Digital Filter", 1, &value_hw);
#elif defined(XDUOO_X20)
long int value_hw = value;
alsa_controls_set_ints("ES9018_K2M Digital Filter", 1, &value_hw);
#else
(void)value;

View File

@ -223,7 +223,7 @@ struct sound_settings_info
#elif defined(HAVE_ROCKER_CODEC)
#include "rocker_codec.h"
#elif defined(HAVE_XDUOO_LINUX_CODEC)
#include "rocker_codec.h"
#include "xduoolinux_codec.h"
#endif
/* convert caps into defines */
@ -452,6 +452,10 @@ void audiohw_set_volume(int vol_l, int vol_r);
void audiohw_set_lineout_volume(int vol_l, int vol_r);
#endif
#ifdef AUDIOHW_HAVE_SET_OUTPUT
void audiohw_set_output(void);
#endif
#ifndef AUDIOHW_HAVE_CLIPPING
#if defined(AUDIOHW_HAVE_BASS) || defined(AUDIOHW_HAVE_TREBLE) \
|| defined(AUDIOHW_HAVE_EQ)

View File

@ -75,10 +75,6 @@
/* The number of bytes reserved for loadable plugins */
#define PLUGIN_BUFFER_SIZE 0x100000
#define HAVE_ROCKER_CODEC
#define HAVE_HEADPHONE_DETECTION
/* KeyPad configuration for plugins */
@ -108,10 +104,7 @@
#define CPU_FREQ 1008000000
/* No special storage */
#define CONFIG_STORAGE (STORAGE_HOSTFS)//|STORAGE_SD)
//#define MULTIDRIVE_DIR "/mnt/sd_0"
//#define NUM_DRIVES 1
//#define HAVE_HOTSWAP
#define CONFIG_STORAGE STORAGE_HOSTFS
#define HAVE_STORAGE_FLUSH
/* Battery */

View File

@ -63,18 +63,12 @@
/* define this if you have a real-time clock */
#define CONFIG_RTC APPLICATION
/* Define if the device can wake from an RTC alarm */
//#define HAVE_RTC_ALARM
/* The number of bytes reserved for loadable codecs */
#define CODEC_SIZE 0x80000
/* The number of bytes reserved for loadable plugins */
#define PLUGIN_BUFFER_SIZE 0x100000
#define HAVE_HEADPHONE_DETECTION
/* KeyPad configuration for plugins */
@ -125,6 +119,8 @@
/* HW codec is flexible */
#define HW_SAMPR_CAPS SAMPR_CAP_ALL_192
#define AUDIOHW_HAVE_SET_OUTPUT
/* Battery */
#define BATTERY_CAPACITY_DEFAULT 2400 /* default battery capacity */
#define BATTERY_CAPACITY_MIN 2400 /* min. capacity selectable */

View File

@ -60,9 +60,6 @@
/* define this if you have a real-time clock */
#define CONFIG_RTC APPLICATION
/* Define if the device can wake from an RTC alarm */
//#define HAVE_RTC_ALARM
/* The number of bytes reserved for loadable codecs */
#define CODEC_SIZE 0x80000
@ -119,6 +116,8 @@
/* HW codec is flexible */
#define HW_SAMPR_CAPS SAMPR_CAP_ALL_192
#define AUDIOHW_HAVE_SET_OUTPUT
/* Battery */
#define BATTERY_CAPACITY_DEFAULT 2000 /* default battery capacity */
#define BATTERY_CAPACITY_MIN 2000 /* min. capacity selectable */

View File

@ -2,5 +2,7 @@
#define __ROCKER_CODEC__
#define AUDIOHW_CAPS 0
AUDIOHW_SETTING(VOLUME, "dB", 1, 5, -1020, 0, -300, )
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -127, 0, -30)
#endif
void audiohw_mute(int mute);

View File

@ -347,7 +347,7 @@ static inline void cpu_boost_unlock(void)
#ifndef SIMULATOR
bool dbg_ports(void);
#endif
#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(SONY_NWZ_LINUX)
#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(SONY_NWZ_LINUX) || defined(AGPTEK_ROCKER) || defined(XDUOO_X3II) || defined(XDUOO_X20)
bool dbg_hw_info(void);
#endif

View File

@ -5,3 +5,6 @@
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -127, 0, -30)
AUDIOHW_SETTING(FILTER_ROLL_OFF, "", 0, 1, 0, 4, 0)
#endif
void audiohw_mute(int mute);
void audiohw_set_output(void);

View File

@ -1,6 +1,52 @@
#include <stdbool.h>
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2020 by Solomon Peachy
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
bool debug_hw_info(void)
#include "config.h"
#include "font.h"
#include "lcd.h"
#include "kernel.h"
#include "button.h"
#ifndef BOOTLOADER
#include "pcm-alsa.h"
static int line = 0;
bool dbg_hw_info(void)
{
return false;
int btn = 0;
lcd_setfont(FONT_SYSFIXED);
while(btn ^ BUTTON_POWER) {
lcd_clear_display();
line = 0;
lcd_putsf(0, line++, "pcm srate: %d", pcm_alsa_get_rate());
btn = button_read_device();
lcd_update();
sleep(HZ/16);
}
return true;
}
#endif /* !BOOTLOADER */

View File

@ -68,7 +68,7 @@
* with multple applications running */
static char device[] = "plughw:0,0"; /* playback device */
static const snd_pcm_access_t access_ = SND_PCM_ACCESS_RW_INTERLEAVED; /* access mode */
#ifdef SONY_NWZ_LINUX
#if defined(SONY_NWZ_LINUX) || defined(HAVE_FIIO_LINUX_CODEC)
/* Sony NWZ must use 32-bit per sample */
static const snd_pcm_format_t format = SND_PCM_FORMAT_S32_LE; /* sample format */
typedef long sample_t;
@ -77,6 +77,9 @@ static const snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format *
typedef short sample_t;
#endif
static const int channels = 2; /* count of channels */
static unsigned int sample_rate = 0;
static unsigned int real_sample_rate = 0;
static snd_pcm_t *handle = NULL;
static snd_pcm_sframes_t buffer_size = MIX_FRAME_SAMPLES * 32; /* ~16k */
static snd_pcm_sframes_t period_size = MIX_FRAME_SAMPLES * 4; /* ~4k */
@ -93,14 +96,13 @@ static char signal_stack[SIGSTKSZ];
static int recursion;
#endif
static int set_hwparams(snd_pcm_t *handle, unsigned sample_rate)
static int set_hwparams(snd_pcm_t *handle)
{
unsigned int rrate;
int err;
unsigned int srate;
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_malloc(&params);
/* choose all parameters */
err = snd_pcm_hw_params_any(handle, params);
if (err < 0)
@ -130,16 +132,17 @@ static int set_hwparams(snd_pcm_t *handle, unsigned sample_rate)
goto error;
}
/* set the stream rate */
rrate = sample_rate;
err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
sample_rate = srate = pcm_sampr;
err = snd_pcm_hw_params_set_rate_near(handle, params, &srate, 0);
if (err < 0)
{
printf("Rate %iHz not available for playback: %s\n", sample_rate, snd_strerror(err));
goto error;
}
if (rrate != sample_rate)
real_sample_rate = srate;
if (real_sample_rate != sample_rate)
{
printf("Rate doesn't match (requested %iHz, get %iHz)\n", sample_rate, rrate);
printf("Rate doesn't match (requested %iHz, get %iHz)\n", sample_rate, real_sample_rate);
err = -EINVAL;
goto error;
}
@ -159,8 +162,9 @@ static int set_hwparams(snd_pcm_t *handle, unsigned sample_rate)
printf("Unable to set period size %ld for playback: %s\n", period_size, snd_strerror(err));
goto error;
}
if (!frames)
frames = malloc(period_size * channels * sizeof(sample_t));
free(frames);
frames = calloc(1, period_size * channels * sizeof(sample_t));
/* write the parameters to device */
err = snd_pcm_hw_params(handle, params);
@ -229,26 +233,37 @@ error:
* and add 48dB to the input volume. We cannot go lower -43dB because several
* values between -48dB and -43dB would require a fractional multiplier, which is
* stupid to implement for such very low volume. */
static int dig_vol_mult = 2 ^ 16; /* multiplicative factor to apply to each sample */
static int dig_vol_mult_l = 2 ^ 16; /* multiplicative factor to apply to each sample */
static int dig_vol_mult_r = 2 ^ 16; /* multiplicative factor to apply to each sample */
void pcm_alsa_set_digital_volume(int vol_db)
void pcm_alsa_set_digital_volume(int vol_db_l, int vol_db_r)
{
if(vol_db > 0 || vol_db < -43)
if(vol_db_l > 0 || vol_db_r > 0 || vol_db_l < -43 || vol_db_r < -43)
panicf("invalid pcm alsa volume");
if(format != SND_PCM_FORMAT_S32_LE)
panicf("this function assumes 32-bit sample size");
vol_db += 48; /* -42dB .. 0dB => 5dB .. 48dB */
vol_db_l += 48; /* -42dB .. 0dB => 5dB .. 48dB */
vol_db_r += 48; /* -42dB .. 0dB => 5dB .. 48dB */
/* NOTE if vol_dB = 5 then vol_shift = 1 but r = 1 so we do vol_shift - 1 >= 0
* otherwise vol_dB >= 0 implies vol_shift >= 2 so vol_shift - 2 >= 0 */
int vol_shift = vol_db / 3;
int r = vol_db % 3;
if(r == 0)
dig_vol_mult = 1 << vol_shift;
else if(r == 1)
dig_vol_mult = 1 << vol_shift | 1 << (vol_shift - 2);
int vol_shift_l = vol_db_l / 3;
int vol_shift_r = vol_db_r / 3;
int r_l = vol_db_l % 3;
int r_r = vol_db_r % 3;
if(r_l == 0)
dig_vol_mult_l = 1 << vol_shift_l;
else if(r_l == 1)
dig_vol_mult_l = 1 << vol_shift_l | 1 << (vol_shift_l - 2);
else
dig_vol_mult = 1 << vol_shift | 1 << (vol_shift - 1);
printf("%d dB -> factor = %d\n", vol_db - 48, dig_vol_mult);
dig_vol_mult_l = 1 << vol_shift_l | 1 << (vol_shift_l - 1);
printf("l: %d dB -> factor = %d\n", vol_db_l - 48, dig_vol_mult_l);
if(r_r == 0)
dig_vol_mult_r = 1 << vol_shift_r;
else if(r_r == 1)
dig_vol_mult_r = 1 << vol_shift_r | 1 << (vol_shift_r - 2);
else
dig_vol_mult_r = 1 << vol_shift_r | 1 << (vol_shift_r - 1);
printf("r: %d dB -> factor = %d\n", vol_db_r - 48, dig_vol_mult_r);
}
/* copy pcm samples to a spare buffer, suitable for snd_pcm_writei() */
@ -279,8 +294,11 @@ static bool fill_frames(void)
* sample by some value so the sound is not too low */
const short *pcm_ptr = pcm_data;
sample_t *sample_ptr = &frames[2*(period_size-frames_left)];
for (int i = 0; i < copy_n*2; i++)
*sample_ptr++ = *pcm_ptr++ * dig_vol_mult;
for (int i = 0; i < copy_n; i++)
{
*sample_ptr++ = *pcm_ptr++ * dig_vol_mult_l;
*sample_ptr++ = *pcm_ptr++ * dig_vol_mult_r;
}
}
else
{
@ -378,7 +396,7 @@ static int async_rw(snd_pcm_t *handle)
/* fill buffer with silence to initiate playback without noisy click */
sample_size = buffer_size;
samples = malloc(sample_size * channels * sizeof(sample_t));
samples = calloc(1, sample_size * channels * sizeof(sample_t));
snd_pcm_format_set_silence(format, samples, sample_size);
err = snd_pcm_writei(handle, samples, sample_size);
@ -428,7 +446,7 @@ void pcm_play_dma_init(void)
if ((err = snd_pcm_nonblock(handle, 1)))
panicf("Could not set non-block mode: %s\n", snd_strerror(err));
if ((err = set_hwparams(handle, pcm_sampr)) < 0)
if ((err = set_hwparams(handle)) < 0)
{
panicf("Setting of hwparams failed: %s\n", snd_strerror(err));
}
@ -473,15 +491,28 @@ void pcm_play_unlock(void)
#endif
}
#if defined(HAVE_XDUOO_LINUX_CODEC) || defined(HAVE_FIIO_LINUX_CODEC) || defined(HAVE_ROCKER_CODEC)
static void pcm_dma_apply_settings_nolock(void)
{
if (sample_rate != pcm_sampr)
{
audiohw_mute(true);
snd_pcm_drop(handle);
set_hwparams(handle);
audiohw_mute(false);
}
}
#else
static void pcm_dma_apply_settings_nolock(void)
{
snd_pcm_drop(handle);
set_hwparams(handle, pcm_sampr);
set_hwparams(handle);
#if defined(HAVE_NWZ_LINUX_CODEC)
/* Sony NWZ linux driver uses a nonstandard mecanism to set the sampling rate */
audiohw_set_frequency(pcm_sampr);
#endif
}
#endif
void pcm_dma_apply_settings(void)
{
@ -571,11 +602,16 @@ void pcm_play_dma_postinit(void)
audiohw_postinit();
}
void pcm_set_mixer_volume(int volume)
{
(void)volume;
}
int pcm_alsa_get_rate(void)
{
return real_sample_rate;
}
#ifdef HAVE_RECORDING
void pcm_rec_lock(void)
{

View File

@ -22,10 +22,12 @@
#include <config.h>
#ifdef SONY_NWZ_LINUX
#if defined(SONY_NWZ_LINUX) || defined(HAVE_FIIO_LINUX_CODEC)
/* Set the PCM volume in dB: each sample with have this volume applied digitally
* before being sent to ALSA. Volume must satisfy -43 <= dB <= 0 */
void pcm_alsa_set_digital_volume(int vol_db);
void pcm_alsa_set_digital_volume(int vol_db_l, int vol_db_r);
#endif
int pcm_alsa_get_rate(void);
#endif /* __PCM_ALSA_RB_H__ */

View File

@ -1,6 +1 @@
#include <stdbool.h>
bool debug_hw_info(void)
{
return false;
}
#include "../agptek/debug-agptek.c"