blob: f2f94e3bc9e92acb478ef6832010e3e6c08eb287 [file] [log] [blame]
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Miika Pekkarinen
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Miika Pekkarinen20b38972005-07-13 12:48:22 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Miika Pekkarinen20b38972005-07-13 12:48:22 +000021#include <stdio.h>
22#include "config.h"
Michael Sevakis6c399b82009-02-19 20:40:03 +000023#include "system.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000024#include "debug.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000025#include <kernel.h>
Michael Sevakis6077e5b2007-10-06 22:27:27 +000026#include "pcm.h"
Michael Sevakisa2b67032011-06-29 06:37:04 +000027#include "pcm_mixer.h"
28#include "pcmbuf.h"
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +000029#include "playback.h"
Michael Sevakis65109732011-02-23 14:31:13 +000030#include "codec_thread.h"
Jeffrey Goodefaa47bf2009-10-22 00:59:42 +000031
32/* Define LOGF_ENABLE to enable logf output in this file */
33/*#define LOGF_ENABLE*/
Miika Pekkarinen20b38972005-07-13 12:48:22 +000034#include "logf.h"
Thomas Martitz35e8b142010-06-21 16:53:00 +000035#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
Miika Pekkarinen20b38972005-07-13 12:48:22 +000036#include "cpu.h"
37#endif
Miika Pekkarinen20b38972005-07-13 12:48:22 +000038#include <string.h>
Miika Pekkarinenf090dc32005-07-21 11:44:00 +000039#include "settings.h"
40#include "audio.h"
Jeffrey Gooded0ac0452009-11-09 05:58:02 +000041#include "voice_thread.h"
Miika Pekkarinen159c52d2005-08-20 11:13:19 +000042#include "dsp.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000043
Jeffrey Goode04b01e12009-11-05 21:59:36 +000044#define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
45 on the pcm buffer */
46#define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
47 chunks on the pcm buffer (or we run out
48 of buffer descriptors, which is
49 non-fatal) */
50#define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
51 this to the DMA */
Jeffrey Goodec8944c02010-05-28 13:21:24 +000052#define CROSSFADE_BUFSIZE 8192 /* Size of the crossfade buffer */
Jeffrey Goode04b01e12009-11-05 21:59:36 +000053
Jeffrey Goodeba9280d2009-11-16 04:42:34 +000054/* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
55#define BYTERATE (NATIVE_FREQUENCY * 4)
56
Björn Stenberg6427d122009-01-10 21:10:56 +000057#if MEMORYSIZE > 2
Miika Pekkarinena85044b2006-09-16 16:18:11 +000058/* Keep watermark high for iPods at least (2s) */
Jeffrey Goodeba9280d2009-11-16 04:42:34 +000059#define PCMBUF_WATERMARK (BYTERATE * 2)
Björn Stenberg6427d122009-01-10 21:10:56 +000060#else
Jeffrey Goodeba9280d2009-11-16 04:42:34 +000061#define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
Björn Stenberg6427d122009-01-10 21:10:56 +000062#endif
Miika Pekkarinen20b38972005-07-13 12:48:22 +000063
Miika Pekkarinen20b38972005-07-13 12:48:22 +000064/* Structure we can use to queue pcm chunks in memory to be played
65 * by the driver code. */
Jeffrey Goode8edac6e2009-11-09 05:45:05 +000066struct chunkdesc
Miika Pekkarinen20b38972005-07-13 12:48:22 +000067{
Jeffrey Goodec8944c02010-05-28 13:21:24 +000068 unsigned char *addr;
Brandon Low413da2a2006-02-07 20:38:55 +000069 size_t size;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +000070 struct chunkdesc* link;
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +000071 /* true if last chunk in the track */
72 bool end_of_track;
Brandon Low413da2a2006-02-07 20:38:55 +000073};
Miika Pekkarinen20b38972005-07-13 12:48:22 +000074
Jeffrey Goode874c9112009-11-09 18:12:20 +000075#define NUM_CHUNK_DESCS(bufsize) \
Michael Sevakise1dd10d2007-03-19 22:04:17 +000076 ((bufsize) / PCMBUF_MINAVG_CHUNK)
Michael Sevakis0f5cb942006-11-06 18:07:30 +000077
Brandon Low6c0908b2006-04-23 22:54:34 +000078/* Size of the PCM buffer. */
79static size_t pcmbuf_size IDATA_ATTR = 0;
Michael Sevakis0f5cb942006-11-06 18:07:30 +000080static char *pcmbuf_bufend IDATA_ATTR;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +000081static char *pcmbuffer IDATA_ATTR;
82/* Current PCM buffer write index. */
83static size_t pcmbuffer_pos IDATA_ATTR;
84/* Amount pcmbuffer_pos will be increased.*/
85static size_t pcmbuffer_fillpos IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +000086
Michael Sevakisc537d592011-04-27 03:08:23 +000087static struct chunkdesc *first_desc;
88
Jeffrey Goode9e095342009-11-10 03:46:08 +000089/* Gapless playback */
Michael Sevakis65109732011-02-23 14:31:13 +000090static bool track_transition IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +000091
Michael Sevakisa2b67032011-06-29 06:37:04 +000092/* Fade effect */
93static unsigned int fade_vol = MIX_AMP_UNITY;
94
95/* Voice */
96static bool soft_mode = false;
97
Jeffrey Goode9e095342009-11-10 03:46:08 +000098#ifdef HAVE_CROSSFADE
99/* Crossfade buffer */
100static char *fadebuf IDATA_ATTR;
101
Brandon Low6c0908b2006-04-23 22:54:34 +0000102/* Crossfade related state */
103static bool crossfade_enabled;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000104static bool crossfade_enable_request;
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +0000105static bool crossfade_mixmode;
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000106static bool crossfade_auto_skip;
Brandon Low6c0908b2006-04-23 22:54:34 +0000107static bool crossfade_active IDATA_ATTR;
Jeffrey Goode5c69a422009-11-09 17:11:53 +0000108static bool crossfade_track_change_started IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +0000109
110/* Track the current location for processing crossfade */
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000111static struct chunkdesc *crossfade_chunk IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +0000112static size_t crossfade_sample IDATA_ATTR;
113
114/* Counters for fading in new data */
115static size_t crossfade_fade_in_total IDATA_ATTR;
116static size_t crossfade_fade_in_rem IDATA_ATTR;
Michael Giacomelli64699262009-08-11 02:05:38 +0000117#endif
Brandon Low6c0908b2006-04-23 22:54:34 +0000118
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000119static struct chunkdesc *read_chunk IDATA_ATTR;
120static struct chunkdesc *read_end_chunk IDATA_ATTR;
121static struct chunkdesc *write_chunk IDATA_ATTR;
122static struct chunkdesc *write_end_chunk IDATA_ATTR;
Brandon Low413da2a2006-02-07 20:38:55 +0000123static size_t last_chunksize IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000124
Brandon Low413da2a2006-02-07 20:38:55 +0000125static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
Brandon Low413da2a2006-02-07 20:38:55 +0000126static size_t pcmbuf_watermark IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000127
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000128static bool low_latency_mode = false;
Jeffrey Goode0db33082009-11-11 07:02:18 +0000129static bool flush_pcmbuf = false;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000130
Michael Sevakis938593b2007-03-06 21:00:45 +0000131#ifdef HAVE_PRIORITY_SCHEDULING
Michael Sevakis27cf6772008-03-25 02:34:12 +0000132static int codec_thread_priority = PRIORITY_PLAYBACK;
Michael Sevakis938593b2007-03-06 21:00:45 +0000133#endif
Michael Sevakisb425de72007-03-06 20:32:13 +0000134
Brandon Low413da2a2006-02-07 20:38:55 +0000135/* Helpful macros for use in conditionals this assumes some of the above
136 * static variable names */
Jeffrey Goode0db33082009-11-11 07:02:18 +0000137#define COMMIT_IF_NEEDED if(pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || \
138 (pcmbuffer_pos + pcmbuffer_fillpos) >= pcmbuf_size) commit_chunk(false)
Brandon Low413da2a2006-02-07 20:38:55 +0000139#define LOW_DATA(quarter_secs) \
140 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
141
Jeffrey Goodee9f389a2009-11-06 04:25:28 +0000142#ifdef HAVE_CROSSFADE
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000143static void crossfade_start(void);
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000144static void write_to_crossfade(size_t length);
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000145static void pcmbuf_finish_crossfade_enable(void);
Jeffrey Goode9e095342009-11-10 03:46:08 +0000146#endif
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000147
Michael Sevakisc537d592011-04-27 03:08:23 +0000148/* Callbacks into playback.c */
149extern void audio_pcmbuf_position_callback(unsigned int time);
150extern void audio_pcmbuf_track_change(bool pcmbuf);
151extern bool audio_pcmbuf_may_play(void);
152
Jeffrey Goodeb6f15f22009-11-08 04:27:27 +0000153
Jeffrey Goode013fe352009-11-05 17:32:32 +0000154/**************************************/
155
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000156/* define this to show detailed chunkdesc usage information on the sim console */
Jeffrey Goode013fe352009-11-05 17:32:32 +0000157/*#define DESC_DEBUG*/
158
159#ifndef SIMULATOR
160#undef DESC_DEBUG
161#endif
Michael Sevakisc537d592011-04-27 03:08:23 +0000162
Jeffrey Goode013fe352009-11-05 17:32:32 +0000163#ifdef DESC_DEBUG
Jeffrey Goode013fe352009-11-05 17:32:32 +0000164#define DISPLAY_DESC(caller) while(!show_desc(caller))
Jeffrey Goode84ca2362009-11-18 20:56:19 +0000165#define DESC_IDX(desc) (desc ? desc - first_desc : -1)
166#define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \
167 else DEBUGF("%02d", DESC_IDX(desc))
168#define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \
169 else DEBUGF("-- ")
170#define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
171 SHOW_DESC_LINK(desc)
172#define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc)
173#define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc)
Jeffrey Goode013fe352009-11-05 17:32:32 +0000174
175static bool show_desc(char *caller)
176{
177 if (show_desc_in_use) return false;
178 show_desc_in_use = true;
179 DEBUGF("%-14s\t", caller);
Jeffrey Goode84ca2362009-11-18 20:56:19 +0000180 SHOW_POINT("r", read_chunk);
181 SHOW_POINT("re", read_end_chunk);
Jeffrey Goode013fe352009-11-05 17:32:32 +0000182 DEBUGF(" ");
Jeffrey Goode84ca2362009-11-18 20:56:19 +0000183 SHOW_POINT("w", write_chunk);
184 SHOW_POINT("we", write_end_chunk);
Jeffrey Goode013fe352009-11-05 17:32:32 +0000185 DEBUGF("\n");
Jeffrey Goode84ca2362009-11-18 20:56:19 +0000186 int i;
187 for (i = 0; i < pcmbuf_descs(); i++)
188 {
189 SHOW_NUM(i, (first_desc + i));
190 if (i%10 == 9) DEBUGF("\n");
191 }
192 DEBUGF("\n\n");
Jeffrey Goode013fe352009-11-05 17:32:32 +0000193 show_desc_in_use = false;
194 return true;
195}
196#else
197#define DISPLAY_DESC(caller) do{}while(0)
198#endif
199
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000200
Jeffrey Goode0db33082009-11-11 07:02:18 +0000201/** Accept new PCM data */
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000202
Jeffrey Goode0db33082009-11-11 07:02:18 +0000203/* Commit PCM buffer samples as a new chunk for playback */
204static void commit_chunk(bool flush_next_time)
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000205{
Jeffrey Goode73c47912009-11-09 06:53:22 +0000206 if (!pcmbuffer_fillpos)
207 return;
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000208
Jeffrey Goode73c47912009-11-09 06:53:22 +0000209 /* Never use the last buffer descriptor */
210 while (write_chunk == write_end_chunk) {
211 /* If this happens, something is being stupid */
212 if (!pcm_is_playing()) {
213 logf("commit_chunk error");
214 pcmbuf_play_start();
215 }
216 /* Let approximately one chunk of data playback */
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000217 sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE);
Jeffrey Goode73c47912009-11-09 06:53:22 +0000218 }
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000219
Jeffrey Goode73c47912009-11-09 06:53:22 +0000220 /* commit the chunk */
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000221
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000222 register size_t size = pcmbuffer_fillpos;
223 /* Grab the next description to write, and change the write pointer */
224 register struct chunkdesc *pcmbuf_current = write_chunk;
225 write_chunk = pcmbuf_current->link;
226 /* Fill in the values in the new buffer chunk */
227 pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos];
228 pcmbuf_current->size = size;
Michael Sevakis65109732011-02-23 14:31:13 +0000229 pcmbuf_current->end_of_track = false;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000230 pcmbuf_current->link = NULL;
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000231
Jeffrey Goode73c47912009-11-09 06:53:22 +0000232 if (read_chunk != NULL)
233 {
Jeffrey Gooded0ac0452009-11-09 05:58:02 +0000234 if (flush_pcmbuf)
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000235 {
Jeffrey Goode0db33082009-11-11 07:02:18 +0000236 /* Flush! Discard all data after the currently playing chunk,
Jeffrey Goode73c47912009-11-09 06:53:22 +0000237 and make the current chunk play next */
Jeffrey Goode84ca2362009-11-18 20:56:19 +0000238 logf("commit_chunk: flush");
Michael Sevakisc537d592011-04-27 03:08:23 +0000239 pcm_play_lock();
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000240 write_end_chunk->link = read_chunk->link;
241 read_chunk->link = pcmbuf_current;
242 while (write_end_chunk->link)
243 {
244 write_end_chunk = write_end_chunk->link;
245 pcmbuf_unplayed_bytes -= write_end_chunk->size;
246 }
Michael Sevakisc537d592011-04-27 03:08:23 +0000247
248 read_chunk->end_of_track = track_transition;
249 pcm_play_unlock();
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000250 }
251 /* If there is already a read buffer setup, add to it */
252 else
253 read_end_chunk->link = pcmbuf_current;
Jeffrey Goode73c47912009-11-09 06:53:22 +0000254 }
255 else
256 {
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000257 /* Otherwise create the buffer */
258 read_chunk = pcmbuf_current;
259 }
Michael Sevakisc537d592011-04-27 03:08:23 +0000260
Jeffrey Goode0db33082009-11-11 07:02:18 +0000261 /* If flush_next_time is true, then the current chunk will be thrown out
262 * and the next chunk to be committed will be the next to be played.
263 * This is used to empty the PCM buffer for a track change. */
264 flush_pcmbuf = flush_next_time;
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000265
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000266 /* This is now the last buffer to read */
267 read_end_chunk = pcmbuf_current;
268
269 /* Update bytes counters */
270 pcmbuf_unplayed_bytes += size;
271
272 pcmbuffer_pos += size;
273 if (pcmbuffer_pos >= pcmbuf_size)
274 pcmbuffer_pos -= pcmbuf_size;
275
276 pcmbuffer_fillpos = 0;
Jeffrey Goode73c47912009-11-09 06:53:22 +0000277 DISPLAY_DESC("commit_chunk");
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000278}
279
Jeffrey Goode0db33082009-11-11 07:02:18 +0000280/* Set priority of the codec thread */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000281#ifdef HAVE_PRIORITY_SCHEDULING
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000282/*
283 * expects pcm_fill_state in tenth-% units (e.g. full pcm buffer is 10) */
284static void boost_codec_thread(int pcm_fill_state)
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000285{
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000286 static const int prios[11] = {
287 PRIORITY_PLAYBACK_MAX, /* 0 - 10% */
288 PRIORITY_PLAYBACK_MAX+1, /* 10 - 20% */
289 PRIORITY_PLAYBACK_MAX+3, /* 20 - 30% */
290 PRIORITY_PLAYBACK_MAX+5, /* 30 - 40% */
291 PRIORITY_PLAYBACK_MAX+7, /* 40 - 50% */
292 PRIORITY_PLAYBACK_MAX+8, /* 50 - 60% */
293 PRIORITY_PLAYBACK_MAX+9, /* 60 - 70% */
294 /* raiseing priority above 70% shouldn't be needed */
295 PRIORITY_PLAYBACK, /* 70 - 80% */
296 PRIORITY_PLAYBACK, /* 80 - 90% */
297 PRIORITY_PLAYBACK, /* 90 -100% */
298 PRIORITY_PLAYBACK, /* 100% */
299 };
300 int new_prio = prios[pcm_fill_state];
301
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000302 /* Keep voice and codec threads at the same priority or else voice
303 * will starve if the codec thread's priority is boosted. */
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000304 if (new_prio != codec_thread_priority)
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000305 {
Michael Sevakis65109732011-02-23 14:31:13 +0000306 codec_thread_set_priority(new_prio);
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000307 voice_thread_set_priority(new_prio);
308 codec_thread_priority = new_prio;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000309 }
310}
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000311#else
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000312#define boost_codec_thread(pcm_fill_state) do{}while(0)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000313#endif /* HAVE_PRIORITY_SCHEDULING */
314
Jeffrey Goode0db33082009-11-11 07:02:18 +0000315/* Return true if the PCM buffer is able to receive new data.
316 * Also maintain buffer level above the watermark. */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000317static bool prepare_insert(size_t length)
318{
Michael Sevakisa2b67032011-06-29 06:37:04 +0000319 bool playing = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED;
320
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000321 if (low_latency_mode)
322 {
323 /* 1/4s latency. */
Michael Sevakisa2b67032011-06-29 06:37:04 +0000324 if (!LOW_DATA(1) && playing)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000325 return false;
326 }
327
328 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
329 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
330 return false;
331
Jeffrey Goode0db33082009-11-11 07:02:18 +0000332 /* Maintain the buffer level above the watermark */
Michael Sevakisa2b67032011-06-29 06:37:04 +0000333 if (playing)
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000334 {
335 /* Only codec thread initiates boost - voice boosts the cpu when playing
336 a clip */
337#ifndef SIMULATOR
Michael Sevakis65109732011-02-23 14:31:13 +0000338 if (is_codec_thread())
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000339#endif /* SIMULATOR */
340 {
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000341 /* boost cpu if necessary */
342 if (pcmbuf_unplayed_bytes < pcmbuf_watermark)
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000343 trigger_cpu_boost();
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000344 boost_codec_thread(pcmbuf_unplayed_bytes*10/pcmbuf_size);
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000345 }
346
Jeffrey Goode9e095342009-11-10 03:46:08 +0000347#ifdef HAVE_CROSSFADE
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000348 /* Disable crossfade if < .5s of audio */
349 if (LOW_DATA(2))
350 {
351 crossfade_active = false;
352 }
Jeffrey Goode9e095342009-11-10 03:46:08 +0000353#endif
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000354 }
Michael Sevakisa2b67032011-06-29 06:37:04 +0000355 else /* !playing */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000356 {
Jeffrey Goode0db33082009-11-11 07:02:18 +0000357 /* Boost CPU for pre-buffer */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000358 trigger_cpu_boost();
359
Jeffrey Goode0db33082009-11-11 07:02:18 +0000360 /* If pre-buffered to the watermark, start playback */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000361#if MEMORYSIZE > 2
362 if (!LOW_DATA(4))
363#else
364 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
365#endif
366 {
367 logf("pcm starting");
Michael Sevakisc537d592011-04-27 03:08:23 +0000368 if (audio_pcmbuf_may_play())
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000369 pcmbuf_play_start();
370 }
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000371 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000372
373 return true;
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000374}
375
Jeffrey Goode0db33082009-11-11 07:02:18 +0000376/* Request space in the buffer for writing output samples */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000377void *pcmbuf_request_buffer(int *count)
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000378{
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000379#ifdef HAVE_CROSSFADE
Jeffrey Goode0db33082009-11-11 07:02:18 +0000380 /* we're going to crossfade to a new track, which is now on its way */
Jeffrey Goode5c69a422009-11-09 17:11:53 +0000381 if (crossfade_track_change_started)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000382 crossfade_start();
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000383
Jeffrey Goode0db33082009-11-11 07:02:18 +0000384 /* crossfade has begun, put the new track samples in fadebuf */
385 if (crossfade_active)
386 {
Michael Sevakisc537d592011-04-27 03:08:23 +0000387 int cnt = MIN(*count, CROSSFADE_BUFSIZE/4);
388 if (prepare_insert(cnt << 2))
389 {
390 *count = cnt;
391 return fadebuf;
392 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000393 }
394 else
Jeffrey Goode9e095342009-11-10 03:46:08 +0000395#endif
Jeffrey Goode0db33082009-11-11 07:02:18 +0000396 /* if possible, reserve room in the PCM buffer for new samples */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000397 {
398 if(prepare_insert(*count << 2))
399 {
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000400 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
401 if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000402 {
403 /* Usual case, there's space here */
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000404 return &pcmbuffer[pcmbuffer_index];
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000405 }
406 else
407 {
Jeffrey Goode0db33082009-11-11 07:02:18 +0000408 /* Wrap the buffer, the new samples go at the beginning */
409 commit_chunk(false);
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000410 pcmbuffer_pos = 0;
411 return &pcmbuffer[0];
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000412 }
413 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000414 }
Jeffrey Goode0db33082009-11-11 07:02:18 +0000415 /* PCM buffer not ready to receive new data yet */
Jeffrey Gooded0ac0452009-11-09 05:58:02 +0000416 return NULL;
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000417}
418
Jeffrey Goode0db33082009-11-11 07:02:18 +0000419/* Handle new samples to the buffer */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000420void pcmbuf_write_complete(int count)
421{
422 size_t length = (size_t)(unsigned int)count << 2;
423#ifdef HAVE_CROSSFADE
424 if (crossfade_active)
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000425 write_to_crossfade(length);
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000426 else
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000427#endif
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000428 {
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000429 pcmbuffer_fillpos += length;
Jeffrey Goode0db33082009-11-11 07:02:18 +0000430 COMMIT_IF_NEEDED;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000431 }
432}
433
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000434
Jeffrey Goode0db33082009-11-11 07:02:18 +0000435/** Init */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000436
Jeffrey Goode5c69a422009-11-09 17:11:53 +0000437static inline void init_pcmbuffers(void)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000438{
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000439 first_desc = write_chunk;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000440 struct chunkdesc *next = write_chunk;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000441 next++;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000442 write_end_chunk = write_chunk;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000443 while ((void *)next < (void *)pcmbuf_bufend) {
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000444 write_end_chunk->link=next;
445 write_end_chunk=next;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000446 next++;
447 }
448 DISPLAY_DESC("init");
449}
450
Jeffrey Goode5c69a422009-11-09 17:11:53 +0000451static size_t get_next_required_pcmbuf_size(void)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000452{
453 size_t seconds = 1;
454
Jeffrey Goode9e095342009-11-10 03:46:08 +0000455#ifdef HAVE_CROSSFADE
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000456 if (crossfade_enable_request)
Jeffrey Goode9e095342009-11-10 03:46:08 +0000457 seconds += global_settings.crossfade_fade_out_delay +
458 global_settings.crossfade_fade_out_duration;
459#endif
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000460
461#if MEMORYSIZE > 2
462 /* Buffer has to be at least 2s long. */
463 seconds += 2;
464#endif
465 logf("pcmbuf len: %ld", (long)seconds);
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000466 return seconds * BYTERATE;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000467}
468
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000469/* Initialize the pcmbuffer the structure looks like this:
470 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
471size_t pcmbuf_init(unsigned char *bufend)
472{
Michael Sevakisa2b67032011-06-29 06:37:04 +0000473 unsigned char *voicebuf;
474
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000475 pcmbuf_bufend = bufend;
Jeffrey Goode5c69a422009-11-09 17:11:53 +0000476 pcmbuf_size = get_next_required_pcmbuf_size();
Jeffrey Goode874c9112009-11-09 18:12:20 +0000477 write_chunk = (struct chunkdesc *)pcmbuf_bufend -
478 NUM_CHUNK_DESCS(pcmbuf_size);
Michael Sevakisa2b67032011-06-29 06:37:04 +0000479 voicebuf = (unsigned char *)write_chunk -
480 voicebuf_init((unsigned char *)write_chunk);
Jeffrey Goode9e095342009-11-10 03:46:08 +0000481#ifdef HAVE_CROSSFADE
Jeffrey Goodec8944c02010-05-28 13:21:24 +0000482 fadebuf = voicebuf - CROSSFADE_BUFSIZE;
Jeffrey Goode874c9112009-11-09 18:12:20 +0000483 pcmbuffer = fadebuf - pcmbuf_size;
Jeffrey Goode9e095342009-11-10 03:46:08 +0000484#else
485 pcmbuffer = voicebuf - pcmbuf_size;
486#endif
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000487
Jeffrey Goode5c69a422009-11-09 17:11:53 +0000488 init_pcmbuffers();
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000489
Jeffrey Goode9e095342009-11-10 03:46:08 +0000490#ifdef HAVE_CROSSFADE
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000491 pcmbuf_finish_crossfade_enable();
Jeffrey Goode9e095342009-11-10 03:46:08 +0000492#else
493 pcmbuf_watermark = PCMBUF_WATERMARK;
494#endif
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000495
496 pcmbuf_play_stop();
497
Michael Sevakisa2b67032011-06-29 06:37:04 +0000498 pcmbuf_soft_mode(false);
499
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000500 return pcmbuf_bufend - pcmbuffer;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000501}
502
503
Jeffrey Goode0db33082009-11-11 07:02:18 +0000504/** Track change */
Michael Sevakis65109732011-02-23 14:31:13 +0000505void pcmbuf_monitor_track_change(bool monitor)
506{
507 pcm_play_lock();
508
509 if (last_chunksize != 0)
510 {
511 /* If monitoring, wait until this track runs out. Place in
512 currently playing chunk. If not, cancel notification. */
513 track_transition = monitor;
514 read_end_chunk->end_of_track = monitor;
Michael Sevakisc537d592011-04-27 03:08:23 +0000515 if (!monitor)
516 {
517 /* Clear all notifications */
518 struct chunkdesc *desc = first_desc;
519 struct chunkdesc *end = desc + pcmbuf_descs();
520 while (desc < end)
521 desc++->end_of_track = false;
522 }
Michael Sevakis65109732011-02-23 14:31:13 +0000523 }
524 else
525 {
526 /* Post now if PCM stopped and last buffer was sent. */
527 track_transition = false;
528 if (monitor)
Michael Sevakisc537d592011-04-27 03:08:23 +0000529 audio_pcmbuf_track_change(false);
Michael Sevakis65109732011-02-23 14:31:13 +0000530 }
531
532 pcm_play_unlock();
533}
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000534
Michael Sevakisc537d592011-04-27 03:08:23 +0000535bool pcmbuf_start_track_change(bool auto_skip)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000536{
Jeffrey Goode664dc902009-11-11 00:48:17 +0000537 bool crossfade = false;
Jeffrey Goode9e095342009-11-10 03:46:08 +0000538#ifdef HAVE_CROSSFADE
Jeffrey Goode664dc902009-11-11 00:48:17 +0000539 /* Determine whether this track change needs to crossfade */
Jeffrey Goode664dc902009-11-11 00:48:17 +0000540 if(crossfade_enabled && !pcmbuf_is_crossfade_active())
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000541 {
Jeffrey Goode664dc902009-11-11 00:48:17 +0000542 switch(global_settings.crossfade)
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000543 {
Jeffrey Goode664dc902009-11-11 00:48:17 +0000544 case CROSSFADE_ENABLE_AUTOSKIP:
545 crossfade = auto_skip;
546 break;
547 case CROSSFADE_ENABLE_MANSKIP:
548 crossfade = !auto_skip;
549 break;
550 case CROSSFADE_ENABLE_SHUFFLE:
551 crossfade = global_settings.playlist_shuffle;
552 break;
Jeffrey Goode29d27112009-11-12 15:42:37 +0000553 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
554 crossfade = global_settings.playlist_shuffle || !auto_skip;
Jeffrey Goode664dc902009-11-11 00:48:17 +0000555 break;
556 case CROSSFADE_ENABLE_ALWAYS:
557 crossfade = true;
558 break;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000559 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000560 }
Jeffrey Goode9e095342009-11-10 03:46:08 +0000561#endif
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000562
Jeffrey Goode664dc902009-11-11 00:48:17 +0000563 if (!auto_skip || crossfade)
564 /* manual skip or crossfade */
565 {
566 if (crossfade)
567 { logf(" crossfade track change"); }
568 else
569 { logf(" manual track change"); }
Michael Sevakis65109732011-02-23 14:31:13 +0000570
571 pcm_play_lock();
572
573 /* Cancel any pending automatic gapless transition */
574 pcmbuf_monitor_track_change(false);
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000575
Jeffrey Goode664dc902009-11-11 00:48:17 +0000576 /* Can't do two crossfades at once and, no fade if pcm is off now */
577 if (
578#ifdef HAVE_CROSSFADE
579 pcmbuf_is_crossfade_active() ||
580#endif
Michael Sevakisa2b67032011-06-29 06:37:04 +0000581 mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED)
Jeffrey Goode664dc902009-11-11 00:48:17 +0000582 {
583 pcmbuf_play_stop();
Michael Sevakis65109732011-02-23 14:31:13 +0000584 pcm_play_unlock();
Michael Sevakisc537d592011-04-27 03:08:23 +0000585 /* Notify playback that the track change starts now */
586 return true;
Jeffrey Goode664dc902009-11-11 00:48:17 +0000587 }
588
Jeffrey Goode664dc902009-11-11 00:48:17 +0000589 /* Not enough data, or not crossfading, flush the old data instead */
590 if (LOW_DATA(2) || !crossfade || low_latency_mode)
591 {
Jeffrey Goode0db33082009-11-11 07:02:18 +0000592 commit_chunk(true);
Jeffrey Goode664dc902009-11-11 00:48:17 +0000593 }
Jeffrey Goode664dc902009-11-11 00:48:17 +0000594#ifdef HAVE_CROSSFADE
Michael Sevakis65109732011-02-23 14:31:13 +0000595 else
596 {
597 /* Don't enable mix mode when skipping tracks manually. */
598 crossfade_mixmode = auto_skip &&
599 global_settings.crossfade_fade_out_mixmode;
Jeffrey Goode664dc902009-11-11 00:48:17 +0000600
Michael Sevakis65109732011-02-23 14:31:13 +0000601 crossfade_auto_skip = auto_skip;
602
603 crossfade_track_change_started = crossfade;
604 }
Jeffrey Goode664dc902009-11-11 00:48:17 +0000605#endif
Michael Sevakis65109732011-02-23 14:31:13 +0000606 pcm_play_unlock();
607
608 /* Keep trigger outside the play lock or HW FIFO underruns can happen
609 since frequency scaling is *not* always fast */
610 trigger_cpu_boost();
Michael Sevakisc537d592011-04-27 03:08:23 +0000611
612 /* Notify playback that the track change starts now */
613 return true;
Jeffrey Goode664dc902009-11-11 00:48:17 +0000614 }
Jeffrey Goode0db33082009-11-11 07:02:18 +0000615 else /* automatic and not crossfading, so do gapless track change */
Jeffrey Goode664dc902009-11-11 00:48:17 +0000616 {
617 /* The codec is moving on to the next track, but the current track will
618 * continue to play. Set a flag to make sure the elapsed time of the
619 * current track will be updated properly, and mark the current chunk
620 * as the last one in the track. */
621 logf(" gapless track change");
Michael Sevakis65109732011-02-23 14:31:13 +0000622 pcmbuf_monitor_track_change(true);
Michael Sevakisc537d592011-04-27 03:08:23 +0000623 return false;
Jeffrey Goode664dc902009-11-11 00:48:17 +0000624 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000625}
626
627
Jeffrey Goode0db33082009-11-11 07:02:18 +0000628/** Playback */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000629
Jeffrey Goode0db33082009-11-11 07:02:18 +0000630/* PCM driver callback
Jeffrey Goode874c9112009-11-09 18:12:20 +0000631 * This function has 3 major logical parts (separated by brackets both for
632 * readability and variable scoping). The first part performs the
Jeffrey Goode664dc902009-11-11 00:48:17 +0000633 * operations related to finishing off the last chunk we fed to the DMA.
634 * The second part detects the end of playlist condition when the PCM
635 * buffer is empty except for uncommitted samples. Then they are committed
636 * and sent to the PCM driver for playback. The third part performs the
637 * operations involved in sending a new chunk to the DMA. */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000638static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR;
639static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
640{
641 {
642 struct chunkdesc *pcmbuf_current = read_chunk;
Jeffrey Goode664dc902009-11-11 00:48:17 +0000643 /* Take the finished chunk out of circulation */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000644 read_chunk = pcmbuf_current->link;
645
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000646 /* if during a track transition, update the elapsed time in ms */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000647 if (track_transition)
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000648 audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000649
Jeffrey Goode664dc902009-11-11 00:48:17 +0000650 /* if last chunk in the track, stop updates and notify audio thread */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000651 if (pcmbuf_current->end_of_track)
Jeffrey Goode664dc902009-11-11 00:48:17 +0000652 {
653 track_transition = false;
Michael Sevakisc537d592011-04-27 03:08:23 +0000654 audio_pcmbuf_track_change(true);
Jeffrey Goode664dc902009-11-11 00:48:17 +0000655 }
Jeffrey Goode874c9112009-11-09 18:12:20 +0000656
Jeffrey Goode664dc902009-11-11 00:48:17 +0000657 /* Put the finished chunk back into circulation */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000658 write_end_chunk->link = pcmbuf_current;
659 write_end_chunk = pcmbuf_current;
660
Jeffrey Goode9e095342009-11-10 03:46:08 +0000661#ifdef HAVE_CROSSFADE
Jeffrey Goode874c9112009-11-09 18:12:20 +0000662 /* If we've read over the crossfade chunk while it's still fading */
663 if (pcmbuf_current == crossfade_chunk)
664 crossfade_chunk = read_chunk;
Jeffrey Goode9e095342009-11-10 03:46:08 +0000665#endif
Jeffrey Goode874c9112009-11-09 18:12:20 +0000666 }
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000667
Jeffrey Goode874c9112009-11-09 18:12:20 +0000668 {
669 /* Commit last samples at end of playlist */
670 if (pcmbuffer_fillpos && !read_chunk)
671 {
672 logf("pcmbuf_pcm_callback: commit last samples");
Jeffrey Goode0db33082009-11-11 07:02:18 +0000673 commit_chunk(false);
Jeffrey Goode874c9112009-11-09 18:12:20 +0000674 }
675 }
676
677 {
Jeffrey Goodec8944c02010-05-28 13:21:24 +0000678 /* Send the new chunk to the DMA */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000679 if(read_chunk)
680 {
Jeffrey Goodec8944c02010-05-28 13:21:24 +0000681 last_chunksize = read_chunk->size;
682 pcmbuf_unplayed_bytes -= last_chunksize;
683 *size = last_chunksize;
Jeffrey Goode874c9112009-11-09 18:12:20 +0000684 *start = read_chunk->addr;
685 }
686 else
687 {
Jeffrey Goode664dc902009-11-11 00:48:17 +0000688 /* No more chunks */
689 logf("pcmbuf_pcm_callback: no more chunks");
Jeffrey Goode874c9112009-11-09 18:12:20 +0000690 last_chunksize = 0;
691 *size = 0;
692 *start = NULL;
Jeffrey Goode874c9112009-11-09 18:12:20 +0000693 }
694 }
695 DISPLAY_DESC("callback");
696}
697
Jeffrey Goode0db33082009-11-11 07:02:18 +0000698/* Force playback */
Jeffrey Goode874c9112009-11-09 18:12:20 +0000699void pcmbuf_play_start(void)
700{
Michael Sevakisa2b67032011-06-29 06:37:04 +0000701 if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED &&
702 pcmbuf_unplayed_bytes && read_chunk != NULL)
Jeffrey Goode874c9112009-11-09 18:12:20 +0000703 {
Jeffrey Goode664dc902009-11-11 00:48:17 +0000704 logf("pcmbuf_play_start");
Jeffrey Goode874c9112009-11-09 18:12:20 +0000705 last_chunksize = read_chunk->size;
706 pcmbuf_unplayed_bytes -= last_chunksize;
Michael Sevakisa2b67032011-06-29 06:37:04 +0000707 mixer_channel_play_data(PCM_MIXER_CHAN_PLAYBACK,
708 pcmbuf_pcm_callback, NULL, 0);
Jeffrey Goode874c9112009-11-09 18:12:20 +0000709 }
710}
711
712void pcmbuf_play_stop(void)
713{
Jeffrey Goode664dc902009-11-11 00:48:17 +0000714 logf("pcmbuf_play_stop");
Michael Sevakisa2b67032011-06-29 06:37:04 +0000715 mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK);
Jeffrey Goode874c9112009-11-09 18:12:20 +0000716
717 pcmbuf_unplayed_bytes = 0;
Jeffrey Goode874c9112009-11-09 18:12:20 +0000718 if (read_chunk) {
719 write_end_chunk->link = read_chunk;
720 write_end_chunk = read_end_chunk;
721 read_chunk = read_end_chunk = NULL;
722 }
Michael Sevakisba8ce002011-02-25 03:51:52 +0000723 last_chunksize = 0;
Jeffrey Goode874c9112009-11-09 18:12:20 +0000724 pcmbuffer_pos = 0;
725 pcmbuffer_fillpos = 0;
Jeffrey Goode9e095342009-11-10 03:46:08 +0000726#ifdef HAVE_CROSSFADE
Jeffrey Goode874c9112009-11-09 18:12:20 +0000727 crossfade_track_change_started = false;
728 crossfade_active = false;
Jeffrey Goode9e095342009-11-10 03:46:08 +0000729#endif
Jeffrey Goode664dc902009-11-11 00:48:17 +0000730 track_transition = false;
Jeffrey Goode84ca2362009-11-18 20:56:19 +0000731 flush_pcmbuf = false;
Jeffrey Goode874c9112009-11-09 18:12:20 +0000732 DISPLAY_DESC("play_stop");
733
Thomas Martitz9afb55a2010-12-22 16:03:15 +0000734 /* Can unboost the codec thread here no matter who's calling,
735 * pretend full pcm buffer to unboost */
736 boost_codec_thread(10);
Jeffrey Goode874c9112009-11-09 18:12:20 +0000737}
738
739void pcmbuf_pause(bool pause)
740{
Jeffrey Goode664dc902009-11-11 00:48:17 +0000741 logf("pcmbuf_pause: %s", pause?"pause":"play");
Michael Sevakisa2b67032011-06-29 06:37:04 +0000742
743 if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED)
744 mixer_channel_play_pause(PCM_MIXER_CHAN_PLAYBACK, !pause);
Jeffrey Goode874c9112009-11-09 18:12:20 +0000745 else if (!pause)
746 pcmbuf_play_start();
747}
748
749
Jeffrey Goode0db33082009-11-11 07:02:18 +0000750/** Crossfade */
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000751
Jeffrey Goodeb6f15f22009-11-08 04:27:27 +0000752/* Clip sample to signed 16 bit range */
753static inline int32_t clip_sample_16(int32_t sample)
754{
755 if ((int16_t)sample != sample)
756 sample = 0x7fff ^ (sample >> 31);
757 return sample;
758}
759
Michael Giacomelli64699262009-08-11 02:05:38 +0000760#ifdef HAVE_CROSSFADE
Jeffrey Goode15351e82009-11-12 04:24:41 +0000761/* Find the chunk that's (length) deep in the list. Return the position within
762 * the chunk, and leave the chunkdesc pointer pointing to the chunk. */
763static size_t find_chunk(size_t length, struct chunkdesc **chunk)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000764{
Jeffrey Goode15351e82009-11-12 04:24:41 +0000765 while (*chunk && length >= (*chunk)->size)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000766 {
Jeffrey Goode15351e82009-11-12 04:24:41 +0000767 length -= (*chunk)->size;
768 *chunk = (*chunk)->link;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000769 }
Jeffrey Goodec8944c02010-05-28 13:21:24 +0000770 return length;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000771}
772
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000773/* Returns the number of bytes _NOT_ mixed/faded */
774static size_t crossfade_mix_fade(int factor, size_t length, const char *buf,
775 size_t *out_sample, struct chunkdesc **out_chunk)
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000776{
Steve Bavinb2dc7f02009-11-26 14:41:31 +0000777 if (length == 0)
778 return 0;
779
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000780 const int16_t *input_buf = (const int16_t *)buf;
781 int16_t *output_buf = (int16_t *)((*out_chunk)->addr);
782 int16_t *chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
783 output_buf = &output_buf[*out_sample];
784 int32_t sample;
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000785
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000786 while (length)
787 {
788 /* fade left and right channel at once to keep buffer alignment */
789 int i;
790 for (i = 0; i < 2; i++)
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000791 {
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000792 if (input_buf)
793 /* fade the input buffer and mix into the chunk */
794 {
795 sample = *input_buf++;
796 sample = ((sample * factor) >> 8) + *output_buf;
797 *output_buf++ = clip_sample_16(sample);
798 }
799 else
800 /* fade the chunk only */
801 {
802 sample = *output_buf;
803 *output_buf++ = (sample * factor) >> 8;
804 }
805 }
806
807 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
808
809 /* move to next chunk as needed */
810 if (output_buf >= chunk_end)
811 {
812 *out_chunk = (*out_chunk)->link;
813 if (!(*out_chunk))
814 return length;
815 output_buf = (int16_t *)((*out_chunk)->addr);
816 chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000817 }
818 }
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000819 *out_sample = output_buf - (int16_t *)((*out_chunk)->addr);
820 return 0;
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000821}
822
Jeffrey Goode0db33082009-11-11 07:02:18 +0000823/* Initializes crossfader, calculates all necessary parameters and performs
824 * fade-out with the PCM buffer. */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000825static void crossfade_start(void)
826{
Brandon Low6c0908b2006-04-23 22:54:34 +0000827 size_t crossfade_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000828 size_t crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000829 size_t fade_out_rem;
830 size_t fade_out_delay;
831 size_t fade_in_delay;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000832
Jeffrey Goode5c69a422009-11-09 17:11:53 +0000833 crossfade_track_change_started = false;
Brandon Low413da2a2006-02-07 20:38:55 +0000834 /* Reject crossfade if less than .5s of data */
835 if (LOW_DATA(2)) {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000836 logf("crossfade rejected");
837 pcmbuf_play_stop();
838 return ;
839 }
840
841 logf("crossfade_start");
Jeffrey Goode0db33082009-11-11 07:02:18 +0000842 commit_chunk(false);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000843 crossfade_active = true;
Brandon Low6c0908b2006-04-23 22:54:34 +0000844
Brandon Low413da2a2006-02-07 20:38:55 +0000845 /* Initialize the crossfade buffer size to all of the buffered data that
846 * has not yet been sent to the DMA */
Brandon Low6c0908b2006-04-23 22:54:34 +0000847 crossfade_rem = pcmbuf_unplayed_bytes;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000848 crossfade_chunk = read_chunk->link;
Brandon Low8ef18272006-04-24 12:41:30 +0000849 crossfade_sample = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000850
Jeffrey Goode15351e82009-11-12 04:24:41 +0000851 /* Get fade out info from settings. */
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000852 fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE;
853 fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000854
Brandon Low9aa49a42006-04-25 02:28:21 +0000855 crossfade_need = fade_out_delay + fade_out_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000856 if (crossfade_rem > crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000857 {
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000858 if (crossfade_auto_skip)
859 /* Automatic track changes only modify the last part of the buffer,
860 * so find the right chunk and sample to start the crossfade */
861 {
862 crossfade_sample = find_chunk(crossfade_rem - crossfade_need,
Jeffrey Goodec8944c02010-05-28 13:21:24 +0000863 &crossfade_chunk) / 2;
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000864 crossfade_rem = crossfade_need;
865 }
866 else
867 /* Manual skips occur immediately, but give time to process */
868 {
869 crossfade_rem -= crossfade_chunk->size;
870 crossfade_chunk = crossfade_chunk->link;
871 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000872 }
873 /* Truncate fade out duration if necessary. */
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000874 if (crossfade_rem < crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000875 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000876 size_t crossfade_short = crossfade_need - crossfade_rem;
877 if (fade_out_rem >= crossfade_short)
Brandon Low6c0908b2006-04-23 22:54:34 +0000878 fade_out_rem -= crossfade_short;
879 else
880 {
881 fade_out_delay -= crossfade_short - fade_out_rem;
882 fade_out_rem = 0;
883 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000884 }
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000885 crossfade_rem -= fade_out_delay + fade_out_rem;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000886
Jeffrey Goode15351e82009-11-12 04:24:41 +0000887 /* Completely process the crossfade fade-out effect with current PCM buffer */
888 if (!crossfade_mixmode)
889 {
890 /* Fade out the specified amount of the already processed audio */
891 size_t total_fade_out = fade_out_rem;
892 size_t fade_out_sample;
893 struct chunkdesc *fade_out_chunk = crossfade_chunk;
894
895 /* Find the right chunk and sample to start fading out */
896 fade_out_delay += crossfade_sample * 2;
Jeffrey Goodec8944c02010-05-28 13:21:24 +0000897 fade_out_sample = find_chunk(fade_out_delay, &fade_out_chunk) / 2;
Jeffrey Goode15351e82009-11-12 04:24:41 +0000898
899 while (fade_out_rem > 0)
900 {
901 /* Each 1/10 second of audio will have the same fade applied */
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000902 size_t block_rem = MIN(BYTERATE / 10, fade_out_rem);
Jeffrey Goode15351e82009-11-12 04:24:41 +0000903 int factor = (fade_out_rem << 8) / total_fade_out;
904
905 fade_out_rem -= block_rem;
906
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000907 crossfade_mix_fade(factor, block_rem, NULL,
908 &fade_out_sample, &fade_out_chunk);
Jeffrey Goode15351e82009-11-12 04:24:41 +0000909 }
Jeffrey Goode8cb4b362011-05-09 21:52:06 +0000910
Jeffrey Gooded1963e12009-11-13 21:58:41 +0000911 /* zero out the rest of the buffer */
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000912 crossfade_mix_fade(0, crossfade_rem, NULL,
913 &fade_out_sample, &fade_out_chunk);
Jeffrey Goode15351e82009-11-12 04:24:41 +0000914 }
915
916 /* Initialize fade-in counters */
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000917 crossfade_fade_in_total = global_settings.crossfade_fade_in_duration * BYTERATE;
Brandon Low6c0908b2006-04-23 22:54:34 +0000918 crossfade_fade_in_rem = crossfade_fade_in_total;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000919
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000920 fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE;
Brandon Low6c0908b2006-04-23 22:54:34 +0000921
Jeffrey Goode15351e82009-11-12 04:24:41 +0000922 /* Find the right chunk and sample to start fading in */
923 fade_in_delay += crossfade_sample * 2;
Jeffrey Goodec8944c02010-05-28 13:21:24 +0000924 crossfade_sample = find_chunk(fade_in_delay, &crossfade_chunk) / 2;
Jeffrey Goode15351e82009-11-12 04:24:41 +0000925 logf("crossfade_start done!");
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000926}
927
Jeffrey Goode0db33082009-11-11 07:02:18 +0000928/* Perform fade-in of new track */
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000929static void write_to_crossfade(size_t length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000930{
Brandon Low9602dd72006-04-25 12:34:28 +0000931 if (length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000932 {
Jeffrey Goodeba9280d2009-11-16 04:42:34 +0000933 char *buf = fadebuf;
Brandon Low9602dd72006-04-25 12:34:28 +0000934 if (crossfade_fade_in_rem)
Brandon Low9aa49a42006-04-25 02:28:21 +0000935 {
936 size_t samples;
Michael Sevakis53981252007-11-16 14:26:05 +0000937 int16_t *input_buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000938
939 /* Fade factor for this packet */
940 int factor =
941 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
942 crossfade_fade_in_total;
943 /* Bytes to fade */
944 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
945
946 /* We _will_ fade this many bytes */
947 crossfade_fade_in_rem -= fade_rem;
948
949 if (crossfade_chunk)
950 {
951 /* Mix the data */
952 size_t fade_total = fade_rem;
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000953 fade_rem = crossfade_mix_fade(factor, fade_rem, buf,
954 &crossfade_sample, &crossfade_chunk);
Brandon Low9602dd72006-04-25 12:34:28 +0000955 length -= fade_total - fade_rem;
956 buf += fade_total - fade_rem;
Brandon Low08cdc432006-04-25 16:12:43 +0000957 if (!length)
958 return;
Brandon Low9602dd72006-04-25 12:34:28 +0000959 }
960
Brandon Low9aa49a42006-04-25 02:28:21 +0000961 samples = fade_rem / 2;
Michael Sevakis53981252007-11-16 14:26:05 +0000962 input_buf = (int16_t *)buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000963 /* Fade remaining samples in place */
Jeffrey Goode96bb9ef2009-10-23 01:01:00 +0000964 while (samples--)
Brandon Low9aa49a42006-04-25 02:28:21 +0000965 {
Michael Sevakis53981252007-11-16 14:26:05 +0000966 int32_t sample = *input_buf;
Brandon Low9aa49a42006-04-25 02:28:21 +0000967 *input_buf++ = (sample * factor) >> 8;
Brandon Low9aa49a42006-04-25 02:28:21 +0000968 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000969 }
Brandon Low413da2a2006-02-07 20:38:55 +0000970
Brandon Low9602dd72006-04-25 12:34:28 +0000971 if (crossfade_chunk)
Brandon Low6c0908b2006-04-23 22:54:34 +0000972 {
Brandon Low9602dd72006-04-25 12:34:28 +0000973 /* Mix the data */
974 size_t mix_total = length;
Jeffrey Goode873c5b62009-11-15 04:57:40 +0000975 /* A factor of 256 means mix only, no fading */
976 length = crossfade_mix_fade(256, length, buf,
977 &crossfade_sample, &crossfade_chunk);
Brandon Low9602dd72006-04-25 12:34:28 +0000978 buf += mix_total - length;
Brandon Low08cdc432006-04-25 16:12:43 +0000979 if (!length)
980 return;
Brandon Low6c0908b2006-04-23 22:54:34 +0000981 }
Brandon Low9602dd72006-04-25 12:34:28 +0000982
Jeffrey Goode013fe352009-11-05 17:32:32 +0000983 while (length > 0)
984 {
Jeffrey Goode0db33082009-11-11 07:02:18 +0000985 COMMIT_IF_NEEDED;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000986 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000987 size_t copy_n = MIN(length, pcmbuf_size - pcmbuffer_index);
988 memcpy(&pcmbuffer[pcmbuffer_index], buf, copy_n);
Jeffrey Goode013fe352009-11-05 17:32:32 +0000989 buf += copy_n;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +0000990 pcmbuffer_fillpos += copy_n;
Jeffrey Goode013fe352009-11-05 17:32:32 +0000991 length -= copy_n;
992 }
Brandon Low9aa49a42006-04-25 02:28:21 +0000993 }
Jeffrey Goode0db33082009-11-11 07:02:18 +0000994 /* if no more fading-in to do, stop the crossfade */
995 if (!(crossfade_fade_in_rem || crossfade_chunk))
996 crossfade_active = false;
Brandon Low413da2a2006-02-07 20:38:55 +0000997}
998
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000999static void pcmbuf_finish_crossfade_enable(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001000{
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001001 /* Copy the pending setting over now */
Jeffrey Goode8edac6e2009-11-09 05:45:05 +00001002 crossfade_enabled = crossfade_enable_request;
Jeffrey Goode8cb4b362011-05-09 21:52:06 +00001003
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001004 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
1005 /* If crossfading, try to keep the buffer full other than 1 second */
Jeffrey Goodeba9280d2009-11-16 04:42:34 +00001006 (pcmbuf_size - BYTERATE) :
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001007 /* Otherwise, just use the default */
1008 PCMBUF_WATERMARK;
1009}
Brandon Low37faaab2006-04-20 02:30:59 +00001010
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001011bool pcmbuf_is_crossfade_active(void)
1012{
Jeffrey Goode5c69a422009-11-09 17:11:53 +00001013 return crossfade_active || crossfade_track_change_started;
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001014}
1015
1016void pcmbuf_request_crossfade_enable(bool on_off)
1017{
1018 /* Next setting to be used, not applied now */
Jeffrey Goode8edac6e2009-11-09 05:45:05 +00001019 crossfade_enable_request = on_off;
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001020}
1021
1022bool pcmbuf_is_same_size(void)
1023{
Jeffrey Goode5c69a422009-11-09 17:11:53 +00001024 /* if pcmbuffer is NULL, then not set up yet even once so always */
1025 bool same_size = pcmbuffer ?
1026 (get_next_required_pcmbuf_size() == pcmbuf_size) : true;
Jeffrey Goode8cb4b362011-05-09 21:52:06 +00001027
Jeffrey Goode5c69a422009-11-09 17:11:53 +00001028 /* no buffer change needed, so finish crossfade setup now */
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001029 if (same_size)
1030 pcmbuf_finish_crossfade_enable();
Jeffrey Goode8cb4b362011-05-09 21:52:06 +00001031
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001032 return same_size;
1033}
Jeffrey Goode9e095342009-11-10 03:46:08 +00001034#endif /* HAVE_CROSSFADE */
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001035
1036
Jeffrey Goode0db33082009-11-11 07:02:18 +00001037/** Debug menu, other metrics */
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001038
1039/* Amount of bytes left in the buffer. */
1040size_t pcmbuf_free(void)
1041{
Jeffrey Goode8edac6e2009-11-09 05:45:05 +00001042 if (read_chunk != NULL)
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001043 {
Jeffrey Goodec8944c02010-05-28 13:21:24 +00001044 void *read = (void *)read_chunk->addr;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +00001045 void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos];
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001046 if (read < write)
1047 return (size_t)(read - write) + pcmbuf_size;
1048 else
1049 return (size_t) (read - write);
1050 }
Jeffrey Goode0db33082009-11-11 07:02:18 +00001051 return pcmbuf_size - pcmbuffer_fillpos;
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001052}
1053
1054size_t pcmbuf_get_bufsize(void)
1055{
1056 return pcmbuf_size;
1057}
1058
1059int pcmbuf_used_descs(void)
1060{
Jeffrey Goode8edac6e2009-11-09 05:45:05 +00001061 struct chunkdesc *temp = read_chunk;
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001062 unsigned int i = 0;
Jeffrey Goode8edac6e2009-11-09 05:45:05 +00001063 while (temp) {
1064 temp = temp->link;
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001065 i++;
1066 }
1067 return i;
1068}
1069
1070int pcmbuf_descs(void)
1071{
Jeffrey Goode874c9112009-11-09 18:12:20 +00001072 return NUM_CHUNK_DESCS(pcmbuf_size);
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001073}
1074
1075#ifdef ROCKBOX_HAS_LOGF
Jeffrey Goode0db33082009-11-11 07:02:18 +00001076unsigned char *pcmbuf_get_meminfo(size_t *length)
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001077{
Jeffrey Goode8edac6e2009-11-09 05:45:05 +00001078 *length = pcmbuf_bufend - pcmbuffer;
1079 return pcmbuffer;
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001080}
1081#endif
1082
1083
Michael Sevakisa2b67032011-06-29 06:37:04 +00001084/** Fading and channel volume control */
1085
1086/* Sync the channel amplitude to all states */
1087static void pcmbuf_update_volume(void)
1088{
1089 unsigned int vol = fade_vol;
1090
1091 if (soft_mode)
1092 vol >>= 2;
1093
1094 mixer_channel_set_amplitude(PCM_MIXER_CHAN_PLAYBACK, vol);
1095}
1096
1097/* Quiet-down the channel if 'shhh' is true or else play at normal level */
1098void pcmbuf_soft_mode(bool shhh)
1099{
1100 soft_mode = shhh;
1101 pcmbuf_update_volume();
1102}
1103
1104/* Fade channel in or out */
1105void pcmbuf_fade(bool fade, bool in)
1106{
1107 if (!fade)
1108 {
1109 /* Simply set the level */
1110 fade_vol = in ? MIX_AMP_UNITY : MIX_AMP_MUTE;
1111 }
1112 else
1113 {
1114 /* Start from the opposing end */
1115 fade_vol = in ? MIX_AMP_MUTE : MIX_AMP_UNITY;
1116 }
1117
1118 pcmbuf_update_volume();
1119
1120 if (fade)
1121 {
1122 /* Do this on thread for now */
1123 int old_prio = thread_set_priority(thread_self(), PRIORITY_REALTIME);
1124
1125 while (1)
1126 {
1127 /* Linear fade actually sounds better */
1128 if (in)
1129 fade_vol += MIN(MIX_AMP_UNITY/16, MIX_AMP_UNITY - fade_vol);
1130 else
1131 fade_vol -= MIN(MIX_AMP_UNITY/16, fade_vol - MIX_AMP_MUTE);
1132
1133 pcmbuf_update_volume();
1134
1135 if (fade_vol > MIX_AMP_MUTE && fade_vol < MIX_AMP_UNITY)
1136 {
1137 sleep(0);
1138 continue;
1139 }
1140
1141 break;
1142 }
1143
1144 thread_set_priority(thread_self(), old_prio);
1145 }
1146}
1147
1148
Jeffrey Goode0db33082009-11-11 07:02:18 +00001149/** Misc */
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001150
1151bool pcmbuf_is_lowdata(void)
1152{
Jeffrey Goode9e095342009-11-10 03:46:08 +00001153 if (!pcm_is_playing() || pcm_is_paused()
1154#ifdef HAVE_CROSSFADE
1155 || pcmbuf_is_crossfade_active()
1156#endif
1157 )
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001158 return false;
1159
1160#if MEMORYSIZE > 2
1161 /* 1 seconds of buffer is low data */
1162 return LOW_DATA(4);
1163#else
1164 /* under watermark is low data */
1165 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1166#endif
1167}
1168
1169void pcmbuf_set_low_latency(bool state)
1170{
1171 low_latency_mode = state;
1172}
1173
1174unsigned long pcmbuf_get_latency(void)
1175{
Michael Sevakisa2b67032011-06-29 06:37:04 +00001176 return (pcmbuf_unplayed_bytes +
1177 mixer_channel_get_bytes_waiting(PCM_MIXER_CHAN_PLAYBACK)) * 1000 / BYTERATE;
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001178}