Enable setting of global output samplerate on certain targets.

Replaces the NATIVE_FREQUENCY constant with a configurable frequency.

The user may select 48000Hz if the hardware supports it. The default is
still 44100Hz and the minimum is 44100Hz. The setting is located in the
playback settings, under "Frequency".

"Frequency" was duplicated in english.lang for now to avoid having to
fix every .lang file for the moment and throwing everything out of sync
because of the new play_frequency feature in features.txt. The next
cleanup should combine it with the one included for recording and
generalize the ID label.

If the hardware doesn't support 48000Hz, no setting will be available.

On particular hardware where very high rates are practical and desireable,
the upper bound can be extended by patching.

The PCM mixer can be configured to play at the full hardware frequency
range. The DSP core can configure to the hardware minimum up to the
maximum playback setting (some buffers must be reserved according to
the maximum rate).

If only 44100Hz is supported or possible on a given target for playback,
using the DSP and mixer at other samperates is possible if the hardware
offers them.

Change-Id: I6023cf0c0baa8bc6292b6919b4dd3618a6a25622
Reviewed-on: http://gerrit.rockbox.org/479
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested-by: Michael Sevakis <jethead71@rockbox.org>
This commit is contained in:
Michael Sevakis 2013-05-23 13:58:51 -04:00
parent 00faabef5e
commit d37bf24d90
44 changed files with 677 additions and 230 deletions

View file

@ -21,10 +21,10 @@
#include "config.h"
#include "system.h"
#include "settings.h"
#include "dsp_core.h" /* for NATIVE_FREQUENCY */
#include "pcm.h"
#include "pcm_mixer.h"
#include "misc.h"
#include "fixedpoint.h"
/** Beep generation, CPU optimized **/
#include "asm/beep.c"
@ -39,8 +39,10 @@ static uint32_t beep_amplitude; /* Amplitude of square wave generator */
#endif
static int beep_count; /* Number of samples remaining to generate */
/* Reserve enough static space for keyclick to fit */
#define BEEP_BUF_COUNT (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
#define BEEP_COUNT(fs, duration) ((fs) / 1000 * (duration))
/* Reserve enough static space for keyclick to fit in worst case */
#define BEEP_BUF_COUNT BEEP_COUNT(PLAY_SAMPR_MAX, KEYCLICK_DURATION)
static int16_t beep_buf[BEEP_BUF_COUNT*2] IBSS_ATTR __attribute__((aligned(4)));
/* Callback to generate the beep frames - also don't want inlining of
@ -75,9 +77,10 @@ void beep_play(unsigned int frequency, unsigned int duration,
amplitude = INT16_MAX;
/* Setup the parameters for the square wave generator */
uint32_t fout = mixer_get_frequency();
beep_phase = 0;
beep_step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
beep_count = NATIVE_FREQUENCY / 1000 * duration;
beep_step = fp_div(frequency, fout, 32);
beep_count = BEEP_COUNT(fout, duration);
#ifdef BEEP_GENERIC
beep_amplitude = amplitude;

View file

@ -507,6 +507,7 @@ static void run_codec(void)
codec_queue_ack(Q_CODEC_RUN);
trigger_cpu_boost();
dsp_configure(ci.dsp, DSP_SET_OUT_FREQUENCY, pcmbuf_get_frequency());
if (!encoder)
{

View file

@ -274,3 +274,7 @@ lowmem
#if defined(HAVE_HARDWARE_CLICK)
hardware_click
#endif
#if defined(HAVE_PLAY_FREQ)
play_frequency
#endif

View file

@ -13156,3 +13156,20 @@
*: "Slow"
</voice>
</phrase>
<phrase>
id: LANG_PLAYBACK_FREQUENCY
desc: in playback settings (merge with LANG_RECORDING_FREQUENCY if cleaning)
user: core
<source>
*: none
play_frequency: "Frequency"
</source>
<dest>
*: none
play_frequency: "Frequency"
</dest>
<voice>
*: none
play_frequency: "Frequency"
</voice>
</phrase>

View file

@ -37,6 +37,10 @@
#include "misc.h"
#if CONFIG_CODEC == SWCODEC
#include "playback.h"
#include "pcm_sampr.h"
#ifdef HAVE_PLAY_FREQ
#include "talk.h"
#endif
#endif
@ -192,6 +196,10 @@ MENUITEM_SETTING(prevent_skip, &global_settings.prevent_skip, NULL);
MENUITEM_SETTING(resume_rewind, &global_settings.resume_rewind, NULL);
#endif
MENUITEM_SETTING(pause_rewind, &global_settings.pause_rewind, NULL);
#ifdef HAVE_PLAY_FREQ
MENUITEM_SETTING(play_frequency, &global_settings.play_frequency,
playback_callback);
#endif
MAKE_MENU(playback_settings,ID2P(LANG_PLAYBACK),0,
Icon_Playback_menu,
@ -217,12 +225,15 @@ MAKE_MENU(playback_settings,ID2P(LANG_PLAYBACK),0,
#ifdef HAVE_HEADPHONE_DETECTION
,&unplug_menu
#endif
,&skip_length, &prevent_skip,
,&skip_length, &prevent_skip
#if CONFIG_CODEC == SWCODEC
&resume_rewind,
,&resume_rewind
#endif
,&pause_rewind
#ifdef HAVE_PLAY_FREQ
,&play_frequency
#endif
&pause_rewind,
);
static int playback_callback(int action,const struct menu_item_ex *this_item)
@ -243,9 +254,19 @@ static int playback_callback(int action,const struct menu_item_ex *this_item)
break;
case ACTION_EXIT_MENUITEM: /* on exit */
/* Playing or not */
#ifdef HAVE_PLAY_FREQ
if (this_item == &play_frequency)
{
settings_apply_play_freq(global_settings.play_frequency, false);
break;
}
#endif /* HAVE_PLAY_FREQ */
if (!(audio_status() & AUDIO_STATUS_PLAY))
break;
/* Playing only */
if (this_item == &shuffle_item)
{
if (old_shuffle == global_settings.playlist_shuffle)

View file

@ -40,7 +40,6 @@
#include "settings.h"
#include "audio.h"
#include "voice_thread.h"
#include "dsp_core.h"
/* This is the target fill size of chunks on the pcm buffer
Can be any number of samples but power of two sizes make for faster and
@ -66,11 +65,11 @@
chunks */
/* Return data level in 1/4-second increments */
#define DATA_LEVEL(quarter_secs) (NATIVE_FREQUENCY * (quarter_secs))
#define DATA_LEVEL(quarter_secs) (pcmbuf_sampr * (quarter_secs))
/* Number of bytes played per second:
(sample rate * 2 channels * 2 bytes/sample) */
#define BYTERATE (NATIVE_FREQUENCY * 4)
#define BYTERATE (pcmbuf_sampr * 2 * 2)
#if MEMORYSIZE > 2
/* Keep watermark high for large memory target - at least (2s) */
@ -104,6 +103,7 @@ static size_t pcmbuf_size;
static struct chunkdesc *pcmbuf_descriptors;
static unsigned int pcmbuf_desc_count;
static unsigned int position_key = 1;
static unsigned int pcmbuf_sampr = 0;
static size_t chunk_ridx;
static size_t chunk_widx;
@ -111,8 +111,7 @@ static size_t chunk_widx;
static size_t pcmbuf_bytes_waiting;
static struct chunkdesc *current_desc;
/* Only written if HAVE_CROSSFADE */
static size_t pcmbuf_watermark = PCMBUF_WATERMARK;
static size_t pcmbuf_watermark = 0;
static bool low_latency_mode = false;
@ -545,6 +544,8 @@ size_t pcmbuf_init(void *bufend)
}
pcmbuf_finish_crossfade_enable();
#else
pcmbuf_watermark = PCMBUF_WATERMARK;
#endif /* HAVE_CROSSFADE */
init_buffer_state();
@ -1331,3 +1332,13 @@ void pcmbuf_set_low_latency(bool state)
{
low_latency_mode = state;
}
void pcmbuf_update_frequency(void)
{
pcmbuf_sampr = mixer_get_frequency();
}
unsigned int pcmbuf_get_frequency(void)
{
return pcmbuf_sampr;
}

View file

@ -81,5 +81,7 @@ void pcmbuf_sync_position_update(void);
/* Misc */
bool pcmbuf_is_lowdata(void);
void pcmbuf_set_low_latency(bool state);
void pcmbuf_update_frequency(void);
unsigned int pcmbuf_get_frequency(void);
#endif /* PCMBUF_H */

View file

@ -2028,8 +2028,11 @@ static int audio_fill_file_buffer(void)
/* Must reset the buffer before use if trashed or voice only - voice
file size shouldn't have changed so we can go straight from
AUDIOBUF_STATE_VOICED_ONLY to AUDIOBUF_STATE_INITIALIZED */
if (buffer_state != AUDIOBUF_STATE_INITIALIZED)
if (buffer_state != AUDIOBUF_STATE_INITIALIZED ||
!pcmbuf_is_same_size())
{
audio_reset_buffer(AUDIOBUF_STATE_INITIALIZED);
}
logf("Starting buffer fill");
@ -2510,6 +2513,11 @@ static void audio_start_playback(size_t offset, unsigned int flags)
#ifndef PLATFORM_HAS_VOLUME_CHANGE
sound_set_volume(global_settings.volume);
#endif
#ifdef HAVE_PLAY_FREQ
settings_apply_play_freq(global_settings.play_frequency, true);
#endif
pcmbuf_update_frequency();
/* Be sure channel is audible */
pcmbuf_fade(false, true);
@ -3755,6 +3763,7 @@ void INIT_ATTR playback_init(void)
mutex_init(&id3_mutex);
track_list_init();
buffering_init();
pcmbuf_update_frequency();
add_event(PLAYBACK_EVENT_VOICE_PLAYING, false, playback_voice_event);
#ifdef HAVE_CROSSFADE
/* Set crossfade setting for next buffer init which should be about... */

View file

@ -798,6 +798,8 @@ static const struct plugin_api rockbox_api = {
/* new stuff at the end, sort into place next time
the API gets incompatible */
mixer_set_frequency,
mixer_get_frequency,
};
int plugin_load(const char* plugin, const void* parameter)

View file

@ -155,7 +155,7 @@ void* plugin_get_buffer(size_t *buffer_size);
#define PLUGIN_MAGIC 0x526F634B /* RocK */
/* increase this every time the api struct changes */
#define PLUGIN_API_VERSION 223
#define PLUGIN_API_VERSION 224
/* update this to latest version if a change to the api struct breaks
backwards compatibility (and please take the opportunity to sort in any
@ -970,6 +970,8 @@ struct plugin_api {
/* new stuff at the end, sort into place next time
the API gets incompatible */
void (*mixer_set_frequency)(unsigned int samplerate);
unsigned int (*mixer_get_frequency)(void);
};
/* plugin header */

View file

@ -37,6 +37,8 @@ resistor.c
remote_control.c
#endif
test_codec.c
test_sampr.c
#ifdef HAVE_BACKLIGHT

View file

@ -481,6 +481,7 @@ static void audio_thread(void)
init_mad();
td.dsp = rb->dsp_get_config(CODEC_IDX_AUDIO);
rb->dsp_configure(td.dsp, DSP_SET_OUT_FREQUENCY, CLOCK_RATE);
#ifdef HAVE_PITCHCONTROL
rb->sound_set_pitch(PITCH_SPEED_100);
rb->dsp_set_timestretch(PITCH_SPEED_100);

View file

@ -44,7 +44,7 @@
#define AUDIOBUF_ALLOC_SIZE (AUDIOBUF_SIZE+AUDIOBUF_GUARD_SIZE)
/** PCM buffer **/
#define CLOCK_RATE NATIVE_FREQUENCY /* Our clock rate in ticks/second (samplerate) */
#define CLOCK_RATE 44100 /* Our clock rate in ticks/second (samplerate) */
/* Define this as "1" to have a test tone instead of silence clip */
#define SILENCE_TEST_TONE 0

View file

@ -51,6 +51,8 @@ static uint32_t volatile clock_time IBSS_ATTR; /* Timestamp adjusted */
static int pcm_skipped = 0;
static int pcm_underruns = 0;
static unsigned int old_sampr = 0;
/* Small silence clip. ~5.80ms @ 44.1kHz */
static int16_t silence[256*2] ALIGNED_ATTR(4) = { 0 };
@ -380,9 +382,13 @@ bool pcm_output_init(void)
}
#endif
old_sampr = rb->mixer_get_frequency();
rb->mixer_set_frequency(CLOCK_RATE);
return true;
}
void pcm_output_exit(void)
{
if (old_sampr != 0)
rb->mixer_set_frequency(old_sampr);
}

View file

@ -1200,13 +1200,14 @@ static long anim_peaks_vertical(void)
/** Waveform View **/
#ifdef OSCILLOSCOPE_GRAPHMODE
static int16_t waveform_buffer[2*ALIGN_UP(NATIVE_FREQUENCY, 2048)+2*2048]
static int16_t waveform_buffer[2*ALIGN_UP(PLAY_SAMPR_MAX, 2048)+2*2048]
MEM_ALIGN_ATTR;
static size_t waveform_buffer_threshold = 0;
static size_t volatile waveform_buffer_have = 0;
static size_t waveform_buffer_break = 0;
static unsigned long mixer_sampr = PLAY_SAMPR_DEFAULT;
#define PCM_SAMPLESIZE (2*sizeof(int16_t))
#define PCM_BYTERATE (NATIVE_FREQUENCY*PCM_SAMPLESIZE)
#define PCM_BYTERATE(sampr) ((sampr)*PCM_SAMPLESIZE)
#define WAVEFORM_SCALE_PCM(full_scale, sample) \
((((full_scale) * (sample)) + (1 << 14)) >> 15)
@ -1390,7 +1391,7 @@ static long anim_waveform_horizontal(void)
return cur_tick + HZ/5;
}
int count = (NATIVE_FREQUENCY*osc_delay + 100*HZ - 1) / (100*HZ);
int count = (mixer_sampr*osc_delay + 100*HZ - 1) / (100*HZ);
waveform_buffer_set_threshold(count*PCM_SAMPLESIZE);
@ -1516,7 +1517,8 @@ static long anim_waveform_horizontal(void)
osd_lcd_update();
long delay = get_next_delay();
return cur_tick + delay - waveform_buffer_have * HZ / PCM_BYTERATE;
return cur_tick + delay - waveform_buffer_have * HZ /
PCM_BYTERATE(mixer_sampr);
}
static void anim_waveform_plot_filled_v(int y, int y_prev,
@ -1583,7 +1585,7 @@ static long anim_waveform_vertical(void)
return cur_tick + HZ/5;
}
int count = (NATIVE_FREQUENCY*osc_delay + 100*HZ - 1) / (100*HZ);
int count = (mixer_sampr*osc_delay + 100*HZ - 1) / (100*HZ);
waveform_buffer_set_threshold(count*PCM_SAMPLESIZE);
@ -1709,7 +1711,8 @@ static long anim_waveform_vertical(void)
osd_lcd_update();
long delay = get_next_delay();
return cur_tick + delay - waveform_buffer_have * HZ / PCM_BYTERATE;
return cur_tick + delay - waveform_buffer_have * HZ
/ PCM_BYTERATE(mixer_sampr);
}
static void anim_waveform_exit(void)
@ -1872,6 +1875,10 @@ static void osc_setup(void)
osd_lcd_update();
#endif
#ifdef OSCILLOSCOPE_GRAPHMODE
mixer_sampr = rb->mixer_get_frequency();
#endif
/* Turn off backlight timeout */
backlight_ignore_timeout();
graphmode_setup();

View file

@ -502,7 +502,12 @@ static void configure(int setting, intptr_t value)
{
case DSP_SET_FREQUENCY:
DEBUGF("samplerate=%d\n",(int)value);
wavinfo.samplerate = use_dsp ? NATIVE_FREQUENCY : (int)value;
if (use_dsp) {
wavinfo.samplerate = rb->dsp_configure(
ci.dsp, DSP_GET_OUT_FREQUENCY, 0);
} else {
wavinfo.samplerate = (int)value;
}
break;
case DSP_SET_SAMPLE_DEPTH:

View file

@ -71,4 +71,8 @@ static inline void dsp_process_end(struct dsp_loop_context *ctx)
#endif
#define DSP_OUT_MIN_HZ PLAY_SAMPR_HW_MIN
#define DSP_OUT_MAX_HZ PLAY_SAMPR_MAX
#define DSP_OUT_DEFAULT_HZ PLAY_SAMPR_DEFAULT
#endif

View file

@ -85,6 +85,11 @@ struct system_status global_status;
#ifdef HAVE_RECORDING
#include "enc_config.h"
#endif
#include "pcm_sampr.h"
#ifdef HAVE_PLAY_FREQ
#include "pcm_mixer.h"
#include "dsp_core.h"
#endif
#endif /* CONFIG_CODEC == SWCODEC */
#define NVRAM_BLOCK_SIZE 44
@ -720,6 +725,36 @@ void settings_apply_pm_range(void)
}
#endif /* HAVE_LCD_BITMAP */
#ifdef HAVE_PLAY_FREQ
void settings_apply_play_freq(int value, bool playback)
{
static const unsigned long play_sampr[] = { SAMPR_44, SAMPR_48 };
static int prev_setting = 0;
if ((unsigned)value >= ARRAYLEN(play_sampr))
value = 0;
bool changed = value != prev_setting;
prev_setting = value;
long offset = 0;
bool playing = changed && !playback &&
audio_status() == AUDIO_STATUS_PLAY;
if (playing)
offset = audio_current_track()->offset;
if (changed && !playback)
audio_hard_stop();
/* Other sub-areas of playback pick it up from the mixer */
mixer_set_frequency(play_sampr[value]);
if (playing)
audio_play(offset);
}
#endif /* HAVE_PLAY_FREQ */
void sound_settings_apply(void)
{
#ifdef AUDIOHW_HAVE_BASS
@ -976,6 +1011,9 @@ void settings_apply(bool read_disk)
set_codepage(global_settings.default_codepage);
CHART("<set_codepage");
#ifdef HAVE_PLAY_FREQ
settings_apply_play_freq(global_settings.play_frequency, false);
#endif
#if CONFIG_CODEC == SWCODEC
#ifdef HAVE_CROSSFADE
audio_set_crossfade(global_settings.crossfade);

View file

@ -223,6 +223,9 @@ void settings_apply_skins(void);
void settings_apply(bool read_disk);
void settings_apply_pm_range(void);
#ifdef HAVE_PLAY_FREQ
void settings_apply_play_freq(int value, bool playback);
#endif
void settings_display(void);
enum optiontype { INT, BOOL };
@ -821,6 +824,10 @@ struct user_settings
#ifdef HAVE_QUICKSCREEN
bool shortcuts_replaces_qs;
#endif
#ifdef HAVE_PLAY_FREQ
int play_frequency; /* core audio output frequency selection */
#endif
};
/** global variables **/

View file

@ -807,6 +807,11 @@ const struct settings_list settings[] = {
,ID2P(LANG_REPEAT_AB)
#endif
), /* CHOICE_SETTING( repeat_mode ) */
#ifdef HAVE_PLAY_FREQ
STRINGCHOICE_SETTING(0, play_frequency, LANG_PLAYBACK_FREQUENCY, 0,
"playback frequency", "44.1 kHz,48 kHz", NULL, 2,
TALK_ID_DECIMAL(441, 1, UNIT_KHZ), TALK_ID(48, UNIT_KHZ)),
#endif /* HAVE_PLAY_FREQ */
/* LCD */
#ifdef HAVE_LCD_CONTRAST
/* its easier to leave this one un-macro()ed for the time being */

View file

@ -18,7 +18,7 @@
* KIND, either express or implied.
*
****************************************************************************/
#include <sys/types.h>
#include "config.h"
#include "system.h"
#include "core_alloc.h"
#include "thread.h"
@ -30,8 +30,7 @@
#include "pcm_mixer.h"
#include "codecs/libspeex/speex/speex.h"
/* Default number of native-frequency PCM frames to queue - adjust as
necessary per-target */
/* Default number of PCM frames to queue - adjust as necessary per-target */
#define VOICE_FRAMES 4
/* Define any of these as "1" and uncomment the LOGF_ENABLE line to log
@ -84,8 +83,8 @@ static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
static int quiet_counter SHAREDDATA_ATTR = 0;
static bool voice_playing = false;
#define VOICE_PCM_FRAME_COUNT ((NATIVE_FREQUENCY*VOICE_FRAME_COUNT + \
VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
#define VOICE_PCM_FRAME_COUNT ((PLAY_SAMPR_MAX*VOICE_FRAME_COUNT + \
VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
#define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*2*sizeof (int16_t))
/* Voice processing states */
@ -356,11 +355,13 @@ static enum voice_state voice_message(struct voice_thread_data *td)
{
/* Stop any clip still playing */
voice_stop_playback();
dsp_configure(td->dsp, DSP_FLUSH, 0);
}
if (quiet_counter <= 0)
{
voice_playing = true;
dsp_configure(td->dsp, DSP_SET_OUT_FREQUENCY, mixer_get_frequency());
send_event(PLAYBACK_EVENT_VOICE_PLAYING, &voice_playing);
}