2005-03-18 11:39:28 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Linus Nielsen Feltzing
|
|
|
|
*
|
|
|
|
* 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 "config.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include <kernel.h>
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "i2c.h"
|
|
|
|
#include "uda1380.h"
|
|
|
|
#include "system.h"
|
|
|
|
#endif
|
2005-06-19 18:41:53 +00:00
|
|
|
#include "logf.h"
|
2005-03-18 11:39:28 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2005-06-05 23:05:10 +00:00
|
|
|
#include "pcm_playback.h"
|
2005-03-18 11:39:28 +00:00
|
|
|
#include "lcd.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "buffer.h"
|
2005-03-28 00:00:24 +00:00
|
|
|
#include "sprintf.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-06-18 22:23:54 +00:00
|
|
|
#ifdef HAVE_UDA1380
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
#define CHUNK_SIZE 32768
|
|
|
|
/* Must be a power of 2 */
|
|
|
|
#define NUM_PCM_BUFFERS (PCMBUF_SIZE / CHUNK_SIZE)
|
|
|
|
#define NUM_PCM_BUFFERS_MASK (NUM_PCM_BUFFERS - 1)
|
2005-06-29 20:50:58 +00:00
|
|
|
#define PCM_WATERMARK (CHUNK_SIZE * 6)
|
2005-06-20 12:40:30 +00:00
|
|
|
#define PCM_CF_WATERMARK (PCMBUF_SIZE - CHUNK_SIZE*8)
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-03-28 00:00:24 +00:00
|
|
|
static bool pcm_playing;
|
2005-03-31 06:49:10 +00:00
|
|
|
static bool pcm_paused;
|
|
|
|
static int pcm_freq = 0x6; /* 44.1 is default */
|
2005-03-18 11:39:28 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
static char *audiobuffer;
|
2005-06-27 19:29:49 +00:00
|
|
|
static long audiobuffer_pos;
|
|
|
|
long audiobuffer_free;
|
|
|
|
static long audiobuffer_fillpos;
|
2005-06-05 23:05:10 +00:00
|
|
|
static bool boost_mode;
|
|
|
|
|
2005-06-29 20:50:58 +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,
|
|
|
|
CFM_FLUSH
|
|
|
|
};
|
|
|
|
|
|
|
|
static int crossfade_mode;
|
2005-06-09 09:47:00 +00:00
|
|
|
static bool crossfade_enabled;
|
2005-06-09 07:19:16 +00:00
|
|
|
static bool crossfade_active;
|
2005-06-20 14:27:06 +00:00
|
|
|
static bool crossfade_init;
|
2005-06-09 07:19:16 +00:00
|
|
|
static int crossfade_pos;
|
|
|
|
static int crossfade_amount;
|
|
|
|
static int crossfade_rem;
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
static char *guardbuf;
|
2005-06-15 18:59:04 +00:00
|
|
|
static void (*pcm_event_handler)(void);
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
static unsigned char *next_start;
|
|
|
|
static long next_size;
|
|
|
|
|
2005-07-01 17:05:09 +00:00
|
|
|
static int last_chunksize = 0;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
struct pcmbufdesc
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
int size;
|
|
|
|
void (*callback)(void); /* Call this when the buffer has been played */
|
|
|
|
} pcmbuffers[NUM_PCM_BUFFERS];
|
|
|
|
|
|
|
|
volatile int pcmbuf_read_index;
|
|
|
|
volatile int pcmbuf_write_index;
|
|
|
|
int pcmbuf_unplayed_bytes;
|
|
|
|
int pcmbuf_watermark;
|
|
|
|
void (*pcmbuf_watermark_callback)(int bytes_left);
|
|
|
|
|
2005-03-18 11:39:28 +00:00
|
|
|
/* Set up the DMA transfer that kicks in when the audio FIFO gets empty */
|
2005-03-31 06:49:10 +00:00
|
|
|
static void dma_start(const void *addr, long size)
|
|
|
|
{
|
|
|
|
pcm_playing = true;
|
2005-03-18 11:39:28 +00:00
|
|
|
|
2005-03-31 06:49:10 +00:00
|
|
|
addr = (void *)((unsigned long)addr & ~3); /* Align data */
|
|
|
|
size &= ~3; /* Size must be multiple of 4 */
|
2005-03-18 11:39:28 +00:00
|
|
|
|
2005-03-31 06:49:10 +00:00
|
|
|
/* Reset the audio FIFO */
|
|
|
|
IIS2CONFIG = 0x800;
|
2005-07-01 07:55:19 +00:00
|
|
|
EBU1CONFIG = 0x800;
|
2005-07-01 17:05:09 +00:00
|
|
|
|
2005-03-18 11:39:28 +00:00
|
|
|
/* Set up DMA transfer */
|
2005-03-31 06:49:10 +00:00
|
|
|
SAR0 = ((unsigned long)addr); /* Source address */
|
|
|
|
DAR0 = (unsigned long)&PDOR3; /* Destination address */
|
|
|
|
BCR0 = size; /* Bytes to transfer */
|
2005-03-18 11:39:28 +00:00
|
|
|
|
2005-03-31 06:49:10 +00:00
|
|
|
/* Enable the FIFO and force one write to it */
|
2005-07-01 07:55:19 +00:00
|
|
|
IIS2CONFIG = (pcm_freq << 12) | 0x300 | 4 << 2;
|
|
|
|
/* Also send the audio to S/PDIF */
|
|
|
|
EBU1CONFIG = 7 << 12 | 3 << 8 | 5 << 2;
|
2005-03-31 06:49:10 +00:00
|
|
|
DCR0 = DMA_INT | DMA_EEXT | DMA_CS | DMA_SINC | DMA_START;
|
2005-03-18 11:39:28 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void pcm_boost(bool state)
|
|
|
|
{
|
|
|
|
static bool boost_state = false;
|
|
|
|
|
2005-06-11 20:14:10 +00:00
|
|
|
if (crossfade_active || boost_mode)
|
2005-06-09 07:19:16 +00:00
|
|
|
return ;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
if (state != boost_state) {
|
2005-06-20 18:31:38 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2005-06-05 23:05:10 +00:00
|
|
|
cpu_boost(state);
|
2005-06-20 18:31:38 +00:00
|
|
|
#endif
|
2005-06-05 23:05:10 +00:00
|
|
|
boost_state = state;
|
|
|
|
}
|
|
|
|
}
|
2005-03-28 00:00:24 +00:00
|
|
|
|
2005-06-10 10:58:45 +00:00
|
|
|
/* Stops the DMA transfer and interrupt */
|
|
|
|
static void dma_stop(void)
|
|
|
|
{
|
|
|
|
pcm_playing = false;
|
|
|
|
|
2005-06-30 20:02:56 +00:00
|
|
|
DCR0 = 0;
|
2005-06-10 10:58:45 +00:00
|
|
|
/* Reset the FIFO */
|
|
|
|
IIS2CONFIG = 0x800;
|
2005-07-01 07:55:19 +00:00
|
|
|
EBU1CONFIG = 0x800;
|
2005-07-01 17:05:09 +00:00
|
|
|
|
|
|
|
pcmbuf_unplayed_bytes = 0;
|
|
|
|
last_chunksize = 0;
|
|
|
|
audiobuffer_pos = 0;
|
|
|
|
audiobuffer_fillpos = 0;
|
|
|
|
audiobuffer_free = PCMBUF_SIZE;
|
|
|
|
pcmbuf_read_index = 0;
|
|
|
|
pcmbuf_write_index = 0;
|
|
|
|
next_start = NULL;
|
|
|
|
next_size = 0;
|
|
|
|
crossfade_init = 0;
|
|
|
|
pcm_paused = false;
|
2005-06-10 10:58:45 +00:00
|
|
|
}
|
|
|
|
|
2005-03-28 00:00:24 +00:00
|
|
|
/* sets frequency of input to DAC */
|
|
|
|
void pcm_set_frequency(unsigned int frequency)
|
|
|
|
{
|
2005-03-31 06:49:10 +00:00
|
|
|
switch(frequency)
|
|
|
|
{
|
2005-03-28 00:00:24 +00:00
|
|
|
case 11025:
|
|
|
|
pcm_freq = 0x4;
|
2005-07-01 07:55:19 +00:00
|
|
|
uda1380_set_nsorder(3);
|
2005-03-28 00:00:24 +00:00
|
|
|
break;
|
2005-07-01 07:55:19 +00:00
|
|
|
case 22050:
|
2005-03-28 00:00:24 +00:00
|
|
|
pcm_freq = 0x6;
|
2005-07-01 07:55:19 +00:00
|
|
|
uda1380_set_nsorder(3);
|
2005-03-28 00:00:24 +00:00
|
|
|
break;
|
2005-07-01 07:55:19 +00:00
|
|
|
case 44100:
|
2005-03-28 00:00:24 +00:00
|
|
|
default:
|
2005-07-01 07:55:19 +00:00
|
|
|
pcm_freq = 0xC;
|
|
|
|
uda1380_set_nsorder(5);
|
2005-03-31 06:49:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-03-28 00:00:24 +00:00
|
|
|
}
|
|
|
|
|
2005-03-18 11:39:28 +00:00
|
|
|
/* the registered callback function to ask for more mp3 data */
|
|
|
|
static void (*callback_for_more)(unsigned char**, long*) = NULL;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
int pcm_play_num_used_buffers(void)
|
|
|
|
{
|
|
|
|
return (pcmbuf_write_index - pcmbuf_read_index) & NUM_PCM_BUFFERS_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pcm_play_callback(unsigned char** start, long* size)
|
|
|
|
{
|
|
|
|
struct pcmbufdesc *desc = &pcmbuffers[pcmbuf_read_index];
|
|
|
|
int sz;
|
|
|
|
|
|
|
|
pcmbuf_unplayed_bytes -= last_chunksize;
|
|
|
|
audiobuffer_free += last_chunksize;
|
|
|
|
|
|
|
|
|
|
|
|
if(desc->size == 0)
|
|
|
|
{
|
|
|
|
/* The buffer is finished, call the callback function */
|
|
|
|
if(desc->callback)
|
|
|
|
desc->callback();
|
|
|
|
|
|
|
|
/* Advance to the next buffer */
|
|
|
|
pcmbuf_read_index = (pcmbuf_read_index + 1) & NUM_PCM_BUFFERS_MASK;
|
|
|
|
desc = &pcmbuffers[pcmbuf_read_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pcm_play_num_used_buffers())
|
|
|
|
{
|
|
|
|
/* Play max 64K at a time */
|
|
|
|
sz = MIN(desc->size, 32768);
|
|
|
|
*start = desc->addr;
|
|
|
|
*size = sz;
|
|
|
|
|
|
|
|
/* Update the buffer descriptor */
|
|
|
|
desc->size -= sz;
|
|
|
|
desc->addr += sz;
|
|
|
|
|
|
|
|
last_chunksize = sz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No more buffers */
|
|
|
|
*size = 0;
|
2005-06-15 18:59:04 +00:00
|
|
|
if (pcm_event_handler)
|
|
|
|
pcm_event_handler();
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
#if 1
|
|
|
|
if(pcmbuf_unplayed_bytes <= pcmbuf_watermark)
|
|
|
|
{
|
|
|
|
if(pcmbuf_watermark_callback)
|
|
|
|
{
|
|
|
|
pcmbuf_watermark_callback(pcmbuf_unplayed_bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-03-18 11:39:28 +00:00
|
|
|
void pcm_play_data(const unsigned char* start, int size,
|
|
|
|
void (*get_more)(unsigned char** start, long* size))
|
|
|
|
{
|
|
|
|
callback_for_more = get_more;
|
2005-06-07 06:34:54 +00:00
|
|
|
get_more(&next_start, &next_size);
|
2005-07-01 17:05:09 +00:00
|
|
|
dma_start(start, size);
|
2005-06-14 07:54:09 +00:00
|
|
|
|
|
|
|
/* Sleep a while, then power on audio output */
|
2005-06-14 09:25:28 +00:00
|
|
|
sleep(HZ/16);
|
|
|
|
uda1380_enable_output(true);
|
2005-03-18 11:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_stop(void)
|
|
|
|
{
|
2005-06-20 12:40:30 +00:00
|
|
|
crossfade_active = false;
|
2005-06-14 18:59:34 +00:00
|
|
|
pcm_set_boost_mode(false);
|
2005-06-14 09:25:28 +00:00
|
|
|
if (pcm_playing) {
|
|
|
|
uda1380_enable_output(false);
|
|
|
|
pcm_boost(false);
|
2005-06-19 18:41:53 +00:00
|
|
|
sleep(HZ/16);
|
2005-06-14 07:54:09 +00:00
|
|
|
dma_stop();
|
2005-06-14 09:25:28 +00:00
|
|
|
}
|
2005-03-18 11:39:28 +00:00
|
|
|
}
|
|
|
|
|
2005-03-31 06:49:10 +00:00
|
|
|
void pcm_play_pause(bool play)
|
|
|
|
{
|
2005-06-05 23:05:10 +00:00
|
|
|
if(pcm_paused && play && pcmbuf_unplayed_bytes)
|
2005-03-31 06:49:10 +00:00
|
|
|
{
|
2005-06-30 20:02:56 +00:00
|
|
|
logf("unpause");
|
|
|
|
/* Reset chunk size so dma has enough data to fill the fifo. */
|
2005-07-01 17:13:33 +00:00
|
|
|
/* This shouldn't be needed anymore. */
|
|
|
|
//SAR0 = (unsigned long)next_start;
|
|
|
|
//BCR0 = next_size;
|
2005-03-31 06:49:10 +00:00
|
|
|
/* Enable the FIFO and force one write to it */
|
2005-07-01 07:55:19 +00:00
|
|
|
IIS2CONFIG = (pcm_freq << 12) | 0x300 | 4 << 2;
|
|
|
|
EBU1CONFIG = 7 << 12 | 3 << 8 | 5 << 2;
|
2005-06-30 20:02:56 +00:00
|
|
|
DCR0 |= DMA_EEXT | DMA_START;
|
2005-03-31 06:49:10 +00:00
|
|
|
}
|
|
|
|
else if(!pcm_paused && !play)
|
|
|
|
{
|
2005-06-30 20:02:56 +00:00
|
|
|
logf("pause");
|
|
|
|
/* Disable DMA peripheral request. */
|
|
|
|
DCR0 &= ~DMA_EEXT;
|
2005-03-31 06:49:10 +00:00
|
|
|
IIS2CONFIG = 0x800;
|
2005-07-01 07:55:19 +00:00
|
|
|
EBU1CONFIG = 0x800;
|
2005-03-31 06:49:10 +00:00
|
|
|
}
|
2005-06-10 20:46:39 +00:00
|
|
|
pcm_paused = !play;
|
2005-03-31 06:49:10 +00:00
|
|
|
}
|
|
|
|
|
2005-03-18 11:39:28 +00:00
|
|
|
bool pcm_is_playing(void)
|
|
|
|
{
|
|
|
|
return pcm_playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DMA0 Interrupt is called when the DMA has finished transfering a chunk */
|
|
|
|
void DMA0(void) __attribute__ ((interrupt_handler, section(".icode")));
|
|
|
|
void DMA0(void)
|
|
|
|
{
|
2005-03-28 00:00:24 +00:00
|
|
|
int res = DSR0;
|
|
|
|
|
2005-03-18 11:39:28 +00:00
|
|
|
DSR0 = 1; /* Clear interrupt */
|
2005-07-01 17:05:09 +00:00
|
|
|
DCR0 &= ~DMA_EEXT;
|
2005-06-30 20:02:56 +00:00
|
|
|
|
2005-03-31 06:49:10 +00:00
|
|
|
/* Stop on error */
|
|
|
|
if(res & 0x70)
|
2005-03-28 00:00:24 +00:00
|
|
|
{
|
|
|
|
dma_stop();
|
2005-07-01 17:05:09 +00:00
|
|
|
logf("DMA Error");
|
2005-03-28 00:00:24 +00:00
|
|
|
}
|
2005-03-18 11:39:28 +00:00
|
|
|
else
|
|
|
|
{
|
2005-06-05 23:05:10 +00:00
|
|
|
if(next_size)
|
2005-03-31 06:49:10 +00:00
|
|
|
{
|
2005-06-05 23:05:10 +00:00
|
|
|
SAR0 = (unsigned long)next_start; /* Source address */
|
|
|
|
BCR0 = next_size; /* Bytes to transfer */
|
2005-07-01 17:05:09 +00:00
|
|
|
DCR0 |= DMA_EEXT;
|
2005-06-07 06:34:54 +00:00
|
|
|
if (callback_for_more)
|
2005-06-05 23:05:10 +00:00
|
|
|
callback_for_more(&next_start, &next_size);
|
2005-03-31 06:49:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Finished playing */
|
|
|
|
dma_stop();
|
2005-07-01 17:05:09 +00:00
|
|
|
logf("DMA No Data");
|
2005-03-31 06:49:10 +00:00
|
|
|
}
|
2005-03-18 11:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IPR |= (1<<14); /* Clear pending interrupt request */
|
|
|
|
}
|
2005-03-31 06:49:10 +00:00
|
|
|
|
|
|
|
void pcm_init(void)
|
|
|
|
{
|
|
|
|
pcm_playing = false;
|
|
|
|
pcm_paused = false;
|
|
|
|
|
|
|
|
uda1380_init();
|
|
|
|
|
|
|
|
BUSMASTER_CTRL = 0x81; /* PARK[1,0]=10 + BCR24BIT */
|
|
|
|
DIVR0 = 54; /* DMA0 is mapped into vector 54 in system.c */
|
|
|
|
DMAROUTE = (DMAROUTE & 0xffffff00) | DMA0_REQ_AUDIO_1;
|
|
|
|
DMACONFIG = 1; /* DMA0Req = PDOR3 */
|
|
|
|
|
|
|
|
/* Reset the audio FIFO */
|
|
|
|
IIS2CONFIG = 0x800;
|
|
|
|
|
|
|
|
/* Enable interrupt at level 7, priority 0 */
|
|
|
|
ICR4 = (ICR4 & 0xffff00ff) | 0x00001c00;
|
|
|
|
IMR &= ~(1<<14); /* bit 14 is DMA0 */
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
pcm_play_init();
|
2005-03-31 06:49:10 +00:00
|
|
|
pcm_set_frequency(44100);
|
|
|
|
}
|
2005-04-14 11:51:31 +00:00
|
|
|
|
|
|
|
void pcm_play_set_watermark(int numbytes, void (*callback)(int bytes_left))
|
|
|
|
{
|
|
|
|
pcmbuf_watermark = numbytes;
|
|
|
|
pcmbuf_watermark_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcm_play_add_chunk(void *addr, int size, void (*callback)(void))
|
|
|
|
{
|
|
|
|
/* We don't use the last buffer, since we can't see the difference
|
|
|
|
between the full and empty condition */
|
2005-06-05 23:05:10 +00:00
|
|
|
if(pcm_play_num_used_buffers() < (NUM_PCM_BUFFERS - 2))
|
2005-04-14 11:51:31 +00:00
|
|
|
{
|
|
|
|
pcmbuffers[pcmbuf_write_index].addr = addr;
|
|
|
|
pcmbuffers[pcmbuf_write_index].size = size;
|
|
|
|
pcmbuffers[pcmbuf_write_index].callback = callback;
|
|
|
|
pcmbuf_write_index = (pcmbuf_write_index+1) & NUM_PCM_BUFFERS_MASK;
|
|
|
|
pcmbuf_unplayed_bytes += size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void pcm_watermark_callback(int bytes_left)
|
2005-04-14 11:51:31 +00:00
|
|
|
{
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Fill audio buffer by boosting cpu */
|
|
|
|
pcm_boost(true);
|
2005-06-19 20:20:16 +00:00
|
|
|
if (bytes_left <= CHUNK_SIZE * 2)
|
|
|
|
crossfade_active = false;
|
2005-04-14 11:51:31 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void pcm_set_boost_mode(bool state)
|
|
|
|
{
|
2005-06-10 17:33:16 +00:00
|
|
|
if (state)
|
|
|
|
pcm_boost(true);
|
2005-06-11 20:14:10 +00:00
|
|
|
boost_mode = state;
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2005-04-14 11:51:31 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void audiobuffer_add_event(void (*event_handler)(void))
|
2005-04-14 11:51:31 +00:00
|
|
|
{
|
2005-06-15 18:59:04 +00:00
|
|
|
pcm_event_handler = event_handler;
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2005-04-14 11:51:31 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
unsigned int audiobuffer_get_latency(void)
|
|
|
|
{
|
|
|
|
int latency;
|
2005-04-14 11:51:31 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* This has to be done better. */
|
2005-06-20 06:49:21 +00:00
|
|
|
latency = (PCMBUF_SIZE - audiobuffer_free - CHUNK_SIZE)/4 / (44100/1000);
|
2005-06-05 23:05:10 +00:00
|
|
|
if (latency < 0)
|
|
|
|
latency = 0;
|
2005-04-14 11:51:31 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
return latency;
|
|
|
|
}
|
2005-04-14 11:51:31 +00:00
|
|
|
|
2005-06-07 06:34:54 +00:00
|
|
|
bool pcm_is_lowdata(void)
|
|
|
|
{
|
2005-06-14 14:36:46 +00:00
|
|
|
if (!pcm_is_playing() || pcm_paused)
|
2005-06-07 06:34:54 +00:00
|
|
|
return false;
|
|
|
|
|
2005-06-19 20:20:16 +00:00
|
|
|
if (pcmbuf_unplayed_bytes < PCM_WATERMARK || crossfade_active)
|
2005-06-07 06:34:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-06-20 14:27:06 +00:00
|
|
|
bool pcm_crossfade_init(void)
|
2005-06-09 07:19:16 +00:00
|
|
|
{
|
2005-06-19 20:20:16 +00:00
|
|
|
if (PCMBUF_SIZE - audiobuffer_free < CHUNK_SIZE * 8 || !crossfade_enabled) {
|
2005-06-14 18:59:34 +00:00
|
|
|
return false;
|
2005-06-09 07:19:16 +00:00
|
|
|
}
|
2005-06-19 18:41:53 +00:00
|
|
|
logf("crossfading!");
|
2005-06-29 20:50:58 +00:00
|
|
|
crossfade_mode = CFM_CROSSFADE;
|
2005-06-20 14:27:06 +00:00
|
|
|
crossfade_init = true;
|
2005-06-19 20:20:16 +00:00
|
|
|
|
2005-06-20 14:27:06 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-06-29 20:50:58 +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.
|
|
|
|
*/
|
|
|
|
void pcm_flush_audio(void)
|
|
|
|
{
|
2005-06-30 16:28:40 +00:00
|
|
|
if (crossfade_init || crossfade_active || !pcm_playing)
|
2005-06-29 20:50:58 +00:00
|
|
|
return ;
|
|
|
|
|
|
|
|
crossfade_mode = CFM_FLUSH;
|
|
|
|
crossfade_init = true;
|
|
|
|
}
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
void pcm_flush_fillpos(void)
|
2005-06-20 14:27:06 +00:00
|
|
|
{
|
2005-06-19 20:20:16 +00:00
|
|
|
if (audiobuffer_fillpos) {
|
|
|
|
while (!pcm_play_add_chunk(&audiobuffer[audiobuffer_pos],
|
|
|
|
audiobuffer_fillpos, pcm_event_handler)) {
|
2005-07-01 17:05:09 +00:00
|
|
|
pcm_boost(false);
|
2005-06-19 20:20:16 +00:00
|
|
|
yield();
|
2005-07-01 17:05:09 +00:00
|
|
|
/* This is a fatal error situation that should never happen. */
|
|
|
|
if (!pcm_playing)
|
|
|
|
break ;
|
2005-06-19 20:20:16 +00:00
|
|
|
}
|
|
|
|
pcm_event_handler = NULL;
|
2005-06-20 18:06:33 +00:00
|
|
|
audiobuffer_pos += audiobuffer_fillpos;
|
|
|
|
if (audiobuffer_pos >= PCMBUF_SIZE)
|
|
|
|
audiobuffer_pos -= PCMBUF_SIZE;
|
2005-06-26 19:41:29 +00:00
|
|
|
audiobuffer_free -= audiobuffer_fillpos;
|
|
|
|
audiobuffer_fillpos = 0;
|
2005-06-19 20:20:16 +00:00
|
|
|
}
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void crossfade_start(void)
|
|
|
|
{
|
|
|
|
crossfade_init = 0;
|
2005-06-29 20:50:58 +00:00
|
|
|
if (PCMBUF_SIZE - audiobuffer_free < CHUNK_SIZE * 4) {
|
|
|
|
if (crossfade_mode == CFM_FLUSH)
|
|
|
|
pcm_play_stop();
|
2005-06-26 19:41:29 +00:00
|
|
|
return ;
|
2005-06-29 20:50:58 +00:00
|
|
|
}
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
pcm_flush_fillpos();
|
2005-06-09 07:19:16 +00:00
|
|
|
pcm_boost(true);
|
|
|
|
crossfade_active = true;
|
|
|
|
crossfade_pos = audiobuffer_pos;
|
2005-06-29 20:50:58 +00:00
|
|
|
|
|
|
|
switch (crossfade_mode) {
|
|
|
|
case CFM_CROSSFADE:
|
|
|
|
crossfade_amount = (PCMBUF_SIZE - audiobuffer_free - (CHUNK_SIZE * 2))/2;
|
|
|
|
crossfade_rem = crossfade_amount;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case CFM_FLUSH:
|
|
|
|
crossfade_amount = (PCMBUF_SIZE - audiobuffer_free - (CHUNK_SIZE * 2))/2;
|
|
|
|
crossfade_rem = crossfade_amount;
|
|
|
|
break ;
|
|
|
|
}
|
2005-06-09 07:19:16 +00:00
|
|
|
|
|
|
|
crossfade_pos -= crossfade_amount*2;
|
|
|
|
if (crossfade_pos < 0)
|
2005-06-19 18:41:53 +00:00
|
|
|
crossfade_pos += PCMBUF_SIZE;
|
2005-06-09 07:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline
|
2005-06-19 20:20:16 +00:00
|
|
|
int crossfade(short *buf, const short *buf2, int length)
|
2005-06-09 07:19:16 +00:00
|
|
|
{
|
2005-06-29 20:50:58 +00:00
|
|
|
int size, i;
|
|
|
|
int val1, val2;
|
2005-06-19 18:41:53 +00:00
|
|
|
|
|
|
|
size = MIN(length, crossfade_rem);
|
2005-06-29 20:50:58 +00:00
|
|
|
switch (crossfade_mode) {
|
|
|
|
case CFM_CROSSFADE:
|
|
|
|
val1 = (crossfade_rem<<10)/crossfade_amount;
|
|
|
|
val2 = ((crossfade_amount-crossfade_rem)<<10)/crossfade_amount;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
buf[i] = ((buf[i] * val1) + (buf2[i] * val2)) >> 10;
|
|
|
|
}
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case CFM_FLUSH:
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
buf[i] = buf2[i];
|
|
|
|
}
|
|
|
|
//memcpy((char *)buf, (char *)buf2, size*2);
|
|
|
|
break ;
|
2005-06-09 07:19:16 +00:00
|
|
|
}
|
2005-06-29 20:50:58 +00:00
|
|
|
|
|
|
|
crossfade_rem -= size;
|
2005-06-19 20:20:16 +00:00
|
|
|
if (crossfade_rem <= 0)
|
2005-06-19 18:41:53 +00:00
|
|
|
crossfade_active = false;
|
2005-06-29 20:50:58 +00:00
|
|
|
|
2005-06-19 20:20:16 +00:00
|
|
|
return size;
|
2005-06-09 07:19:16 +00:00
|
|
|
}
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
inline static bool prepare_insert(long length)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2005-06-29 20:50:58 +00:00
|
|
|
if (crossfade_init)
|
|
|
|
crossfade_start();
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
if (audiobuffer_free < length + audiobuffer_fillpos
|
|
|
|
+ CHUNK_SIZE && !crossfade_active) {
|
2005-06-11 20:14:10 +00:00
|
|
|
pcm_boost(false);
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
2005-04-14 11:51:31 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-07-01 17:05:09 +00:00
|
|
|
if (!pcm_is_playing()) {
|
2005-06-05 23:05:10 +00:00
|
|
|
pcm_boost(true);
|
2005-06-09 07:19:16 +00:00
|
|
|
crossfade_active = false;
|
2005-06-14 07:54:09 +00:00
|
|
|
if (audiobuffer_free < PCMBUF_SIZE - CHUNK_SIZE*4)
|
2005-06-05 23:05:10 +00:00
|
|
|
pcm_play_start();
|
2005-04-14 11:51:31 +00:00
|
|
|
}
|
2005-06-26 19:41:29 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
void* pcm_request_buffer(long length, long *realsize)
|
2005-06-26 19:41:29 +00:00
|
|
|
{
|
|
|
|
void *ptr = NULL;
|
|
|
|
|
|
|
|
if (!prepare_insert(length)) {
|
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crossfade_active) {
|
|
|
|
*realsize = MIN(length, PCMBUF_GUARD);
|
|
|
|
ptr = &guardbuf[0];
|
|
|
|
} else {
|
|
|
|
*realsize = MIN(length, PCMBUF_SIZE - audiobuffer_pos
|
|
|
|
- audiobuffer_fillpos);
|
|
|
|
if (*realsize < length) {
|
|
|
|
*realsize += MIN((long)(length - *realsize), PCMBUF_GUARD);
|
|
|
|
}
|
|
|
|
ptr = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2005-06-29 20:50:58 +00:00
|
|
|
bool pcm_is_crossfade_active(void)
|
|
|
|
{
|
|
|
|
return crossfade_active;
|
|
|
|
}
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
void pcm_flush_buffer(long length)
|
2005-06-26 19:41:29 +00:00
|
|
|
{
|
|
|
|
int copy_n;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
if (crossfade_active) {
|
|
|
|
buf = &guardbuf[0];
|
|
|
|
length = MIN(length, PCMBUF_GUARD);
|
|
|
|
while (length > 0 && crossfade_active) {
|
2005-06-27 19:29:49 +00:00
|
|
|
copy_n = MIN(length, PCMBUF_SIZE - crossfade_pos);
|
2005-06-26 19:41:29 +00:00
|
|
|
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 -= PCMBUF_SIZE;
|
|
|
|
}
|
|
|
|
|
2005-06-27 07:06:58 +00:00
|
|
|
while (length > 0) {
|
2005-06-27 19:29:49 +00:00
|
|
|
copy_n = MIN(length, PCMBUF_SIZE - audiobuffer_pos);
|
2005-06-27 07:06:58 +00:00
|
|
|
memcpy(&audiobuffer[audiobuffer_pos], buf, copy_n);
|
2005-06-27 13:39:21 +00:00
|
|
|
audiobuffer_fillpos = copy_n;
|
2005-06-27 07:06:58 +00:00
|
|
|
buf += copy_n;
|
|
|
|
length -= copy_n;
|
|
|
|
if (length > 0)
|
|
|
|
pcm_flush_fillpos();
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-06-27 07:06:58 +00:00
|
|
|
}
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2005-06-27 07:06:58 +00:00
|
|
|
audiobuffer_fillpos += length;
|
|
|
|
|
|
|
|
try_flush:
|
|
|
|
if (audiobuffer_fillpos < CHUNK_SIZE && PCMBUF_SIZE
|
|
|
|
- audiobuffer_pos - audiobuffer_fillpos > 0)
|
|
|
|
return ;
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
copy_n = audiobuffer_fillpos - (PCMBUF_SIZE - audiobuffer_pos);
|
2005-06-27 07:06:58 +00:00
|
|
|
if (copy_n > 0) {
|
|
|
|
audiobuffer_fillpos -= copy_n;
|
2005-06-26 19:41:29 +00:00
|
|
|
pcm_flush_fillpos();
|
2005-06-27 19:29:49 +00:00
|
|
|
copy_n = MIN(copy_n, PCMBUF_GUARD);
|
2005-06-27 07:06:58 +00:00
|
|
|
memcpy(&audiobuffer[0], &guardbuf[0], copy_n);
|
|
|
|
audiobuffer_fillpos = copy_n;
|
|
|
|
goto try_flush;
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-06-27 07:06:58 +00:00
|
|
|
pcm_flush_fillpos();
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
bool pcm_insert_buffer(char *buf, long length)
|
2005-06-26 19:41:29 +00:00
|
|
|
{
|
2005-06-27 19:29:49 +00:00
|
|
|
long copy_n = 0;
|
2005-06-26 19:41:29 +00:00
|
|
|
|
|
|
|
if (!prepare_insert(length))
|
|
|
|
return false;
|
2005-06-29 21:44:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (crossfade_active) {
|
|
|
|
while (length > 0 && crossfade_active) {
|
2005-06-27 19:29:49 +00:00
|
|
|
copy_n = MIN(length, PCMBUF_SIZE - crossfade_pos);
|
2005-06-29 21:44:23 +00:00
|
|
|
|
|
|
|
copy_n = 2 * crossfade((short *)&audiobuffer[crossfade_pos],
|
|
|
|
(const short *)buf, copy_n/2);
|
2005-06-09 07:19:16 +00:00
|
|
|
buf += copy_n;
|
|
|
|
length -= copy_n;
|
|
|
|
crossfade_pos += copy_n;
|
|
|
|
if (crossfade_pos >= PCMBUF_SIZE)
|
|
|
|
crossfade_pos -= PCMBUF_SIZE;
|
2005-06-29 21:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
copy_n = MIN(length, PCMBUF_SIZE - audiobuffer_pos);
|
|
|
|
memcpy(&audiobuffer[audiobuffer_pos], buf, copy_n);
|
|
|
|
audiobuffer_fillpos = copy_n;
|
2005-06-09 09:47:00 +00:00
|
|
|
buf += copy_n;
|
|
|
|
length -= copy_n;
|
2005-06-29 21:44:23 +00:00
|
|
|
if (length > 0)
|
|
|
|
pcm_flush_fillpos();
|
2005-06-09 07:19:16 +00:00
|
|
|
}
|
2005-06-29 21:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
copy_n = MIN(length, PCMBUF_SIZE - audiobuffer_pos -
|
|
|
|
audiobuffer_fillpos);
|
|
|
|
copy_n = MIN(CHUNK_SIZE - audiobuffer_fillpos, copy_n);
|
|
|
|
|
|
|
|
memcpy(&audiobuffer[audiobuffer_pos+audiobuffer_fillpos],
|
|
|
|
buf, copy_n);
|
|
|
|
buf += copy_n;
|
2005-06-30 16:28:40 +00:00
|
|
|
audiobuffer_fillpos += copy_n;
|
2005-06-29 21:44:23 +00:00
|
|
|
length -= copy_n;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* Pre-buffer to meet CHUNK_SIZE requirement */
|
2005-07-01 17:05:09 +00:00
|
|
|
if (audiobuffer_fillpos < CHUNK_SIZE && length == 0) {
|
2005-06-05 23:05:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2005-06-30 16:28:40 +00:00
|
|
|
|
|
|
|
pcm_flush_fillpos();
|
2005-04-14 11:51:31 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_init(void)
|
|
|
|
{
|
|
|
|
audiobuffer = &audiobuf[(audiobufend - audiobuf) -
|
2005-06-26 19:41:29 +00:00
|
|
|
PCMBUF_SIZE - PCMBUF_GUARD];
|
|
|
|
guardbuf = &audiobuffer[PCMBUF_SIZE];
|
2005-06-05 23:05:10 +00:00
|
|
|
audiobuffer_free = PCMBUF_SIZE;
|
|
|
|
audiobuffer_pos = 0;
|
|
|
|
audiobuffer_fillpos = 0;
|
|
|
|
boost_mode = 0;
|
|
|
|
pcmbuf_read_index = 0;
|
|
|
|
pcmbuf_write_index = 0;
|
|
|
|
pcmbuf_unplayed_bytes = 0;
|
2005-06-19 20:20:16 +00:00
|
|
|
crossfade_active = false;
|
2005-06-20 14:27:06 +00:00
|
|
|
crossfade_init = false;
|
2005-06-15 18:59:04 +00:00
|
|
|
pcm_event_handler = NULL;
|
2005-06-09 09:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_crossfade_enable(bool on_off)
|
|
|
|
{
|
|
|
|
crossfade_enabled = on_off;
|
2005-04-14 11:51:31 +00:00
|
|
|
}
|
|
|
|
|
2005-06-10 10:58:45 +00:00
|
|
|
bool pcm_is_crossfade_enabled(void)
|
|
|
|
{
|
|
|
|
return crossfade_enabled;
|
|
|
|
}
|
|
|
|
|
2005-04-14 11:51:31 +00:00
|
|
|
void pcm_play_start(void)
|
|
|
|
{
|
|
|
|
struct pcmbufdesc *desc = &pcmbuffers[pcmbuf_read_index];
|
|
|
|
int size;
|
|
|
|
char *start;
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
if (crossfade_enabled) {
|
|
|
|
pcm_play_set_watermark(PCM_CF_WATERMARK, pcm_watermark_callback);
|
|
|
|
} else {
|
|
|
|
pcm_play_set_watermark(PCM_WATERMARK, pcm_watermark_callback);
|
|
|
|
}
|
2005-06-09 07:19:16 +00:00
|
|
|
crossfade_active = false;
|
2005-06-29 20:50:58 +00:00
|
|
|
|
2005-04-14 11:51:31 +00:00
|
|
|
if(!pcm_is_playing())
|
|
|
|
{
|
|
|
|
size = MIN(desc->size, 32768);
|
|
|
|
start = desc->addr;
|
|
|
|
last_chunksize = size;
|
|
|
|
desc->size -= size;
|
|
|
|
desc->addr += size;
|
2005-06-05 23:05:10 +00:00
|
|
|
pcm_play_data(start, size, pcm_play_callback);
|
2005-04-14 11:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-18 22:23:54 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_UDA1380 */
|