2005-07-13 12:48:22 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Miika Pekkarinen
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "config.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include <kernel.h>
|
|
|
|
#include "pcmbuf.h"
|
|
|
|
#include "pcm_playback.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
#include "cpu.h"
|
|
|
|
#endif
|
|
|
|
#include "system.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include "buffer.h"
|
2005-07-21 11:44:00 +00:00
|
|
|
#include "settings.h"
|
|
|
|
#include "audio.h"
|
2005-08-20 11:13:19 +00:00
|
|
|
#include "dsp.h"
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-07 19:17:51 +00:00
|
|
|
#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 1)
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Size of the PCM buffer. */
|
|
|
|
static size_t pcmbuf_size IDATA_ATTR = 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
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 of bytes left in the buffer. */
|
|
|
|
size_t audiobuffer_free IDATA_ATTR;
|
|
|
|
/* Amount audiobuffer_pos will be increased.*/
|
|
|
|
static size_t audiobuffer_fillpos IDATA_ATTR;
|
|
|
|
static char *guardbuf IDATA_ATTR;
|
|
|
|
|
|
|
|
static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
|
|
|
|
static void (*position_callback)(size_t size) IDATA_ATTR;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
|
|
|
/* Crossfade related. */
|
2006-02-07 20:38:55 +00:00
|
|
|
static int crossfade_mode IDATA_ATTR;
|
|
|
|
static bool crossfade_enabled IDATA_ATTR;
|
|
|
|
static bool crossfade_active IDATA_ATTR;
|
|
|
|
static bool crossfade_init IDATA_ATTR;
|
|
|
|
static size_t crossfade_pos IDATA_ATTR;
|
|
|
|
static size_t crossfade_rem IDATA_ATTR;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static struct mutex pcmbuf_mutex IDATA_ATTR;
|
2006-01-22 10:25:07 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
/* Crossfade modes. If CFM_CROSSFADE is selected, normal
|
|
|
|
* crossfader will activate. Selecting CFM_FLUSH is a special
|
|
|
|
* operation that only overwrites the pcm buffer without crossfading.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
CFM_CROSSFADE,
|
2005-07-22 16:46:27 +00:00
|
|
|
CFM_MIX,
|
2005-07-13 12:48:22 +00:00
|
|
|
CFM_FLUSH
|
|
|
|
};
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static size_t crossfade_fade_in_amount IDATA_ATTR;
|
|
|
|
static size_t crossfade_fade_in_rem IDATA_ATTR;
|
|
|
|
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
/* Structure we can use to queue pcm chunks in memory to be played
|
|
|
|
* by the driver code. */
|
|
|
|
struct pcmbufdesc
|
|
|
|
{
|
|
|
|
void *addr;
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t size;
|
|
|
|
struct pcmbufdesc* link;
|
2005-07-13 12:48:22 +00:00
|
|
|
/* Call this when the buffer has been played */
|
|
|
|
void (*callback)(void);
|
2006-02-07 20:38:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static size_t pcmbuf_descsize;
|
|
|
|
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;
|
|
|
|
static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
|
|
|
|
static size_t pcmbuf_mix_used_bytes IDATA_ATTR;
|
|
|
|
static size_t pcmbuf_watermark IDATA_ATTR;
|
2006-02-22 01:56:44 +00:00
|
|
|
static short *mixpos IDATA_ATTR;
|
2006-02-07 19:17:51 +00:00
|
|
|
static bool low_latency_mode = false;
|
2005-07-13 12:48:22 +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)
|
|
|
|
|
|
|
|
static void pcmbuf_flush_audio(void);
|
|
|
|
static void pcmbuf_under_watermark(void);
|
|
|
|
|
|
|
|
#if defined(HAVE_ADJUSTABLE_CPU_FREQ) && !defined(SIMULATOR)
|
2005-08-28 19:55:30 +00:00
|
|
|
static bool boost_mode;
|
|
|
|
|
2005-08-28 14:16:03 +00:00
|
|
|
void pcmbuf_boost(bool state)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
static bool boost_state = false;
|
|
|
|
|
|
|
|
if (crossfade_init || crossfade_active || boost_mode)
|
2006-02-07 20:38:55 +00:00
|
|
|
return;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
|
|
|
if (state != boost_state) {
|
|
|
|
cpu_boost(state);
|
|
|
|
boost_state = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-28 19:55:30 +00:00
|
|
|
void pcmbuf_set_boost_mode(bool state)
|
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
pcmbuf_boost(true);
|
|
|
|
boost_mode = state;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
#define CALL_IF_EXISTS(function, args...) if (function) function(args)
|
|
|
|
/* This function has 2 major logical parts (separated by brackets both for
|
|
|
|
* readability and variable scoping). The first part performs the
|
|
|
|
* operastions related to finishing off the last buffer we fed to the DMA.
|
|
|
|
* The second part performs the operations involved in sending a new buffer
|
|
|
|
* to the DMA. Finally the function checks the status of the buffer and
|
|
|
|
* boosts if necessary */
|
|
|
|
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-02-07 20:38:55 +00:00
|
|
|
{
|
|
|
|
size_t finished_size = last_chunksize;
|
|
|
|
audiobuffer_free += finished_size;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
/* The buffer is finished, call the callback functions */
|
|
|
|
CALL_IF_EXISTS(position_callback, finished_size);
|
|
|
|
}
|
|
|
|
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-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;
|
|
|
|
pcmbuf_unplayed_bytes -= current_size;
|
|
|
|
*realsize = current_size;
|
|
|
|
last_chunksize = current_size;
|
|
|
|
*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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
void pcmbuf_set_position_callback(void (*callback)(size_t size))
|
|
|
|
{
|
2006-01-21 22:42:44 +00:00
|
|
|
position_callback = callback;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_set_watermark_bytes(size_t numbytes)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
pcmbuf_watermark = numbytes;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (pcmbuf_read) {
|
|
|
|
/* If there is already a read buffer setup, add to it */
|
|
|
|
pcmbuf_read_end->link = pcmbuf_current;
|
|
|
|
} 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;
|
|
|
|
if (pcmbuf_mix_used_bytes > size)
|
|
|
|
pcmbuf_mix_used_bytes -= size;
|
2005-07-13 12:48:22 +00:00
|
|
|
else
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_mix_used_bytes = 0;
|
|
|
|
|
|
|
|
audiobuffer_pos += size;
|
|
|
|
if (audiobuffer_pos >= pcmbuf_size)
|
|
|
|
audiobuffer_pos = 0;
|
|
|
|
|
|
|
|
audiobuffer_fillpos = 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_under_watermark(void)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
/* Fill audio buffer by boosting cpu */
|
|
|
|
pcmbuf_boost(true);
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Disable crossfade if < .5s of audio */
|
|
|
|
if (LOW_DATA(2) && crossfade_mode != CFM_FLUSH)
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_active = false;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Be careful how this calculation is rearranted, it's easy to overflow */
|
|
|
|
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-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
/* 0.5 seconds of buffer is low data */
|
|
|
|
return LOW_DATA(2);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-01-27 16:25:44 +00:00
|
|
|
bool pcmbuf_crossfade_init(bool manual_skip)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
if (pcmbuf_unplayed_bytes < PCMBUF_TARGET_CHUNK * 8
|
2006-01-20 22:02:44 +00:00
|
|
|
|| !pcmbuf_is_crossfade_enabled()
|
2006-02-07 19:17:51 +00:00
|
|
|
|| crossfade_active || crossfade_init || low_latency_mode) {
|
2005-07-15 16:42:01 +00:00
|
|
|
pcmbuf_flush_audio();
|
2005-07-13 12:48:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
logf("pcmbuf_crossfade_init");
|
|
|
|
pcmbuf_boost(true);
|
2005-07-22 16:46:27 +00:00
|
|
|
|
2006-01-27 16:25:44 +00:00
|
|
|
/* Don't enable mix mode when skipping tracks manually. */
|
|
|
|
if (manual_skip)
|
|
|
|
crossfade_mode = CFM_CROSSFADE;
|
|
|
|
else
|
|
|
|
crossfade_mode = global_settings.crossfade_fade_out_mixmode
|
|
|
|
? CFM_MIX : CFM_CROSSFADE;
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_init = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcmbuf_play_stop(void)
|
|
|
|
{
|
2006-01-22 10:25:07 +00:00
|
|
|
mutex_lock(&pcmbuf_mutex);
|
|
|
|
/** Prevent a very tiny pop from happening by muting audio
|
|
|
|
* until dma has been initialized. */
|
|
|
|
pcm_mute(true);
|
2005-07-13 12:48:22 +00:00
|
|
|
pcm_play_stop();
|
2006-01-22 10:25:07 +00:00
|
|
|
pcm_mute(false);
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_unplayed_bytes = 0;
|
2005-08-20 11:13:19 +00:00
|
|
|
pcmbuf_mix_used_bytes = 0;
|
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;
|
2005-07-21 11:44:00 +00:00
|
|
|
audiobuffer_free = pcmbuf_size;
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_init = false;
|
|
|
|
crossfade_active = false;
|
|
|
|
|
|
|
|
pcmbuf_set_boost_mode(false);
|
|
|
|
pcmbuf_boost(false);
|
2006-01-22 10:25:07 +00:00
|
|
|
|
|
|
|
mutex_unlock(&pcmbuf_mutex);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
int pcmbuf_used_descs(void) {
|
|
|
|
struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
|
|
|
|
unsigned int i = 0;
|
|
|
|
while (pcmbuf_temp) {
|
|
|
|
pcmbuf_temp = pcmbuf_temp->link;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pcmbuf_descs(void) {
|
|
|
|
return pcmbuf_size / PCMBUF_MINAVG_CHUNK;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_pcmbuf_descsize(void) {
|
|
|
|
return pcmbuf_descsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pcmbuf_init_pcmbuffers(void) {
|
|
|
|
struct pcmbufdesc *next = pcmbuf_write;
|
|
|
|
next++;
|
|
|
|
pcmbuf_write_end = pcmbuf_write;
|
|
|
|
while ((void *)next < (void *)audiobufend) {
|
|
|
|
pcmbuf_write_end->link=next;
|
|
|
|
pcmbuf_write_end=next;
|
|
|
|
next++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the pcmbuffer the structure looks like this:
|
|
|
|
* ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */
|
|
|
|
void pcmbuf_init(size_t bufsize)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-01-22 10:25:07 +00:00
|
|
|
mutex_init(&pcmbuf_mutex);
|
2005-07-21 11:44:00 +00:00
|
|
|
pcmbuf_size = bufsize;
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_descsize = pcmbuf_descs()*sizeof(struct pcmbufdesc);
|
|
|
|
audiobuffer = (char *)&audiobuf[(audiobufend - audiobuf) -
|
|
|
|
(pcmbuf_size + PCMBUF_FADE_CHUNK + pcmbuf_descsize)];
|
2005-07-21 11:44:00 +00:00
|
|
|
guardbuf = &audiobuffer[pcmbuf_size];
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_write = (struct pcmbufdesc *)(&guardbuf[PCMBUF_FADE_CHUNK]);
|
|
|
|
pcmbuf_init_pcmbuffers();
|
2006-01-22 00:03:20 +00:00
|
|
|
position_callback = NULL;
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_event_handler = NULL;
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
/** Initialize a track switch so that audio playback will not stop but
|
|
|
|
* the switch to next track would happen as soon as possible.
|
|
|
|
*/
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_flush_audio(void)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
if (crossfade_init || crossfade_active || !pcm_is_playing()) {
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2005-07-19 19:57:23 +00:00
|
|
|
pcmbuf_boost(true);
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_mode = CFM_FLUSH;
|
|
|
|
crossfade_init = true;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
void pcmbuf_pause(bool pause) {
|
|
|
|
pcm_mute(pause);
|
|
|
|
pcm_play_pause(!pause);
|
|
|
|
pcmbuf_boost(!pause);
|
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
/* Force playback. */
|
|
|
|
void pcmbuf_play_start(void)
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
mutex_lock(&pcmbuf_mutex);
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (!pcm_is_playing() && pcmbuf_unplayed_bytes)
|
2006-01-22 10:25:07 +00:00
|
|
|
{
|
|
|
|
/** Prevent a very tiny pop from happening by muting audio
|
|
|
|
* until dma has been initialized. */
|
|
|
|
pcm_mute(true);
|
|
|
|
|
2006-02-07 20:38:55 +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
|
|
|
|
|
|
|
/* Now unmute the audio. */
|
|
|
|
pcm_mute(false);
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
mutex_unlock(&pcmbuf_mutex);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2005-11-06 16:40:20 +00:00
|
|
|
/**
|
|
|
|
* Commit samples waiting to the pcm buffer.
|
|
|
|
*/
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_flush_fillpos(void)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-01-22 10:25:07 +00:00
|
|
|
mutex_lock(&pcmbuf_mutex);
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
if (audiobuffer_fillpos) {
|
|
|
|
/* Never use the last buffer descriptor */
|
|
|
|
while (pcmbuf_write == pcmbuf_write_end) {
|
|
|
|
logf("pcmbuf_flush_fillpos no descriptors");
|
|
|
|
/* Deboost to let the playback catchup */
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_boost(false);
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Let someone else have fun in the meantime */
|
2005-07-13 12:48:22 +00:00
|
|
|
sleep(1);
|
|
|
|
/* This is a fatal error situation that should never happen. */
|
|
|
|
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-19 19:57:23 +00:00
|
|
|
return ;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_add_chunk();
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
|
2006-01-22 10:25:07 +00:00
|
|
|
mutex_unlock(&pcmbuf_mutex);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2005-11-06 16:40:20 +00:00
|
|
|
/**
|
|
|
|
* Completely process the crossfade fade out effect with current pcm buffer.
|
|
|
|
*/
|
2006-02-07 20:38:55 +00:00
|
|
|
static void crossfade_process_buffer(unsigned int fade_in_delay,
|
|
|
|
unsigned int fade_out_delay, size_t fade_out_rem)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t amount;
|
|
|
|
size_t pos;
|
2005-11-06 16:40:20 +00:00
|
|
|
short *buf;
|
|
|
|
|
|
|
|
/* Fade out the entire current buffer according to settings. */
|
|
|
|
amount = fade_out_rem;
|
|
|
|
pos = crossfade_pos + fade_out_delay*2;
|
|
|
|
|
|
|
|
while (fade_out_rem > 0 && crossfade_mode == CFM_CROSSFADE)
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t blocksize = MIN(8192, fade_out_rem);
|
2005-11-06 16:40:20 +00:00
|
|
|
int factor = (fade_out_rem<<8)/amount;
|
|
|
|
|
|
|
|
/* Prevent pcmbuffer from wrapping. */
|
2006-02-07 20:38:55 +00:00
|
|
|
if (pos >= pcmbuf_size) pos -= pcmbuf_size;
|
|
|
|
|
2005-12-28 21:13:52 +00:00
|
|
|
blocksize = MIN((pcmbuf_size - pos)/2, blocksize);
|
2005-11-06 16:40:20 +00:00
|
|
|
buf = (short *)&audiobuffer[pos];
|
|
|
|
|
|
|
|
fade_out_rem -= blocksize;
|
|
|
|
pos += blocksize * 2;
|
|
|
|
while (blocksize > 0)
|
|
|
|
{
|
|
|
|
*buf = (*buf * factor) >> 8;
|
|
|
|
*buf++;
|
|
|
|
blocksize--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And finally set the mixing position where we should start fading in. */
|
|
|
|
crossfade_rem -= fade_in_delay;
|
|
|
|
crossfade_pos += fade_in_delay*2;
|
|
|
|
if (crossfade_pos >= pcmbuf_size)
|
|
|
|
crossfade_pos -= pcmbuf_size;
|
|
|
|
logf("process done!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-02-07 20:38:55 +00:00
|
|
|
size_t fade_out_rem = 0;
|
|
|
|
unsigned int fade_out_delay = 0;
|
|
|
|
unsigned fade_in_delay = 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
|
|
|
crossfade_init = 0;
|
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");
|
|
|
|
pcmbuf_boost(true);
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_flush_fillpos();
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_active = true;
|
|
|
|
crossfade_pos = audiobuffer_pos;
|
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 */
|
|
|
|
crossfade_rem = pcmbuf_unplayed_bytes / 2;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
|
|
|
switch (crossfade_mode) {
|
2005-07-22 16:46:27 +00:00
|
|
|
case CFM_MIX:
|
2005-07-13 12:48:22 +00:00
|
|
|
case CFM_CROSSFADE:
|
2005-11-06 16:40:20 +00:00
|
|
|
/* Get fade out delay from settings. */
|
|
|
|
fade_out_delay = NATIVE_FREQUENCY
|
|
|
|
* global_settings.crossfade_fade_out_delay * 2;
|
|
|
|
|
|
|
|
/* Get fade out duration from settings. */
|
|
|
|
fade_out_rem = NATIVE_FREQUENCY
|
|
|
|
* global_settings.crossfade_fade_out_duration * 2;
|
|
|
|
|
|
|
|
/* We want only to modify the last part of the buffer. */
|
|
|
|
if (crossfade_rem > fade_out_rem + fade_out_delay)
|
|
|
|
crossfade_rem = fade_out_rem + fade_out_delay;
|
|
|
|
|
2005-12-28 21:13:52 +00:00
|
|
|
/* Truncate fade out duration if necessary. */
|
|
|
|
if (crossfade_rem < fade_out_rem + fade_out_delay)
|
|
|
|
fade_out_rem -= (fade_out_rem + fade_out_delay) - crossfade_rem;
|
|
|
|
|
2005-11-06 16:40:20 +00:00
|
|
|
/* Get also fade in duration and delays from settings. */
|
|
|
|
crossfade_fade_in_rem = NATIVE_FREQUENCY
|
|
|
|
* global_settings.crossfade_fade_in_duration * 2;
|
|
|
|
crossfade_fade_in_amount = crossfade_fade_in_rem;
|
|
|
|
|
|
|
|
/* We should avoid to divide by zero. */
|
|
|
|
if (crossfade_fade_in_amount == 0)
|
|
|
|
crossfade_fade_in_amount = 1;
|
|
|
|
|
|
|
|
fade_in_delay = NATIVE_FREQUENCY
|
|
|
|
* global_settings.crossfade_fade_in_delay * 2;
|
|
|
|
|
|
|
|
/* Decrease the fade out delay if necessary. */
|
2006-02-07 20:38:55 +00:00
|
|
|
if (crossfade_rem < fade_out_rem + fade_out_delay)
|
|
|
|
fade_out_delay -=
|
|
|
|
(fade_out_rem + fade_out_delay) - crossfade_rem;
|
2005-07-13 12:48:22 +00:00
|
|
|
break ;
|
|
|
|
|
|
|
|
case CFM_FLUSH:
|
2005-12-15 18:44:59 +00:00
|
|
|
crossfade_fade_in_rem = 0;
|
|
|
|
crossfade_fade_in_amount = 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
if (crossfade_pos < crossfade_rem * 2)
|
2005-07-21 11:44:00 +00:00
|
|
|
crossfade_pos += pcmbuf_size;
|
2006-02-07 20:38:55 +00:00
|
|
|
crossfade_pos -= crossfade_rem*2;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
|
|
|
if (crossfade_mode != CFM_FLUSH) {
|
|
|
|
/* Process the fade out part of the crossfade. */
|
|
|
|
crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fades in samples passed to the function and inserts them
|
|
|
|
* to the pcm buffer.
|
|
|
|
*/
|
2006-02-07 20:38:55 +00:00
|
|
|
static void fade_insert(const short *inbuf, size_t length)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t copy_n;
|
2005-11-06 16:40:20 +00:00
|
|
|
int factor;
|
2006-02-07 20:38:55 +00:00
|
|
|
unsigned int i, samples;
|
2005-11-06 16:40:20 +00:00
|
|
|
short *buf;
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8)
|
|
|
|
/crossfade_fade_in_amount;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
while (audiobuffer_free < length)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
|
|
|
pcmbuf_boost(false);
|
|
|
|
sleep(1);
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
audiobuffer_free -= length;
|
2005-11-06 16:40:20 +00:00
|
|
|
|
|
|
|
while (length > 0) {
|
2006-02-07 20:38:55 +00:00
|
|
|
unsigned int audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
|
|
|
|
/* Flush as needed */
|
|
|
|
if (NEED_FLUSH(audiobuffer_index))
|
|
|
|
{
|
|
|
|
pcmbuf_flush_fillpos();
|
|
|
|
audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
buf = (short *)&audiobuffer[audiobuffer_index];
|
2005-11-06 16:40:20 +00:00
|
|
|
samples = copy_n / 2;
|
|
|
|
for (i = 0; i < samples; i++)
|
|
|
|
buf[i] = (inbuf[i] * factor) >> 8;
|
|
|
|
|
|
|
|
inbuf += samples;
|
|
|
|
audiobuffer_fillpos += copy_n;
|
|
|
|
length -= copy_n;
|
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2005-11-06 16:40:20 +00:00
|
|
|
/**
|
|
|
|
* Fades in buf2 and mixes it with buf.
|
|
|
|
*/
|
2006-02-07 20:38:55 +00:00
|
|
|
static int crossfade(short *buf, const short *buf2, unsigned int length)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t size;
|
|
|
|
unsigned int i;
|
|
|
|
size_t size_insert = 0;
|
2005-11-06 16:40:20 +00:00
|
|
|
int factor;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
size = MIN(length, crossfade_rem);
|
2005-07-13 12:48:22 +00:00
|
|
|
switch (crossfade_mode) {
|
2005-11-06 16:40:20 +00:00
|
|
|
/* Fade in the current stream and mix it. */
|
2005-07-22 16:46:27 +00:00
|
|
|
case CFM_MIX:
|
2005-07-13 12:48:22 +00:00
|
|
|
case CFM_CROSSFADE:
|
2006-02-07 20:38:55 +00:00
|
|
|
factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8) /
|
|
|
|
crossfade_fade_in_amount;
|
2005-07-13 12:48:22 +00:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
2006-02-07 20:38:55 +00:00
|
|
|
buf[i] = MIN(32767, MAX(-32768,
|
|
|
|
buf[i] + ((buf2[i] * factor) >> 8)));
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
break ;
|
|
|
|
|
2005-07-22 16:46:27 +00:00
|
|
|
/* Join two streams. */
|
2005-07-13 12:48:22 +00:00
|
|
|
case CFM_FLUSH:
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
buf[i] = buf2[i];
|
|
|
|
}
|
|
|
|
//memcpy((char *)buf, (char *)buf2, size*2);
|
|
|
|
break ;
|
|
|
|
}
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
if (crossfade_fade_in_rem > size)
|
|
|
|
crossfade_fade_in_rem = crossfade_fade_in_rem - size;
|
|
|
|
else
|
|
|
|
crossfade_fade_in_rem = 0;
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
crossfade_rem -= size;
|
2006-02-07 20:38:55 +00:00
|
|
|
if (crossfade_rem == 0)
|
2005-11-06 16:40:20 +00:00
|
|
|
{
|
2005-12-15 18:44:59 +00:00
|
|
|
if (crossfade_fade_in_rem > 0 && crossfade_fade_in_amount > 0)
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
size_insert = MIN(crossfade_fade_in_rem, length - size);
|
2005-12-15 18:44:59 +00:00
|
|
|
fade_insert(&buf2[size], size_insert*2);
|
|
|
|
crossfade_fade_in_rem -= size_insert;
|
|
|
|
}
|
2005-11-06 16:40:20 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
if (crossfade_fade_in_rem == 0)
|
2005-11-06 16:40:20 +00:00
|
|
|
crossfade_active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size + size_insert;
|
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;
|
|
|
|
audiobuffer_free -= length;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_crossfade(const char *buf, size_t length) {
|
|
|
|
size_t copy_n;
|
|
|
|
|
|
|
|
while (length > 0 && crossfade_active) {
|
|
|
|
copy_n = MIN(length, pcmbuf_size - crossfade_pos);
|
|
|
|
copy_n = 2 * crossfade((short *)&audiobuffer[crossfade_pos],
|
|
|
|
(const short *)buf, copy_n/2);
|
|
|
|
buf += copy_n;
|
|
|
|
length -= copy_n;
|
|
|
|
crossfade_pos += copy_n;
|
|
|
|
if (crossfade_pos >= pcmbuf_size)
|
|
|
|
crossfade_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcmbuf_flush_buffer(buf, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool prepare_insert(size_t length)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
if (crossfade_init)
|
|
|
|
crossfade_start();
|
2006-02-07 19:17:51 +00:00
|
|
|
|
|
|
|
if (low_latency_mode)
|
|
|
|
{
|
|
|
|
/* 1/4s latency. */
|
|
|
|
if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 4
|
|
|
|
&& pcm_is_playing())
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-13 12:48:22 +00:00
|
|
|
|
2006-02-13 17:08:53 +00:00
|
|
|
/* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
|
|
|
|
if (audiobuffer_free < length + PCMBUF_MIN_CHUNK && !crossfade_active)
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_boost(false);
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-19 19:57:23 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
if (!pcm_is_playing())
|
|
|
|
{
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_boost(true);
|
|
|
|
crossfade_active = false;
|
2006-02-07 19:17:51 +00:00
|
|
|
/* Pre-buffer 1s. */
|
2006-02-07 20:38:55 +00:00
|
|
|
if (!LOW_DATA(4))
|
|
|
|
{
|
2005-07-13 12:48:22 +00:00
|
|
|
logf("pcm starting");
|
2006-01-22 10:25:07 +00:00
|
|
|
pcmbuf_play_start();
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-02-13 16:55:25 +00:00
|
|
|
} else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
|
|
|
|
pcmbuf_under_watermark();
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
void* pcmbuf_request_buffer(size_t length, size_t *realsize)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
if (crossfade_active) {
|
2006-02-07 20:38:55 +00:00
|
|
|
*realsize = MIN(length, PCMBUF_FADE_CHUNK);
|
|
|
|
return &guardbuf[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(prepare_insert(length))
|
|
|
|
{
|
|
|
|
size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
|
2006-02-13 17:08:53 +00:00
|
|
|
*realsize = length;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* pcmbuf_request_voice_buffer(size_t length, size_t *realsize, bool mix)
|
|
|
|
{
|
|
|
|
if (mix)
|
|
|
|
{
|
|
|
|
*realsize = MIN(length, PCMBUF_FADE_CHUNK);
|
|
|
|
return &guardbuf[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return pcmbuf_request_buffer(length, realsize);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pcmbuf_is_crossfade_active(void)
|
|
|
|
{
|
|
|
|
return crossfade_active || crossfade_init;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
void pcmbuf_write_complete(size_t length)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
if (crossfade_active) {
|
2006-02-07 20:38:55 +00:00
|
|
|
length = MIN(length, PCMBUF_FADE_CHUNK);
|
|
|
|
flush_crossfade(&guardbuf[0],length);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
audiobuffer_free -= length;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
bool pcmbuf_insert_buffer(const char *buf, size_t length)
|
2005-07-13 12:48:22 +00:00
|
|
|
{
|
|
|
|
if (!prepare_insert(length))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (crossfade_active) {
|
2006-02-07 20:38:55 +00:00
|
|
|
flush_crossfade(buf,length);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
2006-02-07 20:38:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pcmbuf_flush_buffer(buf, length);
|
2005-07-13 12:48:22 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-22 01:56:44 +00:00
|
|
|
/* Get a pointer to where to mix immediate audio */
|
|
|
|
static inline short* get_mix_insert_pos(void) {
|
|
|
|
/* Give at least 1/8s clearance here */
|
|
|
|
size_t pcmbuf_mix_back_pos =
|
|
|
|
pcmbuf_unplayed_bytes - NATIVE_FREQUENCY * 4 / 8;
|
|
|
|
|
|
|
|
if (audiobuffer_pos < pcmbuf_mix_back_pos)
|
|
|
|
return (short *)&audiobuffer[pcmbuf_size +
|
|
|
|
audiobuffer_pos - pcmbuf_mix_back_pos];
|
|
|
|
else
|
|
|
|
return (short *)&audiobuffer[audiobuffer_pos - pcmbuf_mix_back_pos];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2006-02-22 01:56:44 +00:00
|
|
|
unsigned int count = 0, i = 0;
|
|
|
|
bool state = false;
|
2006-02-07 20:38:55 +00:00
|
|
|
unsigned int interval = NATIVE_FREQUENCY / frequency;
|
2006-02-22 01:56:44 +00:00
|
|
|
short *buf;
|
|
|
|
short *pcmbuf_end = (short *)guardbuf;
|
|
|
|
bool playing = pcm_is_playing();
|
|
|
|
size_t samples = NATIVE_FREQUENCY / 1000 * duration;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-02-22 01:56:44 +00:00
|
|
|
if (playing) {
|
|
|
|
buf = get_mix_insert_pos();
|
|
|
|
} else {
|
|
|
|
buf = (short *)audiobuffer;
|
|
|
|
}
|
|
|
|
while (i++ < samples)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2006-02-22 01:56:44 +00:00
|
|
|
long sample = *buf;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (state) {
|
2006-02-22 01:56:44 +00:00
|
|
|
*buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
|
|
|
|
if (buf > pcmbuf_end)
|
|
|
|
buf = (short *)audiobuffer;
|
|
|
|
sample = *buf;
|
|
|
|
*buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
|
2005-08-20 11:13:19 +00:00
|
|
|
} else {
|
2006-02-22 01:56:44 +00:00
|
|
|
*buf++ = MIN(MAX(sample - amplitude, -32768), 32767);
|
|
|
|
if (buf > pcmbuf_end)
|
|
|
|
buf = (short *)audiobuffer;
|
|
|
|
sample = *buf;
|
|
|
|
*buf++ = MIN(MAX(sample - amplitude, -32768), 32767);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (++count >= interval)
|
|
|
|
{
|
|
|
|
count = 0;
|
2006-02-22 01:56:44 +00:00
|
|
|
state = !state;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
2006-02-22 01:56:44 +00:00
|
|
|
if (buf > pcmbuf_end)
|
|
|
|
buf = (short *)audiobuffer;
|
|
|
|
}
|
|
|
|
if (!playing) {
|
|
|
|
pcm_play_data(NULL, (unsigned char *)audiobuffer, samples * 4);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pcmbuf_mix_usage(void)
|
|
|
|
{
|
|
|
|
return pcmbuf_mix_used_bytes * 100 / pcmbuf_unplayed_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcmbuf_reset_mixpos(void)
|
|
|
|
{
|
2006-02-22 01:56:44 +00:00
|
|
|
mixpos = get_mix_insert_pos();
|
2005-08-20 11:13:19 +00:00
|
|
|
pcmbuf_mix_used_bytes = 0;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
void pcmbuf_mix(char *buf, size_t length)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
|
|
|
short *ibuf = (short *)buf;
|
2006-02-22 01:56:44 +00:00
|
|
|
short *pcmbuf_end = (short *)guardbuf;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
|
|
|
if (pcmbuf_mix_used_bytes == 0)
|
|
|
|
pcmbuf_reset_mixpos();
|
|
|
|
|
|
|
|
pcmbuf_mix_used_bytes += length;
|
|
|
|
length /= 2;
|
|
|
|
|
|
|
|
while (length-- > 0) {
|
2006-02-22 01:56:44 +00:00
|
|
|
long sample = *ibuf++;
|
|
|
|
sample += *mixpos >> 2;
|
|
|
|
*mixpos++ = MIN(MAX(sample, -32768), 32767);
|
|
|
|
|
|
|
|
if (mixpos >= pcmbuf_end)
|
|
|
|
mixpos = (short *)audiobuffer;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
void pcmbuf_crossfade_enable(bool on_off)
|
|
|
|
{
|
|
|
|
crossfade_enabled = on_off;
|
2005-07-21 11:44:00 +00:00
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
if (crossfade_enabled) {
|
2006-02-07 20:38:55 +00:00
|
|
|
/* If crossfading, try to keep the buffer full other than 2 second */
|
|
|
|
pcmbuf_set_watermark_bytes(pcmbuf_size - PCMBUF_WATERMARK * 2);
|
2005-07-13 12:48:22 +00:00
|
|
|
} else {
|
2006-02-07 20:38:55 +00:00
|
|
|
/* Otherwise, just keep it above 1 second */
|
2006-01-21 22:42:44 +00:00
|
|
|
pcmbuf_set_watermark_bytes(PCMBUF_WATERMARK);
|
2005-07-21 11:44:00 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|