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>
|
|
|
|
#include "pcmbuf.h"
|
2007-10-06 22:27:27 +00:00
|
|
|
#include "pcm.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"
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
#include "cpu.h"
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include "buffer.h"
|
2005-07-21 11:44:00 +00:00
|
|
|
#include "settings.h"
|
|
|
|
#include "audio.h"
|
2007-11-18 17:12:19 +00:00
|
|
|
#include "voice_thread.h"
|
2005-08-20 11:13:19 +00:00
|
|
|
#include "dsp.h"
|
2006-09-16 16:18:11 +00:00
|
|
|
#include "thread.h"
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2007-11-16 14:26:05 +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-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) */
|
2006-10-13 20:48:23 +00:00
|
|
|
#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
|
2009-01-10 21:10:56 +00:00
|
|
|
#else
|
|
|
|
#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
|
|
|
|
#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. */
|
|
|
|
struct pcmbufdesc
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
|
|
|
struct pcmbufdesc* link;
|
|
|
|
/* Call this when the buffer has been played */
|
|
|
|
void (*callback)(void);
|
|
|
|
};
|
|
|
|
|
2007-03-19 22:04:17 +00:00
|
|
|
#define PCMBUF_DESCS(bufsize) \
|
|
|
|
((bufsize) / PCMBUF_MINAVG_CHUNK)
|
|
|
|
#define PCMBUF_DESCS_SIZE(bufsize) \
|
|
|
|
(PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Size of the PCM buffer. */
|
|
|
|
static size_t pcmbuf_size IDATA_ATTR = 0;
|
2006-11-06 18:07:30 +00:00
|
|
|
static char *pcmbuf_bufend IDATA_ATTR;
|
2006-02-07 20:38:55 +00:00
|
|
|
static char *audiobuffer IDATA_ATTR;
|
|
|
|
/* Current audio buffer write index. */
|
|
|
|
static size_t audiobuffer_pos IDATA_ATTR;
|
|
|
|
/* Amount audiobuffer_pos will be increased.*/
|
|
|
|
static size_t audiobuffer_fillpos IDATA_ATTR;
|
2006-04-22 14:40:13 +00:00
|
|
|
static char *fadebuf IDATA_ATTR;
|
|
|
|
static char *voicebuf IDATA_ATTR;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
|
2008-03-28 12:51:33 +00:00
|
|
|
static void (*position_callback)(size_t size) IDATA_ATTR;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Crossfade related state */
|
|
|
|
static bool crossfade_enabled;
|
2007-03-19 22:04:17 +00:00
|
|
|
static bool crossfade_enabled_pending;
|
2006-05-14 14:08:26 +00:00
|
|
|
static bool crossfade_mixmode;
|
2006-02-07 20:38:55 +00:00
|
|
|
static bool crossfade_active IDATA_ATTR;
|
|
|
|
static bool crossfade_init IDATA_ATTR;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Track the current location for processing crossfade */
|
|
|
|
static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
|
2009-08-11 02:05:38 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2006-04-23 22:54:34 +00:00
|
|
|
static size_t crossfade_sample IDATA_ATTR;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Counters for fading in new data */
|
|
|
|
static size_t crossfade_fade_in_total IDATA_ATTR;
|
2006-02-07 20:38:55 +00:00
|
|
|
static size_t crossfade_fade_in_rem IDATA_ATTR;
|
2009-08-11 02:05:38 +00:00
|
|
|
#endif
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
|
|
|
|
static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
|
|
|
|
static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
|
|
|
|
static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
|
|
|
|
static size_t last_chunksize IDATA_ATTR;
|
2006-04-24 01:32:28 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
|
|
|
|
static size_t pcmbuf_watermark IDATA_ATTR;
|
2006-04-24 01:32:28 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
|
|
|
|
static size_t pcmbuf_mix_sample IDATA_ATTR;
|
2006-04-24 01:32:28 +00:00
|
|
|
|
2006-02-07 19:17:51 +00:00
|
|
|
static bool low_latency_mode = false;
|
2006-04-23 22:54:34 +00:00
|
|
|
static bool pcmbuf_flush;
|
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
|
|
|
|
2008-12-10 09:11:43 +00:00
|
|
|
extern unsigned int codec_thread_id;
|
2006-09-16 16:18:11 +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 */
|
|
|
|
#define NEED_FLUSH(position) \
|
|
|
|
(audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
|
|
|
|
#define LOW_DATA(quarter_secs) \
|
|
|
|
(pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
|
|
|
|
|
2006-04-25 12:34:28 +00:00
|
|
|
static bool prepare_insert(size_t length);
|
2009-01-10 21:10:56 +00:00
|
|
|
static void pcmbuf_under_watermark(bool under);
|
2006-04-20 02:30:59 +00:00
|
|
|
static bool pcmbuf_flush_fillpos(void);
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
#define CALL_IF_EXISTS(function, args...) if (function) function(args)
|
2009-11-01 19:39:23 +00:00
|
|
|
/* This function has 3 major logical parts (separated by brackets both for
|
2006-02-07 20:38:55 +00:00
|
|
|
* readability and variable scoping). The first part performs the
|
2009-11-01 19:39:23 +00:00
|
|
|
* operations related to finishing off the last buffer 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.
|
|
|
|
* The third part performs the operations involved in sending a new buffer
|
|
|
|
* to the DMA. */
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
|
|
|
|
static void pcmbuf_callback(unsigned char** start, size_t* size)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
|
|
|
|
/* Take the finished buffer out of circulation */
|
|
|
|
pcmbuf_read = pcmbuf_current->link;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* The buffer is finished, call the callback functions */
|
|
|
|
CALL_IF_EXISTS(position_callback, last_chunksize);
|
2006-02-07 20:38:55 +00:00
|
|
|
CALL_IF_EXISTS(pcmbuf_current->callback);
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Put the finished buffer back into circulation */
|
|
|
|
pcmbuf_write_end->link = pcmbuf_current;
|
|
|
|
pcmbuf_write_end = pcmbuf_current;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-04-25 16:12:43 +00:00
|
|
|
/* If we've read over the mix chunk while it's still mixing there */
|
2006-04-22 14:40:13 +00:00
|
|
|
if (pcmbuf_current == pcmbuf_mix_chunk)
|
|
|
|
pcmbuf_mix_chunk = NULL;
|
2006-04-25 16:12:43 +00:00
|
|
|
/* If we've read over the crossfade chunk while it's still fading */
|
2006-04-25 02:28:21 +00:00
|
|
|
if (pcmbuf_current == crossfade_chunk)
|
2006-04-25 16:15:11 +00:00
|
|
|
crossfade_chunk = pcmbuf_read;
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
2009-11-01 19:39:23 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
/* Commit last samples at end of playlist */
|
|
|
|
if (audiobuffer_fillpos && !pcmbuf_read)
|
|
|
|
{
|
|
|
|
logf("pcmbuf callback: commit last samples");
|
|
|
|
pcmbuf_flush_fillpos();
|
|
|
|
}
|
|
|
|
}
|
2006-01-21 22:42:44 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Send the new buffer to the pcm */
|
|
|
|
struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
|
|
|
|
size_t *realsize = size;
|
|
|
|
unsigned char** realstart = start;
|
|
|
|
if(pcmbuf_new)
|
|
|
|
{
|
|
|
|
size_t current_size = pcmbuf_new->size;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_unplayed_bytes -= current_size;
|
|
|
|
last_chunksize = current_size;
|
2006-04-22 14:40:13 +00:00
|
|
|
*realsize = current_size;
|
2006-02-07 20:38:55 +00:00
|
|
|
*realstart = pcmbuf_new->addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No more buffers */
|
|
|
|
last_chunksize = 0;
|
|
|
|
*realsize = 0;
|
|
|
|
*realstart = NULL;
|
|
|
|
CALL_IF_EXISTS(pcmbuf_event_handler);
|
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
void pcmbuf_set_position_callback(void (*callback)(size_t size))
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
2006-01-21 22:42:44 +00:00
|
|
|
position_callback = callback;
|
|
|
|
}
|
|
|
|
|
2007-03-19 22:04:17 +00:00
|
|
|
static void pcmbuf_set_watermark_bytes(void)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2007-03-19 22:04:17 +00:00
|
|
|
pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
|
|
|
|
/* If crossfading, try to keep the buffer full other than 1 second */
|
|
|
|
(pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
|
2009-01-10 21:10:56 +00:00
|
|
|
/* Otherwise, just use the default */
|
2007-03-19 22:04:17 +00:00
|
|
|
PCMBUF_WATERMARK;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
|
|
|
|
* in a separate function for the moment */
|
|
|
|
static inline void pcmbuf_add_chunk(void)
|
|
|
|
{
|
|
|
|
register size_t size = audiobuffer_fillpos;
|
|
|
|
/* Grab the next description to write, and change the write pointer */
|
|
|
|
register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
|
|
|
|
pcmbuf_write = pcmbuf_current->link;
|
|
|
|
/* Fill in the values in the new buffer chunk */
|
|
|
|
pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
|
|
|
|
pcmbuf_current->size = size;
|
|
|
|
pcmbuf_current->callback = pcmbuf_event_handler;
|
|
|
|
pcmbuf_current->link = NULL;
|
|
|
|
/* This is single use only */
|
|
|
|
pcmbuf_event_handler = NULL;
|
2006-09-17 08:34:42 +00:00
|
|
|
if (pcmbuf_read != NULL) {
|
2006-04-23 22:54:34 +00:00
|
|
|
if (pcmbuf_flush)
|
|
|
|
{
|
|
|
|
pcmbuf_write_end->link = pcmbuf_read->link;
|
|
|
|
pcmbuf_read->link = pcmbuf_current;
|
|
|
|
while (pcmbuf_write_end->link)
|
|
|
|
{
|
|
|
|
pcmbuf_write_end = pcmbuf_write_end->link;
|
|
|
|
pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
|
|
|
|
}
|
|
|
|
pcmbuf_flush = false;
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
/* If there is already a read buffer setup, add to it */
|
2006-04-23 22:54:34 +00:00
|
|
|
else
|
|
|
|
pcmbuf_read_end->link = pcmbuf_current;
|
2006-02-07 20:38:55 +00:00
|
|
|
} else {
|
|
|
|
/* Otherwise create the buffer */
|
|
|
|
pcmbuf_read = pcmbuf_current;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
/* This is now the last buffer to read */
|
|
|
|
pcmbuf_read_end = pcmbuf_current;
|
|
|
|
|
|
|
|
/* Update bytes counters */
|
|
|
|
pcmbuf_unplayed_bytes += size;
|
|
|
|
|
|
|
|
audiobuffer_pos += size;
|
|
|
|
if (audiobuffer_pos >= pcmbuf_size)
|
2006-04-22 14:40:13 +00:00
|
|
|
audiobuffer_pos -= pcmbuf_size;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
audiobuffer_fillpos = 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-10-15 11:57:52 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2007-03-06 20:32:13 +00:00
|
|
|
static void boost_codec_thread(bool boost)
|
|
|
|
{
|
2007-11-18 17:12:19 +00:00
|
|
|
/* Keep voice and codec threads at the same priority or else voice
|
|
|
|
* will starve if the codec thread's priority is boosted. */
|
2007-03-06 20:32:13 +00:00
|
|
|
if (boost)
|
2006-10-15 11:57:52 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
|
|
|
|
/ (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
|
|
|
|
|
|
|
|
if (priority != codec_thread_priority)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
codec_thread_priority = priority;
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_set_priority(codec_thread_id, priority);
|
2008-03-25 02:34:12 +00:00
|
|
|
voice_thread_set_priority(priority);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
2006-10-15 11:57:52 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
else if (codec_thread_priority != PRIORITY_PLAYBACK)
|
2006-10-15 11:57:52 +00:00
|
|
|
{
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
|
2008-03-25 02:34:12 +00:00
|
|
|
voice_thread_set_priority(PRIORITY_PLAYBACK);
|
|
|
|
codec_thread_priority = PRIORITY_PLAYBACK;
|
2006-10-15 11:57:52 +00:00
|
|
|
}
|
2007-03-06 20:32:13 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
2006-10-15 11:57:52 +00:00
|
|
|
|
2009-01-10 21:10:56 +00:00
|
|
|
static void pcmbuf_under_watermark(bool under)
|
2007-03-06 20:32:13 +00:00
|
|
|
{
|
|
|
|
/* Only codec thread initiates boost - voice boosts the cpu when playing
|
|
|
|
a clip */
|
2007-03-06 21:00:45 +00:00
|
|
|
#ifndef SIMULATOR
|
2008-12-10 08:57:10 +00:00
|
|
|
if (thread_get_current() == codec_thread_id)
|
2007-03-06 21:00:45 +00:00
|
|
|
#endif /* SIMULATOR */
|
2007-03-06 20:32:13 +00:00
|
|
|
{
|
2009-01-10 21:10:56 +00:00
|
|
|
if (under)
|
|
|
|
{
|
2009-02-16 18:45:43 +00:00
|
|
|
/* Fill audio buffer by boosting cpu */
|
|
|
|
trigger_cpu_boost();
|
2007-03-06 20:32:13 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2009-01-10 21:10:56 +00:00
|
|
|
/* If buffer is critically low, override UI priority, else
|
|
|
|
set back to the original priority. */
|
|
|
|
boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
|
2007-03-06 20:32:13 +00:00
|
|
|
#endif
|
2009-01-10 21:10:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-10 21:38:56 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2009-01-10 21:10:56 +00:00
|
|
|
boost_codec_thread(false);
|
2009-01-10 21:38:56 +00:00
|
|
|
#endif
|
2009-01-10 21:10:56 +00:00
|
|
|
}
|
2007-03-06 20:32:13 +00:00
|
|
|
}
|
2006-10-15 11:57:52 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Disable crossfade if < .5s of audio */
|
2006-04-23 22:54:34 +00:00
|
|
|
if (LOW_DATA(2))
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_active = false;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
void pcmbuf_set_event_handler(void (*event_handler)(void))
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
pcmbuf_event_handler = event_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int pcmbuf_get_latency(void)
|
|
|
|
{
|
2008-01-08 14:33:17 +00:00
|
|
|
/* Be careful how this calculation is rearranged, it's easy to overflow */
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
|
|
|
|
return bytes / 4 / (NATIVE_FREQUENCY/1000);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 19:17:51 +00:00
|
|
|
void pcmbuf_set_low_latency(bool state)
|
|
|
|
{
|
|
|
|
low_latency_mode = state;
|
|
|
|
}
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
bool pcmbuf_is_lowdata(void)
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
if (!pcm_is_playing() || pcm_is_paused() ||
|
|
|
|
crossfade_init || crossfade_active)
|
2005-07-13 12:48:22 +00:00
|
|
|
return false;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2009-01-10 21:10:56 +00:00
|
|
|
#if MEMORYSIZE > 2
|
2006-09-16 16:18:11 +00:00
|
|
|
/* 1 seconds of buffer is low data */
|
|
|
|
return LOW_DATA(4);
|
2009-01-10 21:10:56 +00:00
|
|
|
#else
|
|
|
|
/* under watermark is low data */
|
|
|
|
return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
|
|
|
|
#endif
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Amount of bytes left in the buffer. */
|
|
|
|
inline size_t pcmbuf_free(void)
|
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
if (pcmbuf_read != NULL)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
void *read = pcmbuf_read->addr;
|
|
|
|
void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
|
2006-04-23 22:54:34 +00:00
|
|
|
if (read < write)
|
2006-09-17 08:34:42 +00:00
|
|
|
return (size_t)(read - write) + pcmbuf_size;
|
|
|
|
else
|
|
|
|
return (size_t) (read - write);
|
2006-04-23 22:54:34 +00:00
|
|
|
}
|
|
|
|
return pcmbuf_size;
|
|
|
|
}
|
|
|
|
|
2006-01-27 16:25:44 +00:00
|
|
|
bool pcmbuf_crossfade_init(bool manual_skip)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Can't do two crossfades at once and, no fade if pcm is off now */
|
|
|
|
if (crossfade_init || crossfade_active || !pcm_is_playing())
|
|
|
|
{
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-15 11:57:52 +00:00
|
|
|
trigger_cpu_boost();
|
2008-04-03 14:24:37 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Not enough data, or crossfade disabled, flush the old data instead */
|
2006-08-02 18:35:24 +00:00
|
|
|
if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
2006-05-05 03:13:06 +00:00
|
|
|
pcmbuf_flush_fillpos();
|
2006-04-23 22:54:34 +00:00
|
|
|
pcmbuf_flush = true;
|
2005-07-13 12:48:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-04-23 22:54:34 +00:00
|
|
|
|
2006-01-27 16:25:44 +00:00
|
|
|
/* Don't enable mix mode when skipping tracks manually. */
|
2006-05-14 14:08:26 +00:00
|
|
|
if (manual_skip)
|
|
|
|
crossfade_mixmode = false;
|
|
|
|
else
|
|
|
|
crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
|
2008-04-03 14:24:37 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_init = true;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
return true;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcmbuf_play_stop(void)
|
|
|
|
{
|
|
|
|
pcm_play_stop();
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_unplayed_bytes = 0;
|
2006-04-22 14:40:13 +00:00
|
|
|
pcmbuf_mix_chunk = NULL;
|
2006-02-07 20:38:55 +00:00
|
|
|
if (pcmbuf_read) {
|
|
|
|
pcmbuf_write_end->link = pcmbuf_read;
|
|
|
|
pcmbuf_write_end = pcmbuf_read_end;
|
|
|
|
pcmbuf_read = pcmbuf_read_end = NULL;
|
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
audiobuffer_pos = 0;
|
|
|
|
audiobuffer_fillpos = 0;
|
|
|
|
crossfade_init = false;
|
|
|
|
crossfade_active = false;
|
2006-04-23 22:54:34 +00:00
|
|
|
pcmbuf_flush = false;
|
2007-03-06 20:32:13 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
/* Can unboost the codec thread here no matter who's calling */
|
|
|
|
boost_codec_thread(false);
|
|
|
|
#endif
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 12:04:52 +00:00
|
|
|
int pcmbuf_used_descs(void)
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
|
|
|
|
unsigned int i = 0;
|
|
|
|
while (pcmbuf_temp) {
|
|
|
|
pcmbuf_temp = pcmbuf_temp->link;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-12-03 12:04:52 +00:00
|
|
|
int pcmbuf_descs(void)
|
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
return PCMBUF_DESCS(pcmbuf_size);
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 12:04:52 +00:00
|
|
|
static void pcmbuf_init_pcmbuffers(void)
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
struct pcmbufdesc *next = pcmbuf_write;
|
|
|
|
next++;
|
|
|
|
pcmbuf_write_end = pcmbuf_write;
|
2006-11-06 18:07:30 +00:00
|
|
|
while ((void *)next < (void *)pcmbuf_bufend) {
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_write_end->link=next;
|
|
|
|
pcmbuf_write_end=next;
|
|
|
|
next++;
|
|
|
|
}
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2007-03-19 22:04:17 +00:00
|
|
|
static size_t pcmbuf_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;
|
|
|
|
|
|
|
|
if (crossfade_enabled_pending)
|
|
|
|
seconds += global_settings.crossfade_fade_out_delay
|
|
|
|
+ global_settings.crossfade_fade_out_duration;
|
|
|
|
|
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-01-10 21:10:56 +00:00
|
|
|
return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
|
2007-03-19 22:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
|
|
|
|
{
|
|
|
|
return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
|
|
|
|
PCMBUF_DESCS_SIZE(bufsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcmbuf_is_same_size(void)
|
|
|
|
{
|
|
|
|
if (audiobuffer == NULL)
|
|
|
|
return true; /* Not set up yet even once so always */
|
|
|
|
|
|
|
|
size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
|
|
|
|
return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
|
2006-11-06 18:07:30 +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
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
pcmbuf_bufend = bufend;
|
2007-03-19 22:04:17 +00:00
|
|
|
pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
|
|
|
|
audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
|
2006-04-22 14:40:13 +00:00
|
|
|
fadebuf = &audiobuffer[pcmbuf_size];
|
|
|
|
voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
|
2006-11-06 18:07:30 +00:00
|
|
|
pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_init_pcmbuffers();
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2006-01-22 00:03:20 +00:00
|
|
|
position_callback = NULL;
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_event_handler = NULL;
|
2007-03-19 22:04:17 +00:00
|
|
|
|
|
|
|
pcmbuf_crossfade_enable_finished();
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_play_stop();
|
2007-03-19 22:04:17 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
return pcmbuf_bufend - audiobuffer;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t pcmbuf_get_bufsize(void)
|
2005-07-21 11:44:00 +00:00
|
|
|
{
|
|
|
|
return pcmbuf_size;
|
|
|
|
}
|
|
|
|
|
2007-03-19 22:04:17 +00:00
|
|
|
#ifdef ROCKBOX_HAS_LOGF
|
|
|
|
unsigned char * pcmbuf_get_meminfo(size_t *length)
|
|
|
|
{
|
|
|
|
*length = pcmbuf_bufend - audiobuffer;
|
|
|
|
return audiobuffer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-24 20:36:58 +00:00
|
|
|
void pcmbuf_pause(bool pause)
|
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
if (pcm_is_playing())
|
|
|
|
pcm_play_pause(!pause);
|
|
|
|
else if (!pause)
|
|
|
|
pcmbuf_play_start();
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
/* Force playback. */
|
|
|
|
void pcmbuf_play_start(void)
|
|
|
|
{
|
2007-06-24 20:36:58 +00:00
|
|
|
if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
|
2006-01-22 10:25:07 +00:00
|
|
|
{
|
2007-06-24 20:36:58 +00:00
|
|
|
last_chunksize = pcmbuf_read->size;
|
|
|
|
pcmbuf_unplayed_bytes -= last_chunksize;
|
|
|
|
pcm_play_data(pcmbuf_callback,
|
|
|
|
(unsigned char *)pcmbuf_read->addr, last_chunksize);
|
2006-01-22 10:25:07 +00:00
|
|
|
}
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2005-11-06 16:40:20 +00:00
|
|
|
/**
|
|
|
|
* Commit samples waiting to the pcm buffer.
|
|
|
|
*/
|
2006-04-20 02:30:59 +00:00
|
|
|
static bool pcmbuf_flush_fillpos(void)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
if (audiobuffer_fillpos) {
|
|
|
|
/* Never use the last buffer descriptor */
|
|
|
|
while (pcmbuf_write == pcmbuf_write_end) {
|
2006-03-24 13:43:15 +00:00
|
|
|
/* If this happens, something is being stupid */
|
2005-07-13 12:48:22 +00:00
|
|
|
if (!pcm_is_playing()) {
|
2006-02-07 20:38:55 +00:00
|
|
|
logf("pcmbuf_flush_fillpos error");
|
2006-01-22 10:25:07 +00:00
|
|
|
pcmbuf_play_start();
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-03-24 13:43:15 +00:00
|
|
|
/* Let approximately one chunk of data playback */
|
2006-09-16 16:18:11 +00:00
|
|
|
sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_add_chunk();
|
2006-04-20 02:30:59 +00:00
|
|
|
return true;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-04-20 02:30:59 +00:00
|
|
|
return false;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2009-08-11 02:05:38 +00:00
|
|
|
/**
|
|
|
|
* Low memory targets don't have crossfade, so don't compile crossfade
|
|
|
|
* specific code in order to save some memory. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CROSSFADE
|
2005-11-06 16:40:20 +00:00
|
|
|
/**
|
|
|
|
* Completely process the crossfade fade out effect with current pcm buffer.
|
|
|
|
*/
|
2006-03-30 00:47:05 +00:00
|
|
|
static void crossfade_process_buffer(size_t fade_in_delay,
|
|
|
|
size_t fade_out_delay, size_t fade_out_rem)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2006-05-14 14:08:26 +00:00
|
|
|
if (!crossfade_mixmode)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2006-03-30 00:47:05 +00:00
|
|
|
/* Fade out the specified amount of the already processed audio */
|
|
|
|
size_t total_fade_out = fade_out_rem;
|
2006-04-23 22:54:34 +00:00
|
|
|
size_t fade_out_sample;
|
|
|
|
struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Find the right chunk to start fading out */
|
2006-04-24 03:43:43 +00:00
|
|
|
fade_out_delay += crossfade_sample * 2;
|
2006-09-27 15:23:10 +00:00
|
|
|
while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
|
|
|
fade_out_delay -= fade_out_chunk->size;
|
|
|
|
fade_out_chunk = fade_out_chunk->link;
|
|
|
|
}
|
|
|
|
/* The start sample within the chunk */
|
|
|
|
fade_out_sample = fade_out_delay / 2;
|
2008-04-03 14:24:37 +00:00
|
|
|
|
2006-03-30 00:47:05 +00:00
|
|
|
while (fade_out_rem > 0)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2006-03-30 00:47:05 +00:00
|
|
|
/* Each 1/10 second of audio will have the same fade applied */
|
2006-04-25 10:39:56 +00:00
|
|
|
size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
|
2006-04-23 22:54:34 +00:00
|
|
|
int factor = (fade_out_rem << 8) / total_fade_out;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-03-30 00:47:05 +00:00
|
|
|
fade_out_rem -= block_rem;
|
|
|
|
|
|
|
|
/* Fade this block */
|
2006-09-17 08:34:42 +00:00
|
|
|
while (block_rem > 0 && fade_out_chunk != NULL)
|
2006-03-30 00:47:05 +00:00
|
|
|
{
|
|
|
|
/* Fade one sample */
|
2007-11-16 14:26:05 +00:00
|
|
|
int16_t *buf = (int16_t *)fade_out_chunk->addr;
|
|
|
|
int32_t sample = buf[fade_out_sample];
|
2006-04-23 22:54:34 +00:00
|
|
|
buf[fade_out_sample++] = (sample * factor) >> 8;
|
2006-03-30 00:47:05 +00:00
|
|
|
|
2006-04-25 10:39:56 +00:00
|
|
|
block_rem -= 2;
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Move to the next chunk as needed */
|
|
|
|
if (fade_out_sample * 2 >= fade_out_chunk->size)
|
2006-03-30 00:47:05 +00:00
|
|
|
{
|
2006-04-23 22:54:34 +00:00
|
|
|
fade_out_chunk = fade_out_chunk->link;
|
|
|
|
fade_out_sample = 0;
|
2006-03-30 00:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
2005-11-06 16:40:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Find the right chunk and sample to start fading in */
|
2006-04-24 03:43:43 +00:00
|
|
|
fade_in_delay += crossfade_sample * 2;
|
2006-09-27 15:23:10 +00:00
|
|
|
while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
|
2006-04-23 22:54:34 +00:00
|
|
|
{
|
|
|
|
fade_in_delay -= crossfade_chunk->size;
|
|
|
|
crossfade_chunk = crossfade_chunk->link;
|
|
|
|
}
|
|
|
|
crossfade_sample = fade_in_delay / 2;
|
2005-11-06 16:40:20 +00:00
|
|
|
logf("process done!");
|
|
|
|
}
|
|
|
|
|
2006-04-23 22:54:34 +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
|
|
|
|
2006-04-20 02:30:59 +00:00
|
|
|
crossfade_init = 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");
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_flush_fillpos();
|
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;
|
|
|
|
crossfade_chunk = pcmbuf_read->link;
|
2006-04-24 12:41:30 +00:00
|
|
|
crossfade_sample = 0;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Get fade out delay from settings. */
|
|
|
|
fade_out_delay =
|
|
|
|
NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Get fade out duration from settings. */
|
|
|
|
fade_out_rem =
|
|
|
|
NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-04-25 02:28:21 +00:00
|
|
|
crossfade_need = fade_out_delay + fade_out_rem;
|
2006-04-23 22:54:34 +00:00
|
|
|
/* We want only to modify the last part of the buffer. */
|
2006-04-25 02:28:21 +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_extra = crossfade_rem - crossfade_need;
|
2006-04-23 22:54:34 +00:00
|
|
|
while (crossfade_extra > crossfade_chunk->size)
|
|
|
|
{
|
|
|
|
crossfade_extra -= crossfade_chunk->size;
|
|
|
|
crossfade_chunk = crossfade_chunk->link;
|
|
|
|
}
|
|
|
|
crossfade_sample = crossfade_extra / 2;
|
|
|
|
}
|
|
|
|
/* Truncate fade out duration if necessary. */
|
2006-04-25 02:28:21 +00:00
|
|
|
else 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
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
/* Get also fade in duration and delays from settings. */
|
|
|
|
crossfade_fade_in_total =
|
|
|
|
NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
|
|
|
|
crossfade_fade_in_rem = crossfade_fade_in_total;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-04-23 22:54:34 +00:00
|
|
|
fade_in_delay =
|
|
|
|
NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
|
|
|
|
|
|
|
|
crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
|
2005-11-06 16:40:20 +00:00
|
|
|
}
|
|
|
|
|
2006-04-25 12:34:28 +00:00
|
|
|
/* Returns the number of bytes _NOT_ mixed */
|
2009-10-23 01:01:00 +00:00
|
|
|
static size_t crossfade_mix(int factor, const char *buf, size_t length)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2007-11-16 14:26:05 +00:00
|
|
|
const int16_t *input_buf = (const int16_t *)buf;
|
|
|
|
int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
|
|
|
|
int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
|
2006-04-25 02:28:21 +00:00
|
|
|
output_buf = &output_buf[crossfade_sample];
|
2008-03-30 10:11:31 +00:00
|
|
|
int32_t sample;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2009-10-23 01:01:00 +00:00
|
|
|
while (length)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2008-03-30 10:11:31 +00:00
|
|
|
/* fade left and right channel at once to keep buffer alignment */
|
2009-10-23 01:01:00 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 2; i++)
|
2005-12-15 18:44:59 +00:00
|
|
|
{
|
2009-10-23 01:01:00 +00:00
|
|
|
sample = *input_buf++;
|
|
|
|
sample = ((sample * factor) >> 8) + *output_buf;
|
|
|
|
*output_buf++ = clip_sample_16(sample);
|
2005-12-15 18:44:59 +00:00
|
|
|
}
|
2008-04-03 14:24:37 +00:00
|
|
|
|
2008-03-30 10:11:31 +00:00
|
|
|
length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
|
2006-04-25 12:34:28 +00:00
|
|
|
|
|
|
|
if (output_buf >= chunk_end)
|
|
|
|
{
|
|
|
|
crossfade_chunk = crossfade_chunk->link;
|
|
|
|
if (!crossfade_chunk)
|
|
|
|
return length;
|
2007-11-16 14:34:15 +00:00
|
|
|
output_buf = (int16_t *)crossfade_chunk->addr;
|
2007-11-16 14:26:05 +00:00
|
|
|
chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
|
2006-04-25 12:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-16 14:26:05 +00:00
|
|
|
crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
|
2006-04-25 12:34:28 +00:00
|
|
|
return 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_flush_buffer(const char *buf, size_t length)
|
|
|
|
{
|
|
|
|
size_t copy_n;
|
|
|
|
while (length > 0) {
|
|
|
|
size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
|
|
|
|
if (NEED_FLUSH(audiobuffer_index))
|
|
|
|
{
|
|
|
|
pcmbuf_flush_fillpos();
|
|
|
|
audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
|
|
|
|
}
|
|
|
|
copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
|
|
|
|
memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
|
|
|
|
buf += copy_n;
|
|
|
|
audiobuffer_fillpos += copy_n;
|
|
|
|
length -= copy_n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-25 02:28:21 +00:00
|
|
|
static void flush_crossfade(char *buf, 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
|
|
|
{
|
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-10-23 01:01:00 +00:00
|
|
|
fade_rem = crossfade_mix(factor, buf, fade_rem);
|
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-10-23 01:01:00 +00:00
|
|
|
length = crossfade_mix(256, buf, length);
|
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
|
|
|
|
2006-04-25 16:12:43 +00:00
|
|
|
/* Flush samples to the buffer */
|
|
|
|
while (!prepare_insert(length))
|
|
|
|
sleep(1);
|
|
|
|
pcmbuf_flush_buffer(buf, length);
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-08-11 02:05:38 +00:00
|
|
|
#endif
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
static bool prepare_insert(size_t length)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-02-07 19:17:51 +00:00
|
|
|
if (low_latency_mode)
|
|
|
|
{
|
|
|
|
/* 1/4s latency. */
|
2009-01-10 21:10:56 +00:00
|
|
|
if (!LOW_DATA(1) && pcm_is_playing())
|
2006-02-07 19:17:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-02-13 17:08:53 +00:00
|
|
|
/* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
|
2006-04-25 12:34:28 +00:00
|
|
|
if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
|
2005-07-13 12:48:22 +00:00
|
|
|
return false;
|
2005-07-19 19:57:23 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
if (!pcm_is_playing())
|
|
|
|
{
|
2006-10-15 11:57:52 +00:00
|
|
|
trigger_cpu_boost();
|
2008-04-03 14:24:37 +00:00
|
|
|
|
2009-01-10 21:10:56 +00:00
|
|
|
/* Pre-buffer up to watermark */
|
|
|
|
#if MEMORYSIZE > 2
|
2006-02-07 20:38:55 +00:00
|
|
|
if (!LOW_DATA(4))
|
2009-01-10 21:10:56 +00:00
|
|
|
#else
|
|
|
|
if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
|
2006-08-12 23:01:52 +00:00
|
|
|
#endif
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
2005-07-13 12:48:22 +00:00
|
|
|
logf("pcm starting");
|
2007-10-06 22:27:27 +00:00
|
|
|
if (!(audio_status() & AUDIO_STATUS_PAUSE))
|
|
|
|
pcmbuf_play_start();
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2008-04-03 14:24:37 +00:00
|
|
|
}
|
2009-01-10 21:10:56 +00:00
|
|
|
else
|
|
|
|
pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
|
2006-02-13 16:55:25 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-02-07 00:51:50 +00:00
|
|
|
void* pcmbuf_request_buffer(int *count)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2009-08-11 02:05:38 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2006-04-20 02:30:59 +00:00
|
|
|
if (crossfade_init)
|
|
|
|
crossfade_start();
|
2009-08-11 02:05:38 +00:00
|
|
|
#endif
|
2006-04-20 02:30:59 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
if (crossfade_active) {
|
2007-02-07 00:51:50 +00:00
|
|
|
*count = MIN(*count, PCMBUF_MIX_CHUNK/4);
|
2006-04-22 14:40:13 +00:00
|
|
|
return fadebuf;
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-07 00:51:50 +00:00
|
|
|
if(prepare_insert(*count << 2))
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
|
|
|
size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
|
2006-02-13 17:08:53 +00:00
|
|
|
if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
|
|
|
|
{
|
|
|
|
/* Usual case, there's space here */
|
|
|
|
return &audiobuffer[audiobuffer_index];
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-13 17:08:53 +00:00
|
|
|
/* Flush and wrap the buffer */
|
|
|
|
pcmbuf_flush_fillpos();
|
|
|
|
audiobuffer_pos = 0;
|
|
|
|
return &audiobuffer[0];
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
void * pcmbuf_request_voice_buffer(int *count)
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
2007-11-18 17:12:19 +00:00
|
|
|
/* A get-it-to-work-for-now hack (audio status could change by
|
|
|
|
completion) */
|
|
|
|
if (audio_status() & AUDIO_STATUS_PLAY)
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
2007-11-18 17:12:19 +00:00
|
|
|
if (pcmbuf_read == NULL)
|
2006-09-17 08:34:42 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
|
|
|
|
(pcmbuf_mix_chunk || pcmbuf_read->link))
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2007-02-07 00:51:50 +00:00
|
|
|
*count = MIN(*count, PCMBUF_MIX_CHUNK/4);
|
2006-04-22 14:40:13 +00:00
|
|
|
return voicebuf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
else
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2007-02-07 00:51:50 +00:00
|
|
|
return pcmbuf_request_buffer(count);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pcmbuf_is_crossfade_active(void)
|
|
|
|
{
|
|
|
|
return crossfade_active || crossfade_init;
|
|
|
|
}
|
|
|
|
|
2007-02-07 00:51:50 +00:00
|
|
|
void pcmbuf_write_complete(int count)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2007-02-07 00:51:50 +00:00
|
|
|
size_t length = (size_t)(unsigned int)count << 2;
|
2009-08-11 02:05:38 +00:00
|
|
|
#ifdef HAVE_CROSSFADE
|
2006-04-20 02:30:59 +00:00
|
|
|
if (crossfade_active)
|
2006-04-25 16:12:43 +00:00
|
|
|
{
|
2006-04-22 14:40:13 +00:00
|
|
|
flush_crossfade(fadebuf, length);
|
2006-04-25 16:12:43 +00:00
|
|
|
if (!(crossfade_fade_in_rem || crossfade_chunk))
|
|
|
|
crossfade_active = false;
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
else
|
2009-08-11 02:05:38 +00:00
|
|
|
#endif
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
|
|
|
audiobuffer_fillpos += length;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
|
|
|
|
pcmbuf_flush_fillpos();
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 00:07:47 +00:00
|
|
|
#ifndef HAVE_HARDWARE_BEEP
|
2008-12-13 06:01:08 +00:00
|
|
|
#define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
|
|
|
|
#define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
/* Generates a constant square wave sound with a given frequency
|
|
|
|
in Hertz for a duration in milliseconds. */
|
2006-02-22 01:56:44 +00:00
|
|
|
void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2008-12-13 06:01:08 +00:00
|
|
|
unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
|
|
|
|
int32_t phase = 0;
|
2008-12-13 01:32:39 +00:00
|
|
|
int16_t *bufptr, *bufstart, *bufend;
|
2007-11-16 14:26:05 +00:00
|
|
|
int32_t sample;
|
2008-12-13 06:01:08 +00:00
|
|
|
int nsamples = NATIVE_FREQUENCY / 1000 * duration;
|
|
|
|
bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
|
2008-12-13 01:32:39 +00:00
|
|
|
int i;
|
|
|
|
|
2008-12-13 06:01:08 +00:00
|
|
|
bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size);
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2008-04-03 14:24:37 +00:00
|
|
|
/* Find the insertion point and set bufstart to the start of it */
|
|
|
|
if (mix)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2008-12-13 01:32:39 +00:00
|
|
|
/* Get the currently playing chunk at the current position. */
|
|
|
|
bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
|
|
|
|
|
2008-12-14 11:42:47 +00:00
|
|
|
/* If above isn't implemented or pcm is stopped, no beepeth. */
|
|
|
|
if (!bufstart || !pcm_is_playing())
|
|
|
|
return;
|
2008-12-13 01:32:39 +00:00
|
|
|
|
|
|
|
/* Give 5ms clearance. */
|
|
|
|
bufstart += NATIVE_FREQUENCY * 4 / 200;
|
|
|
|
|
2009-02-08 22:32:41 +00:00
|
|
|
#ifdef HAVE_PCM_DMA_ADDRESS
|
|
|
|
/* Returned peak addresses are DMA addresses */
|
|
|
|
bufend = pcm_dma_addr(bufend);
|
|
|
|
#endif
|
|
|
|
|
2008-12-13 06:01:08 +00:00
|
|
|
/* Wrapped above? */
|
|
|
|
if (bufstart >= bufend)
|
|
|
|
bufstart -= pcmbuf_size;
|
|
|
|
|
2008-12-13 01:32:39 +00:00
|
|
|
/* NOTE: On some targets using hardware DMA, cache range flushing may
|
|
|
|
* be required or the writes may not be picked up by the controller.
|
|
|
|
* An incremental flush should be done periodically during the mixdown. */
|
2006-04-05 13:00:31 +00:00
|
|
|
}
|
2008-12-13 06:01:08 +00:00
|
|
|
else if (nsamples <= MINIBUF_SAMPLES)
|
|
|
|
{
|
2009-02-16 18:45:43 +00:00
|
|
|
static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
|
2008-12-13 06:01:08 +00:00
|
|
|
/* Use mini buffer */
|
|
|
|
bufstart = minibuf;
|
|
|
|
bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
|
|
|
|
}
|
|
|
|
else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED)
|
2006-04-05 13:00:31 +00:00
|
|
|
{
|
2008-04-03 14:24:37 +00:00
|
|
|
/* Use audiobuffer */
|
|
|
|
bufstart = (int16_t *)audiobuffer;
|
|
|
|
}
|
2008-12-13 06:01:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No place */
|
|
|
|
return;
|
|
|
|
}
|
2008-12-13 01:32:39 +00:00
|
|
|
|
2008-04-03 14:24:37 +00:00
|
|
|
bufptr = bufstart;
|
2008-12-13 01:32:39 +00:00
|
|
|
|
|
|
|
/* Mix square wave into buffer */
|
|
|
|
for (i = 0; i < nsamples; ++i)
|
2008-04-03 14:24:37 +00:00
|
|
|
{
|
2008-12-07 00:07:47 +00:00
|
|
|
int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
|
2008-04-03 14:24:37 +00:00
|
|
|
sample = mix ? *bufptr : 0;
|
2008-12-07 00:07:47 +00:00
|
|
|
*bufptr++ = clip_sample_16(sample + amp);
|
2008-12-13 01:32:39 +00:00
|
|
|
if (bufptr >= bufend)
|
2008-04-03 14:24:37 +00:00
|
|
|
bufptr = (int16_t *)audiobuffer;
|
|
|
|
sample = mix ? *bufptr : 0;
|
2008-12-07 00:07:47 +00:00
|
|
|
*bufptr++ = clip_sample_16(sample + amp);
|
2008-12-13 01:32:39 +00:00
|
|
|
if (bufptr >= bufend)
|
2008-04-03 14:24:37 +00:00
|
|
|
bufptr = (int16_t *)audiobuffer;
|
|
|
|
|
2008-12-07 00:07:47 +00:00
|
|
|
phase += step;
|
2008-04-03 14:24:37 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 01:32:39 +00:00
|
|
|
pcm_play_lock();
|
2008-12-13 06:01:08 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
pcm_rec_lock();
|
|
|
|
#endif
|
2008-12-13 01:32:39 +00:00
|
|
|
|
2008-12-13 06:01:08 +00:00
|
|
|
/* Kick off playback if required and it won't interfere */
|
|
|
|
if (!pcm_is_playing()
|
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
&& !pcm_is_recording()
|
|
|
|
#endif
|
|
|
|
)
|
2008-04-03 14:24:37 +00:00
|
|
|
{
|
2008-12-13 01:32:39 +00:00
|
|
|
pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
2008-12-13 01:32:39 +00:00
|
|
|
|
|
|
|
pcm_play_unlock();
|
2008-12-13 06:01:08 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
pcm_rec_unlock();
|
|
|
|
#endif
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
2008-12-07 00:07:47 +00:00
|
|
|
#endif /* HAVE_HARDWARE_BEEP */
|
2008-04-03 14:24:37 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
/* Returns pcm buffer usage in percents (0 to 100). */
|
|
|
|
int pcmbuf_usage(void)
|
|
|
|
{
|
|
|
|
return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
|
|
|
|
}
|
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
int pcmbuf_mix_free(void)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2006-04-22 14:40:13 +00:00
|
|
|
if (pcmbuf_mix_chunk)
|
|
|
|
{
|
|
|
|
size_t my_mix_end =
|
2007-11-16 14:26:05 +00:00
|
|
|
(size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
|
2006-04-22 14:40:13 +00:00
|
|
|
size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
|
|
|
|
if (my_write_pos < my_mix_end)
|
|
|
|
my_write_pos += pcmbuf_size;
|
|
|
|
return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
|
|
|
|
}
|
|
|
|
return 100;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
void pcmbuf_write_voice_complete(int count)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2007-11-18 17:12:19 +00:00
|
|
|
/* A get-it-to-work-for-now hack (audio status could have changed) */
|
|
|
|
if (!(audio_status() & AUDIO_STATUS_PLAY))
|
|
|
|
{
|
|
|
|
pcmbuf_write_complete(count);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-16 14:26:05 +00:00
|
|
|
int16_t *ibuf = (int16_t *)voicebuf;
|
|
|
|
int16_t *obuf;
|
2006-04-22 14:40:13 +00:00
|
|
|
size_t chunk_samples;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
|
|
|
pcmbuf_mix_chunk = pcmbuf_read->link;
|
|
|
|
/* Start 1/8s into the next chunk */
|
|
|
|
pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
|
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
if (!pcmbuf_mix_chunk)
|
|
|
|
return;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2007-11-16 14:26:05 +00:00
|
|
|
obuf = (int16_t *)pcmbuf_mix_chunk->addr;
|
2007-11-16 14:34:15 +00:00
|
|
|
chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2007-02-07 00:51:50 +00:00
|
|
|
count <<= 1;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2007-11-16 14:49:17 +00:00
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
int32_t sample = *ibuf++;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
if (pcmbuf_mix_sample >= chunk_samples)
|
|
|
|
{
|
|
|
|
pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
|
|
|
|
if (!pcmbuf_mix_chunk)
|
|
|
|
return;
|
|
|
|
pcmbuf_mix_sample = 0;
|
|
|
|
obuf = pcmbuf_mix_chunk->addr;
|
|
|
|
chunk_samples = pcmbuf_mix_chunk->size / 2;
|
|
|
|
}
|
2006-04-23 05:30:52 +00:00
|
|
|
sample += obuf[pcmbuf_mix_sample] >> 2;
|
2007-11-16 14:26:05 +00:00
|
|
|
obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
void pcmbuf_crossfade_enable(bool on_off)
|
|
|
|
{
|
2007-03-19 22:04:17 +00:00
|
|
|
/* Next setting to be used, not applied now */
|
|
|
|
crossfade_enabled_pending = on_off;
|
|
|
|
}
|
2005-07-21 11:44:00 +00:00
|
|
|
|
2007-03-19 22:04:17 +00:00
|
|
|
void pcmbuf_crossfade_enable_finished(void)
|
|
|
|
{
|
|
|
|
/* Copy the pending setting over now */
|
|
|
|
crossfade_enabled = crossfade_enabled_pending;
|
|
|
|
pcmbuf_set_watermark_bytes();
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pcmbuf_is_crossfade_enabled(void)
|
|
|
|
{
|
2005-11-06 16:40:20 +00:00
|
|
|
if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
|
|
|
|
return global_settings.playlist_shuffle;
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
return crossfade_enabled;
|
|
|
|
}
|