summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <franklin@rockbox.org>2020-08-08 00:43:05 -0400
committerSolomon Peachy <pizza@shaftnet.org>2023-05-17 13:23:14 -0400
commit4554b908590e0f033ab76383516f921f92758435 (patch)
tree3e61dd71b5f34875f9d6968b0706867b068fb2b5
parente232f69214cfe0ad83e8a6051c58ef54754e59bb (diff)
downloadrockbox-4554b908590e0f033ab76383516f921f92758435.tar.gz
rockbox-4554b908590e0f033ab76383516f921f92758435.zip
sdl: fix two bugs in ROCKBOXAUD_PlayAudio()
First was an off-by-one bug in the loop over buffers. The condition was `i < this->hidden->n_buffers', but should have been <=, not <. This wasn't a critical issue, because the last iteration would check the currently playing buffer - an unlikely situation. The second logic bug would make the code always think it had found an empty buffer, even when it had not, since `idx' was being unconditionally set every loop, rendering it unusable for a sentinel value. Fix that. Change-Id: I137330965d699acf827e59b6aa5c5f9ddf2ca617
-rw-r--r--apps/plugins/sdl/src/audio/rockbox/SDL_rockboxaudio.c38
1 files changed, 21 insertions, 17 deletions
diff --git a/apps/plugins/sdl/src/audio/rockbox/SDL_rockboxaudio.c b/apps/plugins/sdl/src/audio/rockbox/SDL_rockboxaudio.c
index cb72687d48..ac268b4a19 100644
--- a/apps/plugins/sdl/src/audio/rockbox/SDL_rockboxaudio.c
+++ b/apps/plugins/sdl/src/audio/rockbox/SDL_rockboxaudio.c
@@ -132,38 +132,42 @@ static void ROCKBOXAUD_WaitAudio(_THIS)
132} 132}
133 133
134/* when this is called, SDL wants us to play the samples in mixbuf */ 134/* when this is called, SDL wants us to play the samples in mixbuf */
135static void ROCKBOXAUD_PlayAudio(_THIS) 135static void
136ROCKBOXAUD_PlayAudio(_THIS)
136{ 137{
137 /* There are two cases in which we should be called: 138 /* There are two cases in which we should be called:
138 * - There is an empty buffer (marked with status = 0) 139 * - There is an empty buffer (marked with status = 0)
139 * - There are more than two buffers marked as playing, meaning at least one is stale. 140 * - There are more than two buffers marked as playing, meaning at least one is stale.
140 */ 141 */
141 int idx = -1;
142 142
143 /* Find the next empty or stale buffer and fill. */ 143 /* Find the next empty or stale buffer and fill. */
144 for(int i = 1; i < this->hidden->n_buffers; ++i) 144 for(int i = 1; i <= this->hidden->n_buffers; ++i)
145 { 145 {
146 idx = (this->hidden->current_playing + i) % this->hidden->n_buffers; 146 int idx = (this->hidden->current_playing + i) % this->hidden->n_buffers;
147 147
148 /* Empty or stale. */ 148 /* Empty or stale. */
149 if(this->hidden->status[idx] == 0 || 149 if(this->hidden->status[idx] == 0 ||
150 this->hidden->status[idx] == 2) 150 this->hidden->status[idx] == 2) {
151 break; 151
152 } 152 LOGF("found empty buffer: %d (status: %d)", idx, this->hidden->status[idx]);
153 if(idx < 0) 153
154 return; 154 /* probably premature optimization here */
155 char *dst = (char*)this->hidden->rb_buf[idx], *src = this->hidden->mixbuf;
156 int size = this->spec.size / 2;
157 memcpy(dst, src, size);
155 158
156 /* probably premature optimization here */ 159 this->hidden->status[idx] = 1;
157 char *dst = (char*)this->hidden->rb_buf[idx], *src = this->hidden->mixbuf; 160 rb->yield();
158 int size = this->hidden->mixlen / 2;
159 memcpy(dst, src, size);
160 161
161 this->hidden->status[idx] = 1; 162 memcpy(dst + size, src + size, this->spec.size - size);
162 rb->yield();
163 163
164 memcpy(dst + size, src + size, this->hidden->mixlen - size); 164 LOGF("filled buffer %d (status %d %d %d %d)", idx, this->hidden->status[0], this->hidden->status[1], this->hidden->status[2], this->hidden->status[3]);
165
166 return;
167 }
168 }
165 169
166 //LOGF("filled buffer %d (status %d %d %d)", idx, this->hidden->status[0], this->hidden->status[1], this->hidden->status[2]); 170 LOGF("WARNING: PlayDevice could not find buffer to fill; DROPPING SAMPLES!");
167} 171}
168 172
169static SDL_AudioDevice *ROCKBOXAUD_CreateDevice(int devindex) 173static SDL_AudioDevice *ROCKBOXAUD_CreateDevice(int devindex)