2005-06-05 23:05:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "lcd.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "backlight.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "tree.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "sprintf.h"
|
|
|
|
#include "settings.h"
|
2005-06-22 19:41:30 +00:00
|
|
|
#include "codecs.h"
|
2005-06-05 23:05:10 +00:00
|
|
|
#include "audio.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "mp3_playback.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "status.h"
|
|
|
|
#include "main_menu.h"
|
|
|
|
#include "ata.h"
|
|
|
|
#include "screens.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "playback.h"
|
2005-07-13 12:48:22 +00:00
|
|
|
#include "pcmbuf.h"
|
2005-06-05 23:05:10 +00:00
|
|
|
#include "pcm_playback.h"
|
2005-12-20 21:49:47 +00:00
|
|
|
#include "pcm_record.h"
|
2005-06-05 23:05:10 +00:00
|
|
|
#include "buffer.h"
|
2005-06-26 19:41:29 +00:00
|
|
|
#include "dsp.h"
|
2006-01-21 23:43:57 +00:00
|
|
|
#include "abrepeat.h"
|
2005-06-05 23:05:10 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
#include "icons.h"
|
|
|
|
#include "peakmeter.h"
|
|
|
|
#include "action.h"
|
|
|
|
#endif
|
|
|
|
#include "lang.h"
|
|
|
|
#include "bookmark.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "sound.h"
|
2005-06-14 22:27:57 +00:00
|
|
|
#include "metadata.h"
|
2005-08-20 11:13:19 +00:00
|
|
|
#include "talk.h"
|
2005-11-13 10:57:35 +00:00
|
|
|
#ifdef CONFIG_TUNER
|
|
|
|
#include "radio.h"
|
|
|
|
#endif
|
2005-11-16 15:12:15 +00:00
|
|
|
#include "splash.h"
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
static volatile bool audio_codec_loaded;
|
|
|
|
static volatile bool voice_codec_loaded;
|
2005-06-05 23:05:10 +00:00
|
|
|
static volatile bool playing;
|
|
|
|
static volatile bool paused;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
#define CODEC_VORBIS "/.rockbox/codecs/vorbis.codec"
|
|
|
|
#define CODEC_MPA_L3 "/.rockbox/codecs/mpa.codec"
|
|
|
|
#define CODEC_FLAC "/.rockbox/codecs/flac.codec"
|
|
|
|
#define CODEC_WAV "/.rockbox/codecs/wav.codec"
|
|
|
|
#define CODEC_A52 "/.rockbox/codecs/a52.codec"
|
|
|
|
#define CODEC_MPC "/.rockbox/codecs/mpc.codec"
|
|
|
|
#define CODEC_WAVPACK "/.rockbox/codecs/wavpack.codec"
|
2005-09-22 21:55:37 +00:00
|
|
|
#define CODEC_ALAC "/.rockbox/codecs/alac.codec"
|
2005-10-31 20:56:29 +00:00
|
|
|
#define CODEC_AAC "/.rockbox/codecs/aac.codec"
|
2005-11-11 19:45:36 +00:00
|
|
|
#define CODEC_SHN "/.rockbox/codecs/shorten.codec"
|
2006-02-01 16:42:02 +00:00
|
|
|
#define CODEC_AIFF "/.rockbox/codecs/aiff.codec"
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-11-18 20:21:13 +00:00
|
|
|
#define AUDIO_DEFAULT_FIRST_LIMIT (1024*1024*10)
|
2005-06-19 18:41:53 +00:00
|
|
|
#define AUDIO_FILL_CYCLE (1024*256)
|
2005-06-29 14:46:27 +00:00
|
|
|
#define AUDIO_DEFAULT_WATERMARK (1024*512)
|
2005-06-10 15:02:10 +00:00
|
|
|
#define AUDIO_DEFAULT_FILECHUNK (1024*32)
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
enum {
|
|
|
|
Q_AUDIO_PLAY = 1,
|
|
|
|
Q_AUDIO_STOP,
|
|
|
|
Q_AUDIO_PAUSE,
|
|
|
|
Q_AUDIO_RESUME,
|
|
|
|
Q_AUDIO_NEXT,
|
|
|
|
Q_AUDIO_PREV,
|
|
|
|
Q_AUDIO_FF_REWIND,
|
|
|
|
Q_AUDIO_FLUSH_RELOAD,
|
|
|
|
Q_AUDIO_CODEC_DONE,
|
|
|
|
Q_AUDIO_FLUSH,
|
|
|
|
Q_AUDIO_TRACK_CHANGED,
|
|
|
|
Q_AUDIO_DIR_NEXT,
|
|
|
|
Q_AUDIO_DIR_PREV,
|
|
|
|
Q_AUDIO_SEAMLESS_SEEK,
|
|
|
|
Q_AUDIO_POSTINIT,
|
|
|
|
|
|
|
|
Q_CODEC_LOAD,
|
|
|
|
Q_CODEC_LOAD_DISK,
|
|
|
|
};
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* As defined in plugins/lib/xxx2wav.h */
|
|
|
|
#define MALLOC_BUFSIZE (512*1024)
|
2005-10-30 09:30:14 +00:00
|
|
|
#define GUARD_BUFSIZE (32*1024)
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
/* As defined in plugin.lds */
|
2005-09-01 20:57:33 +00:00
|
|
|
#define CODEC_IRAM_ORIGIN 0x1000c000
|
|
|
|
#define CODEC_IRAM_SIZE 0xc000
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
extern bool audio_is_initialized;
|
|
|
|
|
|
|
|
/* Buffer control thread. */
|
|
|
|
static struct event_queue audio_queue;
|
|
|
|
static long audio_stack[(DEFAULT_STACK_SIZE + 0x1000)/sizeof(long)];
|
|
|
|
static const char audio_thread_name[] = "audio";
|
|
|
|
|
|
|
|
/* Codec thread. */
|
|
|
|
static struct event_queue codec_queue;
|
2005-10-19 19:35:24 +00:00
|
|
|
static long codec_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)] IBSS_ATTR;
|
2005-06-05 23:05:10 +00:00
|
|
|
static const char codec_thread_name[] = "codec";
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
/* Voice codec thread. */
|
|
|
|
static struct event_queue voice_codec_queue;
|
|
|
|
/* Not enough IRAM for this. */
|
2005-10-19 19:35:24 +00:00
|
|
|
static long voice_codec_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)] IBSS_ATTR;
|
2005-08-20 11:13:19 +00:00
|
|
|
static const char voice_codec_thread_name[] = "voice codec";
|
|
|
|
|
2005-07-01 18:22:04 +00:00
|
|
|
static struct mutex mutex_bufferfill;
|
2005-08-20 11:13:19 +00:00
|
|
|
static struct mutex mutex_codecthread;
|
|
|
|
|
|
|
|
static struct mp3entry id3_voice;
|
|
|
|
|
|
|
|
static char *voicebuf;
|
|
|
|
static int voice_remaining;
|
|
|
|
static bool voice_is_playing;
|
|
|
|
static void (*voice_getmore)(unsigned char** start, int* size);
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Is file buffer currently being refilled? */
|
|
|
|
static volatile bool filling;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
volatile int current_codec;
|
|
|
|
extern unsigned char codecbuf[];
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Ring buffer where tracks and codecs are loaded. */
|
2005-08-20 11:13:19 +00:00
|
|
|
static char *filebuf;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* Total size of the ring buffer. */
|
2005-08-20 11:13:19 +00:00
|
|
|
int filebuflen;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* Bytes available in the buffer. */
|
2005-08-20 11:13:19 +00:00
|
|
|
int filebufused;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* Ring buffer read and write indexes. */
|
2005-06-07 06:34:54 +00:00
|
|
|
static volatile int buf_ridx;
|
|
|
|
static volatile int buf_widx;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-07-15 16:42:01 +00:00
|
|
|
/* Step count to the next unbuffered track. */
|
2005-06-19 18:41:53 +00:00
|
|
|
static int last_peek_offset;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Track information (count in file buffer, read/write indexes for
|
|
|
|
track ring structure. */
|
2005-06-22 20:37:31 +00:00
|
|
|
int track_count;
|
2005-06-07 06:34:54 +00:00
|
|
|
static volatile int track_ridx;
|
|
|
|
static volatile int track_widx;
|
2005-06-05 23:05:10 +00:00
|
|
|
static bool track_changed;
|
|
|
|
|
|
|
|
/* Partially loaded song's file handle to continue buffering later. */
|
|
|
|
static int current_fd;
|
|
|
|
|
|
|
|
/* Information about how many bytes left on the buffer re-fill run. */
|
2005-06-27 19:29:49 +00:00
|
|
|
static long fill_bytesleft;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* Track info structure about songs in the file buffer. */
|
|
|
|
static struct track_info tracks[MAX_TRACK];
|
|
|
|
|
|
|
|
/* Pointer to track info structure about current song playing. */
|
2005-06-29 21:36:30 +00:00
|
|
|
static struct track_info *cur_ti;
|
2006-01-21 22:42:44 +00:00
|
|
|
static struct track_info *prev_ti;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-12-03 12:14:26 +00:00
|
|
|
/* Have we reached end of the current playlist. */
|
|
|
|
static bool playlist_end = false;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Codec API including function callbacks. */
|
2005-06-22 19:41:30 +00:00
|
|
|
extern struct codec_api ci;
|
2005-08-20 11:13:19 +00:00
|
|
|
extern struct codec_api ci_voice;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* When we change a song and buffer is not in filling state, this
|
|
|
|
variable keeps information about whether to go a next/previous track. */
|
|
|
|
static int new_track;
|
|
|
|
|
2005-06-29 21:36:30 +00:00
|
|
|
/* Callback function to call when current track has really changed. */
|
2005-08-18 06:05:15 +00:00
|
|
|
void (*track_changed_callback)(struct mp3entry *id3);
|
2005-07-02 16:52:30 +00:00
|
|
|
void (*track_buffer_callback)(struct mp3entry *id3, bool last_track);
|
2005-07-03 18:36:24 +00:00
|
|
|
void (*track_unbuffer_callback)(struct mp3entry *id3, bool last_track);
|
2005-06-29 21:36:30 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
static void playback_init(void);
|
|
|
|
|
2005-06-10 15:02:10 +00:00
|
|
|
/* Configuration */
|
|
|
|
static int conf_bufferlimit;
|
|
|
|
static int conf_watermark;
|
|
|
|
static int conf_filechunk;
|
2005-07-21 11:44:00 +00:00
|
|
|
static int buffer_margin;
|
2005-06-10 13:43:12 +00:00
|
|
|
|
2005-06-06 00:35:21 +00:00
|
|
|
static bool v1first = false;
|
|
|
|
|
2005-06-08 10:33:01 +00:00
|
|
|
static void mp3_set_elapsed(struct mp3entry* id3);
|
|
|
|
int mp3_get_file_pos(void);
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
#ifdef TIME_CODEC
|
|
|
|
bool is_filling(void)
|
|
|
|
{
|
|
|
|
return filling;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
static void do_swap(int idx_old, int idx_new)
|
|
|
|
{
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
unsigned char *iram_p = (unsigned char *)(CODEC_IRAM_ORIGIN);
|
|
|
|
unsigned char *iram_buf[2];
|
|
|
|
#endif
|
|
|
|
unsigned char *dram_buf[2];
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
iram_buf[0] = &filebuf[filebuflen];
|
|
|
|
iram_buf[1] = &filebuf[filebuflen+CODEC_IRAM_SIZE];
|
|
|
|
memcpy(iram_buf[idx_old], iram_p, CODEC_IRAM_SIZE);
|
|
|
|
memcpy(iram_p, iram_buf[idx_new], CODEC_IRAM_SIZE);
|
|
|
|
#endif
|
|
|
|
|
2005-12-05 22:44:42 +00:00
|
|
|
dram_buf[0] = (unsigned char *)&filebuf[filebuflen+CODEC_IRAM_SIZE*2];
|
|
|
|
dram_buf[1] = (unsigned char *)&filebuf[filebuflen+CODEC_IRAM_SIZE*2+CODEC_SIZE];
|
2005-08-20 11:13:19 +00:00
|
|
|
memcpy(dram_buf[idx_old], codecbuf, CODEC_SIZE);
|
|
|
|
memcpy(codecbuf, dram_buf[idx_new], CODEC_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void swap_codec(void)
|
|
|
|
{
|
|
|
|
int last_codec;
|
|
|
|
|
|
|
|
logf("swapping codec:%d", current_codec);
|
|
|
|
|
|
|
|
/* We should swap codecs' IRAM contents and code space. */
|
|
|
|
do_swap(current_codec, !current_codec);
|
|
|
|
|
|
|
|
last_codec = current_codec;
|
|
|
|
current_codec = !current_codec;
|
|
|
|
|
|
|
|
/* Release the semaphore and force a task switch. */
|
|
|
|
mutex_unlock(&mutex_codecthread);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
/* Waiting until we are ready to run again. */
|
|
|
|
mutex_lock(&mutex_codecthread);
|
|
|
|
|
|
|
|
/* Check if codec swap did not happen. */
|
|
|
|
if (current_codec != last_codec)
|
|
|
|
{
|
|
|
|
logf("no codec switch happened!");
|
|
|
|
do_swap(current_codec, !current_codec);
|
|
|
|
current_codec = !current_codec;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidate_icache();
|
|
|
|
logf("codec resuming:%d", current_codec);
|
|
|
|
}
|
|
|
|
|
2005-08-28 19:55:30 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2005-08-28 14:16:03 +00:00
|
|
|
static void voice_boost_cpu(bool state)
|
|
|
|
{
|
2005-08-28 19:55:30 +00:00
|
|
|
static bool voice_cpu_boosted = false;
|
2005-11-19 14:04:07 +00:00
|
|
|
|
|
|
|
if (!voice_codec_loaded)
|
|
|
|
state = false;
|
|
|
|
|
2005-08-28 14:16:03 +00:00
|
|
|
if (state != voice_cpu_boosted)
|
|
|
|
{
|
|
|
|
cpu_boost(state);
|
|
|
|
voice_cpu_boosted = state;
|
|
|
|
}
|
|
|
|
}
|
2005-08-28 19:55:30 +00:00
|
|
|
#else
|
|
|
|
#define voice_boost_cpu(state) do { } while(0)
|
|
|
|
#endif
|
2005-08-28 14:16:03 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
bool codec_pcmbuf_insert_split_callback(const void *ch1, const void *ch2,
|
|
|
|
size_t length)
|
2005-06-26 19:41:29 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
const char* src[2];
|
2005-06-26 19:41:29 +00:00
|
|
|
char *dest;
|
2005-07-16 12:25:28 +00:00
|
|
|
long input_size;
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t output_size;
|
2005-07-16 12:25:28 +00:00
|
|
|
|
|
|
|
src[0] = ch1;
|
|
|
|
src[1] = ch2;
|
|
|
|
|
2005-09-10 21:02:42 +00:00
|
|
|
while (paused)
|
|
|
|
{
|
|
|
|
sleep(1);
|
|
|
|
if (ci.stop_codec || ci.reload_codec || ci.seek_time)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-16 12:25:28 +00:00
|
|
|
if (dsp_stereo_mode() == STEREO_NONINTERLEAVED)
|
|
|
|
{
|
|
|
|
length *= 2; /* Length is per channel */
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
while (length > 0) {
|
2006-02-07 20:38:55 +00:00
|
|
|
long est_output_size = dsp_output_size(length);
|
2005-10-21 06:40:45 +00:00
|
|
|
/* This will prevent old audio from playing when skipping tracks. */
|
2006-02-07 20:38:55 +00:00
|
|
|
if (current_codec == CODEC_IDX_VOICE) {
|
|
|
|
while ((dest = pcmbuf_request_voice_buffer(est_output_size,
|
|
|
|
&output_size, audio_codec_loaded)) == NULL)
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ci.reload_codec || ci.stop_codec)
|
2005-10-21 06:40:45 +00:00
|
|
|
return true;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
|
|
|
while ((dest = pcmbuf_request_buffer(est_output_size,
|
|
|
|
&output_size)) == NULL) {
|
|
|
|
sleep(1);
|
|
|
|
if (ci.reload_codec || ci.stop_codec)
|
|
|
|
return true;
|
|
|
|
}
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2005-08-10 23:17:55 +00:00
|
|
|
/* Get the real input_size for output_size bytes, guarding
|
|
|
|
* against resampling buffer overflows. */
|
2005-07-16 12:25:28 +00:00
|
|
|
input_size = dsp_input_size(output_size);
|
2005-08-10 23:17:55 +00:00
|
|
|
|
2005-07-16 12:25:28 +00:00
|
|
|
if (input_size <= 0) {
|
2005-08-10 23:17:55 +00:00
|
|
|
DEBUGF("Warning: dsp_input_size(%ld=dsp_output_size(%ld))=%ld <= 0\n",
|
|
|
|
output_size, length, input_size);
|
2006-02-07 20:38:55 +00:00
|
|
|
/* this cannot happen */
|
|
|
|
break;
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
if ((size_t)input_size > length) {
|
|
|
|
DEBUGF("Error: dsp_input_size(%ld=dsp_output_size(%ld))=%ld > %ld\n",
|
|
|
|
output_size, length, input_size, length);
|
|
|
|
input_size = length;
|
|
|
|
}
|
|
|
|
|
2005-07-16 12:25:28 +00:00
|
|
|
output_size = dsp_process(dest, src, input_size);
|
2005-08-20 11:13:19 +00:00
|
|
|
|
|
|
|
/* Hotswap between audio and voice codecs as necessary. */
|
|
|
|
switch (current_codec)
|
|
|
|
{
|
|
|
|
case CODEC_IDX_AUDIO:
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_write_complete(output_size);
|
2005-08-20 11:13:19 +00:00
|
|
|
if (voice_is_playing && pcmbuf_usage() > 30
|
|
|
|
&& pcmbuf_mix_usage() < 20)
|
|
|
|
{
|
2005-08-28 14:16:03 +00:00
|
|
|
voice_boost_cpu(true);
|
2005-08-20 11:13:19 +00:00
|
|
|
swap_codec();
|
2005-08-28 14:16:03 +00:00
|
|
|
voice_boost_cpu(false);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case CODEC_IDX_VOICE:
|
|
|
|
if (audio_codec_loaded) {
|
|
|
|
pcmbuf_mix(dest, output_size);
|
|
|
|
if ((pcmbuf_usage() < 10)
|
|
|
|
|| pcmbuf_mix_usage() > 70)
|
|
|
|
swap_codec();
|
|
|
|
} else {
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_write_complete(output_size);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
2005-07-16 12:25:28 +00:00
|
|
|
length -= input_size;
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
bool codec_pcmbuf_insert_callback(const char *buf, size_t length)
|
2005-06-26 19:41:29 +00:00
|
|
|
{
|
2005-07-16 12:25:28 +00:00
|
|
|
/* TODO: The audiobuffer API should probably be updated, and be based on
|
|
|
|
* pcmbuf_insert_split().
|
|
|
|
*/
|
|
|
|
long real_length = length;
|
|
|
|
|
|
|
|
if (dsp_stereo_mode() == STEREO_NONINTERLEAVED)
|
|
|
|
{
|
|
|
|
length /= 2; /* Length is per channel */
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-07-16 12:25:28 +00:00
|
|
|
|
|
|
|
/* Second channel is only used for non-interleaved stereo. */
|
|
|
|
return codec_pcmbuf_insert_split_callback(buf, buf + (real_length / 2),
|
|
|
|
length);
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
void* get_codec_memory_callback(long *size)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
|
|
|
*size = MALLOC_BUFSIZE;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (voice_codec_loaded)
|
|
|
|
return &audiobuf[talk_get_bufsize()];
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
return &audiobuf[0];
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_position_callback(size_t size) ICODE_ATTR;
|
|
|
|
static void pcmbuf_position_callback(size_t size) {
|
|
|
|
unsigned int time = size * 1000 / 4 / NATIVE_FREQUENCY +
|
|
|
|
prev_ti->id3.elapsed;
|
2006-01-21 22:42:44 +00:00
|
|
|
if (time >= prev_ti->id3.length) {
|
|
|
|
pcmbuf_set_position_callback(NULL);
|
2006-01-22 04:35:28 +00:00
|
|
|
prev_ti->id3.elapsed = prev_ti->id3.length;
|
2006-01-21 22:42:44 +00:00
|
|
|
} else {
|
|
|
|
prev_ti->id3.elapsed = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void codec_set_elapsed_callback(unsigned int value)
|
|
|
|
{
|
|
|
|
unsigned int latency;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (ci.stop_codec || current_codec == CODEC_IDX_VOICE)
|
2005-06-20 06:49:21 +00:00
|
|
|
return ;
|
|
|
|
|
2006-01-21 23:43:57 +00:00
|
|
|
#ifdef AB_REPEAT_ENABLE
|
|
|
|
ab_position_report(value);
|
|
|
|
#endif
|
2005-07-13 12:48:22 +00:00
|
|
|
latency = pcmbuf_get_latency();
|
2005-06-15 12:53:50 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
if (value < latency) {
|
|
|
|
cur_ti->id3.elapsed = 0;
|
|
|
|
} else if (value - latency > cur_ti->id3.elapsed
|
|
|
|
|| value - latency < cur_ti->id3.elapsed - 2) {
|
|
|
|
cur_ti->id3.elapsed = value - latency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-12 16:45:38 +00:00
|
|
|
void codec_set_offset_callback(unsigned int value)
|
|
|
|
{
|
|
|
|
unsigned int latency;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (ci.stop_codec || current_codec == CODEC_IDX_VOICE)
|
2005-07-12 16:45:38 +00:00
|
|
|
return ;
|
|
|
|
|
2005-07-22 06:32:55 +00:00
|
|
|
latency = pcmbuf_get_latency() * cur_ti->id3.bitrate / 8;
|
2005-07-12 16:45:38 +00:00
|
|
|
|
|
|
|
if (value < latency) {
|
|
|
|
cur_ti->id3.offset = 0;
|
2005-08-10 22:54:59 +00:00
|
|
|
} else {
|
2005-07-12 16:45:38 +00:00
|
|
|
cur_ti->id3.offset = value - latency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
long codec_filebuf_callback(void *ptr, long size)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
|
|
|
char *buf = (char *)ptr;
|
|
|
|
int copy_n;
|
|
|
|
int part_n;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (ci.stop_codec || !playing || current_codec == CODEC_IDX_VOICE)
|
2005-06-05 23:05:10 +00:00
|
|
|
return 0;
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
copy_n = MIN((off_t)size, (off_t)cur_ti->available + cur_ti->filerem);
|
|
|
|
|
|
|
|
while (copy_n > cur_ti->available) {
|
|
|
|
yield();
|
2005-07-30 20:46:38 +00:00
|
|
|
if (ci.stop_codec || ci.reload_codec)
|
2005-06-05 23:05:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_n == 0)
|
|
|
|
return 0;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
part_n = MIN(copy_n, filebuflen - buf_ridx);
|
|
|
|
memcpy(buf, &filebuf[buf_ridx], part_n);
|
2005-06-05 23:05:10 +00:00
|
|
|
if (part_n < copy_n) {
|
2005-08-20 11:13:19 +00:00
|
|
|
memcpy(&buf[part_n], &filebuf[0], copy_n - part_n);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf_ridx += copy_n;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (buf_ridx >= filebuflen)
|
|
|
|
buf_ridx -= filebuflen;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.curpos += copy_n;
|
|
|
|
cur_ti->available -= copy_n;
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused -= copy_n;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
return copy_n;
|
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
void* voice_request_data(long *realsize, long reqsize)
|
|
|
|
{
|
|
|
|
while (queue_empty(&voice_codec_queue) && (voice_remaining == 0
|
|
|
|
|| voicebuf == NULL) && !ci_voice.stop_codec)
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
if (audio_codec_loaded && (pcmbuf_usage() < 30
|
|
|
|
|| !voice_is_playing || voicebuf == NULL))
|
|
|
|
{
|
|
|
|
swap_codec();
|
|
|
|
}
|
2005-08-28 14:16:03 +00:00
|
|
|
else if (!voice_is_playing)
|
|
|
|
{
|
|
|
|
voice_boost_cpu(false);
|
|
|
|
if (!pcm_is_playing())
|
|
|
|
pcmbuf_boost(false);
|
2005-08-20 11:13:19 +00:00
|
|
|
sleep(HZ/16);
|
2005-08-28 14:16:03 +00:00
|
|
|
}
|
2005-08-20 11:13:19 +00:00
|
|
|
|
|
|
|
if (voice_remaining)
|
|
|
|
{
|
|
|
|
voice_is_playing = true;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (voice_getmore != NULL)
|
|
|
|
{
|
|
|
|
voice_getmore((unsigned char **)&voicebuf, (int *)&voice_remaining);
|
|
|
|
|
|
|
|
if (!voice_remaining)
|
|
|
|
{
|
|
|
|
voice_is_playing = false;
|
|
|
|
/* Force pcm playback. */
|
|
|
|
pcmbuf_play_start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reqsize < 0)
|
|
|
|
reqsize = 0;
|
|
|
|
|
|
|
|
voice_is_playing = true;
|
|
|
|
*realsize = voice_remaining;
|
|
|
|
if (*realsize > reqsize)
|
|
|
|
*realsize = reqsize;
|
|
|
|
|
|
|
|
if (*realsize == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return voicebuf;
|
|
|
|
}
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
void* codec_request_buffer_callback(long *realsize, long reqsize)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2005-06-27 19:29:49 +00:00
|
|
|
long part_n;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
|
|
|
/* Voice codec. */
|
|
|
|
if (current_codec == CODEC_IDX_VOICE) {
|
|
|
|
return voice_request_data(realsize, reqsize);
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
if (ci.stop_codec || !playing) {
|
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*realsize = MIN((off_t)reqsize, (off_t)cur_ti->available + cur_ti->filerem);
|
|
|
|
if (*realsize == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((int)*realsize > cur_ti->available) {
|
|
|
|
yield();
|
2005-07-30 20:46:38 +00:00
|
|
|
if (ci.stop_codec || ci.reload_codec) {
|
2005-06-10 20:29:35 +00:00
|
|
|
*realsize = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
part_n = MIN((int)*realsize, filebuflen - buf_ridx);
|
2005-06-05 23:05:10 +00:00
|
|
|
if (part_n < *realsize) {
|
|
|
|
part_n += GUARD_BUFSIZE;
|
|
|
|
if (part_n < *realsize)
|
|
|
|
*realsize = part_n;
|
2005-08-20 11:13:19 +00:00
|
|
|
memcpy(&filebuf[filebuflen], &filebuf[0], *realsize -
|
|
|
|
(filebuflen - buf_ridx));
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
return (char *)&filebuf[buf_ridx];
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-07-01 18:22:04 +00:00
|
|
|
static bool rebuffer_and_seek(int newpos)
|
|
|
|
{
|
|
|
|
int fd;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2005-07-01 18:22:04 +00:00
|
|
|
logf("Re-buffering song");
|
|
|
|
mutex_lock(&mutex_bufferfill);
|
|
|
|
|
|
|
|
/* (Re-)open current track's file handle. */
|
|
|
|
fd = open(playlist_peek(0), O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
logf("Open failed!");
|
|
|
|
mutex_unlock(&mutex_bufferfill);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (current_fd >= 0)
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = fd;
|
|
|
|
|
|
|
|
/* Clear codec buffer. */
|
|
|
|
audio_invalidate_tracks();
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused = 0;
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = false;
|
2005-07-01 18:22:04 +00:00
|
|
|
buf_ridx = buf_widx = 0;
|
|
|
|
cur_ti->filerem = cur_ti->filesize - newpos;
|
|
|
|
cur_ti->filepos = newpos;
|
|
|
|
cur_ti->start_pos = newpos;
|
|
|
|
ci.curpos = newpos;
|
|
|
|
cur_ti->available = 0;
|
|
|
|
lseek(current_fd, newpos, SEEK_SET);
|
2005-11-01 13:39:01 +00:00
|
|
|
|
2005-07-01 18:22:04 +00:00
|
|
|
mutex_unlock(&mutex_bufferfill);
|
|
|
|
|
2005-07-01 21:00:02 +00:00
|
|
|
while (cur_ti->available == 0 && cur_ti->filerem > 0) {
|
2005-12-01 19:38:08 +00:00
|
|
|
sleep(1);
|
2005-11-17 19:31:29 +00:00
|
|
|
if (ci.stop_codec || ci.reload_codec || !queue_empty(&audio_queue))
|
2005-07-01 21:00:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-11-01 13:39:01 +00:00
|
|
|
|
2005-07-01 18:22:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-06-27 19:29:49 +00:00
|
|
|
void codec_advance_buffer_callback(long amount)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2005-08-20 11:13:19 +00:00
|
|
|
if (current_codec == CODEC_IDX_VOICE) {
|
|
|
|
//logf("voice ad.buf:%d", amount);
|
|
|
|
amount = MAX(0, MIN(amount, voice_remaining));
|
|
|
|
voicebuf += amount;
|
|
|
|
voice_remaining -= amount;
|
|
|
|
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2005-07-01 18:22:04 +00:00
|
|
|
if (amount > cur_ti->available + cur_ti->filerem)
|
2005-06-08 10:33:01 +00:00
|
|
|
amount = cur_ti->available + cur_ti->filerem;
|
2005-12-01 19:38:08 +00:00
|
|
|
|
|
|
|
while (amount > cur_ti->available && filling)
|
|
|
|
sleep(1);
|
2005-06-13 22:09:12 +00:00
|
|
|
|
2005-07-20 18:50:07 +00:00
|
|
|
if (amount > cur_ti->available) {
|
2005-07-01 18:22:04 +00:00
|
|
|
if (!rebuffer_and_seek(ci.curpos + amount))
|
|
|
|
ci.stop_codec = true;
|
|
|
|
return ;
|
2005-06-08 10:33:01 +00:00
|
|
|
}
|
2005-06-13 22:09:12 +00:00
|
|
|
|
|
|
|
buf_ridx += amount;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (buf_ridx >= filebuflen)
|
|
|
|
buf_ridx -= filebuflen;
|
2005-06-13 22:09:12 +00:00
|
|
|
cur_ti->available -= amount;
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused -= amount;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.curpos += amount;
|
2005-07-12 16:45:38 +00:00
|
|
|
codec_set_offset_callback(ci.curpos);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void codec_advance_buffer_loc_callback(void *ptr)
|
|
|
|
{
|
2005-06-27 19:29:49 +00:00
|
|
|
long amount;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
|
|
|
if (current_codec == CODEC_IDX_VOICE)
|
|
|
|
amount = (int)ptr - (int)voicebuf;
|
|
|
|
else
|
|
|
|
amount = (int)ptr - (int)&filebuf[buf_ridx];
|
2005-06-05 23:05:10 +00:00
|
|
|
codec_advance_buffer_callback(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t codec_mp3_get_filepos_callback(int newtime)
|
|
|
|
{
|
|
|
|
off_t newpos;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
cur_ti->id3.elapsed = newtime;
|
2005-06-07 06:34:54 +00:00
|
|
|
newpos = mp3_get_file_pos();
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
return newpos;
|
|
|
|
}
|
|
|
|
|
2005-08-21 18:12:31 +00:00
|
|
|
void codec_seek_complete_callback(void)
|
|
|
|
{
|
2005-10-10 18:20:40 +00:00
|
|
|
/* assume we're called from non-voice codec, as they shouldn't seek */
|
|
|
|
ci.seek_time = 0;
|
2005-08-21 18:12:31 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
bool codec_seek_buffer_callback(off_t newpos)
|
|
|
|
{
|
|
|
|
int difference;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
|
|
|
if (current_codec == CODEC_IDX_VOICE)
|
|
|
|
return false;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
if (newpos < 0)
|
|
|
|
newpos = 0;
|
|
|
|
|
|
|
|
if (newpos >= cur_ti->filesize)
|
|
|
|
newpos = cur_ti->filesize - 1;
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
difference = newpos - ci.curpos;
|
2005-07-01 18:22:04 +00:00
|
|
|
/* Seeking forward */
|
2005-06-05 23:05:10 +00:00
|
|
|
if (difference >= 0) {
|
|
|
|
logf("seek: +%d", difference);
|
|
|
|
codec_advance_buffer_callback(difference);
|
|
|
|
return true;
|
|
|
|
}
|
2005-07-01 18:22:04 +00:00
|
|
|
|
|
|
|
/* Seeking backward */
|
2005-06-05 23:05:10 +00:00
|
|
|
difference = -difference;
|
|
|
|
if (ci.curpos - difference < 0)
|
|
|
|
difference = ci.curpos;
|
|
|
|
|
2005-07-01 18:22:04 +00:00
|
|
|
/* We need to reload the song. */
|
|
|
|
if (newpos < cur_ti->start_pos)
|
|
|
|
return rebuffer_and_seek(newpos);
|
|
|
|
|
|
|
|
/* Seeking inside buffer space. */
|
2005-06-05 23:05:10 +00:00
|
|
|
logf("seek: -%d", difference);
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused += difference;
|
2005-06-05 23:05:10 +00:00
|
|
|
cur_ti->available += difference;
|
|
|
|
buf_ridx -= difference;
|
|
|
|
if (buf_ridx < 0)
|
2005-08-20 11:13:19 +00:00
|
|
|
buf_ridx = filebuflen + buf_ridx;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.curpos -= difference;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-21 11:44:00 +00:00
|
|
|
static void set_filebuf_watermark(int seconds)
|
|
|
|
{
|
|
|
|
long bytes;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (current_codec == CODEC_IDX_VOICE)
|
|
|
|
return ;
|
2005-10-16 08:01:02 +00:00
|
|
|
|
|
|
|
if (!filebuf)
|
|
|
|
return; /* Audio buffers not yet set up */
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2005-07-21 11:44:00 +00:00
|
|
|
bytes = MAX((int)cur_ti->id3.bitrate * seconds * (1000/8), conf_watermark);
|
2005-08-20 11:13:19 +00:00
|
|
|
bytes = MIN(bytes, filebuflen / 2);
|
2005-07-21 11:44:00 +00:00
|
|
|
conf_watermark = bytes;
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void codec_configure_callback(int setting, void *value)
|
2005-06-10 15:02:10 +00:00
|
|
|
{
|
|
|
|
switch (setting) {
|
|
|
|
case CODEC_SET_FILEBUF_WATERMARK:
|
|
|
|
conf_watermark = (unsigned int)value;
|
2005-07-21 11:44:00 +00:00
|
|
|
set_filebuf_watermark(buffer_margin);
|
2005-06-10 15:02:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CODEC_SET_FILEBUF_CHUNKSIZE:
|
|
|
|
conf_filechunk = (unsigned int)value;
|
|
|
|
break;
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
case CODEC_DSP_ENABLE:
|
|
|
|
if ((bool)value)
|
2005-07-13 12:48:22 +00:00
|
|
|
ci.pcmbuf_insert = codec_pcmbuf_insert_callback;
|
2005-06-26 19:41:29 +00:00
|
|
|
else
|
2005-07-13 12:48:22 +00:00
|
|
|
ci.pcmbuf_insert = pcmbuf_insert_buffer;
|
2005-06-26 19:41:29 +00:00
|
|
|
break ;
|
|
|
|
|
2005-06-10 15:02:10 +00:00
|
|
|
default:
|
2005-06-26 20:01:33 +00:00
|
|
|
if (!dsp_configure(setting, value)) {
|
2005-06-26 19:41:29 +00:00
|
|
|
logf("Illegal key: %d", setting);
|
2005-06-26 20:01:33 +00:00
|
|
|
}
|
2005-06-10 15:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-02 16:52:30 +00:00
|
|
|
void audio_set_track_buffer_event(void (*handler)(struct mp3entry *id3,
|
|
|
|
bool last_track))
|
|
|
|
{
|
|
|
|
track_buffer_callback = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_set_track_unbuffer_event(void (*handler)(struct mp3entry *id3,
|
2005-07-03 18:36:24 +00:00
|
|
|
bool last_track))
|
2005-07-02 16:52:30 +00:00
|
|
|
{
|
|
|
|
track_unbuffer_callback = handler;
|
|
|
|
}
|
|
|
|
|
2005-08-18 06:05:15 +00:00
|
|
|
void audio_set_track_changed_event(void (*handler)(struct mp3entry *id3))
|
2005-06-29 21:36:30 +00:00
|
|
|
{
|
|
|
|
track_changed_callback = handler;
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void codec_track_changed(void)
|
2005-06-15 18:59:04 +00:00
|
|
|
{
|
|
|
|
track_changed = true;
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_TRACK_CHANGED, 0);
|
2005-06-15 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
static void pcmbuf_track_changed_callback(void)
|
|
|
|
{
|
|
|
|
track_changed = true;
|
|
|
|
pcmbuf_set_position_callback(NULL);
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_TRACK_CHANGED, 0);
|
|
|
|
}
|
|
|
|
|
2005-07-05 15:51:59 +00:00
|
|
|
/* Give codecs or file buffering the right amount of processing time
|
|
|
|
to prevent pcm audio buffer from going empty. */
|
2005-12-01 18:44:11 +00:00
|
|
|
static void yield_codecs(void)
|
2005-06-07 06:34:54 +00:00
|
|
|
{
|
2005-06-07 18:03:33 +00:00
|
|
|
yield();
|
2005-09-10 21:02:42 +00:00
|
|
|
if (!pcm_is_playing() && !paused)
|
2005-06-07 06:34:54 +00:00
|
|
|
sleep(5);
|
2005-07-13 12:48:22 +00:00
|
|
|
while ((pcmbuf_is_crossfade_active() || pcmbuf_is_lowdata())
|
2005-07-05 15:51:59 +00:00
|
|
|
&& !ci.stop_codec && playing && queue_empty(&audio_queue)
|
2005-08-20 11:13:19 +00:00
|
|
|
&& filebufused > (128*1024))
|
2005-10-30 08:51:47 +00:00
|
|
|
sleep(1);
|
2005-06-07 06:34:54 +00:00
|
|
|
}
|
|
|
|
|
2005-07-07 09:53:02 +00:00
|
|
|
/* FIXME: This code should be made more generic and move to metadata.c */
|
|
|
|
void strip_id3v1_tag(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static const unsigned char tag[] = "TAG";
|
|
|
|
int tagptr;
|
|
|
|
bool found = true;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (filebufused >= 128)
|
2005-07-07 09:53:02 +00:00
|
|
|
{
|
|
|
|
tagptr = buf_widx - 128;
|
|
|
|
if (tagptr < 0)
|
2005-08-20 11:13:19 +00:00
|
|
|
tagptr += filebuflen;
|
2005-07-07 09:53:02 +00:00
|
|
|
|
|
|
|
for(i = 0;i < 3;i++)
|
|
|
|
{
|
2005-08-20 11:13:19 +00:00
|
|
|
if(tagptr >= filebuflen)
|
|
|
|
tagptr -= filebuflen;
|
2005-07-07 09:53:02 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if(filebuf[tagptr] != tag[i])
|
2005-07-07 09:53:02 +00:00
|
|
|
{
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tagptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(found)
|
|
|
|
{
|
|
|
|
/* Skip id3v1 tag */
|
|
|
|
logf("Skipping ID3v1 tag\n");
|
|
|
|
buf_widx -= 128;
|
|
|
|
tracks[track_widx].available -= 128;
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused -= 128;
|
2005-07-07 09:53:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void audio_fill_file_buffer(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2005-06-27 19:29:49 +00:00
|
|
|
long i, size;
|
2005-06-05 23:05:10 +00:00
|
|
|
int rc;
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2005-06-19 18:41:53 +00:00
|
|
|
if (current_fd < 0)
|
|
|
|
return ;
|
2005-07-01 18:22:04 +00:00
|
|
|
|
|
|
|
/* Throw away buffered codec. */
|
|
|
|
if (tracks[track_widx].start_pos != 0)
|
|
|
|
tracks[track_widx].codecsize = 0;
|
|
|
|
|
2005-10-30 08:51:47 +00:00
|
|
|
mutex_lock(&mutex_bufferfill);
|
2005-06-05 23:05:10 +00:00
|
|
|
i = 0;
|
2005-06-19 18:41:53 +00:00
|
|
|
size = MIN(tracks[track_widx].filerem, AUDIO_FILL_CYCLE);
|
|
|
|
while (i < size) {
|
2005-06-07 06:34:54 +00:00
|
|
|
/* Give codecs some processing time. */
|
|
|
|
yield_codecs();
|
|
|
|
|
2005-06-19 18:41:53 +00:00
|
|
|
if (fill_bytesleft == 0)
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
2005-08-20 11:13:19 +00:00
|
|
|
rc = MIN(conf_filechunk, filebuflen - buf_widx);
|
2005-07-10 06:58:02 +00:00
|
|
|
rc = MIN(rc, fill_bytesleft);
|
2005-08-20 11:13:19 +00:00
|
|
|
rc = read(current_fd, &filebuf[buf_widx], rc);
|
2005-06-05 23:05:10 +00:00
|
|
|
if (rc <= 0) {
|
|
|
|
tracks[track_widx].filerem = 0;
|
|
|
|
break ;
|
|
|
|
}
|
2005-06-07 06:34:54 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
buf_widx += rc;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (buf_widx >= filebuflen)
|
|
|
|
buf_widx -= filebuflen;
|
2005-06-05 23:05:10 +00:00
|
|
|
i += rc;
|
|
|
|
tracks[track_widx].available += rc;
|
2005-07-07 07:15:05 +00:00
|
|
|
tracks[track_widx].filerem -= rc;
|
|
|
|
tracks[track_widx].filepos += rc;
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused += rc;
|
2005-06-05 23:05:10 +00:00
|
|
|
fill_bytesleft -= rc;
|
|
|
|
}
|
2006-01-18 14:20:34 +00:00
|
|
|
|
|
|
|
if (tracks[track_widx].filerem == 0) {
|
|
|
|
strip_id3v1_tag();
|
|
|
|
}
|
|
|
|
|
2005-10-30 08:51:47 +00:00
|
|
|
mutex_unlock(&mutex_bufferfill);
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-06-19 18:41:53 +00:00
|
|
|
/*logf("Filled:%d/%d", tracks[track_widx].available,
|
|
|
|
tracks[track_widx].filerem);*/
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static int get_codec_base_type(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AFMT_MPA_L1:
|
|
|
|
case AFMT_MPA_L2:
|
|
|
|
case AFMT_MPA_L3:
|
|
|
|
return AFMT_MPA_L3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool loadcodec(bool start_play)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
|
|
|
off_t size;
|
|
|
|
int fd;
|
|
|
|
int i, rc;
|
|
|
|
const char *codec_path;
|
|
|
|
int copy_n;
|
|
|
|
int prev_track;
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
switch (tracks[track_widx].id3.codectype) {
|
2005-06-05 23:05:10 +00:00
|
|
|
case AFMT_OGG_VORBIS:
|
|
|
|
logf("Codec: Vorbis");
|
|
|
|
codec_path = CODEC_VORBIS;
|
|
|
|
break;
|
2005-06-18 09:51:10 +00:00
|
|
|
case AFMT_MPA_L1:
|
2005-06-08 10:33:01 +00:00
|
|
|
case AFMT_MPA_L2:
|
2005-06-05 23:05:10 +00:00
|
|
|
case AFMT_MPA_L3:
|
2005-06-18 09:51:10 +00:00
|
|
|
logf("Codec: MPA L1/L2/L3");
|
2005-06-05 23:05:10 +00:00
|
|
|
codec_path = CODEC_MPA_L3;
|
|
|
|
break;
|
|
|
|
case AFMT_PCM_WAV:
|
|
|
|
logf("Codec: PCM WAV");
|
|
|
|
codec_path = CODEC_WAV;
|
|
|
|
break;
|
|
|
|
case AFMT_FLAC:
|
|
|
|
logf("Codec: FLAC");
|
|
|
|
codec_path = CODEC_FLAC;
|
|
|
|
break;
|
2005-06-11 10:08:17 +00:00
|
|
|
case AFMT_A52:
|
|
|
|
logf("Codec: A52");
|
|
|
|
codec_path = CODEC_A52;
|
|
|
|
break;
|
2005-06-11 12:40:27 +00:00
|
|
|
case AFMT_MPC:
|
|
|
|
logf("Codec: Musepack");
|
|
|
|
codec_path = CODEC_MPC;
|
|
|
|
break;
|
2005-06-13 06:00:35 +00:00
|
|
|
case AFMT_WAVPACK:
|
|
|
|
logf("Codec: WAVPACK");
|
|
|
|
codec_path = CODEC_WAVPACK;
|
|
|
|
break;
|
2005-09-22 21:55:37 +00:00
|
|
|
case AFMT_ALAC:
|
|
|
|
logf("Codec: ALAC");
|
|
|
|
codec_path = CODEC_ALAC;
|
|
|
|
break;
|
2005-10-31 20:56:29 +00:00
|
|
|
case AFMT_AAC:
|
|
|
|
logf("Codec: AAC");
|
|
|
|
codec_path = CODEC_AAC;
|
|
|
|
break;
|
2005-11-11 19:45:36 +00:00
|
|
|
case AFMT_SHN:
|
|
|
|
logf("Codec: SHN");
|
|
|
|
codec_path = CODEC_SHN;
|
|
|
|
break;
|
2006-02-01 16:42:02 +00:00
|
|
|
case AFMT_AIFF:
|
|
|
|
logf("Codec: PCM AIFF");
|
|
|
|
codec_path = CODEC_AIFF;
|
|
|
|
break;
|
2005-06-05 23:05:10 +00:00
|
|
|
default:
|
|
|
|
logf("Codec: Unsupported");
|
|
|
|
codec_path = NULL;
|
2005-11-21 11:14:51 +00:00
|
|
|
return false;
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tracks[track_widx].codecsize = 0;
|
|
|
|
|
|
|
|
if (!start_play) {
|
|
|
|
prev_track = track_widx - 1;
|
|
|
|
if (prev_track < 0)
|
|
|
|
prev_track = MAX_TRACK-1;
|
2005-12-01 18:44:11 +00:00
|
|
|
if (track_count > 0 &&
|
|
|
|
get_codec_base_type(tracks[track_widx].id3.codectype) ==
|
|
|
|
get_codec_base_type(tracks[prev_track].id3.codectype))
|
|
|
|
{
|
2005-06-05 23:05:10 +00:00
|
|
|
logf("Reusing prev. codec");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Load the codec directly from disk and save some memory. */
|
|
|
|
cur_ti = &tracks[track_widx];
|
|
|
|
ci.filesize = cur_ti->filesize;
|
|
|
|
ci.id3 = (struct mp3entry *)&cur_ti->id3;
|
|
|
|
ci.taginfo_ready = (bool *)&cur_ti->taginfo_ready;
|
|
|
|
ci.curpos = 0;
|
|
|
|
playing = true;
|
|
|
|
logf("Starting codec");
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&codec_queue, Q_CODEC_LOAD_DISK, (void *)codec_path);
|
2005-06-05 23:05:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(codec_path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
logf("Codec doesn't exist!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = filesize(fd);
|
2005-06-10 15:02:10 +00:00
|
|
|
if ((off_t)fill_bytesleft < size + conf_watermark) {
|
2005-06-05 23:05:10 +00:00
|
|
|
logf("Not enough space");
|
2005-07-17 21:44:58 +00:00
|
|
|
/* Set codectype back to zero to indicate no codec was loaded. */
|
|
|
|
tracks[track_widx].id3.codectype = 0;
|
2005-06-19 18:41:53 +00:00
|
|
|
fill_bytesleft = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < size) {
|
2005-06-07 06:34:54 +00:00
|
|
|
yield_codecs();
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
copy_n = MIN(conf_filechunk, filebuflen - buf_widx);
|
|
|
|
rc = read(fd, &filebuf[buf_widx], copy_n);
|
2005-06-05 23:05:10 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return false;
|
|
|
|
buf_widx += rc;
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused += rc;
|
2005-07-10 06:58:02 +00:00
|
|
|
fill_bytesleft -= rc;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (buf_widx >= filebuflen)
|
|
|
|
buf_widx -= filebuflen;
|
2005-06-05 23:05:10 +00:00
|
|
|
i += rc;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
logf("Done: %dB", i);
|
|
|
|
|
|
|
|
tracks[track_widx].codecsize = size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static bool read_next_metadata(void)
|
2005-07-03 18:03:20 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char *trackname;
|
|
|
|
int next_track;
|
|
|
|
int status;
|
|
|
|
|
2005-07-10 08:46:12 +00:00
|
|
|
next_track = track_widx;
|
|
|
|
if (tracks[track_widx].taginfo_ready)
|
|
|
|
next_track++;
|
|
|
|
|
|
|
|
if (next_track >= MAX_TRACK)
|
|
|
|
next_track -= MAX_TRACK;
|
|
|
|
|
|
|
|
if (tracks[next_track].taginfo_ready)
|
|
|
|
return true;
|
|
|
|
|
2005-07-30 17:00:06 +00:00
|
|
|
trackname = playlist_peek(last_peek_offset + 1);
|
2005-07-03 18:03:20 +00:00
|
|
|
if (!trackname)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fd = open(trackname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return false;
|
2005-11-18 20:21:13 +00:00
|
|
|
|
|
|
|
/** Start buffer refilling also because we need to spin-up the disk.
|
|
|
|
* In fact, it might be better not to start filling here, because if user
|
|
|
|
* is manipulating the playlist a lot, we will just lose battery. */
|
|
|
|
// filling = true;
|
2005-07-03 18:03:20 +00:00
|
|
|
status = get_metadata(&tracks[next_track],fd,trackname,v1first);
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Preload the glyphs in the tags */
|
|
|
|
if (status) {
|
2005-12-22 21:42:00 +00:00
|
|
|
if (tracks[next_track].id3.title)
|
|
|
|
lcd_getstringsize(tracks[next_track].id3.title, NULL, NULL);
|
|
|
|
if (tracks[next_track].id3.artist)
|
|
|
|
lcd_getstringsize(tracks[next_track].id3.artist, NULL, NULL);
|
|
|
|
if (tracks[next_track].id3.album)
|
|
|
|
lcd_getstringsize(tracks[next_track].id3.album, NULL, NULL);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
2005-07-04 06:06:30 +00:00
|
|
|
track_changed = true;
|
2005-07-03 18:03:20 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static bool audio_load_track(int offset, bool start_play, int peek_offset)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
|
|
|
char *trackname;
|
2005-07-30 17:00:06 +00:00
|
|
|
int fd = -1;
|
2005-06-05 23:05:10 +00:00
|
|
|
off_t size;
|
|
|
|
int rc, i;
|
|
|
|
int copy_n;
|
2005-12-01 18:44:11 +00:00
|
|
|
char msgbuf[80];
|
2005-06-29 14:46:27 +00:00
|
|
|
|
2005-07-10 08:38:16 +00:00
|
|
|
/* Stop buffer filling if there is no free track entries.
|
|
|
|
Don't fill up the last track entry (we wan't to store next track
|
|
|
|
metadata there). */
|
|
|
|
if (track_count >= MAX_TRACK - 1) {
|
2005-07-05 13:34:52 +00:00
|
|
|
fill_bytesleft = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
2005-07-05 13:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't start loading track if the current write position already
|
|
|
|
contains a BUFFERED track. The entry may contain the metadata
|
|
|
|
which is ok. */
|
|
|
|
if (tracks[track_widx].filesize != 0)
|
|
|
|
return false;
|
|
|
|
|
2005-07-30 17:00:06 +00:00
|
|
|
peek_again:
|
2005-07-05 13:34:52 +00:00
|
|
|
/* Get track name from current playlist read position. */
|
2005-06-29 14:46:27 +00:00
|
|
|
logf("Buffering track:%d/%d", track_widx, track_ridx);
|
2005-07-30 17:00:06 +00:00
|
|
|
/* Handle broken playlists. */
|
|
|
|
while ( (trackname = playlist_peek(peek_offset)) != NULL) {
|
|
|
|
fd = open(trackname, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
logf("Open failed");
|
2005-10-21 06:40:45 +00:00
|
|
|
/* Skip invalid entry from playlist. */
|
|
|
|
playlist_skip_entry(NULL, peek_offset);
|
2005-07-30 17:00:06 +00:00
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
if (!trackname) {
|
2005-06-15 18:59:04 +00:00
|
|
|
logf("End-of-playlist");
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = true;
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-05 13:34:52 +00:00
|
|
|
/* Initialize track entry. */
|
2005-06-05 23:05:10 +00:00
|
|
|
size = filesize(fd);
|
|
|
|
tracks[track_widx].filerem = size;
|
|
|
|
tracks[track_widx].filesize = size;
|
|
|
|
tracks[track_widx].filepos = 0;
|
|
|
|
tracks[track_widx].available = 0;
|
2005-07-16 06:26:29 +00:00
|
|
|
//tracks[track_widx].taginfo_ready = false;
|
2005-07-30 17:00:06 +00:00
|
|
|
tracks[track_widx].playlist_offset = peek_offset;
|
|
|
|
last_peek_offset = peek_offset;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (buf_widx >= filebuflen)
|
|
|
|
buf_widx -= filebuflen;
|
2005-06-10 15:02:10 +00:00
|
|
|
|
|
|
|
/* Set default values */
|
|
|
|
if (start_play) {
|
2005-08-20 11:13:19 +00:00
|
|
|
int last_codec = current_codec;
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
2005-11-18 20:21:13 +00:00
|
|
|
conf_bufferlimit = AUDIO_DEFAULT_FIRST_LIMIT;
|
2005-06-10 15:02:10 +00:00
|
|
|
conf_watermark = AUDIO_DEFAULT_WATERMARK;
|
|
|
|
conf_filechunk = AUDIO_DEFAULT_FILECHUNK;
|
2005-06-26 19:41:29 +00:00
|
|
|
dsp_configure(DSP_RESET, 0);
|
|
|
|
ci.configure(CODEC_DSP_ENABLE, false);
|
2005-08-20 11:13:19 +00:00
|
|
|
current_codec = last_codec;
|
2005-06-10 15:02:10 +00:00
|
|
|
}
|
2005-07-05 13:34:52 +00:00
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
/* Get track metadata if we don't already have it. */
|
|
|
|
if (!tracks[track_widx].taginfo_ready) {
|
|
|
|
if (!get_metadata(&tracks[track_widx],fd,trackname,v1first)) {
|
|
|
|
logf("Metadata error!");
|
|
|
|
tracks[track_widx].filesize = 0;
|
|
|
|
tracks[track_widx].filerem = 0;
|
|
|
|
tracks[track_widx].taginfo_ready = false;
|
|
|
|
close(fd);
|
|
|
|
/* Skip invalid entry from playlist. */
|
|
|
|
playlist_skip_entry(NULL, peek_offset);
|
|
|
|
goto peek_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-05 13:34:52 +00:00
|
|
|
/* Load the codec. */
|
2005-08-20 11:13:19 +00:00
|
|
|
tracks[track_widx].codecbuf = &filebuf[buf_widx];
|
2005-12-01 18:44:11 +00:00
|
|
|
if (!loadcodec(start_play)) {
|
|
|
|
/* We should not use gui_syncplash from audio thread! */
|
|
|
|
snprintf(msgbuf, sizeof(msgbuf)-1, "No codec for: %s", trackname);
|
|
|
|
gui_syncsplash(HZ*2, true, msgbuf);
|
2005-06-05 23:05:10 +00:00
|
|
|
close(fd);
|
2005-12-01 18:44:11 +00:00
|
|
|
|
2005-07-17 21:44:58 +00:00
|
|
|
/* Set filesize to zero to indicate no file was loaded. */
|
|
|
|
tracks[track_widx].filesize = 0;
|
|
|
|
tracks[track_widx].filerem = 0;
|
2005-11-21 11:14:51 +00:00
|
|
|
tracks[track_widx].taginfo_ready = false;
|
2005-07-30 17:00:06 +00:00
|
|
|
|
|
|
|
/* Try skipping to next track. */
|
|
|
|
if (fill_bytesleft > 0) {
|
2005-10-21 06:40:45 +00:00
|
|
|
/* Skip invalid entry from playlist. */
|
|
|
|
playlist_skip_entry(NULL, peek_offset);
|
2005-07-30 17:00:06 +00:00
|
|
|
goto peek_again;
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-12-01 18:44:11 +00:00
|
|
|
|
2005-06-13 15:26:53 +00:00
|
|
|
tracks[track_widx].start_pos = 0;
|
2005-07-21 11:44:00 +00:00
|
|
|
set_filebuf_watermark(buffer_margin);
|
2005-07-05 07:25:55 +00:00
|
|
|
tracks[track_widx].id3.elapsed = 0;
|
2005-06-14 22:27:57 +00:00
|
|
|
|
|
|
|
/* Starting playback from an offset is only support in MPA at the moment */
|
|
|
|
if (offset > 0) {
|
2005-07-07 07:15:05 +00:00
|
|
|
switch (tracks[track_widx].id3.codectype) {
|
|
|
|
case AFMT_MPA_L2:
|
|
|
|
case AFMT_MPA_L3:
|
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
tracks[track_widx].id3.offset = offset;
|
|
|
|
mp3_set_elapsed(&tracks[track_widx].id3);
|
|
|
|
tracks[track_widx].filepos = offset;
|
|
|
|
tracks[track_widx].filerem = tracks[track_widx].filesize - offset;
|
|
|
|
ci.curpos = offset;
|
|
|
|
tracks[track_widx].start_pos = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AFMT_WAVPACK:
|
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
tracks[track_widx].id3.offset = offset;
|
|
|
|
tracks[track_widx].id3.elapsed = tracks[track_widx].id3.length / 2;
|
|
|
|
tracks[track_widx].filepos = offset;
|
|
|
|
tracks[track_widx].filerem = tracks[track_widx].filesize - offset;
|
|
|
|
ci.curpos = offset;
|
|
|
|
tracks[track_widx].start_pos = offset;
|
|
|
|
break;
|
2005-07-11 06:47:35 +00:00
|
|
|
case AFMT_OGG_VORBIS:
|
2005-07-28 18:43:33 +00:00
|
|
|
case AFMT_FLAC:
|
2005-07-11 06:47:35 +00:00
|
|
|
tracks[track_widx].id3.offset = offset;
|
|
|
|
break;
|
2005-07-07 07:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-06-13 22:09:12 +00:00
|
|
|
if (start_play) {
|
|
|
|
track_count++;
|
2005-07-10 06:58:02 +00:00
|
|
|
codec_track_changed();
|
2005-06-13 22:09:12 +00:00
|
|
|
}
|
2005-07-05 13:34:52 +00:00
|
|
|
|
|
|
|
/* Do some initial file buffering. */
|
2005-10-30 08:27:34 +00:00
|
|
|
mutex_lock(&mutex_bufferfill);
|
2005-06-13 15:26:53 +00:00
|
|
|
i = tracks[track_widx].start_pos;
|
2005-06-19 18:41:53 +00:00
|
|
|
size = MIN(size, AUDIO_FILL_CYCLE);
|
2005-06-05 23:05:10 +00:00
|
|
|
while (i < size) {
|
|
|
|
/* Give codecs some processing time to prevent glitches. */
|
2005-06-07 06:34:54 +00:00
|
|
|
yield_codecs();
|
2005-06-10 15:02:10 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
if (fill_bytesleft == 0)
|
|
|
|
break ;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
copy_n = MIN(conf_filechunk, filebuflen - buf_widx);
|
2005-06-05 23:05:10 +00:00
|
|
|
copy_n = MIN(size - i, copy_n);
|
|
|
|
copy_n = MIN((int)fill_bytesleft, copy_n);
|
2005-08-20 11:13:19 +00:00
|
|
|
rc = read(fd, &filebuf[buf_widx], copy_n);
|
2005-07-08 20:01:06 +00:00
|
|
|
if (rc < copy_n) {
|
2005-06-09 19:31:35 +00:00
|
|
|
logf("File error!");
|
2005-07-08 20:01:06 +00:00
|
|
|
tracks[track_widx].filesize = 0;
|
|
|
|
tracks[track_widx].filerem = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
close(fd);
|
2005-10-30 08:27:34 +00:00
|
|
|
mutex_unlock(&mutex_bufferfill);
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
buf_widx += rc;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (buf_widx >= filebuflen)
|
|
|
|
buf_widx -= filebuflen;
|
2005-06-05 23:05:10 +00:00
|
|
|
i += rc;
|
|
|
|
tracks[track_widx].available += rc;
|
|
|
|
tracks[track_widx].filerem -= rc;
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused += rc;
|
2005-06-05 23:05:10 +00:00
|
|
|
fill_bytesleft -= rc;
|
|
|
|
}
|
2005-10-30 08:27:34 +00:00
|
|
|
mutex_unlock(&mutex_bufferfill);
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-06-13 22:09:12 +00:00
|
|
|
if (!start_play)
|
|
|
|
track_count++;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
tracks[track_widx].filepos = i;
|
|
|
|
|
2005-06-14 14:36:46 +00:00
|
|
|
if (current_fd >= 0) {
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Leave the file handle open for faster buffer refill. */
|
|
|
|
if (tracks[track_widx].filerem != 0) {
|
|
|
|
current_fd = fd;
|
|
|
|
logf("Partially buf:%d", tracks[track_widx].available);
|
|
|
|
} else {
|
|
|
|
logf("Completely buf.");
|
|
|
|
close(fd);
|
2005-07-07 09:53:02 +00:00
|
|
|
|
|
|
|
strip_id3v1_tag();
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
if (++track_widx >= MAX_TRACK) {
|
|
|
|
track_widx = 0;
|
|
|
|
}
|
|
|
|
tracks[track_widx].filerem = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void audio_clear_track_entries(bool buffered_only)
|
2005-06-13 22:09:12 +00:00
|
|
|
{
|
2005-07-03 18:36:24 +00:00
|
|
|
int cur_idx, event_count;
|
2005-06-13 22:09:12 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
cur_idx = track_widx;
|
2005-07-03 18:36:24 +00:00
|
|
|
event_count = 0;
|
2005-06-13 22:09:12 +00:00
|
|
|
for (i = 0; i < MAX_TRACK - track_count; i++) {
|
|
|
|
if (++cur_idx >= MAX_TRACK)
|
|
|
|
cur_idx = 0;
|
2005-07-02 16:52:30 +00:00
|
|
|
|
2005-07-03 18:36:24 +00:00
|
|
|
if (tracks[cur_idx].event_sent)
|
|
|
|
event_count++;
|
|
|
|
|
|
|
|
if (!track_unbuffer_callback)
|
|
|
|
memset(&tracks[cur_idx], 0, sizeof(struct track_info));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!track_unbuffer_callback)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
cur_idx = track_widx;
|
|
|
|
for (i = 0; i < MAX_TRACK - track_count; i++) {
|
|
|
|
if (++cur_idx >= MAX_TRACK)
|
|
|
|
cur_idx = 0;
|
|
|
|
|
|
|
|
/* Send an event to notify that track has finished. */
|
|
|
|
if (tracks[cur_idx].event_sent) {
|
|
|
|
event_count--;
|
|
|
|
track_unbuffer_callback(&tracks[cur_idx].id3, event_count == 0);
|
|
|
|
}
|
2005-07-16 06:26:29 +00:00
|
|
|
|
|
|
|
if (tracks[cur_idx].event_sent || !buffered_only)
|
|
|
|
memset(&tracks[cur_idx], 0, sizeof(struct track_info));
|
2005-06-13 22:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-20 22:02:44 +00:00
|
|
|
static void stop_codec_flush(void)
|
|
|
|
{
|
|
|
|
ci.stop_codec = true;
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
while (audio_codec_loaded)
|
|
|
|
yield();
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void audio_stop_playback(bool resume)
|
|
|
|
{
|
2006-01-19 18:21:12 +00:00
|
|
|
logf("stop_playback:%d", resume);
|
2005-12-01 18:44:11 +00:00
|
|
|
paused = false;
|
|
|
|
if (playing)
|
|
|
|
playlist_update_resume_info(resume ? audio_current_track() : NULL);
|
|
|
|
playing = false;
|
|
|
|
filling = false;
|
2006-01-20 22:02:44 +00:00
|
|
|
stop_codec_flush();
|
2005-12-01 18:44:11 +00:00
|
|
|
if (current_fd >= 0) {
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
track_count = 0;
|
|
|
|
/* Mark all entries null. */
|
|
|
|
audio_clear_track_entries(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_play_start(int offset)
|
|
|
|
{
|
|
|
|
if (current_fd >= 0) {
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&tracks, 0, sizeof(struct track_info) * MAX_TRACK);
|
|
|
|
sound_set_volume(global_settings.volume);
|
|
|
|
track_count = 0;
|
|
|
|
track_widx = 0;
|
|
|
|
track_ridx = 0;
|
|
|
|
buf_ridx = 0;
|
|
|
|
buf_widx = 0;
|
|
|
|
filebufused = 0;
|
|
|
|
pcmbuf_set_boost_mode(true);
|
|
|
|
|
|
|
|
fill_bytesleft = filebuflen;
|
|
|
|
filling = true;
|
|
|
|
last_peek_offset = -1;
|
|
|
|
|
|
|
|
if (audio_load_track(offset, true, 0)) {
|
|
|
|
if (track_buffer_callback) {
|
|
|
|
cur_ti->event_sent = true;
|
|
|
|
track_buffer_callback(&cur_ti->id3, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logf("Failure");
|
|
|
|
audio_stop_playback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
pcmbuf_set_boost_mode(false);
|
|
|
|
}
|
|
|
|
|
2005-07-02 16:52:30 +00:00
|
|
|
/* Send callback events to notify about new tracks. */
|
|
|
|
static void generate_postbuffer_events(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int cur_ridx, event_count;
|
|
|
|
|
|
|
|
/* At first determine how many unsent events we have. */
|
|
|
|
cur_ridx = track_ridx;
|
|
|
|
event_count = 0;
|
|
|
|
for (i = 0; i < track_count; i++) {
|
|
|
|
if (!tracks[cur_ridx].event_sent)
|
|
|
|
event_count++;
|
|
|
|
if (++cur_ridx >= MAX_TRACK)
|
|
|
|
cur_ridx -= MAX_TRACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now sent these events. */
|
|
|
|
cur_ridx = track_ridx;
|
|
|
|
for (i = 0; i < track_count; i++) {
|
|
|
|
if (!tracks[cur_ridx].event_sent) {
|
|
|
|
tracks[cur_ridx].event_sent = true;
|
|
|
|
event_count--;
|
2005-07-16 06:26:29 +00:00
|
|
|
/* We still want to set event_sent flags even if not using
|
|
|
|
event callbacks. */
|
|
|
|
if (track_buffer_callback)
|
|
|
|
track_buffer_callback(&tracks[cur_ridx].id3, event_count == 0);
|
2005-07-02 16:52:30 +00:00
|
|
|
}
|
|
|
|
if (++cur_ridx >= MAX_TRACK)
|
|
|
|
cur_ridx -= MAX_TRACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void initialize_buffer_fill(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2005-06-19 18:41:53 +00:00
|
|
|
int cur_idx, i;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-11-18 20:21:13 +00:00
|
|
|
/* Initialize only once; do not truncate the tracks. */
|
2005-06-29 14:46:27 +00:00
|
|
|
if (filling)
|
|
|
|
return ;
|
|
|
|
|
2005-11-27 14:50:21 +00:00
|
|
|
/* Save the current resume position once. */
|
|
|
|
playlist_update_resume_info(audio_current_track());
|
|
|
|
|
2005-11-18 20:21:13 +00:00
|
|
|
fill_bytesleft = filebuflen - filebufused;
|
|
|
|
cur_ti->start_pos = ci.curpos;
|
|
|
|
|
2005-08-28 14:16:03 +00:00
|
|
|
pcmbuf_set_boost_mode(true);
|
|
|
|
|
2005-06-29 14:46:27 +00:00
|
|
|
filling = true;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* Calculate real track count after throwing away old tracks. */
|
|
|
|
cur_idx = track_ridx;
|
|
|
|
for (i = 0; i < track_count; i++) {
|
|
|
|
if (cur_idx == track_widx)
|
|
|
|
break ;
|
|
|
|
|
|
|
|
if (++cur_idx >= MAX_TRACK)
|
|
|
|
cur_idx = 0;
|
|
|
|
}
|
|
|
|
|
2005-07-18 06:10:53 +00:00
|
|
|
track_count = i;
|
2005-07-17 21:44:58 +00:00
|
|
|
if (tracks[track_widx].filesize == 0) {
|
|
|
|
if (--track_widx < 0)
|
|
|
|
track_widx = MAX_TRACK - 1;
|
2005-07-18 06:10:53 +00:00
|
|
|
} else {
|
|
|
|
track_count++;
|
2005-07-17 21:44:58 +00:00
|
|
|
}
|
|
|
|
|
2005-07-16 06:26:29 +00:00
|
|
|
/* Mark all buffered entries null (not metadata for next track). */
|
|
|
|
audio_clear_track_entries(true);
|
2005-06-19 18:41:53 +00:00
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void audio_check_buffer(void)
|
2005-06-19 18:41:53 +00:00
|
|
|
{
|
|
|
|
/* Start buffer filling as necessary. */
|
2005-10-12 08:49:37 +00:00
|
|
|
if ((!conf_watermark || filebufused > conf_watermark
|
|
|
|
|| !queue_empty(&audio_queue) || !playing || ci.stop_codec
|
2005-12-03 12:14:26 +00:00
|
|
|
|| ci.reload_codec || playlist_end) && !filling)
|
2005-06-19 18:41:53 +00:00
|
|
|
return ;
|
|
|
|
|
2005-11-01 13:39:01 +00:00
|
|
|
mutex_lock(&mutex_bufferfill);
|
2005-06-29 14:46:27 +00:00
|
|
|
initialize_buffer_fill();
|
2005-11-01 13:39:01 +00:00
|
|
|
mutex_unlock(&mutex_bufferfill);
|
2005-06-19 18:41:53 +00:00
|
|
|
|
|
|
|
/* Limit buffering size at first run. */
|
2005-07-10 06:58:02 +00:00
|
|
|
if (conf_bufferlimit && fill_bytesleft > conf_bufferlimit
|
2005-08-20 11:13:19 +00:00
|
|
|
- filebufused) {
|
|
|
|
fill_bytesleft = MAX(0, conf_bufferlimit - filebufused);
|
2005-06-19 18:41:53 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* Try to load remainings of the file. */
|
|
|
|
if (tracks[track_widx].filerem > 0)
|
|
|
|
audio_fill_file_buffer();
|
|
|
|
|
2005-06-13 22:09:12 +00:00
|
|
|
/* Increase track write index as necessary. */
|
|
|
|
if (tracks[track_widx].filerem == 0 && tracks[track_widx].filesize != 0) {
|
|
|
|
if (++track_widx == MAX_TRACK)
|
|
|
|
track_widx = 0;
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Load new files to fill the entire buffer. */
|
2005-07-30 17:00:06 +00:00
|
|
|
if (audio_load_track(0, false, last_peek_offset + 1)) {
|
2005-11-18 20:21:13 +00:00
|
|
|
if (conf_bufferlimit)
|
|
|
|
fill_bytesleft = 0;
|
|
|
|
}
|
|
|
|
else if (tracks[track_widx].filerem == 0)
|
|
|
|
fill_bytesleft = 0;
|
|
|
|
|
|
|
|
if (fill_bytesleft <= 0)
|
|
|
|
{
|
2005-07-10 08:46:12 +00:00
|
|
|
/* Read next unbuffered track's metadata as necessary. */
|
|
|
|
read_next_metadata();
|
2005-07-15 21:37:03 +00:00
|
|
|
|
2005-07-02 16:52:30 +00:00
|
|
|
generate_postbuffer_events();
|
2005-06-19 18:41:53 +00:00
|
|
|
filling = false;
|
2005-06-29 20:50:58 +00:00
|
|
|
conf_bufferlimit = 0;
|
2005-07-13 12:48:22 +00:00
|
|
|
pcmbuf_set_boost_mode(false);
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
2005-07-10 08:38:16 +00:00
|
|
|
if (playing)
|
2005-06-19 18:41:53 +00:00
|
|
|
ata_sleep();
|
2005-07-13 12:48:22 +00:00
|
|
|
#endif
|
2005-06-19 18:41:53 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
static void audio_update_trackinfo(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
|
|
|
ci.filesize = cur_ti->filesize;
|
2005-06-11 18:05:16 +00:00
|
|
|
cur_ti->id3.elapsed = 0;
|
|
|
|
cur_ti->id3.offset = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.id3 = (struct mp3entry *)&cur_ti->id3;
|
|
|
|
ci.curpos = 0;
|
2005-07-01 18:22:04 +00:00
|
|
|
cur_ti->start_pos = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.taginfo_ready = (bool *)&cur_ti->taginfo_ready;
|
2006-01-22 04:35:28 +00:00
|
|
|
|
2006-01-20 22:02:44 +00:00
|
|
|
/* Manual track change (always crossfade or flush audio). */
|
|
|
|
if (new_track)
|
|
|
|
{
|
2006-01-27 16:25:44 +00:00
|
|
|
pcmbuf_crossfade_init(true);
|
2005-07-02 17:45:12 +00:00
|
|
|
codec_track_changed();
|
2005-07-17 15:35:17 +00:00
|
|
|
}
|
2005-11-19 14:35:37 +00:00
|
|
|
|
2006-01-20 22:02:44 +00:00
|
|
|
/* Automatic track change with crossfade. */
|
|
|
|
else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active())
|
|
|
|
{
|
2006-01-27 16:25:44 +00:00
|
|
|
pcmbuf_crossfade_init(false);
|
2005-11-19 14:35:37 +00:00
|
|
|
codec_track_changed();
|
2006-01-20 22:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Gapless playback. */
|
|
|
|
else
|
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_set_event_handler(pcmbuf_track_changed_callback);
|
2006-01-20 22:02:44 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
enum {
|
|
|
|
SKIP_FAIL,
|
|
|
|
SKIP_OK_DISK,
|
|
|
|
SKIP_OK_RAM,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Should handle all situations. */
|
2006-01-20 22:02:44 +00:00
|
|
|
static int skip_next_track(bool inside_codec_thread)
|
2005-07-02 17:03:19 +00:00
|
|
|
{
|
2005-12-01 18:44:11 +00:00
|
|
|
logf("skip next");
|
|
|
|
/* Manual track skipping. */
|
|
|
|
if (new_track > 0)
|
|
|
|
last_peek_offset--;
|
|
|
|
|
|
|
|
/* Automatic track skipping. */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!playlist_check(1)) {
|
|
|
|
ci.reload_codec = false;
|
|
|
|
return SKIP_FAIL;
|
|
|
|
}
|
|
|
|
last_peek_offset--;
|
|
|
|
playlist_next(1);
|
2005-07-02 17:03:19 +00:00
|
|
|
}
|
2005-09-10 21:02:42 +00:00
|
|
|
|
2005-11-23 19:13:25 +00:00
|
|
|
if (++track_ridx >= MAX_TRACK)
|
|
|
|
track_ridx = 0;
|
2005-12-01 18:44:11 +00:00
|
|
|
|
|
|
|
/* Wait for new track data. */
|
|
|
|
while (tracks[track_ridx].filesize == 0 && filling
|
2005-11-23 19:13:25 +00:00
|
|
|
&& !ci.stop_codec)
|
|
|
|
yield();
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
if (tracks[track_ridx].filesize <= 0)
|
|
|
|
{
|
|
|
|
logf("Loading from disk...");
|
2005-12-03 12:14:26 +00:00
|
|
|
ci.reload_codec = true;
|
2006-01-19 20:35:06 +00:00
|
|
|
/* Stop playback if manual track change. */
|
|
|
|
if (new_track != 0 && !pcmbuf_is_crossfade_enabled())
|
2006-01-20 22:02:44 +00:00
|
|
|
{
|
|
|
|
if (inside_codec_thread)
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
else
|
|
|
|
stop_codec_flush();
|
|
|
|
}
|
|
|
|
else if (pcmbuf_is_crossfade_enabled())
|
2006-01-27 16:25:44 +00:00
|
|
|
pcmbuf_crossfade_init(new_track != 0);
|
2006-01-19 20:35:06 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PLAY, 0);
|
2005-12-01 18:44:11 +00:00
|
|
|
return SKIP_OK_DISK;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_ridx += cur_ti->available;
|
|
|
|
filebufused -= cur_ti->available;
|
|
|
|
|
|
|
|
cur_ti = &tracks[track_ridx];
|
|
|
|
buf_ridx += cur_ti->codecsize;
|
|
|
|
filebufused -= cur_ti->codecsize;
|
|
|
|
if (buf_ridx >= filebuflen)
|
|
|
|
buf_ridx -= filebuflen;
|
|
|
|
audio_update_trackinfo();
|
|
|
|
|
|
|
|
if (!filling)
|
|
|
|
pcmbuf_set_boost_mode(false);
|
|
|
|
|
|
|
|
return SKIP_OK_RAM;
|
|
|
|
}
|
2005-11-23 19:13:25 +00:00
|
|
|
|
2006-01-20 22:02:44 +00:00
|
|
|
static int skip_previous_track(bool inside_codec_thread)
|
2005-12-01 18:44:11 +00:00
|
|
|
{
|
|
|
|
logf("skip previous");
|
|
|
|
last_peek_offset++;
|
|
|
|
if (--track_ridx < 0)
|
|
|
|
track_ridx += MAX_TRACK;
|
|
|
|
|
|
|
|
if (tracks[track_ridx].filesize == 0 ||
|
|
|
|
filebufused+ci.curpos+tracks[track_ridx].filesize
|
|
|
|
/*+ (off_t)tracks[track_ridx].codecsize*/ > filebuflen) {
|
|
|
|
logf("Loading from disk...");
|
2005-12-03 12:14:26 +00:00
|
|
|
ci.reload_codec = true;
|
2006-01-19 20:35:06 +00:00
|
|
|
/* Stop playback. */
|
|
|
|
/* FIXME: Only stop playback if disk is not spinning! */
|
2006-01-20 22:02:44 +00:00
|
|
|
if (pcmbuf_is_crossfade_enabled())
|
2006-01-27 16:25:44 +00:00
|
|
|
pcmbuf_crossfade_init(true);
|
2006-01-20 22:02:44 +00:00
|
|
|
else if (inside_codec_thread)
|
2006-01-19 20:35:06 +00:00
|
|
|
pcmbuf_play_stop();
|
2006-01-20 22:02:44 +00:00
|
|
|
else
|
|
|
|
stop_codec_flush();
|
2006-01-19 20:35:06 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PLAY, 0);
|
2005-12-01 18:44:11 +00:00
|
|
|
return SKIP_OK_DISK;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_ridx -= ci.curpos + cur_ti->codecsize;
|
|
|
|
filebufused += ci.curpos + cur_ti->codecsize;
|
|
|
|
cur_ti->available = cur_ti->filesize - cur_ti->filerem;
|
|
|
|
|
|
|
|
cur_ti = &tracks[track_ridx];
|
|
|
|
buf_ridx -= cur_ti->filesize;
|
|
|
|
filebufused += cur_ti->filesize;
|
|
|
|
cur_ti->available = cur_ti->filesize;
|
|
|
|
if (buf_ridx < 0)
|
|
|
|
buf_ridx += filebuflen;
|
2006-01-20 22:02:44 +00:00
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
audio_update_trackinfo();
|
|
|
|
|
|
|
|
return SKIP_OK_RAM;
|
2005-11-23 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2005-07-05 13:34:52 +00:00
|
|
|
/* Request the next track with new codec. */
|
2005-11-23 19:13:25 +00:00
|
|
|
static void audio_change_track(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2005-06-19 18:41:53 +00:00
|
|
|
logf("change track");
|
2005-07-05 13:34:52 +00:00
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
if (!ci.reload_codec)
|
2005-11-23 19:13:25 +00:00
|
|
|
{
|
2006-01-20 22:02:44 +00:00
|
|
|
if (skip_next_track(false) == SKIP_FAIL)
|
2005-12-01 18:44:11 +00:00
|
|
|
{
|
|
|
|
logf("No more tracks");
|
|
|
|
while (pcm_is_playing())
|
2005-11-23 19:13:25 +00:00
|
|
|
sleep(1);
|
2005-12-01 18:44:11 +00:00
|
|
|
audio_stop_playback(false);
|
|
|
|
return ;
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
ci.reload_codec = false;
|
|
|
|
/* Needed for fast skipping. */
|
2006-01-20 22:02:44 +00:00
|
|
|
if (cur_ti->codecsize > 0)
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&codec_queue, Q_CODEC_LOAD, 0);
|
2005-07-10 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
bool codec_request_next_track_callback(void)
|
|
|
|
{
|
2006-01-21 22:42:44 +00:00
|
|
|
prev_ti = cur_ti;
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
if (current_codec == CODEC_IDX_VOICE) {
|
|
|
|
voice_remaining = 0;
|
2005-11-17 19:31:29 +00:00
|
|
|
/* Terminate the codec if there are messages waiting on the queue or
|
|
|
|
the core has been requested the codec to be terminated. */
|
2005-08-20 18:55:15 +00:00
|
|
|
return !ci_voice.stop_codec && queue_empty(&voice_codec_queue);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
2006-01-21 22:42:44 +00:00
|
|
|
|
2005-07-10 06:58:02 +00:00
|
|
|
if (ci.stop_codec || !playing)
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
|
|
|
|
2006-02-07 20:38:55 +00:00
|
|
|
#ifdef AB_REPEAT_ENABLE
|
|
|
|
ab_end_of_track_report();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!new_track)
|
|
|
|
pcmbuf_set_position_callback(pcmbuf_position_callback);
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
logf("Request new track");
|
2005-07-24 04:17:34 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
/* Advance to next track. */
|
2005-12-01 18:44:11 +00:00
|
|
|
if (new_track >= 0 || !ci.reload_codec) {
|
2006-01-20 22:02:44 +00:00
|
|
|
if (skip_next_track(true) != SKIP_OK_RAM)
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance to previous track. */
|
2005-12-01 18:44:11 +00:00
|
|
|
else {
|
2006-01-20 22:02:44 +00:00
|
|
|
if (skip_previous_track(true) != SKIP_OK_RAM)
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
new_track = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.reload_codec = false;
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
logf("On-the-fly change");
|
|
|
|
|
2005-07-10 16:33:03 +00:00
|
|
|
/* Check if the next codec is the same file. */
|
2005-12-01 18:44:11 +00:00
|
|
|
if (get_codec_base_type(prev_ti->id3.codectype) !=
|
|
|
|
get_codec_base_type(cur_ti->id3.codectype))
|
|
|
|
{
|
2005-07-08 20:01:06 +00:00
|
|
|
logf("New codec:%d/%d", cur_ti->id3.codectype,
|
|
|
|
tracks[track_ridx].id3.codectype);
|
2005-12-01 18:44:11 +00:00
|
|
|
|
|
|
|
if (cur_ti->codecsize == 0)
|
|
|
|
{
|
|
|
|
logf("Loading from disk [2]...");
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PLAY, 0);
|
2005-12-01 18:44:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ci.reload_codec = true;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-12-01 18:44:11 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-06-13 22:09:12 +00:00
|
|
|
/* Invalidates all but currently playing track. */
|
|
|
|
void audio_invalidate_tracks(void)
|
|
|
|
{
|
|
|
|
if (track_count == 0) {
|
2005-10-07 22:55:30 +00:00
|
|
|
/* This call doesn't seem necessary anymore. Uncomment it
|
|
|
|
if things break */
|
2006-01-27 11:39:46 +00:00
|
|
|
/* queue_post(&audio_queue, Q_AUDIO_PLAY, 0); */
|
2005-06-13 22:09:12 +00:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = false;
|
2005-06-13 22:09:12 +00:00
|
|
|
track_count = 1;
|
2005-07-30 17:00:06 +00:00
|
|
|
last_peek_offset = 0;
|
2005-06-13 22:09:12 +00:00
|
|
|
track_widx = track_ridx;
|
2005-07-16 06:26:29 +00:00
|
|
|
/* Mark all other entries null (also buffered wrong metadata). */
|
|
|
|
audio_clear_track_entries(false);
|
2005-08-20 11:13:19 +00:00
|
|
|
filebufused = cur_ti->available;
|
2005-06-13 22:09:12 +00:00
|
|
|
buf_widx = buf_ridx + cur_ti->available;
|
2005-08-20 11:13:19 +00:00
|
|
|
if (buf_widx >= filebuflen)
|
|
|
|
buf_widx -= filebuflen;
|
2005-07-03 18:03:20 +00:00
|
|
|
read_next_metadata();
|
2005-06-13 22:09:12 +00:00
|
|
|
}
|
|
|
|
|
2005-07-06 19:40:17 +00:00
|
|
|
static void initiate_track_change(int peek_index)
|
|
|
|
{
|
2005-12-01 18:44:11 +00:00
|
|
|
/* Detect if disk is spinning or already loading. */
|
|
|
|
if (filling || ci.reload_codec || !audio_codec_loaded) {
|
2006-01-20 22:02:44 +00:00
|
|
|
if (pcmbuf_is_crossfade_enabled())
|
2006-01-27 16:25:44 +00:00
|
|
|
pcmbuf_crossfade_init(true);
|
2006-01-20 22:02:44 +00:00
|
|
|
else
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
ci.stop_codec = true;
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PLAY, 0);
|
2005-07-17 15:35:17 +00:00
|
|
|
} else {
|
2005-07-19 19:57:23 +00:00
|
|
|
new_track = peek_index;
|
|
|
|
ci.reload_codec = true;
|
2005-07-17 15:35:17 +00:00
|
|
|
}
|
|
|
|
|
2005-07-08 20:01:06 +00:00
|
|
|
codec_track_changed();
|
2005-07-06 19:40:17 +00:00
|
|
|
}
|
|
|
|
|
2005-11-02 22:32:04 +00:00
|
|
|
static void initiate_dir_change(int direction)
|
|
|
|
{
|
|
|
|
if(!playlist_next_dir(direction))
|
|
|
|
return;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PLAY, (bool *)true);
|
2005-11-02 22:32:04 +00:00
|
|
|
|
|
|
|
codec_track_changed();
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void audio_thread(void)
|
|
|
|
{
|
|
|
|
struct event ev;
|
2005-11-17 19:31:29 +00:00
|
|
|
int last_tick = 0;
|
|
|
|
bool play_pending = false;
|
2006-01-27 11:39:46 +00:00
|
|
|
|
|
|
|
/* At first initialize audio system in background. */
|
|
|
|
playback_init();
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
while (1) {
|
2005-11-18 20:21:13 +00:00
|
|
|
if (!play_pending && queue_empty(&audio_queue))
|
2005-11-17 19:31:29 +00:00
|
|
|
{
|
|
|
|
yield_codecs();
|
|
|
|
audio_check_buffer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ata_spin();
|
|
|
|
sleep(1);
|
|
|
|
}
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2005-06-19 18:41:53 +00:00
|
|
|
queue_wait_w_tmo(&audio_queue, &ev, 0);
|
2005-11-17 19:31:29 +00:00
|
|
|
if (ev.id == SYS_TIMEOUT && play_pending)
|
|
|
|
{
|
2006-01-27 11:39:46 +00:00
|
|
|
ev.id = Q_AUDIO_PLAY;
|
2006-01-19 20:35:06 +00:00
|
|
|
ev.data = (bool *)1;
|
2005-11-17 19:31:29 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
switch (ev.id) {
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_PLAY:
|
2005-11-17 19:31:29 +00:00
|
|
|
/* Don't start playing immediately if user is skipping tracks
|
|
|
|
* fast to prevent UI lag. */
|
|
|
|
track_count = 0;
|
|
|
|
last_peek_offset = 0;
|
2005-11-18 20:21:13 +00:00
|
|
|
track_changed = true;
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = false;
|
2005-11-17 19:31:29 +00:00
|
|
|
if (current_tick - last_tick < HZ/2)
|
|
|
|
{
|
|
|
|
play_pending = true;
|
2005-08-06 05:50:14 +00:00
|
|
|
break ;
|
|
|
|
}
|
2005-11-17 19:31:29 +00:00
|
|
|
play_pending = false;
|
2005-11-18 20:21:13 +00:00
|
|
|
last_tick = current_tick;
|
2005-11-17 19:31:29 +00:00
|
|
|
|
2005-11-01 13:39:01 +00:00
|
|
|
/* Do not start crossfading if audio is paused. */
|
2005-11-17 19:31:29 +00:00
|
|
|
if (paused)
|
|
|
|
pcmbuf_play_stop();
|
2005-11-13 10:57:35 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_TUNER
|
|
|
|
/* check if radio is playing */
|
2005-11-19 19:23:44 +00:00
|
|
|
if (get_radio_status() != FMRADIO_OFF) {
|
2005-11-17 19:31:29 +00:00
|
|
|
radio_stop();
|
2005-11-13 10:57:35 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-11-01 13:39:01 +00:00
|
|
|
|
2005-06-19 18:41:53 +00:00
|
|
|
logf("starting...");
|
2005-06-30 16:28:40 +00:00
|
|
|
playing = true;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.stop_codec = true;
|
|
|
|
ci.reload_codec = false;
|
|
|
|
ci.seek_time = 0;
|
2006-01-19 20:35:06 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
while (audio_codec_loaded)
|
2005-07-19 19:57:23 +00:00
|
|
|
yield();
|
2006-01-20 22:02:44 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
audio_play_start((int)ev.data);
|
2005-07-04 22:50:57 +00:00
|
|
|
playlist_update_resume_info(audio_current_track());
|
2005-10-07 22:55:30 +00:00
|
|
|
|
2005-10-22 21:07:35 +00:00
|
|
|
/* If there are no tracks in the playlist, then the playlist
|
2005-10-07 22:55:30 +00:00
|
|
|
was empty or none of the filenames were valid. No point
|
|
|
|
in playing an empty playlist. */
|
|
|
|
if (playlist_amount() == 0) {
|
2005-11-23 19:13:25 +00:00
|
|
|
audio_stop_playback(false);
|
2005-10-07 22:55:30 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_STOP:
|
2005-11-23 19:13:25 +00:00
|
|
|
audio_stop_playback(true);
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_PAUSE:
|
2005-07-06 19:40:17 +00:00
|
|
|
logf("audio_pause");
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_pause(true);
|
2005-07-06 19:40:17 +00:00
|
|
|
paused = true;
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_RESUME:
|
2005-07-06 19:40:17 +00:00
|
|
|
logf("audio_resume");
|
2006-02-07 20:38:55 +00:00
|
|
|
pcmbuf_pause(false);
|
2005-07-06 19:40:17 +00:00
|
|
|
paused = false;
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_NEXT:
|
2005-07-06 19:40:17 +00:00
|
|
|
logf("audio_next");
|
2005-11-17 19:31:29 +00:00
|
|
|
last_tick = current_tick;
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = false;
|
2005-07-06 19:40:17 +00:00
|
|
|
initiate_track_change(1);
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_PREV:
|
2005-07-06 19:40:17 +00:00
|
|
|
logf("audio_prev");
|
2005-11-17 19:31:29 +00:00
|
|
|
last_tick = current_tick;
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = false;
|
2005-07-06 19:40:17 +00:00
|
|
|
initiate_track_change(-1);
|
|
|
|
break;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_FF_REWIND:
|
2005-12-01 19:38:08 +00:00
|
|
|
if (!playing)
|
|
|
|
break ;
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
ci.seek_time = (int)ev.data+1;
|
|
|
|
break ;
|
2005-11-02 22:32:04 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_SEAMLESS_SEEK:
|
2006-01-21 22:58:08 +00:00
|
|
|
if (!playing)
|
|
|
|
break ;
|
|
|
|
ci.seek_time = (int)ev.data+1;
|
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_DIR_NEXT:
|
2005-11-02 22:32:04 +00:00
|
|
|
logf("audio_dir_next");
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = false;
|
2005-11-02 22:32:04 +00:00
|
|
|
if (global_settings.beep)
|
|
|
|
pcmbuf_beep(5000, 100, 2500*global_settings.beep);
|
|
|
|
initiate_dir_change(1);
|
|
|
|
break;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_DIR_PREV:
|
2005-11-02 22:32:04 +00:00
|
|
|
logf("audio_dir_prev");
|
2005-12-03 12:14:26 +00:00
|
|
|
playlist_end = false;
|
2005-11-02 22:32:04 +00:00
|
|
|
if (global_settings.beep)
|
|
|
|
pcmbuf_beep(5000, 100, 2500*global_settings.beep);
|
|
|
|
initiate_dir_change(-1);
|
|
|
|
break;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_FLUSH:
|
2005-06-13 22:09:12 +00:00
|
|
|
audio_invalidate_tracks();
|
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_TRACK_CHANGED:
|
2005-06-29 21:36:30 +00:00
|
|
|
if (track_changed_callback)
|
2005-08-18 06:05:15 +00:00
|
|
|
track_changed_callback(&cur_ti->id3);
|
2005-07-04 22:50:57 +00:00
|
|
|
playlist_update_resume_info(audio_current_track());
|
2006-01-21 22:42:44 +00:00
|
|
|
pcmbuf_set_position_callback(NULL);
|
2005-06-19 18:41:53 +00:00
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_AUDIO_CODEC_DONE:
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
2005-11-17 19:31:29 +00:00
|
|
|
#ifndef SIMULATOR
|
2005-06-05 23:05:10 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
2005-08-20 18:55:15 +00:00
|
|
|
logf("USB: Audio core");
|
2005-11-23 19:13:25 +00:00
|
|
|
audio_stop_playback(true);
|
2005-06-05 23:05:10 +00:00
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&audio_queue);
|
|
|
|
break ;
|
|
|
|
#endif
|
2005-07-04 22:50:57 +00:00
|
|
|
case SYS_TIMEOUT:
|
|
|
|
break;
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void codec_thread(void)
|
|
|
|
{
|
|
|
|
struct event ev;
|
2005-06-27 19:29:49 +00:00
|
|
|
long codecsize;
|
2005-06-05 23:05:10 +00:00
|
|
|
int status;
|
|
|
|
int wrap;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
status = 0;
|
|
|
|
queue_wait(&codec_queue, &ev);
|
2005-12-01 18:44:11 +00:00
|
|
|
new_track = 0;
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
switch (ev.id) {
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_CODEC_LOAD_DISK:
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.stop_codec = false;
|
2005-08-20 11:13:19 +00:00
|
|
|
audio_codec_loaded = true;
|
|
|
|
mutex_lock(&mutex_codecthread);
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
status = codec_load_file((char *)ev.data, &ci);
|
|
|
|
mutex_unlock(&mutex_codecthread);
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_CODEC_LOAD:
|
2005-06-05 23:05:10 +00:00
|
|
|
logf("Codec start");
|
|
|
|
codecsize = cur_ti->codecsize;
|
|
|
|
if (codecsize == 0) {
|
|
|
|
logf("Codec slot is empty!");
|
2005-07-10 06:58:02 +00:00
|
|
|
/* Wait for the pcm buffer to go empty */
|
|
|
|
while (pcm_is_playing())
|
|
|
|
yield();
|
2005-11-23 19:13:25 +00:00
|
|
|
audio_stop_playback(true);
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
|
|
|
ci.stop_codec = false;
|
2005-08-20 11:13:19 +00:00
|
|
|
wrap = (int)&filebuf[filebuflen] - (int)cur_ti->codecbuf;
|
|
|
|
audio_codec_loaded = true;
|
|
|
|
mutex_lock(&mutex_codecthread);
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
status = codec_load_ram(cur_ti->codecbuf, codecsize,
|
|
|
|
&filebuf[0], wrap, &ci);
|
|
|
|
mutex_unlock(&mutex_codecthread);
|
2005-06-05 23:05:10 +00:00
|
|
|
break ;
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
case SYS_USB_CONNECTED:
|
2005-08-20 18:55:15 +00:00
|
|
|
while (voice_codec_loaded) {
|
|
|
|
if (current_codec != CODEC_IDX_VOICE)
|
|
|
|
swap_codec();
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
logf("USB: Audio codec");
|
2005-06-05 23:05:10 +00:00
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&codec_queue);
|
|
|
|
break ;
|
|
|
|
#endif
|
|
|
|
}
|
2005-06-29 20:50:58 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
audio_codec_loaded = false;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
switch (ev.id) {
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_CODEC_LOAD_DISK:
|
|
|
|
case Q_CODEC_LOAD:
|
2005-06-22 19:41:30 +00:00
|
|
|
if (status != CODEC_OK) {
|
2005-06-05 23:05:10 +00:00
|
|
|
logf("Codec failure");
|
2005-11-23 19:13:25 +00:00
|
|
|
// audio_stop_playback();
|
2005-12-01 18:44:11 +00:00
|
|
|
ci.reload_codec = false;
|
2005-11-16 15:12:15 +00:00
|
|
|
gui_syncsplash(HZ*2, true, "Codec failure");
|
2005-06-05 23:05:10 +00:00
|
|
|
} else {
|
|
|
|
logf("Codec finished");
|
|
|
|
}
|
|
|
|
|
2005-12-01 18:44:11 +00:00
|
|
|
if (playing && !ci.stop_codec)
|
2005-06-05 23:05:10 +00:00
|
|
|
audio_change_track();
|
2005-11-23 19:13:25 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
// queue_post(&audio_queue, Q_AUDIO_CODEC_DONE, (void *)status);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
static void reset_buffer(void)
|
|
|
|
{
|
2005-12-05 22:44:42 +00:00
|
|
|
filebuf = (char *)&audiobuf[MALLOC_BUFSIZE];
|
2006-02-07 20:38:55 +00:00
|
|
|
filebuflen = audiobufend - audiobuf - MALLOC_BUFSIZE - GUARD_BUFSIZE -
|
|
|
|
(pcmbuf_get_bufsize() + get_pcmbuf_descsize() + PCMBUF_FADE_CHUNK);
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
|
|
|
|
if (talk_get_bufsize() && voice_codec_loaded)
|
|
|
|
{
|
|
|
|
filebuf = &filebuf[talk_get_bufsize()];
|
|
|
|
filebuflen -= 2*CODEC_IRAM_SIZE + 2*CODEC_SIZE + talk_get_bufsize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void voice_codec_thread(void)
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
voice_codec_loaded = false;
|
|
|
|
while (1) {
|
|
|
|
status = 0;
|
2005-08-21 18:38:27 +00:00
|
|
|
voice_is_playing = false;
|
2005-08-20 11:13:19 +00:00
|
|
|
queue_wait(&voice_codec_queue, &ev);
|
|
|
|
switch (ev.id) {
|
2006-01-27 11:39:46 +00:00
|
|
|
case Q_CODEC_LOAD_DISK:
|
2005-08-20 11:13:19 +00:00
|
|
|
logf("Loading voice codec");
|
2005-11-23 19:13:25 +00:00
|
|
|
audio_stop_playback(true);
|
2005-08-20 11:13:19 +00:00
|
|
|
mutex_lock(&mutex_codecthread);
|
|
|
|
current_codec = CODEC_IDX_VOICE;
|
|
|
|
dsp_configure(DSP_RESET, 0);
|
|
|
|
ci.configure(CODEC_DSP_ENABLE, (bool *)true);
|
|
|
|
voice_remaining = 0;
|
|
|
|
voice_getmore = NULL;
|
|
|
|
voice_codec_loaded = true;
|
|
|
|
reset_buffer();
|
|
|
|
ci_voice.stop_codec = false;
|
|
|
|
|
|
|
|
status = codec_load_file((char *)ev.data, &ci_voice);
|
|
|
|
|
|
|
|
logf("Voice codec finished");
|
2005-11-23 19:13:25 +00:00
|
|
|
audio_stop_playback(true);
|
2005-08-20 11:13:19 +00:00
|
|
|
mutex_unlock(&mutex_codecthread);
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
voice_codec_loaded = false;
|
|
|
|
reset_buffer();
|
|
|
|
break ;
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
case SYS_USB_CONNECTED:
|
2005-08-20 18:55:15 +00:00
|
|
|
logf("USB: Voice codec");
|
2005-08-20 11:13:19 +00:00
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&voice_codec_queue);
|
|
|
|
break ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void voice_init(void)
|
|
|
|
{
|
2005-10-16 12:05:58 +00:00
|
|
|
if (!filebuf)
|
|
|
|
return; /* Audio buffers not yet set up */
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
while (voice_codec_loaded)
|
|
|
|
{
|
|
|
|
logf("Terminating voice codec");
|
|
|
|
ci_voice.stop_codec = true;
|
|
|
|
if (current_codec != CODEC_IDX_VOICE)
|
|
|
|
swap_codec();
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!talk_get_bufsize())
|
|
|
|
return ;
|
|
|
|
|
|
|
|
logf("Starting voice codec");
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&voice_codec_queue, Q_CODEC_LOAD_DISK, (void *)CODEC_MPA_L3);
|
2005-08-20 11:13:19 +00:00
|
|
|
while (!voice_codec_loaded)
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
struct mp3entry* audio_current_track(void)
|
|
|
|
{
|
2005-11-17 19:31:29 +00:00
|
|
|
const char *filename;
|
|
|
|
const char *p;
|
|
|
|
static struct mp3entry temp_id3;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
if (track_count > 0 && cur_ti->taginfo_ready)
|
|
|
|
return (struct mp3entry *)&cur_ti->id3;
|
2005-11-17 19:31:29 +00:00
|
|
|
|
2006-01-19 18:21:12 +00:00
|
|
|
filename = playlist_peek(0);
|
|
|
|
if (!filename)
|
|
|
|
filename = "No file!";
|
|
|
|
|
|
|
|
// if (tagcache_fill_tags(&temp_id3, filename))
|
|
|
|
// return &temp_id3;
|
|
|
|
|
|
|
|
p = strrchr(filename, '/');
|
|
|
|
if (!p)
|
|
|
|
p = filename;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
|
|
|
|
memset(&temp_id3, 0, sizeof(struct mp3entry));
|
|
|
|
strncpy(temp_id3.path, p, sizeof(temp_id3.path)-1);
|
|
|
|
temp_id3.title = &temp_id3.path[0];
|
|
|
|
|
|
|
|
return &temp_id3;
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct mp3entry* audio_next_track(void)
|
|
|
|
{
|
|
|
|
int next_idx = track_ridx + 1;
|
|
|
|
|
|
|
|
if (track_count == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (next_idx >= MAX_TRACK)
|
|
|
|
next_idx = 0;
|
|
|
|
|
|
|
|
if (!tracks[next_idx].taginfo_ready)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &tracks[next_idx].id3;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_has_changed_track(void)
|
|
|
|
{
|
2005-11-17 19:31:29 +00:00
|
|
|
if (track_changed) {
|
2005-06-05 23:05:10 +00:00
|
|
|
track_changed = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_play(int offset)
|
|
|
|
{
|
|
|
|
logf("audio_play");
|
2006-01-20 22:02:44 +00:00
|
|
|
if (pcmbuf_is_crossfade_enabled())
|
|
|
|
{
|
|
|
|
ci.stop_codec = true;
|
|
|
|
sleep(1);
|
2006-01-27 16:25:44 +00:00
|
|
|
pcmbuf_crossfade_init(true);
|
2006-01-20 22:02:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stop_codec_flush();
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
}
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PLAY, (void *)offset);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_stop(void)
|
|
|
|
{
|
|
|
|
logf("audio_stop");
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_STOP, 0);
|
2005-08-20 11:13:19 +00:00
|
|
|
while (playing || audio_codec_loaded)
|
2005-06-29 20:50:58 +00:00
|
|
|
yield();
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-07-20 01:58:27 +00:00
|
|
|
bool mp3_pause_done(void)
|
|
|
|
{
|
|
|
|
return paused;
|
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void audio_pause(void)
|
|
|
|
{
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PAUSE, 0);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_resume(void)
|
|
|
|
{
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_RESUME, 0);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-06-19 18:41:53 +00:00
|
|
|
void audio_next(void)
|
|
|
|
{
|
2005-11-17 19:31:29 +00:00
|
|
|
/* Prevent UI lag and update the WPS immediately. */
|
|
|
|
if (global_settings.beep)
|
|
|
|
pcmbuf_beep(5000, 100, 2500*global_settings.beep);
|
|
|
|
|
|
|
|
if (!playlist_check(1))
|
|
|
|
return ;
|
|
|
|
playlist_next(1);
|
|
|
|
track_changed = true;
|
|
|
|
|
|
|
|
/* Force WPS to update even if audio thread is blocked spinning. */
|
|
|
|
if (mutex_bufferfill.locked)
|
|
|
|
cur_ti->taginfo_ready = false;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_NEXT, 0);
|
2005-06-19 18:41:53 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void audio_prev(void)
|
|
|
|
{
|
2005-11-17 19:31:29 +00:00
|
|
|
/* Prevent UI lag and update the WPS immediately. */
|
|
|
|
if (global_settings.beep)
|
|
|
|
pcmbuf_beep(5000, 100, 2500*global_settings.beep);
|
|
|
|
|
|
|
|
if (!playlist_check(-1))
|
|
|
|
return ;
|
|
|
|
playlist_next(-1);
|
|
|
|
track_changed = true;
|
|
|
|
|
|
|
|
/* Force WPS to update even if audio thread is blocked spinning. */
|
|
|
|
if (mutex_bufferfill.locked)
|
|
|
|
cur_ti->taginfo_ready = false;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_PREV, 0);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2005-11-02 22:32:04 +00:00
|
|
|
void audio_next_dir(void)
|
|
|
|
{
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_DIR_NEXT, 0);
|
2005-11-02 22:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_prev_dir(void)
|
|
|
|
{
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_DIR_PREV, 0);
|
2005-11-02 22:32:04 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void audio_ff_rewind(int newpos)
|
|
|
|
{
|
|
|
|
logf("rewind: %d", newpos);
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_FF_REWIND, (int *)newpos);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-01-21 22:58:08 +00:00
|
|
|
void audio_seamless_seek(int newpos)
|
|
|
|
{
|
|
|
|
logf("seamless_seek: %d", newpos);
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_SEAMLESS_SEEK, (int *)newpos);
|
2006-01-21 22:58:08 +00:00
|
|
|
}
|
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
void audio_flush_and_reload_tracks(void)
|
|
|
|
{
|
|
|
|
logf("flush & reload");
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_FLUSH, 0);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_error_clear(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_status(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (playing)
|
|
|
|
ret |= AUDIO_STATUS_PLAY;
|
|
|
|
|
|
|
|
if (paused)
|
|
|
|
ret |= AUDIO_STATUS_PAUSE;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-06 00:35:21 +00:00
|
|
|
int audio_get_file_pos(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-07 06:34:54 +00:00
|
|
|
|
2005-06-08 10:33:01 +00:00
|
|
|
/* Copied from mpeg.c. Should be moved somewhere else. */
|
|
|
|
static void mp3_set_elapsed(struct mp3entry* id3)
|
|
|
|
{
|
|
|
|
if ( id3->vbr ) {
|
|
|
|
if ( id3->has_toc ) {
|
|
|
|
/* calculate elapsed time using TOC */
|
|
|
|
int i;
|
|
|
|
unsigned int remainder, plen, relpos, nextpos;
|
|
|
|
|
|
|
|
/* find wich percent we're at */
|
|
|
|
for (i=0; i<100; i++ )
|
|
|
|
{
|
2005-09-10 12:28:16 +00:00
|
|
|
if ( id3->offset < id3->toc[i] * (id3->filesize / 256) )
|
2005-06-08 10:33:01 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i--;
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
relpos = id3->toc[i];
|
|
|
|
|
|
|
|
if (i < 99)
|
|
|
|
{
|
|
|
|
nextpos = id3->toc[i+1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextpos = 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
remainder = id3->offset - (relpos * (id3->filesize / 256));
|
|
|
|
|
|
|
|
/* set time for this percent (divide before multiply to prevent
|
|
|
|
overflow on long files. loss of precision is negligible on
|
|
|
|
short files) */
|
|
|
|
id3->elapsed = i * (id3->length / 100);
|
|
|
|
|
|
|
|
/* calculate remainder time */
|
|
|
|
plen = (nextpos - relpos) * (id3->filesize / 256);
|
|
|
|
id3->elapsed += (((remainder * 100) / plen) *
|
|
|
|
(id3->length / 10000));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* no TOC exists. set a rough estimate using average bitrate */
|
|
|
|
int tpk = id3->length / (id3->filesize / 1024);
|
|
|
|
id3->elapsed = id3->offset / 1024 * tpk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2005-09-10 12:28:16 +00:00
|
|
|
/* constant bitrate, use exact calculation */
|
|
|
|
id3->elapsed = id3->offset / (id3->bitrate / 8);
|
2005-06-08 10:33:01 +00:00
|
|
|
}
|
|
|
|
|
2005-06-07 06:34:54 +00:00
|
|
|
/* Copied from mpeg.c. Should be moved somewhere else. */
|
|
|
|
int mp3_get_file_pos(void)
|
|
|
|
{
|
|
|
|
int pos = -1;
|
|
|
|
struct mp3entry *id3 = audio_current_track();
|
|
|
|
|
|
|
|
if (id3->vbr)
|
|
|
|
{
|
|
|
|
if (id3->has_toc)
|
|
|
|
{
|
|
|
|
/* Use the TOC to find the new position */
|
|
|
|
unsigned int percent, remainder;
|
|
|
|
int curtoc, nexttoc, plen;
|
|
|
|
|
|
|
|
percent = (id3->elapsed*100)/id3->length;
|
|
|
|
if (percent > 99)
|
|
|
|
percent = 99;
|
|
|
|
|
|
|
|
curtoc = id3->toc[percent];
|
|
|
|
|
|
|
|
if (percent < 99)
|
|
|
|
nexttoc = id3->toc[percent+1];
|
|
|
|
else
|
|
|
|
nexttoc = 256;
|
|
|
|
|
|
|
|
pos = (id3->filesize/256)*curtoc;
|
|
|
|
|
|
|
|
/* Use the remainder to get a more accurate position */
|
|
|
|
remainder = (id3->elapsed*100)%id3->length;
|
|
|
|
remainder = (remainder*100)/id3->length;
|
|
|
|
plen = (nexttoc - curtoc)*(id3->filesize/256);
|
|
|
|
pos += (plen/100)*remainder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No TOC exists, estimate the new position */
|
|
|
|
pos = (id3->filesize / (id3->length / 1000)) *
|
|
|
|
(id3->elapsed / 1000);
|
|
|
|
}
|
|
|
|
}
|
2005-09-10 12:28:16 +00:00
|
|
|
else if (id3->bitrate)
|
|
|
|
pos = id3->elapsed * (id3->bitrate / 8);
|
2005-06-07 06:34:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos >= (int)(id3->filesize - id3->id3v1len))
|
|
|
|
{
|
|
|
|
/* Don't seek right to the end of the file so that we can
|
|
|
|
transition properly to the next song */
|
|
|
|
pos = id3->filesize - id3->id3v1len - 1;
|
|
|
|
}
|
|
|
|
else if (pos < (int)id3->first_frame_offset)
|
|
|
|
{
|
|
|
|
/* skip past id3v2 tag and other leading garbage */
|
|
|
|
pos = id3->first_frame_offset;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
void mp3_play_data(const unsigned char* start, int size,
|
|
|
|
void (*get_more)(unsigned char** start, int* size))
|
|
|
|
{
|
|
|
|
voice_getmore = get_more;
|
2005-12-05 22:44:42 +00:00
|
|
|
voicebuf = (char *)start;
|
2005-08-20 11:13:19 +00:00
|
|
|
voice_remaining = size;
|
|
|
|
voice_is_playing = true;
|
|
|
|
pcmbuf_reset_mixpos();
|
|
|
|
}
|
|
|
|
|
2005-07-21 11:44:00 +00:00
|
|
|
void audio_set_buffer_margin(int setting)
|
2005-06-06 00:35:21 +00:00
|
|
|
{
|
2005-08-24 16:21:24 +00:00
|
|
|
static const int lookup[] = {5, 15, 30, 60, 120, 180, 300, 600};
|
2005-07-21 11:44:00 +00:00
|
|
|
buffer_margin = lookup[setting];
|
|
|
|
logf("buffer margin: %ds", buffer_margin);
|
|
|
|
set_filebuf_watermark(buffer_margin);
|
|
|
|
}
|
|
|
|
|
2005-07-22 16:46:27 +00:00
|
|
|
/* Set crossfade & PCM buffer length. */
|
2005-11-06 16:40:20 +00:00
|
|
|
void audio_set_crossfade(int enable)
|
2005-07-21 11:44:00 +00:00
|
|
|
{
|
2006-02-07 20:38:55 +00:00
|
|
|
size_t size;
|
2005-07-21 15:47:29 +00:00
|
|
|
bool was_playing = playing;
|
2005-07-21 16:16:30 +00:00
|
|
|
int offset = 0;
|
2005-11-06 16:40:20 +00:00
|
|
|
int seconds = 1;
|
2005-07-21 11:44:00 +00:00
|
|
|
|
2005-10-16 08:01:02 +00:00
|
|
|
if (!filebuf)
|
|
|
|
return; /* Audio buffers not yet set up */
|
|
|
|
|
2005-07-21 15:47:29 +00:00
|
|
|
/* Store the track resume position */
|
|
|
|
if (playing)
|
|
|
|
offset = cur_ti->id3.offset;
|
2005-07-21 11:44:00 +00:00
|
|
|
|
2005-11-06 16:40:20 +00:00
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
seconds = global_settings.crossfade_fade_out_delay
|
|
|
|
+ global_settings.crossfade_fade_out_duration;
|
|
|
|
}
|
2005-07-22 16:46:27 +00:00
|
|
|
|
2005-07-21 11:44:00 +00:00
|
|
|
/* Buffer has to be at least 2s long. */
|
|
|
|
seconds += 2;
|
|
|
|
logf("buf len: %d", seconds);
|
|
|
|
size = seconds * (NATIVE_FREQUENCY*4);
|
|
|
|
if (pcmbuf_get_bufsize() == size)
|
|
|
|
return ;
|
|
|
|
|
2005-07-21 15:47:29 +00:00
|
|
|
/* Playback has to be stopped before changing the buffer size. */
|
2005-11-23 19:13:25 +00:00
|
|
|
audio_stop_playback(true);
|
2005-07-21 15:47:29 +00:00
|
|
|
|
2005-07-21 11:44:00 +00:00
|
|
|
/* Re-initialize audio system. */
|
2005-07-22 16:57:08 +00:00
|
|
|
if (was_playing)
|
2005-12-05 22:44:42 +00:00
|
|
|
gui_syncsplash(0, true, (char *)str(LANG_RESTARTING_PLAYBACK));
|
2005-07-21 11:44:00 +00:00
|
|
|
pcmbuf_init(size);
|
2005-11-06 16:40:20 +00:00
|
|
|
pcmbuf_crossfade_enable(enable);
|
2005-08-20 11:13:19 +00:00
|
|
|
reset_buffer();
|
2005-07-21 11:44:00 +00:00
|
|
|
logf("abuf:%dB", pcmbuf_get_bufsize());
|
2005-08-20 11:13:19 +00:00
|
|
|
logf("fbuf:%dB", filebuflen);
|
2005-07-21 15:47:29 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
voice_init();
|
|
|
|
|
2005-07-21 15:47:29 +00:00
|
|
|
/* Restart playback. */
|
2005-07-22 16:57:08 +00:00
|
|
|
if (was_playing) {
|
2005-07-21 15:47:29 +00:00
|
|
|
audio_play(offset);
|
2005-07-22 16:57:08 +00:00
|
|
|
|
|
|
|
/* Wait for the playback to start again (and display the splash
|
|
|
|
screen during that period. */
|
|
|
|
playing = true;
|
2005-08-20 11:13:19 +00:00
|
|
|
while (playing && !audio_codec_loaded)
|
2005-07-22 16:57:08 +00:00
|
|
|
yield();
|
|
|
|
}
|
2005-06-06 00:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mpeg_id3_options(bool _v1first)
|
|
|
|
{
|
|
|
|
v1first = _v1first;
|
|
|
|
}
|
|
|
|
|
2005-07-02 16:52:30 +00:00
|
|
|
void test_buffer_event(struct mp3entry *id3, bool last_track)
|
|
|
|
{
|
2005-07-03 19:20:40 +00:00
|
|
|
(void)id3;
|
|
|
|
(void)last_track;
|
|
|
|
|
2005-07-08 20:01:06 +00:00
|
|
|
logf("be:%d%s", last_track, id3->path);
|
2005-07-02 16:52:30 +00:00
|
|
|
}
|
|
|
|
|
2005-07-03 18:36:24 +00:00
|
|
|
void test_unbuffer_event(struct mp3entry *id3, bool last_track)
|
2005-07-02 16:52:30 +00:00
|
|
|
{
|
2005-07-03 19:20:40 +00:00
|
|
|
(void)id3;
|
|
|
|
(void)last_track;
|
|
|
|
|
2005-07-08 20:01:06 +00:00
|
|
|
logf("ube:%d%s", last_track, id3->path);
|
2005-07-02 16:52:30 +00:00
|
|
|
}
|
2005-07-03 18:36:24 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
static void playback_init(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2005-08-20 11:13:19 +00:00
|
|
|
static bool voicetagtrue = true;
|
2006-01-27 11:39:46 +00:00
|
|
|
struct event ev;
|
2005-12-20 21:49:47 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
logf("playback api init");
|
2005-07-21 15:47:29 +00:00
|
|
|
pcm_init();
|
2005-12-20 21:49:47 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_RECORDING) && !defined(SIMULATOR)
|
|
|
|
/* Set the input multiplexer to Line In */
|
|
|
|
pcm_rec_mux(0);
|
|
|
|
#endif
|
2006-01-27 11:39:46 +00:00
|
|
|
|
2005-07-02 16:52:30 +00:00
|
|
|
audio_set_track_buffer_event(test_buffer_event);
|
|
|
|
audio_set_track_unbuffer_event(test_unbuffer_event);
|
2006-01-27 11:39:46 +00:00
|
|
|
|
|
|
|
/* Initialize codec api. */
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.read_filebuf = codec_filebuf_callback;
|
2005-07-13 12:48:22 +00:00
|
|
|
ci.pcmbuf_insert = pcmbuf_insert_buffer;
|
|
|
|
ci.pcmbuf_insert_split = codec_pcmbuf_insert_split_callback;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.get_codec_memory = get_codec_memory_callback;
|
|
|
|
ci.request_buffer = codec_request_buffer_callback;
|
|
|
|
ci.advance_buffer = codec_advance_buffer_callback;
|
|
|
|
ci.advance_buffer_loc = codec_advance_buffer_loc_callback;
|
|
|
|
ci.request_next_track = codec_request_next_track_callback;
|
|
|
|
ci.mp3_get_filepos = codec_mp3_get_filepos_callback;
|
|
|
|
ci.seek_buffer = codec_seek_buffer_callback;
|
2005-08-21 18:12:31 +00:00
|
|
|
ci.seek_complete = codec_seek_complete_callback;
|
2005-06-05 23:05:10 +00:00
|
|
|
ci.set_elapsed = codec_set_elapsed_callback;
|
2005-07-12 16:45:38 +00:00
|
|
|
ci.set_offset = codec_set_offset_callback;
|
2005-06-10 15:02:10 +00:00
|
|
|
ci.configure = codec_configure_callback;
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
memcpy(&ci_voice, &ci, sizeof(struct codec_api));
|
|
|
|
memset(&id3_voice, 0, sizeof(struct mp3entry));
|
|
|
|
ci_voice.taginfo_ready = &voicetagtrue;
|
|
|
|
ci_voice.id3 = &id3_voice;
|
|
|
|
ci_voice.pcmbuf_insert = codec_pcmbuf_insert_callback;
|
|
|
|
id3_voice.frequency = 11200;
|
|
|
|
id3_voice.length = 1000000L;
|
2006-01-27 11:39:46 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
create_thread(codec_thread, codec_stack, sizeof(codec_stack),
|
|
|
|
codec_thread_name);
|
2005-08-20 11:13:19 +00:00
|
|
|
create_thread(voice_codec_thread, voice_codec_stack,
|
|
|
|
sizeof(voice_codec_stack), voice_codec_thread_name);
|
2005-10-16 12:05:58 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
queue_wait(&audio_queue, &ev);
|
|
|
|
if (ev.id == Q_AUDIO_POSTINIT)
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
2006-01-28 12:49:05 +00:00
|
|
|
filebuf = (char *)&audiobuf[MALLOC_BUFSIZE];
|
|
|
|
|
2005-10-16 12:05:58 +00:00
|
|
|
/* Apply relevant settings */
|
|
|
|
audio_set_buffer_margin(global_settings.buffer_margin);
|
|
|
|
audio_set_crossfade(global_settings.crossfade);
|
2006-01-27 11:39:46 +00:00
|
|
|
|
|
|
|
sound_settings_apply();
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
void audio_preinit(void)
|
|
|
|
{
|
|
|
|
logf("playback system pre-init");
|
|
|
|
|
|
|
|
filebufused = 0;
|
|
|
|
filling = false;
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
playing = false;
|
|
|
|
audio_codec_loaded = false;
|
|
|
|
voice_is_playing = false;
|
|
|
|
paused = false;
|
|
|
|
track_changed = false;
|
|
|
|
current_fd = -1;
|
|
|
|
track_buffer_callback = NULL;
|
|
|
|
track_unbuffer_callback = NULL;
|
|
|
|
track_changed_callback = NULL;
|
|
|
|
/* Just to prevent cur_ti never be anything random. */
|
|
|
|
cur_ti = &tracks[0];
|
|
|
|
|
|
|
|
mutex_init(&mutex_bufferfill);
|
|
|
|
mutex_init(&mutex_codecthread);
|
|
|
|
|
|
|
|
queue_init(&audio_queue);
|
|
|
|
queue_init(&codec_queue);
|
|
|
|
queue_init(&voice_codec_queue);
|
|
|
|
|
|
|
|
create_thread(audio_thread, audio_stack, sizeof(audio_stack),
|
|
|
|
audio_thread_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_init(void)
|
|
|
|
{
|
|
|
|
logf("playback system post-init");
|
2006-01-28 12:49:05 +00:00
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_POSTINIT, 0);
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|