2005-07-13 12:48:22 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Miika Pekkarinen
|
2011-08-28 07:45:35 +00:00
|
|
|
* Copyright (C) 2011 by Michael Sevakis
|
2005-07-13 12:48:22 +00:00
|
|
|
*
|
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"
|
2011-12-04 18:19:39 +00:00
|
|
|
#include "dsp-util.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
|
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-07-13 12:48:22 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* 2 channels * 2 bytes/sample, interleaved */
|
|
|
|
#define PCMBUF_SAMPLE_SIZE (2 * 2)
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* This is the target fill size of chunks on the pcm buffer
|
|
|
|
Can be any number of samples but power of two sizes make for faster and
|
|
|
|
smaller math - must be < 65536 bytes */
|
|
|
|
#define PCMBUF_CHUNK_SIZE 8192u
|
2011-09-30 06:46:19 +00:00
|
|
|
|
2012-03-27 23:52:15 +00:00
|
|
|
/* Small guard buf to give decent space near end */
|
|
|
|
#define PCMBUF_GUARD_SIZE (PCMBUF_CHUNK_SIZE / 8)
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
/* Mnemonics for common data commit thresholds */
|
|
|
|
#define COMMIT_CHUNKS PCMBUF_CHUNK_SIZE
|
|
|
|
#define COMMIT_ALL_DATA 1u
|
|
|
|
|
2012-03-27 23:52:15 +00:00
|
|
|
/* Size of the crossfade buffer where codec data is written to be faded
|
|
|
|
on commit */
|
|
|
|
#define CROSSFADE_BUFSIZE PCMBUF_CHUNK_SIZE
|
|
|
|
|
|
|
|
/* Maximum contiguous space that PCM buffer will allow (to avoid excessive
|
|
|
|
draining between inserts and observe low-latency mode) */
|
|
|
|
#define PCMBUF_MAX_BUFFER (PCMBUF_CHUNK_SIZE * 4)
|
|
|
|
|
|
|
|
/* Forced buffer insert constraint can thus be from 1KB to 32KB using 8KB
|
|
|
|
chunks */
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2012-01-20 23:10:12 +00:00
|
|
|
/* Return data level in 1/4-second increments */
|
2013-05-23 17:58:51 +00:00
|
|
|
#define DATA_LEVEL(quarter_secs) (pcmbuf_sampr * (quarter_secs))
|
2012-01-20 23:10:12 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Number of bytes played per second */
|
|
|
|
#define BYTERATE (pcmbuf_sampr * PCMBUF_SAMPLE_SIZE)
|
2009-11-16 04:42:34 +00:00
|
|
|
|
2009-01-10 21:10:56 +00:00
|
|
|
#if MEMORYSIZE > 2
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Keep watermark high for large memory target - at least (2s) */
|
2009-11-16 04:42:34 +00:00
|
|
|
#define PCMBUF_WATERMARK (BYTERATE * 2)
|
2011-08-28 07:45:35 +00:00
|
|
|
#define MIN_BUFFER_SIZE (BYTERATE * 3)
|
2012-01-20 23:10:12 +00:00
|
|
|
/* 1 seconds of buffer is low data */
|
|
|
|
#define LOW_DATA DATA_LEVEL(4)
|
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 */
|
2011-08-28 07:45:35 +00:00
|
|
|
#define MIN_BUFFER_SIZE (BYTERATE * 1)
|
2012-01-20 23:10:12 +00:00
|
|
|
/* under watermark is low data */
|
|
|
|
#define LOW_DATA pcmbuf_watermark
|
2009-01-10 21:10:56 +00:00
|
|
|
#endif
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Describes each audio packet - keep it small since there are many of them */
|
2009-11-09 05:45:05 +00:00
|
|
|
struct chunkdesc
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
uint16_t size; /* Actual size (0 < size <= PCMBUF_CHUNK_SIZE) */
|
2016-12-28 05:06:39 +00:00
|
|
|
uint16_t pos_key; /* Who put the position info in
|
|
|
|
(undefined: 0, valid: 1..POSITION_KEY_MAX) */
|
2011-08-28 07:45:35 +00:00
|
|
|
unsigned long elapsed; /* Elapsed time to use */
|
|
|
|
off_t offset; /* Offset to use */
|
2006-04-23 22:54:34 +00:00
|
|
|
};
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
#define POSITION_KEY_MAX UINT16_MAX
|
|
|
|
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* General PCM buffer data */
|
|
|
|
#define INVALID_BUF_INDEX ((size_t)0 - (size_t)1)
|
|
|
|
|
2012-04-27 20:51:54 +00:00
|
|
|
static void *pcmbuf_buffer;
|
|
|
|
static void *pcmbuf_guardbuf;
|
2011-08-28 07:45:35 +00:00
|
|
|
static size_t pcmbuf_size;
|
|
|
|
static struct chunkdesc *pcmbuf_descriptors;
|
|
|
|
static unsigned int pcmbuf_desc_count;
|
|
|
|
static unsigned int position_key = 1;
|
2013-05-23 17:58:51 +00:00
|
|
|
static unsigned int pcmbuf_sampr = 0;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
static size_t chunk_ridx;
|
|
|
|
static size_t chunk_widx;
|
|
|
|
|
|
|
|
static size_t pcmbuf_bytes_waiting;
|
|
|
|
static struct chunkdesc *current_desc;
|
2016-12-28 05:06:39 +00:00
|
|
|
static size_t chunk_transidx;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
2013-05-23 17:58:51 +00:00
|
|
|
static size_t pcmbuf_watermark = 0;
|
2012-01-20 23:10:12 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
static bool low_latency_mode = false;
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
static bool pcmbuf_sync_position = false;
|
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;
|
2011-08-23 01:37:59 +00:00
|
|
|
static enum
|
|
|
|
{
|
|
|
|
PCM_NOT_FADING = 0,
|
|
|
|
PCM_FADING_IN,
|
|
|
|
PCM_FADING_OUT,
|
|
|
|
} fade_state = PCM_NOT_FADING;
|
|
|
|
static bool fade_out_complete = false;
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
/* Voice */
|
|
|
|
static bool soft_mode = false;
|
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Crossfade related state */
|
2016-12-28 05:06:39 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
static int crossfade_setting;
|
|
|
|
static int crossfade_enable_request;
|
|
|
|
|
|
|
|
static enum
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
CROSSFADE_INACTIVE = 0, /* Crossfade is OFF */
|
|
|
|
CROSSFADE_ACTIVE, /* Crossfade is fading in */
|
|
|
|
CROSSFADE_START, /* New crossfade is starting */
|
|
|
|
CROSSFADE_CONTINUE, /* Next track continues fade */
|
2011-08-28 07:45:35 +00:00
|
|
|
} crossfade_status = CROSSFADE_INACTIVE;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
static bool crossfade_mixmode;
|
|
|
|
static bool crossfade_auto_skip;
|
|
|
|
static size_t crossfade_widx;
|
|
|
|
static size_t crossfade_bufidx;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
struct mixfader
|
|
|
|
{
|
|
|
|
int32_t factor; /* Current volume factor to use */
|
|
|
|
int32_t endfac; /* Saturating end factor */
|
|
|
|
int32_t nsamp2; /* Twice the number of samples */
|
|
|
|
int32_t dfact2; /* Twice the range of factors */
|
|
|
|
int32_t ferr; /* Current error accumulator */
|
|
|
|
int32_t dfquo; /* Quotient of fade range / sample range */
|
|
|
|
int32_t dfrem; /* Remainder of fade range / sample range */
|
|
|
|
int32_t dfinc; /* Base increment (-1 or +1) */
|
|
|
|
bool alloc; /* Allocate blocks if needed else abort at EOB */
|
|
|
|
} crossfade_infader;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Defines for operations on position info when mixing/fading -
|
|
|
|
passed in offset parameter */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MIXFADE_KEEP_POS = -1, /* Keep position info in chunk */
|
|
|
|
MIXFADE_NULLIFY_POS = -2, /* Ignore position info in chunk */
|
|
|
|
/* Positive values cause stamping/restamping */
|
|
|
|
};
|
2006-04-24 01:32:28 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
#define MIXFADE_UNITY_BITS 16
|
|
|
|
#define MIXFADE_UNITY (1 << MIXFADE_UNITY_BITS)
|
|
|
|
|
|
|
|
static void crossfade_cancel(void);
|
2011-08-28 07:45:35 +00:00
|
|
|
static void crossfade_start(void);
|
|
|
|
static void write_to_crossfade(size_t size, unsigned long elapsed,
|
|
|
|
off_t offset);
|
|
|
|
static void pcmbuf_finish_crossfade_enable(void);
|
2016-12-28 05:06:39 +00:00
|
|
|
#else
|
|
|
|
#define crossfade_cancel() do {} while(0)
|
2011-08-28 07:45:35 +00:00
|
|
|
#endif /* HAVE_CROSSFADE */
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Thread */
|
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
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Callbacks into playback.c */
|
2011-08-28 07:45:35 +00:00
|
|
|
extern void audio_pcmbuf_position_callback(unsigned long elapsed,
|
|
|
|
off_t offset, unsigned int key);
|
2011-04-27 03:08:23 +00:00
|
|
|
extern void audio_pcmbuf_track_change(bool pcmbuf);
|
|
|
|
extern bool audio_pcmbuf_may_play(void);
|
2011-08-28 07:45:35 +00:00
|
|
|
extern void audio_pcmbuf_sync_position(void);
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2009-11-08 04:27:27 +00:00
|
|
|
|
2009-11-05 17:32:32 +00:00
|
|
|
/**************************************/
|
|
|
|
|
2017-04-06 23:32:35 +00:00
|
|
|
/* start PCM if callback says it's alright */
|
|
|
|
static void start_audio_playback(void)
|
|
|
|
{
|
|
|
|
if (audio_pcmbuf_may_play())
|
|
|
|
pcmbuf_play_start();
|
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Return number of commited bytes in buffer (committed chunks count as
|
|
|
|
a full chunk even if only partially filled) */
|
|
|
|
static size_t pcmbuf_unplayed_bytes(void)
|
|
|
|
{
|
|
|
|
size_t ridx = chunk_ridx;
|
|
|
|
size_t widx = chunk_widx;
|
|
|
|
|
|
|
|
if (ridx > widx)
|
|
|
|
widx += pcmbuf_size;
|
2009-11-05 17:32:32 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
return widx - ridx;
|
|
|
|
}
|
|
|
|
|
2012-01-20 23:10:12 +00:00
|
|
|
/* Returns TRUE if amount of data is under the target fill size */
|
|
|
|
static bool pcmbuf_data_critical(void)
|
|
|
|
{
|
|
|
|
return pcmbuf_unplayed_bytes() < LOW_DATA;
|
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Return the next PCM chunk in the PCM buffer given a byte index into it */
|
|
|
|
static size_t index_next(size_t index)
|
|
|
|
{
|
|
|
|
index = ALIGN_DOWN(index + PCMBUF_CHUNK_SIZE, PCMBUF_CHUNK_SIZE);
|
|
|
|
|
|
|
|
if (index >= pcmbuf_size)
|
|
|
|
index -= pcmbuf_size;
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Convert a byte offset in the PCM buffer into a pointer in the buffer */
|
|
|
|
static FORCE_INLINE void * index_buffer(size_t index)
|
2009-11-05 17:32:32 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
return pcmbuf_buffer + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a pointer in the buffer into an index offset */
|
|
|
|
static FORCE_INLINE size_t buffer_index(void *p)
|
|
|
|
{
|
|
|
|
return (uintptr_t)p - (uintptr_t)pcmbuf_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a chunk descriptor for a byte index in the buffer */
|
|
|
|
static struct chunkdesc * index_chunkdesc(size_t index)
|
|
|
|
{
|
|
|
|
return &pcmbuf_descriptors[index / PCMBUF_CHUNK_SIZE];
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Return the first byte of a chunk for a byte index in the buffer, offset by 'offset'
|
2011-08-28 07:45:35 +00:00
|
|
|
chunks */
|
2016-12-28 05:06:39 +00:00
|
|
|
static size_t index_chunk_offs(size_t index, int offset)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
|
|
|
int i = index / PCMBUF_CHUNK_SIZE;
|
|
|
|
|
|
|
|
if (offset != 0)
|
2009-11-18 20:56:19 +00:00
|
|
|
{
|
2013-06-28 06:17:58 +00:00
|
|
|
i = (i + offset) % (int)pcmbuf_desc_count;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2013-06-28 06:17:17 +00:00
|
|
|
/* remainder => modulus */
|
2011-08-28 07:45:35 +00:00
|
|
|
if (i < 0)
|
|
|
|
i += pcmbuf_desc_count;
|
2009-11-18 20:56:19 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
return i * PCMBUF_CHUNK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if a buffer index lies within the committed data region */
|
|
|
|
static bool index_committed(size_t index)
|
|
|
|
{
|
|
|
|
if (index == INVALID_BUF_INDEX)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t ridx = chunk_ridx;
|
|
|
|
size_t widx = chunk_widx;
|
|
|
|
|
|
|
|
if (widx < ridx)
|
|
|
|
{
|
|
|
|
widx += pcmbuf_size;
|
|
|
|
|
|
|
|
if (index < ridx)
|
|
|
|
index += pcmbuf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index >= ridx && index < widx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Snip the tail of buffer at chunk of specified index plus chunk offset */
|
|
|
|
void snip_buffer_tail(size_t index, int offset)
|
|
|
|
{
|
|
|
|
/* Call with PCM lockout */
|
|
|
|
if (index == INVALID_BUF_INDEX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
index = index_chunk_offs(index, offset);
|
|
|
|
|
|
|
|
if (!index_committed(index) && index != chunk_widx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
chunk_widx = index;
|
|
|
|
pcmbuf_bytes_waiting = 0;
|
|
|
|
index_chunkdesc(index)->pos_key = 0;
|
|
|
|
|
|
|
|
#ifdef HAVE_CROSSFADE
|
|
|
|
/* Kill crossfade if it would now be operating in the void */
|
|
|
|
if (crossfade_status != CROSSFADE_INACTIVE &&
|
|
|
|
!index_committed(crossfade_widx) && crossfade_widx != chunk_widx)
|
|
|
|
{
|
|
|
|
crossfade_cancel();
|
|
|
|
}
|
|
|
|
#endif /* HAVE_CROSSFADE */
|
2009-11-05 17:32:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Split the uncommitted data as needed into chunks, stopping when uncommitted
|
|
|
|
data is below the threshold */
|
|
|
|
static void commit_chunks(size_t threshold)
|
2009-11-08 04:27:27 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t index = chunk_widx;
|
|
|
|
size_t end_index = index + pcmbuf_bytes_waiting;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Copy to the beginning of the buffer all data that must wrap */
|
|
|
|
if (end_index > pcmbuf_size)
|
|
|
|
memcpy(pcmbuf_buffer, pcmbuf_guardbuf, end_index - pcmbuf_size);
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
struct chunkdesc *desc = index_chunkdesc(index);
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
do
|
2009-11-09 06:53:22 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t size = MIN(pcmbuf_bytes_waiting, PCMBUF_CHUNK_SIZE);
|
|
|
|
pcmbuf_bytes_waiting -= size;
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Fill in the values in the new buffer chunk */
|
|
|
|
desc->size = (uint16_t)size;
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Advance the current write chunk and make it available to the
|
|
|
|
PCM callback */
|
|
|
|
chunk_widx = index = index_next(index);
|
|
|
|
desc = index_chunkdesc(index);
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Reset it before using it */
|
|
|
|
desc->pos_key = 0;
|
|
|
|
}
|
|
|
|
while (pcmbuf_bytes_waiting >= threshold);
|
|
|
|
}
|
2009-11-08 04:27:27 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* If uncommitted data count is above or equal to the threshold, commit it */
|
|
|
|
static FORCE_INLINE void commit_if_needed(size_t threshold)
|
|
|
|
{
|
|
|
|
if (pcmbuf_bytes_waiting >= threshold)
|
|
|
|
commit_chunks(threshold);
|
|
|
|
}
|
2009-11-08 04:27:27 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Place positioning information in the chunk */
|
|
|
|
static void stamp_chunk(struct chunkdesc *desc, unsigned long elapsed,
|
|
|
|
off_t offset)
|
|
|
|
{
|
|
|
|
/* One-time stamping of a given chunk by the same track - new track may
|
|
|
|
overwrite */
|
|
|
|
unsigned int key = position_key;
|
2009-11-08 04:27:27 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
if (desc->pos_key != key)
|
|
|
|
{
|
|
|
|
desc->pos_key = key;
|
|
|
|
desc->elapsed = elapsed;
|
|
|
|
desc->offset = offset;
|
|
|
|
}
|
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
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
static const int8_t prios[11] =
|
|
|
|
{
|
2010-12-22 16:03:15 +00:00
|
|
|
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% */
|
2011-08-28 07:45:35 +00:00
|
|
|
/* raising priority above 70% shouldn't be needed */
|
2010-12-22 16:03:15 +00:00
|
|
|
PRIORITY_PLAYBACK, /* 70 - 80% */
|
|
|
|
PRIORITY_PLAYBACK, /* 80 - 90% */
|
|
|
|
PRIORITY_PLAYBACK, /* 90 -100% */
|
|
|
|
PRIORITY_PLAYBACK, /* 100% */
|
|
|
|
};
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2010-12-22 16:03:15 +00:00
|
|
|
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 */
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Get the next available buffer and size - assumes adequate space exists */
|
|
|
|
static void * get_write_buffer(size_t *size)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Obtain current chunk fill address */
|
|
|
|
size_t index = chunk_widx + pcmbuf_bytes_waiting;
|
|
|
|
size_t index_end = pcmbuf_size + PCMBUF_GUARD_SIZE;
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Get count to the end of the buffer where a wrap will happen +
|
|
|
|
the guard */
|
|
|
|
size_t endsize = index_end - index;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Return available unwrapped space */
|
|
|
|
*size = MIN(*size, endsize);
|
|
|
|
|
|
|
|
return index_buffer(index);
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Commit outstanding data leaving less than a chunk size remaining */
|
|
|
|
static void commit_write_buffer(size_t size)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
|
|
|
/* Add this data and commit if one or more chunks are ready */
|
|
|
|
pcmbuf_bytes_waiting += size;
|
|
|
|
commit_if_needed(COMMIT_CHUNKS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Request space in the buffer for writing output samples */
|
|
|
|
void * pcmbuf_request_buffer(int *count)
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
size_t size = *count * PCMBUF_SAMPLE_SIZE;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CROSSFADE
|
|
|
|
/* We're going to crossfade to a new track, which is now on its way */
|
2016-12-28 05:06:39 +00:00
|
|
|
if (crossfade_status > CROSSFADE_ACTIVE)
|
2011-08-28 07:45:35 +00:00
|
|
|
crossfade_start();
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* If crossfade has begun, put the new track samples in the crossfade
|
|
|
|
buffer area */
|
2011-08-28 07:45:35 +00:00
|
|
|
if (crossfade_status != CROSSFADE_INACTIVE && size > CROSSFADE_BUFSIZE)
|
|
|
|
size = CROSSFADE_BUFSIZE;
|
2012-03-27 23:52:15 +00:00
|
|
|
else
|
|
|
|
#endif /* HAVE_CROSSFADE */
|
|
|
|
if (size > PCMBUF_MAX_BUFFER)
|
2016-12-28 05:06:39 +00:00
|
|
|
size = PCMBUF_MAX_BUFFER; /* constrain request */
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
enum channel_status status = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK);
|
|
|
|
size_t remaining = pcmbuf_unplayed_bytes();
|
|
|
|
|
|
|
|
/* Need to have length bytes to prevent wrapping overwriting - leave one
|
|
|
|
descriptor free to guard so that 0 != full in ring buffer */
|
|
|
|
size_t freespace = pcmbuf_free();
|
|
|
|
|
|
|
|
if (pcmbuf_sync_position)
|
|
|
|
audio_pcmbuf_sync_position();
|
|
|
|
|
|
|
|
if (freespace < size + PCMBUF_CHUNK_SIZE)
|
|
|
|
return NULL;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Maintain the buffer level above the watermark */
|
2011-08-28 07:45:35 +00:00
|
|
|
if (status != CHANNEL_STOPPED)
|
2009-11-09 05:45:05 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
if (low_latency_mode)
|
|
|
|
{
|
|
|
|
/* 1/4s latency. */
|
|
|
|
if (remaining > DATA_LEVEL(1))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Boost CPU if necessary */
|
|
|
|
size_t realrem = pcmbuf_size - freespace;
|
|
|
|
|
|
|
|
if (realrem < pcmbuf_watermark)
|
2011-07-21 22:25:09 +00:00
|
|
|
trigger_cpu_boost();
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
boost_codec_thread(realrem*10 / pcmbuf_size);
|
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 */
|
2017-04-06 23:32:35 +00:00
|
|
|
if (!pcmbuf_data_critical())
|
|
|
|
start_audio_playback();
|
2009-11-01 19:39:23 +00:00
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2012-03-27 23:52:15 +00:00
|
|
|
void *buf;
|
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2012-03-27 23:52:15 +00:00
|
|
|
if (crossfade_status != CROSSFADE_INACTIVE)
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
crossfade_bufidx = index_chunk_offs(chunk_ridx, -1);
|
|
|
|
buf = index_buffer(crossfade_bufidx); /* always CROSSFADE_BUFSIZE */
|
2012-03-27 23:52:15 +00:00
|
|
|
}
|
|
|
|
else
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2012-03-27 23:52:15 +00:00
|
|
|
{
|
|
|
|
/* Give the maximum amount available if there's more */
|
|
|
|
if (size + PCMBUF_CHUNK_SIZE < freespace)
|
|
|
|
size = freespace - PCMBUF_CHUNK_SIZE;
|
|
|
|
|
|
|
|
buf = get_write_buffer(&size);
|
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
*count = size / PCMBUF_SAMPLE_SIZE;
|
2011-08-28 07:45:35 +00:00
|
|
|
return buf;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/* Handle new samples to the buffer */
|
2011-08-28 07:45:35 +00:00
|
|
|
void pcmbuf_write_complete(int count, unsigned long elapsed, off_t offset)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
size_t size = count * PCMBUF_SAMPLE_SIZE;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2009-11-06 04:13:36 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2011-08-28 07:45:35 +00:00
|
|
|
if (crossfade_status != CROSSFADE_INACTIVE)
|
|
|
|
{
|
|
|
|
write_to_crossfade(size, elapsed, offset);
|
|
|
|
}
|
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
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
stamp_chunk(index_chunkdesc(chunk_widx), elapsed, offset);
|
|
|
|
commit_write_buffer(size);
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
/* Revert to position updates by PCM */
|
|
|
|
pcmbuf_sync_position = false;
|
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 */
|
2011-08-28 07:45:35 +00:00
|
|
|
static unsigned int get_next_required_pcmbuf_chunks(void)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t size = MIN_BUFFER_SIZE;
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2011-08-28 07:45:35 +00:00
|
|
|
if (crossfade_enable_request != CROSSFADE_ENABLE_OFF)
|
|
|
|
{
|
|
|
|
size_t seconds = global_settings.crossfade_fade_out_delay +
|
|
|
|
global_settings.crossfade_fade_out_duration;
|
|
|
|
size += seconds * BYTERATE;
|
|
|
|
}
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
logf("pcmbuf len: %lu", (unsigned long)(size / BYTERATE));
|
|
|
|
return size / PCMBUF_CHUNK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the ringbuffer state */
|
|
|
|
static void init_buffer_state(void)
|
|
|
|
{
|
|
|
|
/* Reset counters */
|
|
|
|
chunk_ridx = chunk_widx = 0;
|
|
|
|
pcmbuf_bytes_waiting = 0;
|
|
|
|
|
|
|
|
/* Reset first descriptor */
|
2016-12-28 05:06:39 +00:00
|
|
|
if (pcmbuf_descriptors)
|
|
|
|
pcmbuf_descriptors->pos_key = 0;
|
|
|
|
|
|
|
|
/* Clear change notification */
|
|
|
|
chunk_transidx = INVALID_BUF_INDEX;
|
2007-03-19 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Initialize the PCM buffer. The structure looks like this:
|
2016-12-28 05:06:39 +00:00
|
|
|
* ...|---------PCMBUF---------|GUARDBUF|DESCS| */
|
2012-04-27 20:51:54 +00:00
|
|
|
size_t pcmbuf_init(void *bufend)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2012-04-27 20:51:54 +00:00
|
|
|
void *bufstart;
|
2011-07-09 01:49:00 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Set up the buffers */
|
|
|
|
pcmbuf_desc_count = get_next_required_pcmbuf_chunks();
|
|
|
|
pcmbuf_size = pcmbuf_desc_count * PCMBUF_CHUNK_SIZE;
|
|
|
|
pcmbuf_descriptors = (struct chunkdesc *)bufend - pcmbuf_desc_count;
|
|
|
|
|
|
|
|
pcmbuf_buffer = (void *)pcmbuf_descriptors -
|
|
|
|
pcmbuf_size - PCMBUF_GUARD_SIZE;
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Mem-align buffer chunks for more efficient handling in lower layers */
|
|
|
|
pcmbuf_buffer = ALIGN_DOWN(pcmbuf_buffer, (uintptr_t)MEM_ALIGN_SIZE);
|
|
|
|
|
|
|
|
pcmbuf_guardbuf = pcmbuf_buffer + pcmbuf_size;
|
|
|
|
bufstart = pcmbuf_buffer;
|
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();
|
2013-05-23 17:58:51 +00:00
|
|
|
#else
|
|
|
|
pcmbuf_watermark = PCMBUF_WATERMARK;
|
2011-08-28 07:45:35 +00:00
|
|
|
#endif /* HAVE_CROSSFADE */
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
init_buffer_state();
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
pcmbuf_soft_mode(false);
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
return bufend - bufstart;
|
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
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Place a track change notification in a specific descriptor or post it
|
|
|
|
immediately if the buffer is empty or the index is invalid */
|
2016-12-28 05:06:39 +00:00
|
|
|
static void pcmbuf_monitor_track_change_ex(size_t index)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Call with PCM lockout */
|
2011-08-28 07:45:35 +00:00
|
|
|
if (chunk_ridx != chunk_widx && index != INVALID_BUF_INDEX)
|
2011-02-23 14:31:13 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* If monitoring, set flag for one previous to specified chunk */
|
|
|
|
index = index_chunk_offs(index, -1);
|
|
|
|
|
|
|
|
/* Ensure PCM playback hasn't already played this out */
|
|
|
|
if (index_committed(index))
|
|
|
|
{
|
|
|
|
chunk_transidx = index;
|
|
|
|
return;
|
|
|
|
}
|
2011-02-23 14:31:13 +00:00
|
|
|
}
|
2016-12-28 05:06:39 +00:00
|
|
|
|
|
|
|
/* Post now if buffer is no longer coming up */
|
|
|
|
chunk_transidx = INVALID_BUF_INDEX;
|
|
|
|
audio_pcmbuf_track_change(false);
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear end of track and optionally the positioning info for all data */
|
|
|
|
static void pcmbuf_cancel_track_change(bool position)
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Call with PCM lockout */
|
|
|
|
snip_buffer_tail(chunk_transidx, 1);
|
|
|
|
|
|
|
|
chunk_transidx = INVALID_BUF_INDEX;
|
|
|
|
|
|
|
|
if (!position)
|
|
|
|
return;
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t index = chunk_ridx;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
index_chunkdesc(index)->pos_key = 0;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
if (index == chunk_widx)
|
|
|
|
break;
|
|
|
|
|
|
|
|
index = index_next(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Place a track change notification at the end of the buffer or post it
|
|
|
|
immediately if the buffer is empty */
|
|
|
|
void pcmbuf_monitor_track_change(bool monitor)
|
|
|
|
{
|
|
|
|
pcm_play_lock();
|
|
|
|
|
|
|
|
if (monitor)
|
2016-12-28 05:06:39 +00:00
|
|
|
pcmbuf_monitor_track_change_ex(chunk_widx);
|
2011-08-28 07:45:35 +00:00
|
|
|
else
|
|
|
|
pcmbuf_cancel_track_change(false);
|
2011-02-23 14:31:13 +00:00
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
}
|
2009-11-09 18:12:20 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
void pcmbuf_start_track_change(enum pcm_track_change_type type)
|
2009-11-09 18:12:20 +00:00
|
|
|
{
|
2017-04-06 23:32:35 +00:00
|
|
|
/* Commit all outstanding data before starting next track - tracks don't
|
|
|
|
comingle inside a single buffer chunk */
|
|
|
|
commit_if_needed(COMMIT_ALL_DATA);
|
|
|
|
|
|
|
|
if (type == TRACK_CHANGE_AUTO_PILEUP)
|
|
|
|
{
|
|
|
|
/* Fill might not have been above watermark */
|
|
|
|
start_audio_playback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2011-08-28 07:45:35 +00:00
|
|
|
bool crossfade = false;
|
2009-11-10 03:46:08 +00:00
|
|
|
#endif
|
2011-08-28 07:45:35 +00:00
|
|
|
bool auto_skip = type != TRACK_CHANGE_MANUAL;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Update position key so that:
|
|
|
|
1) Positions are keyed to the track to which they belong for sync
|
|
|
|
purposes
|
2011-02-23 14:31:13 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
2) Buffers stamped with the outgoing track's positions are restamped
|
|
|
|
to the incoming track's positions when crossfading
|
|
|
|
*/
|
2016-12-28 05:06:39 +00:00
|
|
|
if (++position_key > POSITION_KEY_MAX)
|
2011-08-28 07:45:35 +00:00
|
|
|
position_key = 1;
|
2009-11-10 03:46:08 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
if (type == TRACK_CHANGE_END_OF_DATA)
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
crossfade_cancel();
|
|
|
|
|
2017-04-06 23:32:35 +00:00
|
|
|
/* Fill might not have been above watermark */
|
|
|
|
start_audio_playback();
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
2009-11-10 03:46:08 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Determine whether this track change needs to crossfaded and how */
|
2016-12-28 05:06:39 +00:00
|
|
|
else if (crossfade_setting != CROSSFADE_ENABLE_OFF)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
if (crossfade_status == CROSSFADE_INACTIVE &&
|
|
|
|
pcmbuf_unplayed_bytes() >= DATA_LEVEL(2) &&
|
|
|
|
!low_latency_mode)
|
2009-11-11 00:48:17 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
switch (crossfade_setting)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
|
|
|
|
crossfade = global_settings.playlist_shuffle || !auto_skip;
|
|
|
|
break;
|
|
|
|
case CROSSFADE_ENABLE_ALWAYS:
|
|
|
|
crossfade = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-11 00:48:17 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
2009-11-09 18:12:20 +00:00
|
|
|
|
2021-11-30 20:02:32 +00:00
|
|
|
if (auto_skip && global_settings.single_mode != SINGLE_MODE_OFF && !global_settings.party_mode)
|
2021-10-31 06:56:46 +00:00
|
|
|
crossfade = false;
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
if (crossfade)
|
|
|
|
{
|
|
|
|
logf("crossfade track change");
|
2011-02-23 14:31:13 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Don't enable mix mode when skipping tracks manually */
|
|
|
|
crossfade_mixmode = auto_skip &&
|
|
|
|
global_settings.crossfade_fade_out_mixmode;
|
2009-11-09 18:12:20 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
crossfade_auto_skip = auto_skip;
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
crossfade_status = CROSSFADE_START;
|
2011-02-23 14:31:13 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
pcmbuf_monitor_track_change(auto_skip);
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
trigger_cpu_boost();
|
2009-11-11 00:48:17 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
else
|
|
|
|
#endif /* HAVE_CROSSFADE */
|
|
|
|
if (auto_skip)
|
2016-12-28 05:06:39 +00:00
|
|
|
{
|
2009-11-11 00:48:17 +00:00
|
|
|
/* The codec is moving on to the next track, but the current track will
|
2011-08-28 07:45:35 +00:00
|
|
|
* continue to play, so mark the last write chunk as the last one in
|
|
|
|
* the track */
|
|
|
|
logf("gapless track change");
|
2016-12-28 05:06:39 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2016-12-28 05:06:39 +00:00
|
|
|
if (crossfade_status == CROSSFADE_ACTIVE)
|
|
|
|
crossfade_status = CROSSFADE_CONTINUE;
|
2011-08-28 07:45:35 +00:00
|
|
|
#endif
|
2016-12-28 05:06:39 +00:00
|
|
|
|
2011-02-23 14:31:13 +00:00
|
|
|
pcmbuf_monitor_track_change(true);
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Discard old data; caller needs no transition notification */
|
|
|
|
logf("manual track change");
|
|
|
|
pcmbuf_play_stop();
|
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
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* PCM driver callback */
|
2012-02-23 13:14:46 +00:00
|
|
|
static void pcmbuf_pcm_callback(const void **start, size_t *size)
|
2005-07-21 11:44:00 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
/*- Process the chunk that just finished -*/
|
|
|
|
size_t index = chunk_ridx;
|
|
|
|
struct chunkdesc *desc = current_desc;
|
2011-08-23 01:37:59 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
if (desc)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
/* If last chunk in the track, notify of track change */
|
2016-12-28 05:06:39 +00:00
|
|
|
if (index == chunk_transidx)
|
|
|
|
{
|
|
|
|
chunk_transidx = INVALID_BUF_INDEX;
|
2011-04-27 03:08:23 +00:00
|
|
|
audio_pcmbuf_track_change(true);
|
2016-12-28 05:06:39 +00:00
|
|
|
}
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Free it for reuse */
|
|
|
|
chunk_ridx = index = index_next(index);
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/*- Process the new one -*/
|
|
|
|
if (index != chunk_widx && !fade_out_complete)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
current_desc = desc = index_chunkdesc(index);
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
*start = index_buffer(index);
|
|
|
|
*size = desc->size;
|
2011-08-23 01:37:59 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
if (desc->pos_key != 0)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Positioning chunk - notify playback */
|
|
|
|
audio_pcmbuf_position_callback(desc->elapsed, desc->offset,
|
|
|
|
desc->pos_key);
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
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-08-28 07:45:35 +00:00
|
|
|
logf("pcmbuf_play_start");
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED &&
|
2011-08-28 07:45:35 +00:00
|
|
|
chunk_widx != chunk_ridx)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
current_desc = NULL;
|
2011-08-23 01:37:59 +00:00
|
|
|
mixer_channel_play_data(PCM_MIXER_CHAN_PLAYBACK, pcmbuf_pcm_callback,
|
2011-08-28 07:45:35 +00:00
|
|
|
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
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Stop channel, empty and reset buffer */
|
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-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
/* Reset channel */
|
2011-06-29 06:37:04 +00:00
|
|
|
mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK);
|
2009-11-06 04:13:36 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Reset buffer */
|
|
|
|
init_buffer_state();
|
|
|
|
|
|
|
|
/* Revert to position updates by PCM */
|
|
|
|
pcmbuf_sync_position = false;
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Fader OFF */
|
|
|
|
crossfade_cancel();
|
2009-11-06 04:13:36 +00:00
|
|
|
|
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
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2016-12-28 05:06:39 +00:00
|
|
|
|
|
|
|
/* Initialize a fader */
|
|
|
|
static void mixfader_init(struct mixfader *faderp, int32_t start_factor,
|
|
|
|
int32_t end_factor, size_t size, bool alloc)
|
|
|
|
{
|
|
|
|
/* Linear fade */
|
|
|
|
faderp->endfac = end_factor;
|
|
|
|
faderp->nsamp2 = size / PCMBUF_SAMPLE_SIZE * 2;
|
|
|
|
faderp->alloc = alloc;
|
|
|
|
|
|
|
|
if (faderp->nsamp2 == 0)
|
|
|
|
{
|
|
|
|
/* No data; set up as if fader finished the fade */
|
|
|
|
faderp->factor = end_factor;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t dfact2 = 2*abs(end_factor - start_factor);
|
|
|
|
faderp->factor = start_factor;
|
|
|
|
faderp->ferr = dfact2 / 2;
|
|
|
|
faderp->dfquo = dfact2 / faderp->nsamp2;
|
|
|
|
faderp->dfrem = dfact2 - faderp->dfquo*faderp->nsamp2;
|
|
|
|
faderp->dfinc = end_factor < start_factor ? -1 : +1;
|
|
|
|
faderp->dfquo *= faderp->dfinc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Query if the fader has finished its envelope */
|
|
|
|
static inline bool mixfader_finished(const struct mixfader *faderp)
|
|
|
|
{
|
|
|
|
return faderp->factor == faderp->endfac;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step fader by one sample */
|
|
|
|
static inline void mixfader_step(struct mixfader *faderp)
|
|
|
|
{
|
|
|
|
if (mixfader_finished(faderp))
|
|
|
|
return;
|
|
|
|
|
|
|
|
faderp->factor += faderp->dfquo;
|
|
|
|
faderp->ferr += faderp->dfrem;
|
|
|
|
|
|
|
|
if (faderp->ferr >= faderp->nsamp2)
|
|
|
|
{
|
|
|
|
faderp->factor += faderp->dfinc;
|
|
|
|
faderp->ferr -= faderp->nsamp2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static FORCE_INLINE int32_t mixfade_sample(const struct mixfader *faderp, int32_t s)
|
|
|
|
{
|
|
|
|
return (faderp->factor * s + MIXFADE_UNITY/2) >> MIXFADE_UNITY_BITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cancel crossfade operation */
|
|
|
|
static void crossfade_cancel(void)
|
|
|
|
{
|
|
|
|
crossfade_status = CROSSFADE_INACTIVE;
|
|
|
|
crossfade_widx = INVALID_BUF_INDEX;
|
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Find the buffer index that's 'size' bytes away from 'index' */
|
|
|
|
static size_t crossfade_find_index(size_t index, size_t size)
|
2009-11-08 04:27:27 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
if (index != INVALID_BUF_INDEX)
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
size_t i = index_chunk_offs(index, 0);
|
2011-08-28 07:45:35 +00:00
|
|
|
size += index - i;
|
|
|
|
|
|
|
|
while (i != chunk_widx)
|
|
|
|
{
|
|
|
|
size_t desc_size = index_chunkdesc(i)->size;
|
2013-06-28 06:17:17 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
if (size < desc_size)
|
2016-12-28 05:06:39 +00:00
|
|
|
{
|
|
|
|
index = i + size;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
size -= desc_size;
|
|
|
|
i = index_next(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
return index;
|
2009-11-08 04:27:27 +00:00
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Align the needed buffer area up to the end of existing data */
|
2016-12-28 05:06:39 +00:00
|
|
|
static size_t crossfade_find_buftail(bool auto_skip, size_t buffer_rem,
|
|
|
|
size_t buffer_need, size_t *buffer_rem_outp)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
size_t index = chunk_ridx;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
if (buffer_rem > buffer_need)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t distance;
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (auto_skip)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
|
|
|
/* Automatic track changes only modify the last part of the buffer,
|
|
|
|
* so find the right chunk and sample to start the crossfade */
|
|
|
|
distance = buffer_rem - buffer_need;
|
|
|
|
buffer_rem = buffer_need;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Manual skips occur immediately, but give 1/5s to process */
|
2016-12-28 05:06:39 +00:00
|
|
|
distance = MIN(BYTERATE / 5, buffer_rem);
|
|
|
|
buffer_rem -= distance;
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
index = crossfade_find_index(index, distance);
|
2006-04-23 22:54:34 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (buffer_rem_outp)
|
|
|
|
*buffer_rem_outp = buffer_rem;
|
|
|
|
|
|
|
|
return index;
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Run a fader on some buffers */
|
|
|
|
static void crossfade_mix_fade(struct mixfader *faderp, size_t size,
|
|
|
|
void *input_buf, size_t *out_index,
|
|
|
|
unsigned long elapsed, off_t offset)
|
2009-11-13 21:58:41 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
if (size == 0)
|
2016-12-28 05:06:39 +00:00
|
|
|
return;
|
2009-11-26 14:41:31 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t index = *out_index;
|
|
|
|
|
|
|
|
if (index == INVALID_BUF_INDEX)
|
2016-12-28 05:06:39 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int16_t *inbuf = input_buf;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
bool alloced = inbuf && faderp->alloc &&
|
|
|
|
index_chunk_offs(index, 0) == chunk_widx;
|
2009-11-15 04:57:40 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
while (size)
|
2009-11-13 21:58:41 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
struct chunkdesc *desc = index_chunkdesc(index);
|
2016-12-28 05:06:39 +00:00
|
|
|
int16_t *outbuf = index_buffer(index);
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
switch (offset)
|
2009-11-15 04:57:40 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
case MIXFADE_NULLIFY_POS:
|
|
|
|
/* Stop position updates for the chunk */
|
|
|
|
desc->pos_key = 0;
|
|
|
|
break;
|
|
|
|
case MIXFADE_KEEP_POS:
|
|
|
|
/* Keep position info as it is */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Replace position info */
|
|
|
|
stamp_chunk(desc, elapsed, offset);
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
size_t amount = (alloced ? PCMBUF_CHUNK_SIZE : desc->size)
|
|
|
|
- (index % PCMBUF_CHUNK_SIZE);
|
|
|
|
int16_t *chunkend = SKIPBYTES(outbuf, amount);
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (size < amount)
|
|
|
|
amount = size;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
size -= amount;
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (alloced)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Fade the input buffer into the new destination chunk */
|
|
|
|
for (size_t s = amount; s != 0; s -= PCMBUF_SAMPLE_SIZE)
|
|
|
|
{
|
|
|
|
*outbuf++ = mixfade_sample(faderp, *inbuf++);
|
|
|
|
*outbuf++ = mixfade_sample(faderp, *inbuf++);
|
|
|
|
mixfader_step(faderp);
|
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
commit_write_buffer(amount);
|
|
|
|
}
|
|
|
|
else if (inbuf)
|
|
|
|
{
|
|
|
|
/* Fade the input buffer and mix into the destination chunk */
|
|
|
|
for (size_t s = amount; s != 0; s -= PCMBUF_SAMPLE_SIZE)
|
2009-11-15 04:57:40 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
int32_t left = outbuf[0];
|
|
|
|
int32_t right = outbuf[1];
|
|
|
|
left += mixfade_sample(faderp, *inbuf++);
|
|
|
|
right += mixfade_sample(faderp, *inbuf++);
|
|
|
|
*outbuf++ = clip_sample_16(left);
|
|
|
|
*outbuf++ = clip_sample_16(right);
|
|
|
|
mixfader_step(faderp);
|
2009-11-15 04:57:40 +00:00
|
|
|
}
|
2016-12-28 05:06:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Fade the chunk in place */
|
|
|
|
for (size_t s = amount; s != 0; s -= PCMBUF_SAMPLE_SIZE)
|
2009-11-15 04:57:40 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
int32_t left = outbuf[0];
|
|
|
|
int32_t right = outbuf[1];
|
|
|
|
*outbuf++ = mixfade_sample(faderp, left);
|
|
|
|
*outbuf++ = mixfade_sample(faderp, right);
|
|
|
|
mixfader_step(faderp);
|
2009-11-15 04:57:40 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
2009-11-15 04:57:40 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (outbuf < chunkend)
|
2009-11-13 21:58:41 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
index += amount;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move destination to next chunk as needed */
|
|
|
|
index = index_next(index);
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (index == chunk_widx)
|
|
|
|
{
|
|
|
|
/* End of existing data */
|
|
|
|
if (!inbuf || !faderp->alloc)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
index = INVALID_BUF_INDEX;
|
|
|
|
break;
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
alloced = true;
|
2009-11-13 21:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
*out_index = index;
|
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)
|
|
|
|
{
|
|
|
|
logf("crossfade_start");
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
pcm_play_lock();
|
2006-04-23 22:54:34 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (crossfade_status == CROSSFADE_CONTINUE)
|
|
|
|
{
|
|
|
|
logf("fade-in continuing");
|
|
|
|
|
|
|
|
crossfade_status = CROSSFADE_ACTIVE;
|
|
|
|
|
|
|
|
if (crossfade_auto_skip)
|
|
|
|
pcmbuf_monitor_track_change_ex(crossfade_widx);
|
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 */
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t unplayed = pcmbuf_unplayed_bytes();
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Reject crossfade if less than .5s of data */
|
|
|
|
if (unplayed < DATA_LEVEL(2))
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
logf("crossfade rejected");
|
2016-12-28 05:06:39 +00:00
|
|
|
crossfade_cancel();
|
2011-08-28 07:45:35 +00:00
|
|
|
pcm_play_unlock();
|
|
|
|
return;
|
2006-04-23 22:54:34 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
/* Fading will happen */
|
|
|
|
crossfade_status = CROSSFADE_ACTIVE;
|
|
|
|
|
|
|
|
/* Get fade info from settings. */
|
|
|
|
size_t fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE;
|
|
|
|
size_t fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE;
|
|
|
|
size_t fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE;
|
|
|
|
size_t fade_in_duration = global_settings.crossfade_fade_in_duration * BYTERATE;
|
|
|
|
|
|
|
|
if (!crossfade_auto_skip)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Forego fade-in delay on manual skip - do the best to preserve auto skip
|
|
|
|
relationship */
|
2016-12-28 05:06:39 +00:00
|
|
|
fade_out_delay -= MIN(fade_out_delay, fade_in_delay);
|
2011-08-28 07:45:35 +00:00
|
|
|
fade_in_delay = 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
size_t fade_out_need = fade_out_delay + fade_out_rem;
|
|
|
|
|
2009-11-12 04:24:41 +00:00
|
|
|
if (!crossfade_mixmode)
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Completely process the crossfade fade-out effect with current PCM buffer */
|
|
|
|
size_t buffer_rem;
|
|
|
|
size_t index = crossfade_find_buftail(crossfade_auto_skip, unplayed,
|
|
|
|
fade_out_need, &buffer_rem);
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
|
|
|
|
if (buffer_rem < fade_out_need)
|
|
|
|
{
|
|
|
|
/* Existing buffers are short */
|
|
|
|
size_t fade_out_short = fade_out_need - buffer_rem;
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
if (fade_out_delay >= fade_out_short)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Truncate fade-out delay */
|
|
|
|
fade_out_delay -= fade_out_short;
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Truncate fade-out and eliminate fade-out delay */
|
|
|
|
fade_out_rem = buffer_rem;
|
|
|
|
fade_out_delay = 0;
|
2011-08-28 07:45:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
fade_out_need = fade_out_delay + fade_out_rem;
|
|
|
|
}
|
2009-11-12 04:24:41 +00:00
|
|
|
|
|
|
|
/* Find the right chunk and sample to start fading out */
|
2016-12-28 05:06:39 +00:00
|
|
|
index = crossfade_find_index(index, fade_out_delay);
|
2009-11-12 04:24:41 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Fade out the specified amount of the already processed audio */
|
|
|
|
struct mixfader outfader;
|
2009-11-12 04:24:41 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
mixfader_init(&outfader, MIXFADE_UNITY, 0, fade_out_rem, false);
|
|
|
|
crossfade_mix_fade(&outfader, fade_out_rem, NULL, &index, 0,
|
|
|
|
MIXFADE_KEEP_POS);
|
2009-11-12 04:24:41 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Zero-out the rest of the buffer */
|
|
|
|
crossfade_mix_fade(&outfader, pcmbuf_size, NULL, &index, 0,
|
|
|
|
MIXFADE_NULLIFY_POS);
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
pcm_play_lock();
|
2009-11-12 04:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize fade-in counters */
|
2016-12-28 05:06:39 +00:00
|
|
|
mixfader_init(&crossfade_infader, 0, MIXFADE_UNITY, fade_in_duration, true);
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Find the right chunk and sample to start fading in - redo from read
|
|
|
|
chunk in case original position were/was overrun in callback - the
|
|
|
|
track change event _must not_ ever fail to happen */
|
|
|
|
unplayed = pcmbuf_unplayed_bytes() + fade_in_delay;
|
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
crossfade_widx = crossfade_find_buftail(crossfade_auto_skip, unplayed,
|
|
|
|
fade_out_need, NULL);
|
2011-08-28 07:45:35 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Move track transistion to chunk before the first one of incoming track */
|
2011-08-28 07:45:35 +00:00
|
|
|
if (crossfade_auto_skip)
|
2016-12-28 05:06:39 +00:00
|
|
|
pcmbuf_monitor_track_change_ex(crossfade_widx);
|
2011-08-28 07:45:35 +00:00
|
|
|
|
|
|
|
pcm_play_unlock();
|
2006-04-23 22:54:34 +00:00
|
|
|
|
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 */
|
2011-08-28 07:45:35 +00:00
|
|
|
static void write_to_crossfade(size_t size, unsigned long elapsed, off_t offset)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
/* Mix the data */
|
|
|
|
crossfade_mix_fade(&crossfade_infader, size, index_buffer(crossfade_bufidx),
|
|
|
|
&crossfade_widx, elapsed, offset);
|
2006-04-25 12:34:28 +00:00
|
|
|
|
2016-12-28 05:06:39 +00:00
|
|
|
/* If no more fading-in to do, stop the crossfade */
|
|
|
|
if (mixfader_finished(&crossfade_infader) &&
|
|
|
|
index_chunk_offs(crossfade_widx, 0) == chunk_widx)
|
2011-08-28 07:45:35 +00:00
|
|
|
{
|
2016-12-28 05:06:39 +00:00
|
|
|
crossfade_cancel();
|
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 */
|
2011-08-28 07:45:35 +00:00
|
|
|
crossfade_setting = crossfade_enable_request;
|
2011-05-09 21:52:06 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
pcmbuf_watermark = (crossfade_setting != CROSSFADE_ENABLE_OFF && pcmbuf_size) ?
|
2009-11-06 04:13:36 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
void pcmbuf_request_crossfade_enable(int setting)
|
2009-11-06 04:13:36 +00:00
|
|
|
{
|
|
|
|
/* Next setting to be used, not applied now */
|
2011-08-28 07:45:35 +00:00
|
|
|
crossfade_enable_request = setting;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pcmbuf_is_same_size(void)
|
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
/* if pcmbuf_buffer is NULL, then not set up yet even once so always */
|
|
|
|
bool same_size = pcmbuf_buffer ?
|
|
|
|
(get_next_required_pcmbuf_chunks() == pcmbuf_desc_count) : 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
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Amount of bytes left in the buffer, accounting for uncommitted bytes */
|
2009-11-06 04:13:36 +00:00
|
|
|
size_t pcmbuf_free(void)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
return pcmbuf_size - pcmbuf_unplayed_bytes() - pcmbuf_bytes_waiting;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Data bytes allocated for buffer */
|
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
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Number of committed descriptors */
|
2009-11-06 04:13:36 +00:00
|
|
|
int pcmbuf_used_descs(void)
|
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
return pcmbuf_unplayed_bytes() / PCMBUF_CHUNK_SIZE;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
2006-02-13 16:55:25 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
/* Total number of descriptors allocated */
|
2009-11-06 04:13:36 +00:00
|
|
|
int pcmbuf_descs(void)
|
|
|
|
{
|
2011-08-28 07:45:35 +00:00
|
|
|
return pcmbuf_desc_count;
|
2009-11-06 04:13:36 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2011-08-23 01:37:59 +00:00
|
|
|
/* Tick that does the fade for the playback channel */
|
|
|
|
static void pcmbuf_fade_tick(void)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
2011-08-23 01:37:59 +00:00
|
|
|
/* ~1/3 second for full range fade */
|
|
|
|
const unsigned int fade_step = MIX_AMP_UNITY / (HZ / 3);
|
|
|
|
|
|
|
|
if (fade_state == PCM_FADING_IN)
|
|
|
|
fade_vol += MIN(fade_step, MIX_AMP_UNITY - fade_vol);
|
|
|
|
else if (fade_state == PCM_FADING_OUT)
|
|
|
|
fade_vol -= MIN(fade_step, fade_vol - MIX_AMP_MUTE);
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
pcmbuf_update_volume();
|
|
|
|
|
2011-08-23 01:37:59 +00:00
|
|
|
if (fade_vol == MIX_AMP_MUTE || fade_vol == MIX_AMP_UNITY)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
2011-08-23 01:37:59 +00:00
|
|
|
/* Fade is complete */
|
|
|
|
tick_remove_task(pcmbuf_fade_tick);
|
|
|
|
if (fade_state == PCM_FADING_OUT)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
2011-08-23 01:37:59 +00:00
|
|
|
/* Tell PCM to stop at its earliest convenience */
|
|
|
|
fade_out_complete = true;
|
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-23 01:37:59 +00:00
|
|
|
fade_state = PCM_NOT_FADING;
|
|
|
|
}
|
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-23 05:58:28 +00:00
|
|
|
/* Fade channel in or out in the background */
|
2011-08-23 01:37:59 +00:00
|
|
|
void pcmbuf_fade(bool fade, bool in)
|
|
|
|
{
|
2011-08-23 05:58:28 +00:00
|
|
|
/* Must pause any active fade */
|
2011-08-23 01:37:59 +00:00
|
|
|
pcm_play_lock();
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-23 01:37:59 +00:00
|
|
|
if (fade_state != PCM_NOT_FADING)
|
|
|
|
tick_remove_task(pcmbuf_fade_tick);
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-23 01:37:59 +00:00
|
|
|
fade_out_complete = false;
|
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
|
|
|
|
if (!fade)
|
|
|
|
{
|
|
|
|
/* Simply set the level */
|
|
|
|
fade_state = PCM_NOT_FADING;
|
|
|
|
fade_vol = in ? MIX_AMP_UNITY : MIX_AMP_MUTE;
|
|
|
|
pcmbuf_update_volume();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set direction and resume fade from current point */
|
|
|
|
fade_state = in ? PCM_FADING_IN : PCM_FADING_OUT;
|
|
|
|
tick_add_task(pcmbuf_fade_tick);
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-23 01:37:59 +00:00
|
|
|
/* Return 'true' if fade is in progress */
|
|
|
|
bool pcmbuf_fading(void)
|
|
|
|
{
|
|
|
|
return fade_state != PCM_NOT_FADING;
|
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-25 00:12:19 +00:00
|
|
|
/* Quiet-down the channel if 'shhh' is true or else play at normal level */
|
|
|
|
void pcmbuf_soft_mode(bool shhh)
|
|
|
|
{
|
|
|
|
/* Have to block the tick or improper order could leave volume in soft
|
|
|
|
mode if fading reads the old value first but updates after us. */
|
|
|
|
int res = fade_state != PCM_NOT_FADING ?
|
|
|
|
tick_remove_task(pcmbuf_fade_tick) : -1;
|
|
|
|
|
|
|
|
soft_mode = shhh;
|
|
|
|
pcmbuf_update_volume();
|
|
|
|
|
|
|
|
if (res == 0)
|
|
|
|
tick_add_task(pcmbuf_fade_tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-20 05:47:21 +00:00
|
|
|
/** Time and position */
|
|
|
|
|
|
|
|
/* Return the current position key value */
|
|
|
|
unsigned int pcmbuf_get_position_key(void)
|
|
|
|
{
|
|
|
|
return position_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set position updates to be synchronous and immediate in addition to during
|
|
|
|
PCM frames - cancelled upon first codec insert or upon stopping */
|
|
|
|
void pcmbuf_sync_position_update(void)
|
|
|
|
{
|
|
|
|
pcmbuf_sync_position = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-11 07:02:18 +00:00
|
|
|
/** Misc */
|
2009-11-06 04:13:36 +00:00
|
|
|
|
|
|
|
bool pcmbuf_is_lowdata(void)
|
|
|
|
{
|
2011-08-28 14:26:59 +00:00
|
|
|
enum channel_status status = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK);
|
|
|
|
|
2017-01-05 08:22:47 +00:00
|
|
|
if (status != CHANNEL_PLAYING)
|
2009-11-06 04:13:36 +00:00
|
|
|
return false;
|
|
|
|
|
2017-01-05 08:22:47 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
|
|
|
if (crossfade_status != CROSSFADE_INACTIVE)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2012-01-20 23:10:12 +00:00
|
|
|
return pcmbuf_data_critical();
|
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;
|
|
|
|
}
|
2013-05-23 17:58:51 +00:00
|
|
|
|
|
|
|
void pcmbuf_update_frequency(void)
|
|
|
|
{
|
|
|
|
pcmbuf_sampr = mixer_get_frequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int pcmbuf_get_frequency(void)
|
|
|
|
{
|
|
|
|
return pcmbuf_sampr;
|
|
|
|
}
|