1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
diff --git a/apps/pcmbuf.c b/apps/pcmbuf.c
index ebc31fb..c59bfba 100644
--- a/apps/pcmbuf.c
+++ b/apps/pcmbuf.c
@@ -273,28 +273,35 @@ static void commit_chunk(bool flush_next_time)

 /* Set priority of the codec thread */
 #ifdef HAVE_PRIORITY_SCHEDULING
-static void boost_codec_thread(bool boost)
+//~ static void prio_tick(void)
+//~ {
+    //~ debugf("codec thread prio: %d", codec_thread_priority);
+//~ }
+static void boost_codec_thread(void)
 {
+    static const int prios[11] = {
+        PRIORITY_PLAYBACK_MAX,
+        PRIORITY_PLAYBACK_MAX+1,
+        PRIORITY_PLAYBACK_MAX+3,
+        PRIORITY_PLAYBACK_MAX+5,
+        PRIORITY_PLAYBACK_MAX+7,
+        PRIORITY_PLAYBACK_MAX+8,
+        PRIORITY_PLAYBACK_MAX+9,
+        /* raised priority above 70% shouldn't be needed */
+        PRIORITY_PLAYBACK,
+        PRIORITY_PLAYBACK,
+        PRIORITY_PLAYBACK,
+        PRIORITY_PLAYBACK,
+    };
+    int fill_state = pcm_is_playing() ?
+            (pcmbuf_unplayed_bytes * 10 / pcmbuf_size) : 10; /* in tenth-% */
+    int new_prio = prios[fill_state];
+
     /* Keep voice and codec threads at the same priority or else voice
      * will starve if the codec thread's priority is boosted. */
-    if (boost)
-    {
-        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);
-        }
-    }
-    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;
-    }
+    thread_set_priority(codec_thread_id, new_prio);
+    voice_thread_set_priority(new_prio);
+    codec_thread_priority = new_prio;
 }
 #else
 #define boost_codec_thread(boost) do{}while(0)
@@ -324,18 +331,10 @@ static bool prepare_insert(size_t length)
         if (thread_get_current() == codec_thread_id)
 #endif /* SIMULATOR */
         {
-            if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
-            {
-                /* Fill PCM buffer by boosting cpu */
+            /* boost cpu if necessary */
+            if (pcmbuf_unplayed_bytes < pcmbuf_watermark)
                 trigger_cpu_boost();
-                /* If buffer is critically low, override UI priority, else
-                   set back to the original priority. */
-                boost_codec_thread(LOW_DATA(2));
-            }
-            else
-            {
-                boost_codec_thread(false);
-            }
+            boost_codec_thread();
         }

 #ifdef HAVE_CROSSFADE
@@ -484,6 +483,7 @@ size_t pcmbuf_init(unsigned char *bufend)

     pcmbuf_play_stop();

+    //~ tick_add_task(prio_tick);
     return pcmbuf_bufend - pcmbuffer;
 }

@@ -682,7 +682,7 @@ void pcmbuf_play_stop(void)
     DISPLAY_DESC("play_stop");

     /* Can unboost the codec thread here no matter who's calling */
-    boost_codec_thread(false);
+    boost_codec_thread();
 }

 void pcmbuf_pause(bool pause)