2005-07-13 12:48:22 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Miika Pekkarinen
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-07-13 12:48:22 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "config.h"
|
2009-02-19 20:40:03 +00:00
|
|
|
#include "system.h"
|
2005-07-13 12:48:22 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include <kernel.h>
|
2007-10-06 22:27:27 +00:00
|
|
|
#include "pcm.h"
|
2011-06-29 06:37:04 +00:00
|
|
|
#include "pcm_mixer.h"
|
|
|
|
#include "pcmbuf.h"
|
2009-11-04 03:58:33 +00:00
|
|
|
#include "playback.h"
|
2011-02-23 14:31:13 +00:00
|
|
|
#include "codec_thread.h"
|
2009-10-22 00:59:42 +00:00
|
|
|
|
|
|
|
/* Define LOGF_ENABLE to enable logf output in this file */
|
|
|
|
/*#define LOGF_ENABLE*/
|
2005-07-13 12:48:22 +00:00
|
|
|
#include "logf.h"
|
2010-06-21 16:53:00 +00:00
|
|
|
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
|
2005-07-13 12:48:22 +00:00
|
|
|
#include "cpu.h"
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
2005-07-21 11:44:00 +00:00
|
|
|
#include "settings.h"
|
|
|
|
#include "audio.h"
|
2009-11-09 05:58:02 +00:00
|
|
|
#include "voice_thread.h"
|
2005-08-20 11:13:19 +00:00
|
|
|
#include "dsp.h"
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2009-11-05 21:59:36 +00:00
|
|
|
#define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
|
|
|
|
on the pcm buffer */
|
|
|
|
#define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
|
|
|
|
chunks on the pcm buffer (or we run out
|
|
|
|
of buffer descriptors, which is
|
|
|
|
non-fatal) */
|
|
|
|
#define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
|
|
|
|
this to the DMA */
|
2010-05-28 13:21:24 +00:00
|
|
|
#define CROSSFADE_BUFSIZE 8192 /* Size of the crossfade buffer */
|
2009-11-05 21:59:36 +00:00
|
|
|
|
2009-11-16 04:42:34 +00:00
|
|
|
/* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
|
|
|
|
#define BYTERATE (NATIVE_FREQUENCY * 4)
|
|
|
|
|
2009-01-10 21:10:56 +00:00
|
|
|
#if MEMORYSIZE > 2
|
2006-09-16 16:18:11 +00:00
|
|
|
/* Keep watermark high for iPods at least (2s) */
|
2009-11-16 04:42:34 +00:00
|
|
|
#define PCMBUF_WATERMARK (BYTERATE * 2)
|
2009-01-10 21:10:56 +00:00
|
|
|
#else
|
2009-11-16 04:42:34 +00:00
|
|
|
#define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
|
2009-01-10 21:10:56 +00:00
|
|
|
#endif
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Structure we can use to queue pcm chunks in memory to be played
|
|
|
|
* by the driver code. */
|
2009-11-09 05:45:05 +00:00
|
|
|
struct chunkdesc
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2010-05-28 13:21:24 +00:00
|
|
|
unsigned char *addr;
|
2006-04-23 22:54:34 +00:00
|
|
|
size_t size;
|
2009-11-09 05:45:05 +00:00
|
|
|
struct chunkdesc* link;
|
2009-11-04 03:58:33 +00:00
|
|
|
/* true if last chunk in the track */
|
|
|
|
bool end_of_track;
|
2006-04-23 22:54:34 +00:00
|
|
|
};
|
|
|
|
|
2009-11-09 18:12:20 +00:00
|
|
|
#define NUM_CHUNK_DESCS(bufsize) \
|
2007-03-19 22:04:17 +00:00
|
|
|
((bufsize) / PCMBUF_MINAVG_CHUNK)
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Size of the PCM buffer. */
|
2011-06-29 09:39:13 +00:00
|
|
|
static size_t pcmbuf_size = 0;
|
|
|
|
static char *pcmbuf_bufend;
|
|
|
|
static char *pcmbuffer;
|
2009-11-09 05:45:05 +00:00
|
|
|
/* Current PCM buffer write index. */
|
2011-06-29 09:39:13 +00:00
|
|
|
static size_t pcmbuffer_pos;
|
2009-11-09 05:45:05 +00:00
|
|
|
/* Amount pcmbuffer_pos will be increased.*/
|
2011-06-29 09:39:13 +00:00
|
|
|
static size_t pcmbuffer_fillpos;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
static struct chunkdesc *first_desc;
|
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
/* Gapless playback */
|
2011-06-29 09:39:13 +00:00
|
|
|
static bool track_transition;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Fade effect */
|
|
|
|
static unsigned int fade_vol = MIX_AMP_UNITY;
|
|
|
|
|
|
|
|
/* Voice */
|
|
|
|
static bool soft_mode = false;
|
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
|
|
|
/* Crossfade buffer */
|
2011-06-29 09:39:13 +00:00
|
|
|
static char *fadebuf;
|
2009-11-10 03:46:08 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Crossfade related state */
|
|
|
|
static bool crossfade_enabled;
|
2009-11-09 05:45:05 +00:00
|
|
|
static bool crossfade_enable_request;
|
2006-05-14 14:08:26 +00:00
|
|
|
static bool crossfade_mixmode;
|
2009-11-13 21:58:41 +00:00
|
|
|
static bool crossfade_auto_skip;
|
2011-06-29 09:39:13 +00:00
|
|
|
static bool crossfade_active;
|
|
|
|
static bool crossfade_track_change_started;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Track the current location for processing crossfade */
|
2011-06-29 09:39:13 +00:00
|
|
|
static struct chunkdesc *crossfade_chunk;
|
|
|
|
static size_t crossfade_sample;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Counters for fading in new data */
|
2011-06-29 09:39:13 +00:00
|
|
|
static size_t crossfade_fade_in_total;
|
|
|
|
static size_t crossfade_fade_in_rem;
|
2009-08-11 02:05:38 +00:00
|
|
|
#endif
|
2006-02-07 20:38:55 +00:00
|
|
|
|
2011-06-29 09:39:13 +00:00
|
|
|
static struct chunkdesc *read_chunk;
|
|
|
|
static struct chunkdesc *read_end_chunk;
|
|
|
|
static struct chunkdesc *write_chunk;
|
|
|
|
static struct chunkdesc *write_end_chunk;
|
|
|
|
static size_t last_chunksize;
|
2006-04-24 01:32:28 +00:00
|
|
|
|
2011-06-29 09:39:13 +00:00
|
|
|
static size_t pcmbuf_unplayed_bytes;
|
|
|
|
static size_t pcmbuf_watermark;
|
2006-04-24 01:32:28 +00:00
|
|
|
|
2006-02-07 19:17:51 +00:00
|
|
|
static bool low_latency_mode = false;
|
2009-11-11 07:02:18 +00:00
|
|
|
static bool flush_pcmbuf = false;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2007-03-06 21:00:45 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2008-03-25 02:34:12 +00:00
|
|
|
static int codec_thread_priority = PRIORITY_PLAYBACK;
|
2007-03-06 21:00:45 +00:00
|
|
|
#endif
|
2007-03-06 20:32:13 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Helpful macros for use in conditionals this assumes some of the above
|
|
|
|
* static variable names */
|
2009-11-11 07:02:18 +00:00
|
|
|
#define COMMIT_IF_NEEDED if(pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || \
|
|
|
|
(pcmbuffer_pos + pcmbuffer_fillpos) >= pcmbuf_size) commit_chunk(false)
|
2006-02-07 20:38:55 +00:00
|
|
|
#define LOW_DATA(quarter_secs) \
|
|
|
|
(pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
|
|
|
|
|
2009-11-06 04:25:28 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-06 04:13:36 +00:00
|
|
|
static void crossfade_start(void);
|
2009-11-16 04:42:34 +00:00
|
|
|
static void write_to_crossfade(size_t length);
|
2009-11-05 21:59:36 +00:00
|
|
|
static void pcmbuf_finish_crossfade_enable(void);
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2009-11-05 21:59:36 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Callbacks into playback.c */
|
|
|
|
extern void audio_pcmbuf_position_callback(unsigned int time);
|
|
|
|
extern void audio_pcmbuf_track_change(bool pcmbuf);
|
|
|
|
extern bool audio_pcmbuf_may_play(void);
|
|
|
|
|
2009-11-08 04:27:27 +00:00
|
|
|
|
2009-11-05 17:32:32 +00:00
|
|
|
/**************************************/
|
|
|
|
|
2009-11-09 05:45:05 +00:00
|
|
|
/* define this to show detailed chunkdesc usage information on the sim console */
|
2009-11-05 17:32:32 +00:00
|
|
|
/*#define DESC_DEBUG*/
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
#undef DESC_DEBUG
|
|
|
|
#endif
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2009-11-05 17:32:32 +00:00
|
|
|
#ifdef DESC_DEBUG
|
|
|
|
#define DISPLAY_DESC(caller) while(!show_desc(caller))
|
2009-11-18 20:56:19 +00:00
|
|
|
#define DESC_IDX(desc) (desc ? desc - first_desc : -1)
|
|
|
|
#define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \
|
|
|
|
else DEBUGF("%02d", DESC_IDX(desc))
|
|
|
|
#define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \
|
|
|
|
else DEBUGF("-- ")
|
|
|
|
#define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
|
|
|
|
SHOW_DESC_LINK(desc)
|
|
|
|
#define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc)
|
|
|
|
#define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc)
|
2009-11-05 17:32:32 +00:00
|
|
|
|
|
|
|
static bool show_desc(char *caller)
|
|
|
|
{
|
|
|
|
if (show_desc_in_use) return false;
|
|
|
|
show_desc_in_use = true;
|
|
|
|
DEBUGF("%-14s\t", caller);
|
2009-11-18 20:56:19 +00:00
|
|
|
SHOW_POINT("r", read_chunk);
|
|
|
|
SHOW_POINT("re", read_end_chunk);
|
2009-11-05 17:32:32 +00:00
|
|
|
DEBUGF(" ");
|
2009-11-18 20:56:19 +00:00
|
|
|
SHOW_POINT("w", write_chunk);
|
|
|
|
SHOW_POINT("we", write_end_chunk);
|
2009-11-05 17:32:32 +00:00
|
|
|
DEBUGF("\n");
|
2009-11-18 20:56:19 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < pcmbuf_descs(); i++)
|
|
|
|
{
|
|
|
|
SHOW_NUM(i, (first_desc + i));
|
|
|
|
if (i%10 == 9) DEBUGF("\n");
|
|
|
|
}
|
|
|
|
DEBUGF("\n\n");
|
2009-11-05 17:32:32 +00:00
|
|
|
show_desc_in_use = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DISPLAY_DESC(caller) do{}while(0)
|
|
|
|
#endif
|
|
|
|
|
2009-11-04 03:58:33 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Accept new PCM data */
|
2009-11-04 03:58:33 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Commit PCM buffer samples as a new chunk for playback */
|
|
|
|
static void commit_chunk(bool flush_next_time)
|
2009-11-08 04:27:27 +00:00
|
|
|
{
|
2009-11-09 06:53:22 +00:00
|
|
|
if (!pcmbuffer_fillpos)
|
|
|
|
return;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-09 06:53:22 +00:00
|
|
|
/* Never use the last buffer descriptor */
|
|
|
|
while (write_chunk == write_end_chunk) {
|
|
|
|
/* If this happens, something is being stupid */
|
|
|
|
if (!pcm_is_playing()) {
|
|
|
|
logf("commit_chunk error");
|
|
|
|
pcmbuf_play_start();
|
|
|
|
}
|
|
|
|
/* Let approximately one chunk of data playback */
|
2009-11-16 04:42:34 +00:00
|
|
|
sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE);
|
2009-11-09 06:53:22 +00:00
|
|
|
}
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-09 06:53:22 +00:00
|
|
|
/* commit the chunk */
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-09 05:45:05 +00:00
|
|
|
register size_t size = pcmbuffer_fillpos;
|
2009-11-08 04:27:27 +00:00
|
|
|
/* Grab the next description to write, and change the write pointer */
|
2009-11-09 05:45:05 +00:00
|
|
|
register struct chunkdesc *pcmbuf_current = write_chunk;
|
|
|
|
write_chunk = pcmbuf_current->link;
|
2009-11-08 04:27:27 +00:00
|
|
|
/* Fill in the values in the new buffer chunk */
|
2009-11-09 05:45:05 +00:00
|
|
|
pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos];
|
2009-11-08 04:27:27 +00:00
|
|
|
pcmbuf_current->size = size;
|
2011-02-23 14:31:13 +00:00
|
|
|
pcmbuf_current->end_of_track = false;
|
2009-11-08 04:27:27 +00:00
|
|
|
pcmbuf_current->link = NULL;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-09 06:53:22 +00:00
|
|
|
if (read_chunk != NULL)
|
|
|
|
{
|
2009-11-09 05:58:02 +00:00
|
|
|
if (flush_pcmbuf)
|
2009-11-08 04:27:27 +00:00
|
|
|
{
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Flush! Discard all data after the currently playing chunk,
|
2009-11-09 06:53:22 +00:00
|
|
|
and make the current chunk play next */
|
2009-11-18 20:56:19 +00:00
|
|
|
logf("commit_chunk: flush");
|
2011-04-27 03:08:23 +00:00
|
|
|
pcm_play_lock();
|
2009-11-09 05:45:05 +00:00
|
|
|
write_end_chunk->link = read_chunk->link;
|
|
|
|
read_chunk->link = pcmbuf_current;
|
|
|
|
while (write_end_chunk->link)
|
2009-11-08 04:27:27 +00:00
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
write_end_chunk = write_end_chunk->link;
|
|
|
|
pcmbuf_unplayed_bytes -= write_end_chunk->size;
|
2009-11-08 04:27:27 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
read_chunk->end_of_track = track_transition;
|
|
|
|
pcm_play_unlock();
|
2009-11-08 04:27:27 +00:00
|
|
|
}
|
|
|
|
/* If there is already a read buffer setup, add to it */
|
|
|
|
else
|
2009-11-09 05:45:05 +00:00
|
|
|
read_end_chunk->link = pcmbuf_current;
|
2009-11-09 06:53:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-08 04:27:27 +00:00
|
|
|
/* Otherwise create the buffer */
|
2009-11-09 05:45:05 +00:00
|
|
|
read_chunk = pcmbuf_current;
|
2009-11-08 04:27:27 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* If flush_next_time is true, then the current chunk will be thrown out
|
|
|
|
* and the next chunk to be committed will be the next to be played.
|
|
|
|
* This is used to empty the PCM buffer for a track change. */
|
|
|
|
flush_pcmbuf = flush_next_time;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-08 04:27:27 +00:00
|
|
|
/* This is now the last buffer to read */
|
2009-11-09 05:45:05 +00:00
|
|
|
read_end_chunk = pcmbuf_current;
|
2009-11-08 04:27:27 +00:00
|
|
|
|
|
|
|
/* Update bytes counters */
|
|
|
|
pcmbuf_unplayed_bytes += size;
|
|
|
|
|
2009-11-09 05:45:05 +00:00
|
|
|
pcmbuffer_pos += size;
|
|
|
|
if (pcmbuffer_pos >= pcmbuf_size)
|
|
|
|
pcmbuffer_pos -= pcmbuf_size;
|
2009-11-08 04:27:27 +00:00
|
|
|
|
2009-11-09 05:45:05 +00:00
|
|
|
pcmbuffer_fillpos = 0;
|
2009-11-09 06:53:22 +00:00
|
|
|
DISPLAY_DESC("commit_chunk");
|
2009-11-08 04:27:27 +00:00
|
|
|
}
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Set priority of the codec thread */
|
2009-11-09 05:45:05 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2010-12-22 16:03:15 +00:00
|
|
|
/*
|
|
|
|
* expects pcm_fill_state in tenth-% units (e.g. full pcm buffer is 10) */
|
|
|
|
static void boost_codec_thread(int pcm_fill_state)
|
2009-11-09 05:45:05 +00:00
|
|
|
{
|
2010-12-22 16:03:15 +00:00
|
|
|
static const int prios[11] = {
|
|
|
|
PRIORITY_PLAYBACK_MAX, /* 0 - 10% */
|
|
|
|
PRIORITY_PLAYBACK_MAX+1, /* 10 - 20% */
|
|
|
|
PRIORITY_PLAYBACK_MAX+3, /* 20 - 30% */
|
|
|
|
PRIORITY_PLAYBACK_MAX+5, /* 30 - 40% */
|
|
|
|
PRIORITY_PLAYBACK_MAX+7, /* 40 - 50% */
|
|
|
|
PRIORITY_PLAYBACK_MAX+8, /* 50 - 60% */
|
|
|
|
PRIORITY_PLAYBACK_MAX+9, /* 60 - 70% */
|
|
|
|
/* raiseing priority above 70% shouldn't be needed */
|
|
|
|
PRIORITY_PLAYBACK, /* 70 - 80% */
|
|
|
|
PRIORITY_PLAYBACK, /* 80 - 90% */
|
|
|
|
PRIORITY_PLAYBACK, /* 90 -100% */
|
|
|
|
PRIORITY_PLAYBACK, /* 100% */
|
|
|
|
};
|
|
|
|
int new_prio = prios[pcm_fill_state];
|
|
|
|
|
2009-11-09 05:45:05 +00:00
|
|
|
/* Keep voice and codec threads at the same priority or else voice
|
|
|
|
* will starve if the codec thread's priority is boosted. */
|
2010-12-22 16:03:15 +00:00
|
|
|
if (new_prio != codec_thread_priority)
|
2009-11-09 05:45:05 +00:00
|
|
|
{
|
2011-02-23 14:31:13 +00:00
|
|
|
codec_thread_set_priority(new_prio);
|
2010-12-22 16:03:15 +00:00
|
|
|
voice_thread_set_priority(new_prio);
|
|
|
|
codec_thread_priority = new_prio;
|
2009-11-09 05:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2010-12-22 16:03:15 +00:00
|
|
|
#define boost_codec_thread(pcm_fill_state) do{}while(0)
|
2009-11-09 05:45:05 +00:00
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Return true if the PCM buffer is able to receive new data.
|
|
|
|
* Also maintain buffer level above the watermark. */
|
2009-11-06 04:13:36 +00:00
|
|
|
static bool prepare_insert(size_t length)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
bool playing = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED;
|
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
if (low_latency_mode)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2009-11-06 04:13:36 +00:00
|
|
|
/* 1/4s latency. */
|
2011-06-29 06:37:04 +00:00
|
|
|
if (!LOW_DATA(1) && playing)
|
2009-11-06 04:13:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
/* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
|
|
|
|
if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
|
|
|
|
return false;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Maintain the buffer level above the watermark */
|
2011-06-29 06:37:04 +00:00
|
|
|
if (playing)
|
2009-11-09 05:45:05 +00:00
|
|
|
{
|
|
|
|
/* Only codec thread initiates boost - voice boosts the cpu when playing
|
|
|
|
a clip */
|
|
|
|
#ifndef SIMULATOR
|
2011-02-23 14:31:13 +00:00
|
|
|
if (is_codec_thread())
|
2009-11-09 05:45:05 +00:00
|
|
|
#endif /* SIMULATOR */
|
|
|
|
{
|
2010-12-22 16:03:15 +00:00
|
|
|
/* boost cpu if necessary */
|
|
|
|
if (pcmbuf_unplayed_bytes < pcmbuf_watermark)
|
2009-11-09 05:45:05 +00:00
|
|
|
trigger_cpu_boost();
|
2010-12-22 16:03:15 +00:00
|
|
|
boost_codec_thread(pcmbuf_unplayed_bytes*10/pcmbuf_size);
|
2009-11-09 05:45:05 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-09 05:45:05 +00:00
|
|
|
/* Disable crossfade if < .5s of audio */
|
|
|
|
if (LOW_DATA(2))
|
|
|
|
{
|
|
|
|
crossfade_active = false;
|
|
|
|
}
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2009-11-09 05:45:05 +00:00
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
else /* !playing */
|
2009-11-01 19:39:23 +00:00
|
|
|
{
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Boost CPU for pre-buffer */
|
2009-11-06 04:13:36 +00:00
|
|
|
trigger_cpu_boost();
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* If pre-buffered to the watermark, start playback */
|
2009-11-06 04:13:36 +00:00
|
|
|
#if MEMORYSIZE > 2
|
|
|
|
if (!LOW_DATA(4))
|
|
|
|
#else
|
|
|
|
if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
|
|
|
|
#endif
|
2009-11-01 19:39:23 +00:00
|
|
|
{
|
2009-11-06 04:13:36 +00:00
|
|
|
logf("pcm starting");
|
2011-04-27 03:08:23 +00:00
|
|
|
if (audio_pcmbuf_may_play())
|
2009-11-06 04:13:36 +00:00
|
|
|
pcmbuf_play_start();
|
2009-11-01 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Request space in the buffer for writing output samples */
|
2009-11-06 04:13:36 +00:00
|
|
|
void *pcmbuf_request_buffer(int *count)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-11 07:02:18 +00:00
|
|
|
/* we're going to crossfade to a new track, which is now on its way */
|
2009-11-09 17:11:53 +00:00
|
|
|
if (crossfade_track_change_started)
|
2009-11-06 04:13:36 +00:00
|
|
|
crossfade_start();
|
2006-01-21 22:42:44 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* crossfade has begun, put the new track samples in fadebuf */
|
|
|
|
if (crossfade_active)
|
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
int cnt = MIN(*count, CROSSFADE_BUFSIZE/4);
|
|
|
|
if (prepare_insert(cnt << 2))
|
|
|
|
{
|
|
|
|
*count = cnt;
|
|
|
|
return fadebuf;
|
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
else
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2009-11-11 07:02:18 +00:00
|
|
|
/* if possible, reserve room in the PCM buffer for new samples */
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2009-11-06 04:13:36 +00:00
|
|
|
if(prepare_insert(*count << 2))
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
|
|
|
|
if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
|
|
|
/* Usual case, there's space here */
|
2009-11-09 05:45:05 +00:00
|
|
|
return &pcmbuffer[pcmbuffer_index];
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Wrap the buffer, the new samples go at the beginning */
|
|
|
|
commit_chunk(false);
|
2009-11-09 05:45:05 +00:00
|
|
|
pcmbuffer_pos = 0;
|
|
|
|
return &pcmbuffer[0];
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2009-11-11 07:02:18 +00:00
|
|
|
/* PCM buffer not ready to receive new data yet */
|
2009-11-09 05:58:02 +00:00
|
|
|
return NULL;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Handle new samples to the buffer */
|
2009-11-06 04:13:36 +00:00
|
|
|
void pcmbuf_write_complete(int count)
|
|
|
|
{
|
|
|
|
size_t length = (size_t)(unsigned int)count << 2;
|
|
|
|
#ifdef HAVE_CROSSFADE
|
|
|
|
if (crossfade_active)
|
2009-11-16 04:42:34 +00:00
|
|
|
write_to_crossfade(length);
|
2009-11-06 04:13:36 +00:00
|
|
|
else
|
2011-05-09 21:52:06 +00:00
|
|
|
#endif
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
pcmbuffer_fillpos += length;
|
2009-11-11 07:02:18 +00:00
|
|
|
COMMIT_IF_NEEDED;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Init */
|
2006-10-15 11:57:52 +00:00
|
|
|
|
2009-11-09 17:11:53 +00:00
|
|
|
static inline void init_pcmbuffers(void)
|
2008-12-03 12:04:52 +00:00
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
first_desc = write_chunk;
|
|
|
|
struct chunkdesc *next = write_chunk;
|
2006-02-07 20:38:55 +00:00
|
|
|
next++;
|
2009-11-09 05:45:05 +00:00
|
|
|
write_end_chunk = write_chunk;
|
2006-11-06 18:07:30 +00:00
|
|
|
while ((void *)next < (void *)pcmbuf_bufend) {
|
2009-11-09 05:45:05 +00:00
|
|
|
write_end_chunk->link=next;
|
|
|
|
write_end_chunk=next;
|
2006-02-07 20:38:55 +00:00
|
|
|
next++;
|
|
|
|
}
|
2009-11-05 17:32:32 +00:00
|
|
|
DISPLAY_DESC("init");
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2009-11-09 17:11:53 +00:00
|
|
|
static size_t get_next_required_pcmbuf_size(void)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2007-03-19 22:04:17 +00:00
|
|
|
size_t seconds = 1;
|
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-09 05:45:05 +00:00
|
|
|
if (crossfade_enable_request)
|
2009-11-10 03:46:08 +00:00
|
|
|
seconds += global_settings.crossfade_fade_out_delay +
|
|
|
|
global_settings.crossfade_fade_out_duration;
|
|
|
|
#endif
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2009-01-10 21:10:56 +00:00
|
|
|
#if MEMORYSIZE > 2
|
2007-03-19 22:04:17 +00:00
|
|
|
/* Buffer has to be at least 2s long. */
|
|
|
|
seconds += 2;
|
|
|
|
#endif
|
2009-08-12 19:00:31 +00:00
|
|
|
logf("pcmbuf len: %ld", (long)seconds);
|
2009-11-16 04:42:34 +00:00
|
|
|
return seconds * BYTERATE;
|
2007-03-19 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Initialize the pcmbuffer the structure looks like this:
|
2006-11-06 18:07:30 +00:00
|
|
|
* ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
|
2007-03-19 22:04:17 +00:00
|
|
|
size_t pcmbuf_init(unsigned char *bufend)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
unsigned char *voicebuf;
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
pcmbuf_bufend = bufend;
|
2009-11-09 17:11:53 +00:00
|
|
|
pcmbuf_size = get_next_required_pcmbuf_size();
|
2009-11-09 18:12:20 +00:00
|
|
|
write_chunk = (struct chunkdesc *)pcmbuf_bufend -
|
|
|
|
NUM_CHUNK_DESCS(pcmbuf_size);
|
2011-06-29 06:37:04 +00:00
|
|
|
voicebuf = (unsigned char *)write_chunk -
|
|
|
|
voicebuf_init((unsigned char *)write_chunk);
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2010-05-28 13:21:24 +00:00
|
|
|
fadebuf = voicebuf - CROSSFADE_BUFSIZE;
|
2009-11-09 18:12:20 +00:00
|
|
|
pcmbuffer = fadebuf - pcmbuf_size;
|
2009-11-10 03:46:08 +00:00
|
|
|
#else
|
|
|
|
pcmbuffer = voicebuf - pcmbuf_size;
|
|
|
|
#endif
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2009-11-09 17:11:53 +00:00
|
|
|
init_pcmbuffers();
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-05 21:59:36 +00:00
|
|
|
pcmbuf_finish_crossfade_enable();
|
2009-11-10 03:46:08 +00:00
|
|
|
#else
|
|
|
|
pcmbuf_watermark = PCMBUF_WATERMARK;
|
|
|
|
#endif
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_play_stop();
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
pcmbuf_soft_mode(false);
|
|
|
|
|
2009-11-09 05:45:05 +00:00
|
|
|
return pcmbuf_bufend - pcmbuffer;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Track change */
|
2011-02-23 14:31:13 +00:00
|
|
|
void pcmbuf_monitor_track_change(bool monitor)
|
|
|
|
{
|
|
|
|
pcm_play_lock();
|
|
|
|
|
|
|
|
if (last_chunksize != 0)
|
|
|
|
{
|
|
|
|
/* If monitoring, wait until this track runs out. Place in
|
|
|
|
currently playing chunk. If not, cancel notification. */
|
|
|
|
track_transition = monitor;
|
|
|
|
read_end_chunk->end_of_track = monitor;
|
2011-04-27 03:08:23 +00:00
|
|
|
if (!monitor)
|
|
|
|
{
|
|
|
|
/* Clear all notifications */
|
|
|
|
struct chunkdesc *desc = first_desc;
|
|
|
|
struct chunkdesc *end = desc + pcmbuf_descs();
|
|
|
|
while (desc < end)
|
|
|
|
desc++->end_of_track = false;
|
|
|
|
}
|
2011-02-23 14:31:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Post now if PCM stopped and last buffer was sent. */
|
|
|
|
track_transition = false;
|
|
|
|
if (monitor)
|
2011-04-27 03:08:23 +00:00
|
|
|
audio_pcmbuf_track_change(false);
|
2011-02-23 14:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
}
|
2009-11-09 18:12:20 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
bool pcmbuf_start_track_change(bool auto_skip)
|
2009-11-09 18:12:20 +00:00
|
|
|
{
|
2009-11-11 00:48:17 +00:00
|
|
|
bool crossfade = false;
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-11 00:48:17 +00:00
|
|
|
/* Determine whether this track change needs to crossfade */
|
|
|
|
if(crossfade_enabled && !pcmbuf_is_crossfade_active())
|
|
|
|
{
|
|
|
|
switch(global_settings.crossfade)
|
|
|
|
{
|
|
|
|
case CROSSFADE_ENABLE_AUTOSKIP:
|
|
|
|
crossfade = auto_skip;
|
|
|
|
break;
|
|
|
|
case CROSSFADE_ENABLE_MANSKIP:
|
|
|
|
crossfade = !auto_skip;
|
|
|
|
break;
|
|
|
|
case CROSSFADE_ENABLE_SHUFFLE:
|
|
|
|
crossfade = global_settings.playlist_shuffle;
|
|
|
|
break;
|
2009-11-12 15:42:37 +00:00
|
|
|
case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
|
|
|
|
crossfade = global_settings.playlist_shuffle || !auto_skip;
|
2009-11-11 00:48:17 +00:00
|
|
|
break;
|
|
|
|
case CROSSFADE_ENABLE_ALWAYS:
|
|
|
|
crossfade = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-11 00:48:17 +00:00
|
|
|
if (!auto_skip || crossfade)
|
|
|
|
/* manual skip or crossfade */
|
|
|
|
{
|
|
|
|
if (crossfade)
|
|
|
|
{ logf(" crossfade track change"); }
|
|
|
|
else
|
|
|
|
{ logf(" manual track change"); }
|
2011-02-23 14:31:13 +00:00
|
|
|
|
|
|
|
pcm_play_lock();
|
|
|
|
|
|
|
|
/* Cancel any pending automatic gapless transition */
|
|
|
|
pcmbuf_monitor_track_change(false);
|
2009-11-10 03:46:08 +00:00
|
|
|
|
2009-11-11 00:48:17 +00:00
|
|
|
/* Can't do two crossfades at once and, no fade if pcm is off now */
|
|
|
|
if (
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-11 00:48:17 +00:00
|
|
|
pcmbuf_is_crossfade_active() ||
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2011-06-29 06:37:04 +00:00
|
|
|
mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED)
|
2009-11-11 00:48:17 +00:00
|
|
|
{
|
|
|
|
pcmbuf_play_stop();
|
2011-02-23 14:31:13 +00:00
|
|
|
pcm_play_unlock();
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Notify playback that the track change starts now */
|
|
|
|
return true;
|
2009-11-11 00:48:17 +00:00
|
|
|
}
|
2009-11-09 18:12:20 +00:00
|
|
|
|
2009-11-11 00:48:17 +00:00
|
|
|
/* Not enough data, or not crossfading, flush the old data instead */
|
|
|
|
if (LOW_DATA(2) || !crossfade || low_latency_mode)
|
|
|
|
{
|
2009-11-11 07:02:18 +00:00
|
|
|
commit_chunk(true);
|
2009-11-11 00:48:17 +00:00
|
|
|
}
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2011-02-23 14:31:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Don't enable mix mode when skipping tracks manually. */
|
|
|
|
crossfade_mixmode = auto_skip &&
|
|
|
|
global_settings.crossfade_fade_out_mixmode;
|
|
|
|
|
|
|
|
crossfade_auto_skip = auto_skip;
|
2009-11-09 18:12:20 +00:00
|
|
|
|
2011-02-23 14:31:13 +00:00
|
|
|
crossfade_track_change_started = crossfade;
|
|
|
|
}
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2011-02-23 14:31:13 +00:00
|
|
|
pcm_play_unlock();
|
|
|
|
|
|
|
|
/* Keep trigger outside the play lock or HW FIFO underruns can happen
|
|
|
|
since frequency scaling is *not* always fast */
|
|
|
|
trigger_cpu_boost();
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
/* Notify playback that the track change starts now */
|
|
|
|
return true;
|
2009-11-11 00:48:17 +00:00
|
|
|
}
|
2009-11-11 07:02:18 +00:00
|
|
|
else /* automatic and not crossfading, so do gapless track change */
|
2009-11-09 18:12:20 +00:00
|
|
|
{
|
2009-11-11 00:48:17 +00:00
|
|
|
/* The codec is moving on to the next track, but the current track will
|
|
|
|
* continue to play. Set a flag to make sure the elapsed time of the
|
|
|
|
* current track will be updated properly, and mark the current chunk
|
|
|
|
* as the last one in the track. */
|
|
|
|
logf(" gapless track change");
|
2011-02-23 14:31:13 +00:00
|
|
|
pcmbuf_monitor_track_change(true);
|
2011-04-27 03:08:23 +00:00
|
|
|
return false;
|
2009-11-09 18:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Playback */
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* PCM driver callback
|
2009-11-06 04:13:36 +00:00
|
|
|
* This function has 3 major logical parts (separated by brackets both for
|
|
|
|
* readability and variable scoping). The first part performs the
|
2009-11-11 00:48:17 +00:00
|
|
|
* operations related to finishing off the last chunk we fed to the DMA.
|
|
|
|
* The second part detects the end of playlist condition when the PCM
|
|
|
|
* buffer is empty except for uncommitted samples. Then they are committed
|
|
|
|
* and sent to the PCM driver for playback. The third part performs the
|
|
|
|
* operations involved in sending a new chunk to the DMA. */
|
2009-11-06 04:13:36 +00:00
|
|
|
static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
|
2005-07-21 11:44:00 +00:00
|
|
|
{
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
struct chunkdesc *pcmbuf_current = read_chunk;
|
2009-11-11 00:48:17 +00:00
|
|
|
/* Take the finished chunk out of circulation */
|
2009-11-09 05:45:05 +00:00
|
|
|
read_chunk = pcmbuf_current->link;
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2009-11-16 04:42:34 +00:00
|
|
|
/* if during a track transition, update the elapsed time in ms */
|
2009-11-06 04:13:36 +00:00
|
|
|
if (track_transition)
|
2009-11-16 04:42:34 +00:00
|
|
|
audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-11 00:48:17 +00:00
|
|
|
/* if last chunk in the track, stop updates and notify audio thread */
|
2009-11-06 04:13:36 +00:00
|
|
|
if (pcmbuf_current->end_of_track)
|
2009-11-11 00:48:17 +00:00
|
|
|
{
|
|
|
|
track_transition = false;
|
2011-04-27 03:08:23 +00:00
|
|
|
audio_pcmbuf_track_change(true);
|
2009-11-11 00:48:17 +00:00
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2009-11-11 00:48:17 +00:00
|
|
|
/* Put the finished chunk back into circulation */
|
2009-11-09 05:45:05 +00:00
|
|
|
write_end_chunk->link = pcmbuf_current;
|
|
|
|
write_end_chunk = pcmbuf_current;
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-06 04:13:36 +00:00
|
|
|
/* If we've read over the crossfade chunk while it's still fading */
|
|
|
|
if (pcmbuf_current == crossfade_chunk)
|
2009-11-09 05:45:05 +00:00
|
|
|
crossfade_chunk = read_chunk;
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
|
|
|
/* Commit last samples at end of playlist */
|
2009-11-09 05:45:05 +00:00
|
|
|
if (pcmbuffer_fillpos && !read_chunk)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
|
|
|
logf("pcmbuf_pcm_callback: commit last samples");
|
2009-11-11 07:02:18 +00:00
|
|
|
commit_chunk(false);
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2010-05-28 13:21:24 +00:00
|
|
|
/* Send the new chunk to the DMA */
|
2009-11-09 18:12:20 +00:00
|
|
|
if(read_chunk)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2010-05-28 13:21:24 +00:00
|
|
|
last_chunksize = read_chunk->size;
|
|
|
|
pcmbuf_unplayed_bytes -= last_chunksize;
|
|
|
|
*size = last_chunksize;
|
2009-11-09 18:12:20 +00:00
|
|
|
*start = read_chunk->addr;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-11 00:48:17 +00:00
|
|
|
/* No more chunks */
|
|
|
|
logf("pcmbuf_pcm_callback: no more chunks");
|
2009-11-06 04:13:36 +00:00
|
|
|
last_chunksize = 0;
|
2009-11-09 18:12:20 +00:00
|
|
|
*size = 0;
|
|
|
|
*start = NULL;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DISPLAY_DESC("callback");
|
2005-07-21 11:44:00 +00:00
|
|
|
}
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Force playback */
|
2009-11-06 04:13:36 +00:00
|
|
|
void pcmbuf_play_start(void)
|
2007-03-19 22:04:17 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED &&
|
|
|
|
pcmbuf_unplayed_bytes && read_chunk != NULL)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2009-11-11 00:48:17 +00:00
|
|
|
logf("pcmbuf_play_start");
|
2009-11-09 05:45:05 +00:00
|
|
|
last_chunksize = read_chunk->size;
|
2009-11-06 04:13:36 +00:00
|
|
|
pcmbuf_unplayed_bytes -= last_chunksize;
|
2011-06-29 06:37:04 +00:00
|
|
|
mixer_channel_play_data(PCM_MIXER_CHAN_PLAYBACK,
|
|
|
|
pcmbuf_pcm_callback, NULL, 0);
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2007-03-19 22:04:17 +00:00
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
void pcmbuf_play_stop(void)
|
|
|
|
{
|
2009-11-11 00:48:17 +00:00
|
|
|
logf("pcmbuf_play_stop");
|
2011-06-29 06:37:04 +00:00
|
|
|
mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK);
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
pcmbuf_unplayed_bytes = 0;
|
2009-11-09 05:45:05 +00:00
|
|
|
if (read_chunk) {
|
|
|
|
write_end_chunk->link = read_chunk;
|
|
|
|
write_end_chunk = read_end_chunk;
|
|
|
|
read_chunk = read_end_chunk = NULL;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2011-02-25 03:51:52 +00:00
|
|
|
last_chunksize = 0;
|
2009-11-09 05:45:05 +00:00
|
|
|
pcmbuffer_pos = 0;
|
|
|
|
pcmbuffer_fillpos = 0;
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-09 17:11:53 +00:00
|
|
|
crossfade_track_change_started = false;
|
2009-11-06 04:13:36 +00:00
|
|
|
crossfade_active = false;
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2009-11-11 00:48:17 +00:00
|
|
|
track_transition = false;
|
2009-11-18 20:56:19 +00:00
|
|
|
flush_pcmbuf = false;
|
2009-11-06 04:13:36 +00:00
|
|
|
DISPLAY_DESC("play_stop");
|
|
|
|
|
2010-12-22 16:03:15 +00:00
|
|
|
/* Can unboost the codec thread here no matter who's calling,
|
|
|
|
* pretend full pcm buffer to unboost */
|
|
|
|
boost_codec_thread(10);
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2007-06-24 20:36:58 +00:00
|
|
|
void pcmbuf_pause(bool pause)
|
|
|
|
{
|
2009-11-11 00:48:17 +00:00
|
|
|
logf("pcmbuf_pause: %s", pause?"pause":"play");
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED)
|
|
|
|
mixer_channel_play_pause(PCM_MIXER_CHAN_PLAYBACK, !pause);
|
2007-10-06 22:27:27 +00:00
|
|
|
else if (!pause)
|
|
|
|
pcmbuf_play_start();
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Crossfade */
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2009-11-08 04:27:27 +00:00
|
|
|
/* Clip sample to signed 16 bit range */
|
|
|
|
static inline int32_t clip_sample_16(int32_t sample)
|
|
|
|
{
|
|
|
|
if ((int16_t)sample != sample)
|
|
|
|
sample = 0x7fff ^ (sample >> 31);
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
2009-08-11 02:05:38 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2009-11-12 04:24:41 +00:00
|
|
|
/* Find the chunk that's (length) deep in the list. Return the position within
|
|
|
|
* the chunk, and leave the chunkdesc pointer pointing to the chunk. */
|
|
|
|
static size_t find_chunk(size_t length, struct chunkdesc **chunk)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2009-11-12 04:24:41 +00:00
|
|
|
while (*chunk && length >= (*chunk)->size)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2009-11-12 04:24:41 +00:00
|
|
|
length -= (*chunk)->size;
|
|
|
|
*chunk = (*chunk)->link;
|
2006-04-23 22:54:34 +00:00
|
|
|
}
|
2010-05-28 13:21:24 +00:00
|
|
|
return length;
|
2005-11-06 16:40:20 +00:00
|
|
|
}
|
|
|
|
|
2009-11-15 04:57:40 +00:00
|
|
|
/* Returns the number of bytes _NOT_ mixed/faded */
|
|
|
|
static size_t crossfade_mix_fade(int factor, size_t length, const char *buf,
|
|
|
|
size_t *out_sample, struct chunkdesc **out_chunk)
|
2009-11-13 21:58:41 +00:00
|
|
|
{
|
2009-11-26 14:41:31 +00:00
|
|
|
if (length == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-11-15 04:57:40 +00:00
|
|
|
const int16_t *input_buf = (const int16_t *)buf;
|
|
|
|
int16_t *output_buf = (int16_t *)((*out_chunk)->addr);
|
|
|
|
int16_t *chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
|
|
|
|
output_buf = &output_buf[*out_sample];
|
|
|
|
int32_t sample;
|
|
|
|
|
|
|
|
while (length)
|
2009-11-13 21:58:41 +00:00
|
|
|
{
|
2009-11-15 04:57:40 +00:00
|
|
|
/* fade left and right channel at once to keep buffer alignment */
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (input_buf)
|
|
|
|
/* fade the input buffer and mix into the chunk */
|
|
|
|
{
|
|
|
|
sample = *input_buf++;
|
|
|
|
sample = ((sample * factor) >> 8) + *output_buf;
|
|
|
|
*output_buf++ = clip_sample_16(sample);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* fade the chunk only */
|
|
|
|
{
|
|
|
|
sample = *output_buf;
|
|
|
|
*output_buf++ = (sample * factor) >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
|
|
|
|
|
|
|
|
/* move to next chunk as needed */
|
|
|
|
if (output_buf >= chunk_end)
|
2009-11-13 21:58:41 +00:00
|
|
|
{
|
2009-11-15 04:57:40 +00:00
|
|
|
*out_chunk = (*out_chunk)->link;
|
|
|
|
if (!(*out_chunk))
|
|
|
|
return length;
|
|
|
|
output_buf = (int16_t *)((*out_chunk)->addr);
|
|
|
|
chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
|
2009-11-13 21:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-15 04:57:40 +00:00
|
|
|
*out_sample = output_buf - (int16_t *)((*out_chunk)->addr);
|
|
|
|
return 0;
|
2009-11-13 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Initializes crossfader, calculates all necessary parameters and performs
|
|
|
|
* fade-out with the PCM buffer. */
|
2005-07-13 12:48:22 +00:00
|
|
|
static void crossfade_start(void)
|
|
|
|
{
|
2006-04-23 22:54:34 +00:00
|
|
|
size_t crossfade_rem;
|
2006-04-25 02:28:21 +00:00
|
|
|
size_t crossfade_need;
|
2006-04-23 22:54:34 +00:00
|
|
|
size_t fade_out_rem;
|
|
|
|
size_t fade_out_delay;
|
|
|
|
size_t fade_in_delay;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2009-11-09 17:11:53 +00:00
|
|
|
crossfade_track_change_started = false;
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Reject crossfade if less than .5s of data */
|
|
|
|
if (LOW_DATA(2)) {
|
2005-07-13 12:48:22 +00:00
|
|
|
logf("crossfade rejected");
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
logf("crossfade_start");
|
2009-11-11 07:02:18 +00:00
|
|
|
commit_chunk(false);
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_active = true;
|
2006-04-23 22:54:34 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Initialize the crossfade buffer size to all of the buffered data that
|
|
|
|
* has not yet been sent to the DMA */
|
2006-04-23 22:54:34 +00:00
|
|
|
crossfade_rem = pcmbuf_unplayed_bytes;
|
2009-11-09 05:45:05 +00:00
|
|
|
crossfade_chunk = read_chunk->link;
|
2006-04-24 12:41:30 +00:00
|
|
|
crossfade_sample = 0;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2009-11-12 04:24:41 +00:00
|
|
|
/* Get fade out info from settings. */
|
2009-11-16 04:42:34 +00:00
|
|
|
fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE;
|
|
|
|
fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-04-25 02:28:21 +00:00
|
|
|
crossfade_need = fade_out_delay + fade_out_rem;
|
|
|
|
if (crossfade_rem > crossfade_need)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2009-11-13 21:58:41 +00:00
|
|
|
if (crossfade_auto_skip)
|
|
|
|
/* Automatic track changes only modify the last part of the buffer,
|
|
|
|
* so find the right chunk and sample to start the crossfade */
|
|
|
|
{
|
|
|
|
crossfade_sample = find_chunk(crossfade_rem - crossfade_need,
|
2010-05-28 13:21:24 +00:00
|
|
|
&crossfade_chunk) / 2;
|
2009-11-13 21:58:41 +00:00
|
|
|
crossfade_rem = crossfade_need;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Manual skips occur immediately, but give time to process */
|
|
|
|
{
|
|
|
|
crossfade_rem -= crossfade_chunk->size;
|
|
|
|
crossfade_chunk = crossfade_chunk->link;
|
|
|
|
}
|
2006-04-23 22:54:34 +00:00
|
|
|
}
|
|
|
|
/* Truncate fade out duration if necessary. */
|
2009-11-13 21:58:41 +00:00
|
|
|
if (crossfade_rem < crossfade_need)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2006-04-25 02:28:21 +00:00
|
|
|
size_t crossfade_short = crossfade_need - crossfade_rem;
|
|
|
|
if (fade_out_rem >= crossfade_short)
|
2006-04-23 22:54:34 +00:00
|
|
|
fade_out_rem -= crossfade_short;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fade_out_delay -= crossfade_short - fade_out_rem;
|
|
|
|
fade_out_rem = 0;
|
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2009-11-13 21:58:41 +00:00
|
|
|
crossfade_rem -= fade_out_delay + fade_out_rem;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2009-11-12 04:24:41 +00:00
|
|
|
/* Completely process the crossfade fade-out effect with current PCM buffer */
|
|
|
|
if (!crossfade_mixmode)
|
|
|
|
{
|
|
|
|
/* Fade out the specified amount of the already processed audio */
|
|
|
|
size_t total_fade_out = fade_out_rem;
|
|
|
|
size_t fade_out_sample;
|
|
|
|
struct chunkdesc *fade_out_chunk = crossfade_chunk;
|
|
|
|
|
|
|
|
/* Find the right chunk and sample to start fading out */
|
|
|
|
fade_out_delay += crossfade_sample * 2;
|
2010-05-28 13:21:24 +00:00
|
|
|
fade_out_sample = find_chunk(fade_out_delay, &fade_out_chunk) / 2;
|
2009-11-12 04:24:41 +00:00
|
|
|
|
|
|
|
while (fade_out_rem > 0)
|
|
|
|
{
|
|
|
|
/* Each 1/10 second of audio will have the same fade applied */
|
2009-11-16 04:42:34 +00:00
|
|
|
size_t block_rem = MIN(BYTERATE / 10, fade_out_rem);
|
2009-11-12 04:24:41 +00:00
|
|
|
int factor = (fade_out_rem << 8) / total_fade_out;
|
|
|
|
|
|
|
|
fade_out_rem -= block_rem;
|
|
|
|
|
2009-11-15 04:57:40 +00:00
|
|
|
crossfade_mix_fade(factor, block_rem, NULL,
|
|
|
|
&fade_out_sample, &fade_out_chunk);
|
2009-11-12 04:24:41 +00:00
|
|
|
}
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-13 21:58:41 +00:00
|
|
|
/* zero out the rest of the buffer */
|
2009-11-15 04:57:40 +00:00
|
|
|
crossfade_mix_fade(0, crossfade_rem, NULL,
|
|
|
|
&fade_out_sample, &fade_out_chunk);
|
2009-11-12 04:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize fade-in counters */
|
2009-11-16 04:42:34 +00:00
|
|
|
crossfade_fade_in_total = global_settings.crossfade_fade_in_duration * BYTERATE;
|
2006-04-23 22:54:34 +00:00
|
|
|
crossfade_fade_in_rem = crossfade_fade_in_total;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2009-11-16 04:42:34 +00:00
|
|
|
fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE;
|
2006-04-23 22:54:34 +00:00
|
|
|
|
2009-11-12 04:24:41 +00:00
|
|
|
/* Find the right chunk and sample to start fading in */
|
|
|
|
fade_in_delay += crossfade_sample * 2;
|
2010-05-28 13:21:24 +00:00
|
|
|
crossfade_sample = find_chunk(fade_in_delay, &crossfade_chunk) / 2;
|
2009-11-12 04:24:41 +00:00
|
|
|
logf("crossfade_start done!");
|
2005-11-06 16:40:20 +00:00
|
|
|
}
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Perform fade-in of new track */
|
2009-11-16 04:42:34 +00:00
|
|
|
static void write_to_crossfade(size_t length)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2006-04-25 12:34:28 +00:00
|
|
|
if (length)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2009-11-16 04:42:34 +00:00
|
|
|
char *buf = fadebuf;
|
2006-04-25 12:34:28 +00:00
|
|
|
if (crossfade_fade_in_rem)
|
2006-04-25 02:28:21 +00:00
|
|
|
{
|
|
|
|
size_t samples;
|
2007-11-16 14:26:05 +00:00
|
|
|
int16_t *input_buf;
|
2006-04-25 12:34:28 +00:00
|
|
|
|
|
|
|
/* Fade factor for this packet */
|
|
|
|
int factor =
|
|
|
|
((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
|
|
|
|
crossfade_fade_in_total;
|
|
|
|
/* Bytes to fade */
|
|
|
|
size_t fade_rem = MIN(length, crossfade_fade_in_rem);
|
|
|
|
|
|
|
|
/* We _will_ fade this many bytes */
|
|
|
|
crossfade_fade_in_rem -= fade_rem;
|
|
|
|
|
|
|
|
if (crossfade_chunk)
|
|
|
|
{
|
|
|
|
/* Mix the data */
|
|
|
|
size_t fade_total = fade_rem;
|
2009-11-15 04:57:40 +00:00
|
|
|
fade_rem = crossfade_mix_fade(factor, fade_rem, buf,
|
|
|
|
&crossfade_sample, &crossfade_chunk);
|
2006-04-25 12:34:28 +00:00
|
|
|
length -= fade_total - fade_rem;
|
|
|
|
buf += fade_total - fade_rem;
|
2006-04-25 16:12:43 +00:00
|
|
|
if (!length)
|
|
|
|
return;
|
2006-04-25 12:34:28 +00:00
|
|
|
}
|
|
|
|
|
2006-04-25 02:28:21 +00:00
|
|
|
samples = fade_rem / 2;
|
2007-11-16 14:26:05 +00:00
|
|
|
input_buf = (int16_t *)buf;
|
2006-04-25 12:34:28 +00:00
|
|
|
/* Fade remaining samples in place */
|
2009-10-23 01:01:00 +00:00
|
|
|
while (samples--)
|
2006-04-25 02:28:21 +00:00
|
|
|
{
|
2007-11-16 14:26:05 +00:00
|
|
|
int32_t sample = *input_buf;
|
2006-04-25 02:28:21 +00:00
|
|
|
*input_buf++ = (sample * factor) >> 8;
|
|
|
|
}
|
2006-04-23 22:54:34 +00:00
|
|
|
}
|
|
|
|
|
2006-04-25 12:34:28 +00:00
|
|
|
if (crossfade_chunk)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2006-04-25 12:34:28 +00:00
|
|
|
/* Mix the data */
|
|
|
|
size_t mix_total = length;
|
2009-11-15 04:57:40 +00:00
|
|
|
/* A factor of 256 means mix only, no fading */
|
|
|
|
length = crossfade_mix_fade(256, length, buf,
|
|
|
|
&crossfade_sample, &crossfade_chunk);
|
2006-04-25 12:34:28 +00:00
|
|
|
buf += mix_total - length;
|
2006-04-25 16:12:43 +00:00
|
|
|
if (!length)
|
|
|
|
return;
|
2006-04-23 22:54:34 +00:00
|
|
|
}
|
2006-04-25 12:34:28 +00:00
|
|
|
|
2009-11-05 17:32:32 +00:00
|
|
|
while (length > 0)
|
|
|
|
{
|
2009-11-11 07:02:18 +00:00
|
|
|
COMMIT_IF_NEEDED;
|
2009-11-09 05:45:05 +00:00
|
|
|
size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
|
|
|
|
size_t copy_n = MIN(length, pcmbuf_size - pcmbuffer_index);
|
|
|
|
memcpy(&pcmbuffer[pcmbuffer_index], buf, copy_n);
|
2009-11-05 17:32:32 +00:00
|
|
|
buf += copy_n;
|
2009-11-09 05:45:05 +00:00
|
|
|
pcmbuffer_fillpos += copy_n;
|
2009-11-05 17:32:32 +00:00
|
|
|
length -= copy_n;
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
2009-11-11 07:02:18 +00:00
|
|
|
/* if no more fading-in to do, stop the crossfade */
|
|
|
|
if (!(crossfade_fade_in_rem || crossfade_chunk))
|
|
|
|
crossfade_active = false;
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
static void pcmbuf_finish_crossfade_enable(void)
|
|
|
|
{
|
|
|
|
/* Copy the pending setting over now */
|
2009-11-09 05:45:05 +00:00
|
|
|
crossfade_enabled = crossfade_enable_request;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
|
|
|
|
/* If crossfading, try to keep the buffer full other than 1 second */
|
2009-11-16 04:42:34 +00:00
|
|
|
(pcmbuf_size - BYTERATE) :
|
2009-11-06 04:13:36 +00:00
|
|
|
/* Otherwise, just use the default */
|
|
|
|
PCMBUF_WATERMARK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcmbuf_is_crossfade_active(void)
|
|
|
|
{
|
2009-11-09 17:11:53 +00:00
|
|
|
return crossfade_active || crossfade_track_change_started;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcmbuf_request_crossfade_enable(bool on_off)
|
|
|
|
{
|
|
|
|
/* Next setting to be used, not applied now */
|
2009-11-09 05:45:05 +00:00
|
|
|
crossfade_enable_request = on_off;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pcmbuf_is_same_size(void)
|
|
|
|
{
|
2009-11-09 17:11:53 +00:00
|
|
|
/* if pcmbuffer is NULL, then not set up yet even once so always */
|
|
|
|
bool same_size = pcmbuffer ?
|
|
|
|
(get_next_required_pcmbuf_size() == pcmbuf_size) : true;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-09 17:11:53 +00:00
|
|
|
/* no buffer change needed, so finish crossfade setup now */
|
2009-11-06 04:13:36 +00:00
|
|
|
if (same_size)
|
|
|
|
pcmbuf_finish_crossfade_enable();
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
return same_size;
|
|
|
|
}
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif /* HAVE_CROSSFADE */
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Debug menu, other metrics */
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
/* Amount of bytes left in the buffer. */
|
|
|
|
size_t pcmbuf_free(void)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
if (read_chunk != NULL)
|
2006-02-07 19:17:51 +00:00
|
|
|
{
|
2010-05-28 13:21:24 +00:00
|
|
|
void *read = (void *)read_chunk->addr;
|
2009-11-09 05:45:05 +00:00
|
|
|
void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos];
|
2009-11-06 04:13:36 +00:00
|
|
|
if (read < write)
|
|
|
|
return (size_t)(read - write) + pcmbuf_size;
|
|
|
|
else
|
|
|
|
return (size_t) (read - write);
|
2006-02-07 19:17:51 +00:00
|
|
|
}
|
2009-11-11 07:02:18 +00:00
|
|
|
return pcmbuf_size - pcmbuffer_fillpos;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
size_t pcmbuf_get_bufsize(void)
|
|
|
|
{
|
|
|
|
return pcmbuf_size;
|
|
|
|
}
|
2008-04-03 14:24:37 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
int pcmbuf_used_descs(void)
|
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
struct chunkdesc *temp = read_chunk;
|
2009-11-06 04:13:36 +00:00
|
|
|
unsigned int i = 0;
|
2009-11-09 05:45:05 +00:00
|
|
|
while (temp) {
|
|
|
|
temp = temp->link;
|
2009-11-06 04:13:36 +00:00
|
|
|
i++;
|
2008-04-03 14:24:37 +00:00
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
return i;
|
|
|
|
}
|
2006-02-13 16:55:25 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
int pcmbuf_descs(void)
|
|
|
|
{
|
2009-11-09 18:12:20 +00:00
|
|
|
return NUM_CHUNK_DESCS(pcmbuf_size);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
#ifdef ROCKBOX_HAS_LOGF
|
2009-11-11 07:02:18 +00:00
|
|
|
unsigned char *pcmbuf_get_meminfo(size_t *length)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2009-11-09 05:45:05 +00:00
|
|
|
*length = pcmbuf_bufend - pcmbuffer;
|
|
|
|
return pcmbuffer;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2009-08-11 02:05:38 +00:00
|
|
|
#endif
|
2006-04-20 02:30:59 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/** Fading and channel volume control */
|
|
|
|
|
|
|
|
/* Sync the channel amplitude to all states */
|
|
|
|
static void pcmbuf_update_volume(void)
|
|
|
|
{
|
|
|
|
unsigned int vol = fade_vol;
|
|
|
|
|
|
|
|
if (soft_mode)
|
|
|
|
vol >>= 2;
|
|
|
|
|
|
|
|
mixer_channel_set_amplitude(PCM_MIXER_CHAN_PLAYBACK, vol);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Quiet-down the channel if 'shhh' is true or else play at normal level */
|
|
|
|
void pcmbuf_soft_mode(bool shhh)
|
|
|
|
{
|
|
|
|
soft_mode = shhh;
|
|
|
|
pcmbuf_update_volume();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fade channel in or out */
|
|
|
|
void pcmbuf_fade(bool fade, bool in)
|
|
|
|
{
|
|
|
|
if (!fade)
|
|
|
|
{
|
|
|
|
/* Simply set the level */
|
|
|
|
fade_vol = in ? MIX_AMP_UNITY : MIX_AMP_MUTE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Start from the opposing end */
|
|
|
|
fade_vol = in ? MIX_AMP_MUTE : MIX_AMP_UNITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcmbuf_update_volume();
|
|
|
|
|
|
|
|
if (fade)
|
|
|
|
{
|
|
|
|
/* Do this on thread for now */
|
2011-07-06 02:21:25 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2011-06-29 06:37:04 +00:00
|
|
|
int old_prio = thread_set_priority(thread_self(), PRIORITY_REALTIME);
|
2011-07-06 02:21:25 +00:00
|
|
|
#endif
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* Linear fade actually sounds better */
|
|
|
|
if (in)
|
|
|
|
fade_vol += MIN(MIX_AMP_UNITY/16, MIX_AMP_UNITY - fade_vol);
|
|
|
|
else
|
|
|
|
fade_vol -= MIN(MIX_AMP_UNITY/16, fade_vol - MIX_AMP_MUTE);
|
|
|
|
|
|
|
|
pcmbuf_update_volume();
|
|
|
|
|
|
|
|
if (fade_vol > MIX_AMP_MUTE && fade_vol < MIX_AMP_UNITY)
|
|
|
|
{
|
|
|
|
sleep(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-06 02:21:25 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2011-06-29 06:37:04 +00:00
|
|
|
thread_set_priority(thread_self(), old_prio);
|
2011-07-06 02:21:25 +00:00
|
|
|
#endif
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Misc */
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
bool pcmbuf_is_lowdata(void)
|
|
|
|
{
|
2009-11-10 03:46:08 +00:00
|
|
|
if (!pcm_is_playing() || pcm_is_paused()
|
|
|
|
#ifdef HAVE_CROSSFADE
|
|
|
|
|| pcmbuf_is_crossfade_active()
|
|
|
|
#endif
|
|
|
|
)
|
2009-11-06 04:13:36 +00:00
|
|
|
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
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
void pcmbuf_set_low_latency(bool state)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2009-11-06 04:13:36 +00:00
|
|
|
low_latency_mode = state;
|
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
unsigned long pcmbuf_get_latency(void)
|
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
return (pcmbuf_unplayed_bytes +
|
|
|
|
mixer_channel_get_bytes_waiting(PCM_MIXER_CHAN_PLAYBACK)) * 1000 / BYTERATE;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|