pcmbuf: sorted functions into logical sections for readability. Tiny changes, nothing functional

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23541 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/pcmbuf.c b/apps/pcmbuf.c
index 1e286fa..ad1dfca 100644
--- a/apps/pcmbuf.c
+++ b/apps/pcmbuf.c
@@ -142,13 +142,13 @@
 #define LOW_DATA(quarter_secs) \
     (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
 
-static bool prepare_insert(size_t length);
-static void pcmbuf_under_watermark(bool under);
 static bool pcmbuf_flush_fillpos(void);
-
+static void pcmbuf_finish_track_change(void);
+static void crossfade_start(void);
+static void flush_crossfade(char *buf, size_t length);
 static bool pcmbuf_crossfade_init(bool manual_skip);
-static bool pcmbuf_is_crossfade_enabled(void);
 static void pcmbuf_finish_crossfade_enable(void);
+static bool pcmbuf_is_crossfade_enabled(void);
 
 
 /**************************************/
@@ -190,70 +190,295 @@
 #define DISPLAY_DESC(caller) do{}while(0)
 #endif
 
-/* Track change functions */
 
-/* The codec is moving on to the next track, but the current track is
- * still playing.  Set flags to make sure the elapsed time of the current
- * track is updated properly, and mark the currently written chunk as the
- * last one in the track. */
-static void pcmbuf_gapless_track_change(void)
-{
-    /* we're starting a track transition */
-    track_transition = true;
-    
-    /* mark the last chunk in the track */
-    end_of_track = true;
-}
+/* Commit PCM data */
 
-static void pcmbuf_crossfade_track_change(void)
+#ifdef HAVE_PRIORITY_SCHEDULING
+static void boost_codec_thread(bool boost)
 {
-    /* Initiate automatic crossfade mode */
-    pcmbuf_crossfade_init(false);
-    /* Notify the wps that the track change starts now */
-    audio_post_track_change(false);
-}
-
-void pcmbuf_start_track_change(bool manual_skip)
-{
-    /* Manual track change (always crossfade or flush audio). */
-    if (manual_skip)
+    /* Keep voice and codec threads at the same priority or else voice
+     * will starve if the codec thread's priority is boosted. */
+    if (boost)
     {
-        pcmbuf_crossfade_init(true);
-        audio_post_track_change(false);
-    }
-    /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */
-    else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active()
-             && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP)
-    {
-        if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP)
+        int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
+                          / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
+
+        if (priority != codec_thread_priority)
         {
-            if (global_settings.playlist_shuffle)
-                /* shuffle mode is on, so crossfade: */
-                pcmbuf_crossfade_track_change();
-            else
-                /* shuffle mode is off, so normal gapless playback */
-                pcmbuf_gapless_track_change();
+            codec_thread_priority = priority;
+            thread_set_priority(codec_thread_id, priority);
+            voice_thread_set_priority(priority);
+        }
+    }
+    else if (codec_thread_priority != PRIORITY_PLAYBACK)
+    {
+        thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
+        voice_thread_set_priority(PRIORITY_PLAYBACK);
+        codec_thread_priority = PRIORITY_PLAYBACK;
+    }
+}
+#endif /* HAVE_PRIORITY_SCHEDULING */
+
+static void pcmbuf_under_watermark(bool under)
+{
+    /* Only codec thread initiates boost - voice boosts the cpu when playing
+       a clip */
+#ifndef SIMULATOR
+    if (thread_get_current() == codec_thread_id)
+#endif /* SIMULATOR */
+    {
+        if (under)
+        {
+            /* Fill audio buffer by boosting cpu */
+            trigger_cpu_boost();
+#ifdef HAVE_PRIORITY_SCHEDULING
+            /* If buffer is critically low, override UI priority, else
+               set back to the original priority. */
+            boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
+#endif
         }
         else
-            /* normal crossfade:  */
-            pcmbuf_crossfade_track_change();
+        {
+#ifdef HAVE_PRIORITY_SCHEDULING
+            boost_codec_thread(false);
+#endif
+        }
+    }
+
+    /* Disable crossfade if < .5s of audio */
+    if (LOW_DATA(2))
+    {
+        crossfade_active = false;
+    }
+}
+
+static bool prepare_insert(size_t length)
+{
+    if (low_latency_mode)
+    {
+        /* 1/4s latency. */
+        if (!LOW_DATA(1) && pcm_is_playing())
+            return false;
+    }
+
+    /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
+    if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
+        return false;
+
+    if (!pcm_is_playing())
+    {
+        trigger_cpu_boost();
+
+        /* Pre-buffer up to watermark */
+#if MEMORYSIZE > 2
+        if (!LOW_DATA(4))
+#else
+        if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
+#endif
+        {
+            logf("pcm starting");
+            if (!(audio_status() & AUDIO_STATUS_PAUSE))
+                pcmbuf_play_start();
+        }
     }
     else
-        /* normal gapless playback. */
-        pcmbuf_gapless_track_change();
+        pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
+
+    return true;
 }
 
-/* Called when the last chunk in the track has been played */
-static void pcmbuf_finish_track_change(void)
+void *pcmbuf_request_buffer(int *count)
 {
-    /* not in a track transition anymore */
-    if(track_transition){logf("pcmbuf: (finish change) track transition false");}
-    track_transition = false;
-    
-    /* notify playback that the track has just finished */
-    audio_post_track_change(true);
+#ifdef HAVE_CROSSFADE
+    if (crossfade_init)
+        crossfade_start();
+#endif
+
+    if (crossfade_active) {
+        *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
+        return fadebuf;
+    }
+    else
+    {
+        if(prepare_insert(*count << 2))
+        {
+            size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
+            if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
+            {
+                /* Usual case, there's space here */
+                return &audiobuffer[audiobuffer_index];
+            }
+            else
+            {
+                /* Flush and wrap the buffer */
+                pcmbuf_flush_fillpos();
+                audiobuffer_pos = 0;
+                return &audiobuffer[0];
+            }
+        }
+        else
+        {
+            return NULL;
+        }
+    }
 }
 
+void pcmbuf_write_complete(int count)
+{
+    size_t length = (size_t)(unsigned int)count << 2;
+#ifdef HAVE_CROSSFADE
+    if (crossfade_active)
+    {
+        flush_crossfade(fadebuf, length);
+        if (!(crossfade_fade_in_rem || crossfade_chunk))
+            crossfade_active = false;
+    }
+    else
+#endif    
+    {
+        audiobuffer_fillpos += length;
+
+        if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
+            pcmbuf_flush_fillpos();
+    }
+}
+
+/* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
+ * in a separate function for the moment */
+static inline void pcmbuf_add_chunk(void)
+{
+    register size_t size = audiobuffer_fillpos;
+    /* Grab the next description to write, and change the write pointer */
+    register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
+    pcmbuf_write = pcmbuf_current->link;
+    /* Fill in the values in the new buffer chunk */
+    pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
+    pcmbuf_current->size = size;
+    pcmbuf_current->end_of_track = end_of_track;
+    pcmbuf_current->link = NULL;
+    end_of_track = false;   /* This is single use only */
+    if (pcmbuf_read != NULL) {
+        if (pcmbuf_flush)
+        {
+            pcmbuf_write_end->link = pcmbuf_read->link;
+            pcmbuf_read->link = pcmbuf_current;
+            while (pcmbuf_write_end->link)
+            {
+                pcmbuf_write_end = pcmbuf_write_end->link;
+                pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
+            }
+            pcmbuf_flush = false;
+        }
+        /* If there is already a read buffer setup, add to it */
+        else
+            pcmbuf_read_end->link = pcmbuf_current;
+    } else {
+        /* Otherwise create the buffer */
+        pcmbuf_read = pcmbuf_current;
+    }
+    /* This is now the last buffer to read */
+    pcmbuf_read_end = pcmbuf_current;
+
+    /* Update bytes counters */
+    pcmbuf_unplayed_bytes += size;
+
+    audiobuffer_pos += size;
+    if (audiobuffer_pos >= pcmbuf_size)
+        audiobuffer_pos -= pcmbuf_size;
+
+    audiobuffer_fillpos = 0;
+    DISPLAY_DESC("add_chunk");
+}
+
+/**
+ * Commit samples waiting to the pcm buffer.
+ */
+static bool pcmbuf_flush_fillpos(void)
+{
+    if (audiobuffer_fillpos) {
+        /* Never use the last buffer descriptor */
+        while (pcmbuf_write == pcmbuf_write_end) {
+            /* If this happens, something is being stupid */
+            if (!pcm_is_playing()) {
+                logf("pcmbuf_flush_fillpos error");
+                pcmbuf_play_start();
+            }
+            /* Let approximately one chunk of data playback */
+            sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
+        }
+        pcmbuf_add_chunk();
+        return true;
+    }
+    return false;
+}
+
+
+/* Init */
+
+static void pcmbuf_init_pcmbuffers(void)
+{
+#ifdef DESC_DEBUG
+    first_desc = pcmbuf_write;
+#endif
+    struct pcmbufdesc *next = pcmbuf_write;
+    next++;
+    pcmbuf_write_end = pcmbuf_write;
+    while ((void *)next < (void *)pcmbuf_bufend) {
+        pcmbuf_write_end->link=next;
+        pcmbuf_write_end=next;
+        next++;
+    }
+    DISPLAY_DESC("init");
+}
+
+static size_t pcmbuf_get_next_required_pcmbuf_size(void)
+{
+    size_t seconds = 1;
+
+    if (crossfade_enabled_pending)
+        seconds += global_settings.crossfade_fade_out_delay
+                   + global_settings.crossfade_fade_out_duration;
+
+#if MEMORYSIZE > 2
+    /* Buffer has to be at least 2s long. */
+    seconds += 2;
+#endif
+    logf("pcmbuf len: %ld", (long)seconds);
+    return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
+}
+
+static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
+{
+    return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
+               PCMBUF_DESCS_SIZE(bufsize));
+}
+
+/* Initialize the pcmbuffer the structure looks like this:
+ * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
+size_t pcmbuf_init(unsigned char *bufend)
+{
+    pcmbuf_bufend = bufend;
+    pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
+    audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
+    fadebuf = &audiobuffer[pcmbuf_size];
+    voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
+    pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
+
+    pcmbuf_init_pcmbuffers();
+
+    if(track_transition){logf("pcmbuf: (init) track transition false");}
+    end_of_track = false;
+    track_transition = false;
+
+    pcmbuf_finish_crossfade_enable();
+
+    pcmbuf_play_stop();
+
+    return pcmbuf_bufend - audiobuffer;
+}
+
+
+/* Playback */
 
 /** PCM driver callback
  * This function has 3 major logical parts (separated by brackets both for
@@ -327,205 +552,16 @@
     DISPLAY_DESC("callback");
 }
 
-/* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
- * in a separate function for the moment */
-static inline void pcmbuf_add_chunk(void)
+/* Force playback. */
+void pcmbuf_play_start(void)
 {
-    register size_t size = audiobuffer_fillpos;
-    /* Grab the next description to write, and change the write pointer */
-    register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
-    pcmbuf_write = pcmbuf_current->link;
-    /* Fill in the values in the new buffer chunk */
-    pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
-    pcmbuf_current->size = size;
-    pcmbuf_current->end_of_track = end_of_track;
-    pcmbuf_current->link = NULL;
-    end_of_track = false;   /* This is single use only */
-    if (pcmbuf_read != NULL) {
-        if (pcmbuf_flush)
-        {
-            pcmbuf_write_end->link = pcmbuf_read->link;
-            pcmbuf_read->link = pcmbuf_current;
-            while (pcmbuf_write_end->link)
-            {
-                pcmbuf_write_end = pcmbuf_write_end->link;
-                pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
-            }
-            pcmbuf_flush = false;
-        }
-        /* If there is already a read buffer setup, add to it */
-        else
-            pcmbuf_read_end->link = pcmbuf_current;
-    } else {
-        /* Otherwise create the buffer */
-        pcmbuf_read = pcmbuf_current;
-    }
-    /* This is now the last buffer to read */
-    pcmbuf_read_end = pcmbuf_current;
-
-    /* Update bytes counters */
-    pcmbuf_unplayed_bytes += size;
-
-    audiobuffer_pos += size;
-    if (audiobuffer_pos >= pcmbuf_size)
-        audiobuffer_pos -= pcmbuf_size;
-
-    audiobuffer_fillpos = 0;
-    DISPLAY_DESC("add_chunk");
-}
-
-/**
- * Commit samples waiting to the pcm buffer.
- */
-static bool pcmbuf_flush_fillpos(void)
-{
-    if (audiobuffer_fillpos) {
-        /* Never use the last buffer descriptor */
-        while (pcmbuf_write == pcmbuf_write_end) {
-            /* If this happens, something is being stupid */
-            if (!pcm_is_playing()) {
-                logf("pcmbuf_flush_fillpos error");
-                pcmbuf_play_start();
-            }
-            /* Let approximately one chunk of data playback */
-            sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
-        }
-        pcmbuf_add_chunk();
-        return true;
-    }
-    return false;
-}
-
-#ifdef HAVE_PRIORITY_SCHEDULING
-static void boost_codec_thread(bool boost)
-{
-    /* Keep voice and codec threads at the same priority or else voice
-     * will starve if the codec thread's priority is boosted. */
-    if (boost)
+    if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
     {
-        int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
-                          / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
-
-        if (priority != codec_thread_priority)
-        {
-            codec_thread_priority = priority;
-            thread_set_priority(codec_thread_id, priority);
-            voice_thread_set_priority(priority);
-        }
+        last_chunksize = pcmbuf_read->size;
+        pcmbuf_unplayed_bytes -= last_chunksize;
+        pcm_play_data(pcmbuf_pcm_callback,
+            (unsigned char *)pcmbuf_read->addr, last_chunksize);
     }
-    else if (codec_thread_priority != PRIORITY_PLAYBACK)
-    {
-        thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
-        voice_thread_set_priority(PRIORITY_PLAYBACK);
-        codec_thread_priority = PRIORITY_PLAYBACK;
-    }
-}
-#endif /* HAVE_PRIORITY_SCHEDULING */
-
-static void pcmbuf_under_watermark(bool under)
-{
-    /* Only codec thread initiates boost - voice boosts the cpu when playing
-       a clip */
-#ifndef SIMULATOR
-    if (thread_get_current() == codec_thread_id)
-#endif /* SIMULATOR */
-    {
-        if (under)
-        {
-            /* Fill audio buffer by boosting cpu */
-            trigger_cpu_boost();
-#ifdef HAVE_PRIORITY_SCHEDULING
-            /* If buffer is critically low, override UI priority, else
-               set back to the original priority. */
-            boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
-#endif
-        }
-        else
-        {
-#ifdef HAVE_PRIORITY_SCHEDULING
-            boost_codec_thread(false);
-#endif
-        }
-    }
-
-    /* Disable crossfade if < .5s of audio */
-    if (LOW_DATA(2))
-    {
-        crossfade_active = false;
-    }
-}
-
-unsigned long pcmbuf_get_latency(void)
-{
-    /* Be careful how this calculation is rearranged, it's easy to overflow */
-    size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
-    return bytes / 4 * 1000 / NATIVE_FREQUENCY;
-}
-
-void pcmbuf_set_low_latency(bool state)
-{
-    low_latency_mode = state;
-}
-
-bool pcmbuf_is_lowdata(void)
-{
-    if (!pcm_is_playing() || pcm_is_paused() ||
-            crossfade_init || crossfade_active)
-        return false;
-
-#if MEMORYSIZE > 2
-    /* 1 seconds of buffer is low data */
-    return LOW_DATA(4);
-#else
-    /* under watermark is low data */
-    return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
-#endif
-}
-
-/* Amount of bytes left in the buffer. */
-inline size_t pcmbuf_free(void)
-{
-    if (pcmbuf_read != NULL)
-    {
-        void *read = pcmbuf_read->addr;
-        void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
-        if (read < write)
-            return (size_t)(read - write) + pcmbuf_size;
-        else
-            return (size_t) (read - write);
-    }
-    return pcmbuf_size;
-}
-
-static bool pcmbuf_crossfade_init(bool manual_skip)
-{
-    /* Can't do two crossfades at once and, no fade if pcm is off now */
-    if (crossfade_init || crossfade_active || !pcm_is_playing())
-    {
-        pcmbuf_play_stop();
-        return false;
-    }
-
-    trigger_cpu_boost();
-
-    /* Not enough data, or crossfade disabled, flush the old data instead */
-    if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
-    {
-        pcmbuf_flush_fillpos();
-        pcmbuf_flush = true;
-        return false;
-    }
-
-    /* Don't enable mix mode when skipping tracks manually. */
-    if (manual_skip)
-        crossfade_mixmode = false;
-    else
-        crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
-
-    crossfade_init = true;
-
-    return true;
-
 }
 
 void pcmbuf_play_stop(void)
@@ -552,115 +588,6 @@
 #endif
 }
 
-int pcmbuf_used_descs(void)
-{
-    struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
-    unsigned int i = 0;
-    while (pcmbuf_temp) {
-        pcmbuf_temp = pcmbuf_temp->link;
-        i++;
-    }
-    return i;
-}
-
-int pcmbuf_descs(void)
-{
-    return PCMBUF_DESCS(pcmbuf_size);
-}
-
-static void pcmbuf_init_pcmbuffers(void)
-{
-#ifdef DESC_DEBUG
-    first_desc = pcmbuf_write;
-#endif
-    struct pcmbufdesc *next = pcmbuf_write;
-    next++;
-    pcmbuf_write_end = pcmbuf_write;
-    while ((void *)next < (void *)pcmbuf_bufend) {
-        pcmbuf_write_end->link=next;
-        pcmbuf_write_end=next;
-        next++;
-    }
-    DISPLAY_DESC("init");
-}
-
-static size_t pcmbuf_get_next_required_pcmbuf_size(void)
-{
-    size_t seconds = 1;
-
-    if (crossfade_enabled_pending)
-        seconds += global_settings.crossfade_fade_out_delay
-                   + global_settings.crossfade_fade_out_duration;
-
-#if MEMORYSIZE > 2
-    /* Buffer has to be at least 2s long. */
-    seconds += 2;
-#endif
-    logf("pcmbuf len: %ld", (long)seconds);
-    return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
-}
-
-static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
-{
-    return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
-               PCMBUF_DESCS_SIZE(bufsize));
-}
-
-bool pcmbuf_is_same_size(void)
-{
-    bool same_size;
-    
-    if (audiobuffer == NULL)
-        same_size = true;   /* Not set up yet even once so always */
-    else
-    {
-        size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
-        same_size = pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
-    }
-    
-    if (same_size)
-        pcmbuf_finish_crossfade_enable();
-    
-    return same_size;
-}
-
-/* Initialize the pcmbuffer the structure looks like this:
- * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
-size_t pcmbuf_init(unsigned char *bufend)
-{
-    pcmbuf_bufend = bufend;
-    pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
-    audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
-    fadebuf = &audiobuffer[pcmbuf_size];
-    voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
-    pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
-
-    pcmbuf_init_pcmbuffers();
-
-    if(track_transition){logf("pcmbuf: (init) track transition false");}
-    end_of_track = false;
-    track_transition = false;
-
-    pcmbuf_finish_crossfade_enable();
-
-    pcmbuf_play_stop();
-
-    return pcmbuf_bufend - audiobuffer;
-}
-
-size_t pcmbuf_get_bufsize(void)
-{
-    return pcmbuf_size;
-}
-
-#ifdef ROCKBOX_HAS_LOGF
-unsigned char * pcmbuf_get_meminfo(size_t *length)
-{
-    *length = pcmbuf_bufend - audiobuffer;
-    return audiobuffer;
-}
-#endif
-
 void pcmbuf_pause(bool pause)
 {
     if (pcm_is_playing())
@@ -669,18 +596,74 @@
         pcmbuf_play_start();
 }
 
-/* Force playback. */
-void pcmbuf_play_start(void)
+
+/* Track change */
+
+/* The codec is moving on to the next track, but the current track is
+ * still playing.  Set flags to make sure the elapsed time of the current
+ * track is updated properly, and mark the currently written chunk as the
+ * last one in the track. */
+static void pcmbuf_gapless_track_change(void)
 {
-    if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
-    {
-        last_chunksize = pcmbuf_read->size;
-        pcmbuf_unplayed_bytes -= last_chunksize;
-        pcm_play_data(pcmbuf_pcm_callback,
-            (unsigned char *)pcmbuf_read->addr, last_chunksize);
-    }
+    /* we're starting a track transition */
+    track_transition = true;
+    
+    /* mark the last chunk in the track */
+    end_of_track = true;
 }
 
+static void pcmbuf_crossfade_track_change(void)
+{
+    /* Initiate automatic crossfade mode */
+    pcmbuf_crossfade_init(false);
+    /* Notify the wps that the track change starts now */
+    audio_post_track_change(false);
+}
+
+void pcmbuf_start_track_change(bool manual_skip)
+{
+    /* Manual track change (always crossfade or flush audio). */
+    if (manual_skip)
+    {
+        pcmbuf_crossfade_init(true);
+        audio_post_track_change(false);
+    }
+    /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */
+    else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active()
+             && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP)
+    {
+        if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP)
+        {
+            if (global_settings.playlist_shuffle)
+                /* shuffle mode is on, so crossfade: */
+                pcmbuf_crossfade_track_change();
+            else
+                /* shuffle mode is off, so normal gapless playback */
+                pcmbuf_gapless_track_change();
+        }
+        else
+            /* normal crossfade:  */
+            pcmbuf_crossfade_track_change();
+    }
+    else
+        /* normal gapless playback. */
+        pcmbuf_gapless_track_change();
+}
+
+/* Called when the last chunk in the track has been played */
+static void pcmbuf_finish_track_change(void)
+{
+    /* not in a track transition anymore */
+    if(track_transition){logf("pcmbuf: (finish change) track transition false");}
+    track_transition = false;
+    
+    /* notify playback that the track has just finished */
+    audio_post_track_change(true);
+}
+
+
+/* Crossfade */
+
 /** 
  * Low memory targets don't have crossfade, so don't compile crossfade
  * specific code in order to save some memory.                         */
@@ -925,95 +908,257 @@
 }
 #endif /* HAVE_CROSSFADE */
 
-static bool prepare_insert(size_t length)
+static bool pcmbuf_crossfade_init(bool manual_skip)
 {
-    if (low_latency_mode)
+    /* Can't do two crossfades at once and, no fade if pcm is off now */
+    if (crossfade_init || crossfade_active || !pcm_is_playing())
     {
-        /* 1/4s latency. */
-        if (!LOW_DATA(1) && pcm_is_playing())
-            return false;
-    }
-
-    /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
-    if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
+        pcmbuf_play_stop();
         return false;
-
-    if (!pcm_is_playing())
-    {
-        trigger_cpu_boost();
-
-        /* Pre-buffer up to watermark */
-#if MEMORYSIZE > 2
-        if (!LOW_DATA(4))
-#else
-        if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
-#endif
-        {
-            logf("pcm starting");
-            if (!(audio_status() & AUDIO_STATUS_PAUSE))
-                pcmbuf_play_start();
-        }
     }
+
+    trigger_cpu_boost();
+
+    /* Not enough data, or crossfade disabled, flush the old data instead */
+    if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
+    {
+        pcmbuf_flush_fillpos();
+        pcmbuf_flush = true;
+        return false;
+    }
+
+    /* Don't enable mix mode when skipping tracks manually. */
+    if (manual_skip)
+        crossfade_mixmode = false;
     else
-        pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
+        crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
+
+    crossfade_init = true;
 
     return true;
+
 }
 
-void *pcmbuf_request_buffer(int *count)
+static void pcmbuf_finish_crossfade_enable(void)
 {
-#ifdef HAVE_CROSSFADE
-    if (crossfade_init)
-        crossfade_start();
-#endif
+    /* Copy the pending setting over now */
+    crossfade_enabled = crossfade_enabled_pending;
+    
+    pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
+        /* If crossfading, try to keep the buffer full other than 1 second */
+        (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
+        /* Otherwise, just use the default */
+        PCMBUF_WATERMARK;
+}
 
-    if (crossfade_active) {
-        *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
-        return fadebuf;
-    }
+static bool pcmbuf_is_crossfade_enabled(void)
+{
+    if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
+        return global_settings.playlist_shuffle;
+
+    return crossfade_enabled;
+}
+
+bool pcmbuf_is_crossfade_active(void)
+{
+    return crossfade_active || crossfade_init;
+}
+
+void pcmbuf_request_crossfade_enable(bool on_off)
+{
+    /* Next setting to be used, not applied now */
+    crossfade_enabled_pending = on_off;
+}
+
+bool pcmbuf_is_same_size(void)
+{
+    bool same_size;
+    
+    if (audiobuffer == NULL)
+        same_size = true;   /* Not set up yet even once so always */
     else
     {
-        if(prepare_insert(*count << 2))
+        size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
+        same_size = pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
+    }
+    
+    if (same_size)
+        pcmbuf_finish_crossfade_enable();
+    
+    return same_size;
+}
+
+
+/* Voice */
+
+/* Returns pcm buffer usage in percents (0 to 100). */
+static int pcmbuf_usage(void)
+{
+    return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
+}
+
+static int pcmbuf_mix_free(void)
+{
+    if (pcmbuf_mix_chunk)
+    {
+        size_t my_mix_end =
+            (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
+        size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
+        if (my_write_pos < my_mix_end)
+            my_write_pos += pcmbuf_size;
+        return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
+    }
+    return 100;
+}
+
+void *pcmbuf_request_voice_buffer(int *count)
+{
+    /* A get-it-to-work-for-now hack (audio status could change by
+       completion) */
+    if (audio_status() & AUDIO_STATUS_PLAY)
+    {
+        if (pcmbuf_read == NULL)
         {
-            size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
-            if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
-            {
-                /* Usual case, there's space here */
-                return &audiobuffer[audiobuffer_index];
-            }
-            else
-            {
-                /* Flush and wrap the buffer */
-                pcmbuf_flush_fillpos();
-                audiobuffer_pos = 0;
-                return &audiobuffer[0];
-            }
+            return NULL;
+        }
+        else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
+                 (pcmbuf_mix_chunk || pcmbuf_read->link))
+        {
+            *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
+            return voicebuf;
         }
         else
         {
             return NULL;
         }
     }
+    else
+    {
+        return pcmbuf_request_buffer(count);
+    }
 }
 
-void pcmbuf_write_complete(int count)
+void pcmbuf_write_voice_complete(int count)
 {
-    size_t length = (size_t)(unsigned int)count << 2;
-#ifdef HAVE_CROSSFADE
-    if (crossfade_active)
+    /* A get-it-to-work-for-now hack (audio status could have changed) */
+    if (!(audio_status() & AUDIO_STATUS_PLAY))
     {
-        flush_crossfade(fadebuf, length);
-        if (!(crossfade_fade_in_rem || crossfade_chunk))
-            crossfade_active = false;
+        pcmbuf_write_complete(count);
+        return;
     }
-    else
-#endif    
-    {
-        audiobuffer_fillpos += length;
 
-        if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
-            pcmbuf_flush_fillpos();
+    int16_t *ibuf = (int16_t *)voicebuf;
+    int16_t *obuf;
+    size_t chunk_samples;
+
+    if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
+    {
+        pcmbuf_mix_chunk = pcmbuf_read->link;
+        /* Start 1/8s into the next chunk */
+        pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
     }
+
+    if (!pcmbuf_mix_chunk)
+        return;
+
+    obuf = (int16_t *)pcmbuf_mix_chunk->addr;
+    chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
+
+    count <<= 1;
+
+    while (count-- > 0)
+    {
+        int32_t sample = *ibuf++;
+
+        if (pcmbuf_mix_sample >= chunk_samples)
+        {
+            pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
+            if (!pcmbuf_mix_chunk)
+                return;
+            pcmbuf_mix_sample = 0;
+            obuf = pcmbuf_mix_chunk->addr;
+            chunk_samples = pcmbuf_mix_chunk->size / 2;
+        }
+        sample += obuf[pcmbuf_mix_sample] >> 2;
+        obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
+    }
+}
+
+
+/* Debug menu, other metrics */
+
+/* Amount of bytes left in the buffer. */
+size_t pcmbuf_free(void)
+{
+    if (pcmbuf_read != NULL)
+    {
+        void *read = pcmbuf_read->addr;
+        void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
+        if (read < write)
+            return (size_t)(read - write) + pcmbuf_size;
+        else
+            return (size_t) (read - write);
+    }
+    return pcmbuf_size;
+}
+
+size_t pcmbuf_get_bufsize(void)
+{
+    return pcmbuf_size;
+}
+
+int pcmbuf_used_descs(void)
+{
+    struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
+    unsigned int i = 0;
+    while (pcmbuf_temp) {
+        pcmbuf_temp = pcmbuf_temp->link;
+        i++;
+    }
+    return i;
+}
+
+int pcmbuf_descs(void)
+{
+    return PCMBUF_DESCS(pcmbuf_size);
+}
+
+#ifdef ROCKBOX_HAS_LOGF
+unsigned char * pcmbuf_get_meminfo(size_t *length)
+{
+    *length = pcmbuf_bufend - audiobuffer;
+    return audiobuffer;
+}
+#endif
+
+
+/* Misc */
+
+bool pcmbuf_is_lowdata(void)
+{
+    if (!pcm_is_playing() || pcm_is_paused() ||
+            crossfade_init || crossfade_active)
+        return false;
+
+#if MEMORYSIZE > 2
+    /* 1 seconds of buffer is low data */
+    return LOW_DATA(4);
+#else
+    /* under watermark is low data */
+    return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
+#endif
+}
+
+void pcmbuf_set_low_latency(bool state)
+{
+    low_latency_mode = state;
+}
+
+unsigned long pcmbuf_get_latency(void)
+{
+    /* Be careful how this calculation is rearranged, it's easy to overflow */
+    size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
+    return bytes / 4 * 1000 / NATIVE_FREQUENCY;
 }
 
 #ifndef HAVE_HARDWARE_BEEP
@@ -1117,127 +1262,3 @@
 #endif
 }
 #endif /* HAVE_HARDWARE_BEEP */
-
-/* Returns pcm buffer usage in percents (0 to 100). */
-static int pcmbuf_usage(void)
-{
-    return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
-}
-
-static int pcmbuf_mix_free(void)
-{
-    if (pcmbuf_mix_chunk)
-    {
-        size_t my_mix_end =
-            (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
-        size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
-        if (my_write_pos < my_mix_end)
-            my_write_pos += pcmbuf_size;
-        return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
-    }
-    return 100;
-}
-
-void *pcmbuf_request_voice_buffer(int *count)
-{
-    /* A get-it-to-work-for-now hack (audio status could change by
-       completion) */
-    if (audio_status() & AUDIO_STATUS_PLAY)
-    {
-        if (pcmbuf_read == NULL)
-        {
-            return NULL;
-        }
-        else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
-                 (pcmbuf_mix_chunk || pcmbuf_read->link))
-        {
-            *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
-            return voicebuf;
-        }
-        else
-        {
-            return NULL;
-        }
-    }
-    else
-    {
-        return pcmbuf_request_buffer(count);
-    }
-}
-
-void pcmbuf_write_voice_complete(int count)
-{
-    /* A get-it-to-work-for-now hack (audio status could have changed) */
-    if (!(audio_status() & AUDIO_STATUS_PLAY))
-    {
-        pcmbuf_write_complete(count);
-        return;
-    }
-
-    int16_t *ibuf = (int16_t *)voicebuf;
-    int16_t *obuf;
-    size_t chunk_samples;
-
-    if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
-    {
-        pcmbuf_mix_chunk = pcmbuf_read->link;
-        /* Start 1/8s into the next chunk */
-        pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
-    }
-
-    if (!pcmbuf_mix_chunk)
-        return;
-
-    obuf = (int16_t *)pcmbuf_mix_chunk->addr;
-    chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
-
-    count <<= 1;
-
-    while (count-- > 0)
-    {
-        int32_t sample = *ibuf++;
-
-        if (pcmbuf_mix_sample >= chunk_samples)
-        {
-            pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
-            if (!pcmbuf_mix_chunk)
-                return;
-            pcmbuf_mix_sample = 0;
-            obuf = pcmbuf_mix_chunk->addr;
-            chunk_samples = pcmbuf_mix_chunk->size / 2;
-        }
-        sample += obuf[pcmbuf_mix_sample] >> 2;
-        obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
-    }
-}
-
-void pcmbuf_request_crossfade_enable(bool on_off)
-{
-    /* Next setting to be used, not applied now */
-    crossfade_enabled_pending = on_off;
-}
-
-static void pcmbuf_finish_crossfade_enable(void)
-{
-    /* Copy the pending setting over now */
-    crossfade_enabled = crossfade_enabled_pending;
-    
-    pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
-        /* If crossfading, try to keep the buffer full other than 1 second */
-        (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
-        /* Otherwise, just use the default */
-        PCMBUF_WATERMARK;
-}
-
-static bool pcmbuf_is_crossfade_enabled(void)
-{
-    if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
-        return global_settings.playlist_shuffle;
-
-    return crossfade_enabled;
-}
-
-bool pcmbuf_is_crossfade_active(void)
-{
-    return crossfade_active || crossfade_init;
-}
diff --git a/apps/pcmbuf.h b/apps/pcmbuf.h
index f8e6905..dff5a63 100644
--- a/apps/pcmbuf.h
+++ b/apps/pcmbuf.h
@@ -21,25 +21,29 @@
 #ifndef PCMBUF_H
 #define PCMBUF_H
 
-/* playback */
+/* Commit PCM data */
+void *pcmbuf_request_buffer(int *count);
+void pcmbuf_write_complete(int count);
+
+/* Init */
 size_t pcmbuf_init(unsigned char *bufend);
+
+/* Playback */
 void pcmbuf_play_start(void);
 void pcmbuf_play_stop(void);
 void pcmbuf_pause(bool pause);
 void pcmbuf_start_track_change(bool manual_skip);
-void *pcmbuf_request_buffer(int *count);
-void pcmbuf_write_complete(int count);
 
-/* crossfade */
+/* Crossfade */
 bool pcmbuf_is_crossfade_active(void);
 void pcmbuf_request_crossfade_enable(bool on_off);
 bool pcmbuf_is_same_size(void);
 
-/* voice */
+/* Voice */
 void *pcmbuf_request_voice_buffer(int *count);
 void pcmbuf_write_voice_complete(int count);
 
-/* debug menu, other metrics */
+/* Debug menu, other metrics */
 size_t pcmbuf_free(void);
 size_t pcmbuf_get_bufsize(void);
 int pcmbuf_descs(void);
@@ -49,9 +53,9 @@
 #endif
 
 /* misc */
-void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude);
 bool pcmbuf_is_lowdata(void);
 void pcmbuf_set_low_latency(bool state);
 unsigned long pcmbuf_get_latency(void);
+void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude);
 
 #endif