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>
diff --git a/apps/beep.c b/apps/beep.c
index 8ac7ccf..25b5e0e 100644
--- a/apps/beep.c
+++ b/apps/beep.c
@@ -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 @@
 #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 @@
         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;
diff --git a/apps/codec_thread.c b/apps/codec_thread.c
index d4b1c64..8f9f5a3 100644
--- a/apps/codec_thread.c
+++ b/apps/codec_thread.c
@@ -507,6 +507,7 @@
     codec_queue_ack(Q_CODEC_RUN);
 
     trigger_cpu_boost();
+    dsp_configure(ci.dsp, DSP_SET_OUT_FREQUENCY, pcmbuf_get_frequency());
 
     if (!encoder)
     {
diff --git a/apps/features.txt b/apps/features.txt
index a65744f..897657f 100644
--- a/apps/features.txt
+++ b/apps/features.txt
@@ -274,3 +274,7 @@
 #if defined(HAVE_HARDWARE_CLICK)
 hardware_click
 #endif
+
+#if defined(HAVE_PLAY_FREQ)
+play_frequency
+#endif
diff --git a/apps/lang/english.lang b/apps/lang/english.lang
index dbd0baa..0dd3b43 100644
--- a/apps/lang/english.lang
+++ b/apps/lang/english.lang
@@ -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>
diff --git a/apps/menus/playback_menu.c b/apps/menus/playback_menu.c
index 6beda93..89472d4 100644
--- a/apps/menus/playback_menu.c
+++ b/apps/menus/playback_menu.c
@@ -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(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 @@
 #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,
+          ,&pause_rewind
+#ifdef HAVE_PLAY_FREQ
+          ,&play_frequency
+#endif
          );
          
 static int playback_callback(int action,const struct menu_item_ex *this_item)
@@ -243,9 +254,19 @@
             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)
diff --git a/apps/pcmbuf.c b/apps/pcmbuf.c
index cc454a4..ff9b3e1 100644
--- a/apps/pcmbuf.c
+++ b/apps/pcmbuf.c
@@ -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 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 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 @@
     }
 
     pcmbuf_finish_crossfade_enable();
+#else 
+    pcmbuf_watermark = PCMBUF_WATERMARK;
 #endif /* HAVE_CROSSFADE */
 
     init_buffer_state();
@@ -1331,3 +1332,13 @@
 {
     low_latency_mode = state;
 }
+
+void pcmbuf_update_frequency(void)
+{
+    pcmbuf_sampr = mixer_get_frequency();
+}
+
+unsigned int pcmbuf_get_frequency(void)
+{
+    return pcmbuf_sampr;
+}
diff --git a/apps/pcmbuf.h b/apps/pcmbuf.h
index 7fa3563..008872b 100644
--- a/apps/pcmbuf.h
+++ b/apps/pcmbuf.h
@@ -81,5 +81,7 @@
 /* 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 */
diff --git a/apps/playback.c b/apps/playback.c
index 24c268f..8b498f2 100644
--- a/apps/playback.c
+++ b/apps/playback.c
@@ -2028,8 +2028,11 @@
     /* 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 @@
 #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 @@
     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... */
diff --git a/apps/plugin.c b/apps/plugin.c
index 24443b5..a5cdfc3 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -798,6 +798,8 @@
     /* 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)
diff --git a/apps/plugin.h b/apps/plugin.h
index 936f977..f926b34 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -155,7 +155,7 @@
 #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 @@
     /* 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 */
diff --git a/apps/plugins/SOURCES b/apps/plugins/SOURCES
index c512a9e..00bf960 100644
--- a/apps/plugins/SOURCES
+++ b/apps/plugins/SOURCES
@@ -37,6 +37,8 @@
 remote_control.c
 #endif
 
+test_codec.c
+test_sampr.c
 
 
 #ifdef HAVE_BACKLIGHT
diff --git a/apps/plugins/mpegplayer/audio_thread.c b/apps/plugins/mpegplayer/audio_thread.c
index 1c167ea..764ad11 100644
--- a/apps/plugins/mpegplayer/audio_thread.c
+++ b/apps/plugins/mpegplayer/audio_thread.c
@@ -481,6 +481,7 @@
     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);
diff --git a/apps/plugins/mpegplayer/mpegplayer.h b/apps/plugins/mpegplayer/mpegplayer.h
index 32cc7b2..4ddf0ca 100644
--- a/apps/plugins/mpegplayer/mpegplayer.h
+++ b/apps/plugins/mpegplayer/mpegplayer.h
@@ -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
diff --git a/apps/plugins/mpegplayer/pcm_output.c b/apps/plugins/mpegplayer/pcm_output.c
index 3af8e91..82e3584 100644
--- a/apps/plugins/mpegplayer/pcm_output.c
+++ b/apps/plugins/mpegplayer/pcm_output.c
@@ -51,6 +51,8 @@
 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 @@
     }
 #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);
 }
diff --git a/apps/plugins/oscilloscope.c b/apps/plugins/oscilloscope.c
index a4ec6a8..4d80749 100644
--- a/apps/plugins/oscilloscope.c
+++ b/apps/plugins/oscilloscope.c
@@ -1200,13 +1200,14 @@
 /** 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 @@
         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 @@
     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 @@
         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 @@
     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 @@
     osd_lcd_update();
 #endif
 
+#ifdef OSCILLOSCOPE_GRAPHMODE
+    mixer_sampr = rb->mixer_get_frequency();
+#endif
+
     /* Turn off backlight timeout */
     backlight_ignore_timeout();
     graphmode_setup();
diff --git a/apps/plugins/test_codec.c b/apps/plugins/test_codec.c
index 7523d9e..0b409f8 100644
--- a/apps/plugins/test_codec.c
+++ b/apps/plugins/test_codec.c
@@ -502,7 +502,12 @@
     {
         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:
diff --git a/apps/rbcodecconfig.h b/apps/rbcodecconfig.h
index ff9fc41..cc51595 100644
--- a/apps/rbcodecconfig.h
+++ b/apps/rbcodecconfig.h
@@ -71,4 +71,8 @@
 
 #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
diff --git a/apps/settings.c b/apps/settings.c
index adc53cd..cf51b07 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -85,6 +85,11 @@
 #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 @@
 }
 #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 @@
     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);
diff --git a/apps/settings.h b/apps/settings.h
index 1aec931..087ff0c 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -223,6 +223,9 @@
 
 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 @@
 #ifdef HAVE_QUICKSCREEN
     bool shortcuts_replaces_qs;
 #endif
+
+#ifdef HAVE_PLAY_FREQ
+    int play_frequency; /* core audio output frequency selection */
+#endif
 };
 
 /** global variables **/
diff --git a/apps/settings_list.c b/apps/settings_list.c
index c1b40a6..980c74f 100644
--- a/apps/settings_list.c
+++ b/apps/settings_list.c
@@ -807,6 +807,11 @@
                    ,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 */
diff --git a/apps/voice_thread.c b/apps/voice_thread.c
index 46471c0..7788f65 100644
--- a/apps/voice_thread.c
+++ b/apps/voice_thread.c
@@ -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 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 @@
         {
             /* 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);
         }