summaryrefslogtreecommitdiffstats
path: root/firmware/target
diff options
context:
space:
mode:
authorSolomon Peachy <pizza@shaftnet.org>2020-09-30 22:12:35 -0400
committerSolomon Peachy <pizza@shaftnet.org>2020-10-01 11:56:57 -0400
commite43726df2cd1cb8275234d60b818d417cfe730b5 (patch)
tree6f1fb0659dccaafd47394c7de860d4dc3e46b0a4 /firmware/target
parent6459fa0765745e951a6731974164bbcdc9551dfe (diff)
downloadrockbox-e43726df2cd1cb8275234d60b818d417cfe730b5.tar.gz
rockbox-e43726df2cd1cb8275234d60b818d417cfe730b5.tar.bz2
rockbox-e43726df2cd1cb8275234d60b818d417cfe730b5.zip
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
Diffstat (limited to 'firmware/target')
-rw-r--r--firmware/target/hosted/agptek/debug-agptek.c52
-rw-r--r--firmware/target/hosted/pcm-alsa.c92
-rw-r--r--firmware/target/hosted/pcm-alsa.h6
-rw-r--r--firmware/target/hosted/xduoo/debug-xduoo.c7
4 files changed, 118 insertions, 39 deletions
diff --git a/firmware/target/hosted/agptek/debug-agptek.c b/firmware/target/hosted/agptek/debug-agptek.c
index 33f3ac4b97..b4fcb4246b 100644
--- a/firmware/target/hosted/agptek/debug-agptek.c
+++ b/firmware/target/hosted/agptek/debug-agptek.c
@@ -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 */
diff --git a/firmware/target/hosted/pcm-alsa.c b/firmware/target/hosted/pcm-alsa.c
index 1b8e3c4477..f33b42429c 100644
--- a/firmware/target/hosted/pcm-alsa.c
+++ b/firmware/target/hosted/pcm-alsa.c
@@ -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_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 = 1 << vol_shift | 1 << (vol_shift - 1);
- printf("%d dB -> factor = %d\n", vol_db - 48, dig_vol_mult);
+ 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)
{
diff --git a/firmware/target/hosted/pcm-alsa.h b/firmware/target/hosted/pcm-alsa.h
index a2514494de..b746f0d259 100644
--- a/firmware/target/hosted/pcm-alsa.h
+++ b/firmware/target/hosted/pcm-alsa.h
@@ -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__ */
diff --git a/firmware/target/hosted/xduoo/debug-xduoo.c b/firmware/target/hosted/xduoo/debug-xduoo.c
index 33f3ac4b97..9812b8f8b9 100644
--- a/firmware/target/hosted/xduoo/debug-xduoo.c
+++ b/firmware/target/hosted/xduoo/debug-xduoo.c
@@ -1,6 +1 @@
-#include <stdbool.h>
-
-bool debug_hw_info(void)
-{
- return false;
-}
+#include "../agptek/debug-agptek.c"