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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-04-13 21:47:00 +00:00
|
|
|
/* TODO: Can use the track changed callback to detect end of track and seek
|
|
|
|
* in the previous track until this happens */
|
|
|
|
/* Design: we have prev_ti already, have a conditional for what type of seek
|
|
|
|
* to do on a seek request, if it is a previous track seek, skip previous,
|
|
|
|
* and in the request_next_track callback set the offset up the same way that
|
|
|
|
* starting from an offset works. */
|
2006-10-13 20:48:23 +00:00
|
|
|
/* TODO: Pause should be handled in here, rather than PCMBUF so that voice can
|
|
|
|
* play whilst audio is paused */
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
#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 "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 "buffer.h"
|
2005-06-26 19:41:29 +00:00
|
|
|
#include "dsp.h"
|
2006-01-21 23:43:57 +00:00
|
|
|
#include "abrepeat.h"
|
2006-10-25 10:17:57 +00:00
|
|
|
#ifdef HAVE_TAGCACHE
|
2006-03-26 16:37:18 +00:00
|
|
|
#include "tagcache.h"
|
2006-10-25 10:17:57 +00:00
|
|
|
#endif
|
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"
|
2006-08-28 22:38:41 +00:00
|
|
|
#include "splash.h"
|
2005-08-20 11:13:19 +00:00
|
|
|
#include "talk.h"
|
2006-11-06 14:24:18 +00:00
|
|
|
#include "ata_idle_notify.h"
|
2006-08-28 22:38:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
#include "recording.h"
|
2006-11-06 18:07:30 +00:00
|
|
|
#include "talk.h"
|
2005-11-13 10:57:35 +00:00
|
|
|
#endif
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
#define PLAYBACK_VOICE
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
|
2006-04-11 03:55:58 +00:00
|
|
|
/* default point to start buffer refill */
|
2005-06-29 14:46:27 +00:00
|
|
|
#define AUDIO_DEFAULT_WATERMARK (1024*512)
|
2006-04-11 03:55:58 +00:00
|
|
|
/* amount of data to read in one read() call */
|
2005-06-10 15:02:10 +00:00
|
|
|
#define AUDIO_DEFAULT_FILECHUNK (1024*32)
|
2006-04-11 03:55:58 +00:00
|
|
|
/* point at which the file buffer will fight for CPU time */
|
2006-04-06 16:21:31 +00:00
|
|
|
#define AUDIO_FILEBUF_CRITICAL (1024*128)
|
2006-04-11 03:55:58 +00:00
|
|
|
/* amount of guess-space to allow for codecs that must hunt and peck
|
|
|
|
* for their correct seeek target, 32k seems a good size */
|
|
|
|
#define AUDIO_REBUFFER_GUESS_SIZE (1024*32)
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* macros to enable logf for queues
|
|
|
|
logging on SYS_TIMEOUT can be disabled */
|
2006-08-31 10:01:07 +00:00
|
|
|
#ifdef SIMULATOR
|
2006-11-06 18:07:30 +00:00
|
|
|
/* Define this for logf output of all queuing except SYS_TIMEOUT */
|
|
|
|
#define PLAYBACK_LOGQUEUES
|
|
|
|
/* Define this to logf SYS_TIMEOUT messages */
|
|
|
|
#define PLAYBACK_LOGQUEUES_SYS_TIMEOUT
|
2006-08-31 10:01:07 +00:00
|
|
|
#endif
|
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_LOGQUEUES
|
2006-08-31 10:01:07 +00:00
|
|
|
#define LOGFQUEUE(s) logf("%s", s)
|
|
|
|
#else
|
|
|
|
#define LOGFQUEUE(s)
|
|
|
|
#endif
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef PLAYBACK_LOGQUEUES_SYS_TIMEOUT
|
|
|
|
#define LOGFQUEUE_SYS_TIMEOUT(s) logf("%s", s)
|
|
|
|
#else
|
|
|
|
#define LOGFQUEUE_SYS_TIMEOUT(s)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Define one constant that includes recording related functionality */
|
|
|
|
#if defined(HAVE_RECORDING) && !defined(SIMULATOR)
|
|
|
|
#define AUDIO_HAVE_RECORDING
|
|
|
|
#endif
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
enum {
|
|
|
|
Q_AUDIO_PLAY = 1,
|
|
|
|
Q_AUDIO_STOP,
|
|
|
|
Q_AUDIO_PAUSE,
|
2006-04-06 21:06:37 +00:00
|
|
|
Q_AUDIO_SKIP,
|
2006-04-07 18:18:36 +00:00
|
|
|
Q_AUDIO_PRE_FF_REWIND,
|
2006-01-27 11:39:46 +00:00
|
|
|
Q_AUDIO_FF_REWIND,
|
2006-04-09 02:15:35 +00:00
|
|
|
Q_AUDIO_REBUFFER_SEEK,
|
2006-04-11 03:55:58 +00:00
|
|
|
Q_AUDIO_CHECK_NEW_TRACK,
|
2006-01-27 11:39:46 +00:00
|
|
|
Q_AUDIO_FLUSH,
|
|
|
|
Q_AUDIO_TRACK_CHANGED,
|
2006-04-06 21:06:37 +00:00
|
|
|
Q_AUDIO_DIR_SKIP,
|
2006-04-26 04:01:35 +00:00
|
|
|
Q_AUDIO_NEW_PLAYLIST,
|
2006-01-27 11:39:46 +00:00
|
|
|
Q_AUDIO_POSTINIT,
|
2006-04-06 16:21:31 +00:00
|
|
|
Q_AUDIO_FILL_BUFFER,
|
2006-11-06 14:24:18 +00:00
|
|
|
#if MEM > 8
|
|
|
|
Q_AUDIO_FILL_BUFFER_IF_ACTIVE_ATA,
|
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
|
|
|
Q_AUDIO_LOAD_ENCODER,
|
|
|
|
#endif
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-04-19 00:23:08 +00:00
|
|
|
Q_CODEC_REQUEST_PENDING,
|
2006-04-14 13:05:08 +00:00
|
|
|
Q_CODEC_REQUEST_COMPLETE,
|
2006-04-14 14:03:43 +00:00
|
|
|
Q_CODEC_REQUEST_FAILED,
|
2006-04-14 13:05:08 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
Q_VOICE_PLAY,
|
|
|
|
Q_VOICE_STOP,
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
Q_CODEC_LOAD,
|
|
|
|
Q_CODEC_LOAD_DISK,
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
2006-08-28 22:38:41 +00:00
|
|
|
Q_ENCODER_LOAD_DISK,
|
|
|
|
Q_ENCODER_RECORD,
|
|
|
|
#endif
|
2006-01-27 11:39:46 +00:00
|
|
|
};
|
2005-06-05 23:05:10 +00:00
|
|
|
|
|
|
|
/* As defined in plugins/lib/xxx2wav.h */
|
2006-08-13 09:19:24 +00:00
|
|
|
#if MEM > 1
|
2005-06-05 23:05:10 +00:00
|
|
|
#define MALLOC_BUFSIZE (512*1024)
|
2005-10-30 09:30:14 +00:00
|
|
|
#define GUARD_BUFSIZE (32*1024)
|
2006-08-12 23:01:52 +00:00
|
|
|
#else
|
|
|
|
#define MALLOC_BUFSIZE (100*1024)
|
|
|
|
#define GUARD_BUFSIZE (8*1024)
|
|
|
|
#endif
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
/* As defined in plugin.lds */
|
2006-11-22 00:41:30 +00:00
|
|
|
#if defined(CPU_PP)
|
2006-03-22 15:19:59 +00:00
|
|
|
#define CODEC_IRAM_ORIGIN 0x4000c000
|
2006-11-10 07:45:30 +00:00
|
|
|
#define CODEC_IRAM_SIZE 0xc000
|
2006-08-23 14:02:35 +00:00
|
|
|
#elif defined(IAUDIO_X5)
|
2006-11-10 07:45:30 +00:00
|
|
|
#define CODEC_IRAM_ORIGIN 0x10010000
|
|
|
|
#define CODEC_IRAM_SIZE 0x10000
|
2006-03-22 15:19:59 +00:00
|
|
|
#else
|
2005-09-01 20:57:33 +00:00
|
|
|
#define CODEC_IRAM_ORIGIN 0x1000c000
|
|
|
|
#define CODEC_IRAM_SIZE 0xc000
|
2006-11-10 07:45:30 +00:00
|
|
|
#endif
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-11-09 21:59:27 +00:00
|
|
|
#ifndef IBSS_ATTR_VOICE_STACK
|
|
|
|
#define IBSS_ATTR_VOICE_STACK IBSS_ATTR
|
|
|
|
#endif
|
|
|
|
|
2006-05-01 18:18:54 +00:00
|
|
|
#ifndef SIMULATOR
|
2005-06-05 23:05:10 +00:00
|
|
|
extern bool audio_is_initialized;
|
2006-05-01 18:18:54 +00:00
|
|
|
#else
|
|
|
|
static bool audio_is_initialized = false;
|
|
|
|
#endif
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Variables are commented with the threads that use them: *
|
|
|
|
* A=audio, C=codec, V=voice. A suffix of - indicates that *
|
|
|
|
* the variable is read but not updated on that thread. */
|
2006-11-13 09:07:18 +00:00
|
|
|
/* TBD: Split out "audio" and "playback" (ie. calling) threads */
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Main state control */
|
|
|
|
static struct event_queue codec_callback_queue; /* Queue for codec callback responses */
|
|
|
|
static volatile bool audio_codec_loaded; /* Is codec loaded? (C/A-) */
|
|
|
|
static volatile bool playing; /* Is audio playing? (A) */
|
|
|
|
static volatile bool paused; /* Is audio paused? (A/C-) */
|
|
|
|
static volatile bool filling IDATA_ATTR; /* Is file buffer currently being refilled? (A/C-) */
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Ring buffer where tracks and codecs are loaded */
|
2006-11-06 18:07:30 +00:00
|
|
|
static unsigned char *filebuf; /* Pointer to start of ring buffer (A/C-) */
|
2006-10-30 08:54:48 +00:00
|
|
|
size_t filebuflen; /* Total size of the ring buffer FIXME: make static (A/C-)*/
|
|
|
|
static volatile size_t buf_ridx IDATA_ATTR; /* Ring buffer read position (A/C) FIXME? should be (C/A-) */
|
|
|
|
static volatile size_t buf_widx IDATA_ATTR; /* Ring buffer read position (A/C-) */
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#define BUFFER_STATE_TRASHED -1 /* Buffer is in a trashed state and must be reset */
|
|
|
|
#define BUFFER_STATE_NORMAL 0 /* Buffer is arranged for voice and audio */
|
|
|
|
#define BUFFER_STATE_VOICED_ONLY 1 /* Buffer is arranged for voice-only use */
|
|
|
|
static int buffer_state = BUFFER_STATE_TRASHED; /* Buffer state */
|
|
|
|
|
2006-10-13 09:16:36 +00:00
|
|
|
#define RINGBUF_ADD(p,v) ((p+v)<filebuflen ? p+v : p+v-filebuflen)
|
2006-10-12 16:51:22 +00:00
|
|
|
#define RINGBUF_SUB(p,v) ((p>=v) ? p-v : p+filebuflen-v)
|
2006-10-17 12:56:22 +00:00
|
|
|
#define RINGBUF_ADD_CROSS(p1,v,p2) ((p1<p2)?(int)(p1+v)-(int)p2:(int)(p1+v-p2)-(int)filebuflen)
|
2006-10-30 08:54:48 +00:00
|
|
|
#define FILEBUFUSED RINGBUF_SUB(buf_widx, buf_ridx) /* Bytes available in the buffer */
|
2006-10-12 16:51:22 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Track info buffer */
|
|
|
|
static struct track_info tracks[MAX_TRACK]; /* Track info structure about songs in the file buffer (A/C-) */
|
|
|
|
static volatile int track_ridx; /* Track being decoded (A/C-) */
|
|
|
|
static int track_widx; /* Track being buffered (A) */
|
|
|
|
static bool track_changed; /* Set to indicate track has changed (A) */
|
|
|
|
static struct track_info *prev_ti; /* Pointer to previous track played info (A/C-) */
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
#define CUR_TI (&tracks[track_ridx]) /* Pointer to current track playing info (A/C-) */
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Audio buffering controls */
|
|
|
|
static int last_peek_offset; /* Step count to the next unbuffered track (A) */
|
|
|
|
static int current_fd; /* Partially loaded track file handle to continue buffering (A) */
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Scrobbler support */
|
|
|
|
static unsigned long prev_track_elapsed; /* Previous track elapsed time (C/A-) */
|
2006-10-12 15:54:36 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Track change controls */
|
|
|
|
static bool automatic_skip = false; /* Was the skip being executed manual or automatic? (C/A-) */
|
|
|
|
static bool playlist_end = false; /* Have we reached end of the current playlist? (A) */
|
|
|
|
static bool dir_skip = false; /* Is a directory skip pending? (A) */
|
|
|
|
static bool new_playlist = false; /* Are we starting a new playlist? (A) */
|
|
|
|
static int wps_offset = 0; /* Pending track change offset, to keep WPS responsive (A) */
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
/* Callbacks.. */
|
|
|
|
void (*track_changed_callback)(struct mp3entry *id3); /* ...when current track has really changed */
|
|
|
|
void (*track_buffer_callback)(struct mp3entry *id3, bool last_track); /* ...when track has been buffered */
|
|
|
|
void (*track_unbuffer_callback)(struct mp3entry *id3, bool last_track); /* ...when track is being unbuffered */
|
2005-06-29 21:36:30 +00:00
|
|
|
|
2005-06-10 15:02:10 +00:00
|
|
|
/* Configuration */
|
2006-10-30 08:54:48 +00:00
|
|
|
static size_t conf_watermark; /* Low water mark (A/C) FIXME */
|
|
|
|
static size_t conf_filechunk; /* Largest chunk the codec accepts (A/C) FIXME */
|
|
|
|
static size_t conf_preseek; /* Codec pre-seek margin (A/C) FIXME */
|
|
|
|
static size_t buffer_margin; /* Buffer margin aka anti-skip buffer (A/C-) */
|
|
|
|
static bool v1first = false; /* ID3 data control, true if V1 then V2 (A) */
|
2005-06-06 00:35:21 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Multiple threads */
|
2006-10-30 08:54:48 +00:00
|
|
|
static const char *get_codec_filename(int enc_spec); /* Returns codec filename (A-/C-/V-) */
|
|
|
|
static void set_filebuf_watermark(int seconds); /* Set low watermark (A/C) FIXME */
|
2005-06-08 10:33:01 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Audio thread */
|
|
|
|
static struct event_queue audio_queue;
|
|
|
|
static long audio_stack[(DEFAULT_STACK_SIZE + 0x1000)/sizeof(long)];
|
|
|
|
static const char audio_thread_name[] = "audio";
|
2006-04-09 15:30:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_thread(void);
|
|
|
|
static void audio_initiate_track_change(long direction);
|
|
|
|
static bool audio_have_tracks(void);
|
2006-11-06 18:07:30 +00:00
|
|
|
static void audio_reset_buffer(size_t pcmbufsize);
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Codec thread */
|
2006-09-17 08:34:42 +00:00
|
|
|
extern struct codec_api ci;
|
2006-09-01 07:59:31 +00:00
|
|
|
static struct event_queue codec_queue;
|
|
|
|
static long codec_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)]
|
|
|
|
IBSS_ATTR;
|
|
|
|
static const char codec_thread_name[] = "codec";
|
2006-10-30 08:54:48 +00:00
|
|
|
struct thread_entry *codec_thread_p; /* For modifying thread priority later. */
|
|
|
|
|
|
|
|
volatile int current_codec IDATA_ATTR; /* Current codec (normal/voice) */
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Voice thread */
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
extern struct codec_api ci_voice;
|
2006-10-19 09:42:58 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
static struct thread_entry *voice_thread_p = NULL;
|
2006-09-01 07:59:31 +00:00
|
|
|
static struct event_queue voice_queue;
|
|
|
|
static long voice_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)]
|
2006-11-09 21:59:27 +00:00
|
|
|
IBSS_ATTR_VOICE_STACK;
|
2006-09-01 07:59:31 +00:00
|
|
|
static const char voice_thread_name[] = "voice codec";
|
2006-10-30 08:54:48 +00:00
|
|
|
|
|
|
|
/* Voice codec swapping control */
|
|
|
|
extern unsigned char codecbuf[]; /* DRAM codec swap buffer */
|
|
|
|
|
|
|
|
#ifdef SIMULATOR
|
|
|
|
static unsigned char sim_iram[CODEC_IRAM_SIZE]; /* IRAM codec swap buffer for sim*/
|
|
|
|
#undef CODEC_IRAM_ORIGIN
|
|
|
|
#define CODEC_IRAM_ORIGIN sim_iram
|
|
|
|
#endif
|
|
|
|
|
2006-11-18 02:18:29 +00:00
|
|
|
static unsigned char *iram_buf[2] = { NULL, NULL }; /* Ptr to IRAM buffers for normal/voice codecs */
|
|
|
|
static unsigned char *dram_buf[2] = { NULL, NULL }; /* Ptr to DRAM buffers for normal/voice codecs */
|
2006-10-30 08:54:48 +00:00
|
|
|
static struct mutex mutex_codecthread; /* Mutex to control which codec (normal/voice) is running */
|
|
|
|
|
|
|
|
/* Voice state */
|
|
|
|
static volatile bool voice_thread_start; /* Set to trigger voice playback (A/V) */
|
|
|
|
static volatile bool voice_is_playing; /* Is voice currently playing? (V) */
|
|
|
|
static volatile bool voice_codec_loaded; /* Is voice codec loaded (V/A-) */
|
|
|
|
static char *voicebuf;
|
|
|
|
static size_t voice_remaining;
|
|
|
|
|
2006-11-18 02:18:29 +00:00
|
|
|
#ifdef IRAM_STEAL
|
|
|
|
static bool voice_iram_stolen = false; /* Voice IRAM has been stolen for other use */
|
|
|
|
#endif
|
|
|
|
|
2006-10-30 08:54:48 +00:00
|
|
|
static void (*voice_getmore)(unsigned char** start, int* size);
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
struct voice_info {
|
|
|
|
void (*callback)(unsigned char **start, int *size);
|
|
|
|
int size;
|
|
|
|
char *buf;
|
|
|
|
};
|
|
|
|
static void voice_thread(void);
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
#endif /* PLAYBACK_VOICE */
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* --- External interfaces --- */
|
2006-03-23 16:18:17 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void mp3_play_data(const unsigned char* start, int size,
|
|
|
|
void (*get_more)(unsigned char** start, int* size))
|
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
static struct voice_info voice_clip;
|
|
|
|
voice_clip.callback = get_more;
|
|
|
|
voice_clip.buf = (char *)start;
|
|
|
|
voice_clip.size = size;
|
2006-09-17 08:34:42 +00:00
|
|
|
LOGFQUEUE("mp3 > voice Q_VOICE_STOP");
|
2006-09-01 07:59:31 +00:00
|
|
|
queue_post(&voice_queue, Q_VOICE_STOP, 0);
|
2006-09-17 08:34:42 +00:00
|
|
|
LOGFQUEUE("mp3 > voice Q_VOICE_PLAY");
|
2006-09-01 07:59:31 +00:00
|
|
|
queue_post(&voice_queue, Q_VOICE_PLAY, &voice_clip);
|
2006-09-17 08:34:42 +00:00
|
|
|
voice_thread_start = true;
|
2006-10-15 11:57:52 +00:00
|
|
|
trigger_cpu_boost();
|
2006-09-17 08:34:42 +00:00
|
|
|
#else
|
|
|
|
(void) start;
|
|
|
|
(void) size;
|
|
|
|
(void) get_more;
|
|
|
|
#endif
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void mp3_play_stop(void)
|
2005-08-28 14:16:03 +00:00
|
|
|
{
|
2006-10-25 06:19:27 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-11-13 09:07:18 +00:00
|
|
|
queue_remove_from_head(&voice_queue, Q_VOICE_STOP);
|
2006-10-25 06:19:27 +00:00
|
|
|
LOGFQUEUE("mp3 > voice Q_VOICE_STOP");
|
|
|
|
queue_post(&voice_queue, Q_VOICE_STOP, (void *)1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
bool mp3_pause_done(void)
|
|
|
|
{
|
|
|
|
return pcm_is_paused();
|
2005-08-28 14:16:03 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void mpeg_id3_options(bool _v1first)
|
2005-06-26 19:41:29 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
v1first = _v1first;
|
|
|
|
}
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2006-11-28 15:00:56 +00:00
|
|
|
/* If voice could be swapped out - wait for it to return
|
|
|
|
* Used by buffer claming functions.
|
|
|
|
*/
|
|
|
|
static void wait_for_voice_swap_in(void)
|
|
|
|
{
|
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
if (NULL == iram_buf[CODEC_IDX_VOICE])
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (current_codec != CODEC_IDX_VOICE)
|
|
|
|
yield();
|
|
|
|
#endif /* PLAYBACK_VOICE */
|
|
|
|
}
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
unsigned char *audio_get_buffer(bool talk_buf, size_t *buffer_size)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-11-18 02:18:29 +00:00
|
|
|
unsigned char *buf, *end;
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2006-11-27 13:54:50 +00:00
|
|
|
if (audio_is_initialized)
|
2006-11-28 15:00:56 +00:00
|
|
|
{
|
2006-11-27 13:54:50 +00:00
|
|
|
audio_stop();
|
2006-11-28 15:00:56 +00:00
|
|
|
wait_for_voice_swap_in();
|
|
|
|
voice_stop();
|
|
|
|
}
|
|
|
|
/* else buffer_state will be BUFFER_STATE_TRASHED at this point */
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2006-11-18 02:18:29 +00:00
|
|
|
if (buffer_size == NULL)
|
|
|
|
{
|
|
|
|
/* Special case for talk_init to use */
|
|
|
|
buffer_state = BUFFER_STATE_TRASHED;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-11-27 13:54:50 +00:00
|
|
|
if (talk_buf || buffer_state == BUFFER_STATE_TRASHED
|
|
|
|
|| !talk_voice_required())
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
|
|
|
logf("get buffer: talk_buf");
|
|
|
|
/* ok to use everything from audiobuf to audiobufend */
|
|
|
|
if (buffer_state != BUFFER_STATE_TRASHED)
|
2006-11-22 09:13:14 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
talk_buffer_steal();
|
2006-11-22 09:13:14 +00:00
|
|
|
buffer_state = BUFFER_STATE_TRASHED;
|
|
|
|
}
|
2006-11-28 15:00:56 +00:00
|
|
|
|
|
|
|
buf = audiobuf;
|
|
|
|
end = audiobufend;
|
2006-11-06 18:07:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* skip talk buffer and move pcm buffer to end */
|
|
|
|
logf("get buffer: voice");
|
2006-11-28 15:00:56 +00:00
|
|
|
buf = audiobuf + talk_get_bufsize();
|
|
|
|
end = audiobufend - pcmbuf_init(pcmbuf_get_bufsize(), audiobufend);
|
2006-11-06 18:07:30 +00:00
|
|
|
buffer_state = BUFFER_STATE_VOICED_ONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer_size = end - buf;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2006-11-18 02:18:29 +00:00
|
|
|
#ifdef IRAM_STEAL
|
|
|
|
void audio_iram_steal(void)
|
|
|
|
{
|
|
|
|
/* We need to stop audio playback in order to use codec IRAM */
|
|
|
|
audio_stop();
|
|
|
|
|
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
if (NULL != iram_buf[CODEC_IDX_VOICE])
|
|
|
|
{
|
2006-11-22 09:13:14 +00:00
|
|
|
/* Can't already be stolen */
|
|
|
|
if (voice_iram_stolen)
|
|
|
|
return;
|
|
|
|
|
2006-11-28 15:00:56 +00:00
|
|
|
wait_for_voice_swap_in();
|
2006-11-18 02:18:29 +00:00
|
|
|
voice_stop();
|
|
|
|
|
|
|
|
/* Save voice IRAM - safe to do here since state is known */
|
|
|
|
memcpy(iram_buf[CODEC_IDX_VOICE], (void *)CODEC_IRAM_ORIGIN,
|
|
|
|
CODEC_IRAM_SIZE);
|
|
|
|
voice_iram_stolen = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Nothing much to do if no voice */
|
|
|
|
voice_iram_stolen = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* IRAM_STEAL */
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
unsigned char *audio_get_recording_buffer(size_t *buffer_size)
|
|
|
|
{
|
|
|
|
/* don't allow overwrite of voice swap area or we'll trash the
|
|
|
|
swapped-out voice codec but can use whole thing if none */
|
2006-11-18 02:18:29 +00:00
|
|
|
unsigned char *end;
|
2006-11-06 18:07:30 +00:00
|
|
|
|
|
|
|
audio_stop();
|
2006-11-28 15:00:56 +00:00
|
|
|
wait_for_voice_swap_in();
|
|
|
|
voice_stop();
|
2006-11-06 18:07:30 +00:00
|
|
|
talk_buffer_steal();
|
|
|
|
|
2006-11-18 02:18:29 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
#ifdef IRAM_STEAL
|
2006-11-22 09:13:14 +00:00
|
|
|
end = dram_buf[CODEC_IDX_VOICE];
|
2006-11-18 02:18:29 +00:00
|
|
|
#else
|
2006-11-22 09:13:14 +00:00
|
|
|
end = iram_buf[CODEC_IDX_VOICE];
|
2006-11-18 02:18:29 +00:00
|
|
|
#endif /* IRAM_STEAL */
|
2006-11-22 09:13:14 +00:00
|
|
|
if (NULL == end)
|
2006-11-18 02:18:29 +00:00
|
|
|
#endif /* PLAYBACK_VOICE */
|
2006-11-22 09:13:14 +00:00
|
|
|
end = audiobufend;
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
buffer_state = BUFFER_STATE_TRASHED;
|
|
|
|
|
|
|
|
*buffer_size = end - audiobuf;
|
|
|
|
|
|
|
|
return (unsigned char *)audiobuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_load_encoder(int afmt)
|
|
|
|
{
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
const char *enc_fn = get_codec_filename(afmt | CODEC_TYPE_ENCODER);
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!enc_fn)
|
2006-11-06 18:07:30 +00:00
|
|
|
return false;
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_remove_encoder();
|
2006-11-06 18:07:30 +00:00
|
|
|
ci.enc_codec_loaded = 0; /* clear any previous error condition */
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
LOGFQUEUE("audio > Q_AUDIO_LOAD_ENCODER");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_LOAD_ENCODER, (void *)enc_fn);
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
while (ci.enc_codec_loaded == 0)
|
2006-09-01 07:59:31 +00:00
|
|
|
yield();
|
2006-11-06 18:07:30 +00:00
|
|
|
|
|
|
|
logf("codec loaded: %d", ci.enc_codec_loaded);
|
|
|
|
|
|
|
|
return ci.enc_codec_loaded > 0;
|
|
|
|
#else
|
|
|
|
(void)afmt;
|
|
|
|
return true;
|
2006-09-01 07:59:31 +00:00
|
|
|
#endif
|
|
|
|
} /* audio_load_encoder */
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_remove_encoder(void)
|
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifndef SIMULATOR
|
|
|
|
/* force encoder codec unload (if currently loaded) */
|
|
|
|
if (ci.enc_codec_loaded <= 0)
|
2006-09-01 07:59:31 +00:00
|
|
|
return;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.stop_codec = true;
|
2006-11-06 18:07:30 +00:00
|
|
|
while (ci.enc_codec_loaded > 0)
|
2006-09-01 07:59:31 +00:00
|
|
|
yield();
|
|
|
|
#endif
|
|
|
|
} /* audio_remove_encoder */
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#endif /* HAVE_RECORDING */
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
struct mp3entry* audio_current_track(void)
|
|
|
|
{
|
|
|
|
const char *filename;
|
|
|
|
const char *p;
|
|
|
|
static struct mp3entry temp_id3;
|
|
|
|
int cur_idx;
|
2006-09-17 08:34:42 +00:00
|
|
|
int offset = ci.new_track + wps_offset;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
cur_idx = track_ridx + offset;
|
2006-09-01 07:59:31 +00:00
|
|
|
cur_idx &= MAX_TRACK_MASK;
|
2006-02-07 20:38:55 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (tracks[cur_idx].taginfo_ready)
|
|
|
|
return &tracks[cur_idx].id3;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
memset(&temp_id3, 0, sizeof(struct mp3entry));
|
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
filename = playlist_peek(offset);
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!filename)
|
|
|
|
filename = "No file!";
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
if (tagcache_fill_tags(&temp_id3, filename))
|
|
|
|
return &temp_id3;
|
|
|
|
#endif
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
p = strrchr(filename, '/');
|
|
|
|
if (!p)
|
|
|
|
p = filename;
|
|
|
|
else
|
|
|
|
p++;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
strncpy(temp_id3.path, p, sizeof(temp_id3.path)-1);
|
|
|
|
temp_id3.title = &temp_id3.path[0];
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return &temp_id3;
|
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
struct mp3entry* audio_next_track(void)
|
|
|
|
{
|
|
|
|
int next_idx = track_ridx;
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!audio_have_tracks())
|
|
|
|
return NULL;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
next_idx++;
|
|
|
|
next_idx &= MAX_TRACK_MASK;
|
2005-07-16 12:25:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!tracks[next_idx].taginfo_ready)
|
|
|
|
return NULL;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return &tracks[next_idx].id3;
|
|
|
|
}
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
bool audio_has_changed_track(void)
|
|
|
|
{
|
|
|
|
if (track_changed)
|
|
|
|
{
|
|
|
|
track_changed = false;
|
|
|
|
return true;
|
|
|
|
}
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return false;
|
2006-04-22 14:40:13 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_play(long offset)
|
2005-06-26 19:41:29 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("audio_play");
|
2006-10-25 08:54:25 +00:00
|
|
|
|
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
/* Truncate any existing voice output so we don't have spelling
|
|
|
|
* etc. over the first part of the played track */
|
|
|
|
LOGFQUEUE("mp3 > voice Q_VOICE_STOP");
|
|
|
|
queue_post(&voice_queue, Q_VOICE_STOP, (void *)1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Start playback */
|
2006-09-01 07:59:31 +00:00
|
|
|
if (playing && offset <= 0)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_NEW_PLAYLIST");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_NEW_PLAYLIST, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-26 07:19:43 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_STOP");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_STOP, 0);
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_PLAY");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_PLAY, (void *)offset);
|
|
|
|
}
|
2006-10-25 08:54:25 +00:00
|
|
|
|
|
|
|
/* Don't return until playback has actually started */
|
2006-09-26 07:19:43 +00:00
|
|
|
while (!playing)
|
|
|
|
yield();
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_stop(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-10-25 08:54:25 +00:00
|
|
|
/* Stop playback */
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_STOP");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_STOP, 0);
|
2006-10-25 08:54:25 +00:00
|
|
|
|
|
|
|
/* Don't return until playback has actually stopped */
|
2006-11-02 07:53:55 +00:00
|
|
|
while(playing || !queue_empty(&audio_queue))
|
2006-09-26 07:19:43 +00:00
|
|
|
yield();
|
2006-04-22 14:40:13 +00:00
|
|
|
}
|
2006-04-17 17:05:05 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_pause(void)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_PAUSE");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_PAUSE, (void *)true);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_resume(void)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_PAUSE resume");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_PAUSE, (void *)false);
|
2006-01-21 22:42:44 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_next(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
if (playlist_check(ci.new_track + wps_offset + 1))
|
|
|
|
{
|
2006-09-26 07:19:43 +00:00
|
|
|
if (global_settings.beep)
|
|
|
|
pcmbuf_beep(5000, 100, 2500*global_settings.beep);
|
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_SKIP 1");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_SKIP, (void *)1);
|
|
|
|
/* Keep wps fast while our message travels inside deep playback queues. */
|
|
|
|
wps_offset++;
|
|
|
|
track_changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No more tracks. */
|
|
|
|
if (global_settings.beep)
|
2006-09-26 07:19:43 +00:00
|
|
|
pcmbuf_beep(1000, 100, 1000*global_settings.beep);
|
2006-09-17 08:34:42 +00:00
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_prev(void)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
if (playlist_check(ci.new_track + wps_offset - 1))
|
|
|
|
{
|
2006-09-26 07:19:43 +00:00
|
|
|
if (global_settings.beep)
|
|
|
|
pcmbuf_beep(5000, 100, 2500*global_settings.beep);
|
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_SKIP -1");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_SKIP, (void *)-1);
|
|
|
|
/* Keep wps fast while our message travels inside deep playback queues. */
|
|
|
|
wps_offset--;
|
|
|
|
track_changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No more tracks. */
|
|
|
|
if (global_settings.beep)
|
2006-09-26 07:19:43 +00:00
|
|
|
pcmbuf_beep(1000, 100, 1000*global_settings.beep);
|
2006-09-17 08:34:42 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_next_dir(void)
|
2005-07-12 16:45:38 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP 1");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_DIR_SKIP, (void *)1);
|
2006-04-22 14:40:13 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_prev_dir(void)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP -1");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_DIR_SKIP, (void *)-1);
|
2005-07-12 16:45:38 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_pre_ff_rewind(void)
|
2006-04-06 16:21:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_PRE_FF_REWIND");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_PRE_FF_REWIND, 0);
|
2006-04-06 16:21:31 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_ff_rewind(long newpos)
|
2006-04-09 02:15:35 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_FF_REWIND");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_FF_REWIND, (int *)newpos);
|
2006-04-09 02:15:35 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_flush_and_reload_tracks(void)
|
2006-04-09 02:15:35 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_FLUSH");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_FLUSH, 0);
|
2006-04-09 02:15:35 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
void audio_error_clear(void)
|
2006-04-09 02:15:35 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
|
|
|
pcm_rec_error_clear();
|
|
|
|
#endif
|
2006-04-09 02:15:35 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
int audio_status(void)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
int ret = 0;
|
2006-04-06 04:07:06 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (playing)
|
|
|
|
ret |= AUDIO_STATUS_PLAY;
|
|
|
|
|
|
|
|
if (paused)
|
|
|
|
ret |= AUDIO_STATUS_PAUSE;
|
|
|
|
|
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
/* Do this here for constitency with mpeg.c version */
|
|
|
|
ret |= pcm_rec_status();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2006-03-24 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
int audio_get_file_pos(void)
|
|
|
|
{
|
2006-04-22 14:40:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_set_buffer_margin(int setting)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
static const int lookup[] = {5, 15, 30, 60, 120, 180, 300, 600};
|
|
|
|
buffer_margin = lookup[setting];
|
|
|
|
logf("buffer margin: %ds", buffer_margin);
|
|
|
|
set_filebuf_watermark(buffer_margin);
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Set crossfade & PCM buffer length. */
|
|
|
|
void audio_set_crossfade(int enable)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
bool was_playing = (playing && audio_is_initialized);
|
|
|
|
size_t offset = 0;
|
|
|
|
#if MEM > 1
|
|
|
|
int seconds = 1;
|
|
|
|
#endif
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!filebuf)
|
|
|
|
return; /* Audio buffers not yet set up */
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
#if MEM > 1
|
|
|
|
if (enable)
|
|
|
|
seconds = global_settings.crossfade_fade_out_delay
|
|
|
|
+ global_settings.crossfade_fade_out_duration;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Buffer has to be at least 2s long. */
|
|
|
|
seconds += 2;
|
|
|
|
logf("buf len: %d", seconds);
|
|
|
|
size = seconds * (NATIVE_FREQUENCY*4);
|
|
|
|
#else
|
|
|
|
enable = 0;
|
|
|
|
size = NATIVE_FREQUENCY*2;
|
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
if (buffer_state == BUFFER_STATE_NORMAL && pcmbuf_is_same_size(size))
|
2006-09-01 07:59:31 +00:00
|
|
|
return ;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (was_playing)
|
|
|
|
{
|
|
|
|
/* Store the track resume position */
|
2006-10-12 15:54:36 +00:00
|
|
|
offset = CUR_TI->id3.offset;
|
2006-09-28 09:20:56 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Playback has to be stopped before changing the buffer size. */
|
2006-09-28 09:20:56 +00:00
|
|
|
gui_syncsplash(0, true, (char *)str(LANG_RESTARTING_PLAYBACK));
|
2006-11-01 19:19:36 +00:00
|
|
|
audio_stop();
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-28 15:17:59 +00:00
|
|
|
voice_stop();
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Re-initialize audio system. */
|
2006-11-06 18:07:30 +00:00
|
|
|
audio_reset_buffer(size);
|
2006-09-01 07:59:31 +00:00
|
|
|
pcmbuf_crossfade_enable(enable);
|
|
|
|
logf("abuf:%dB", pcmbuf_get_bufsize());
|
|
|
|
logf("fbuf:%dB", filebuflen);
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
voice_init();
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Restart playback. */
|
2006-09-28 09:20:56 +00:00
|
|
|
if (was_playing)
|
2006-11-01 19:19:36 +00:00
|
|
|
audio_play(offset);
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_preinit(void)
|
|
|
|
{
|
|
|
|
logf("playback system pre-init");
|
2006-08-31 10:01:07 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
filling = false;
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
playing = false;
|
|
|
|
paused = false;
|
|
|
|
audio_codec_loaded = false;
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
voice_is_playing = false;
|
2006-09-17 08:34:42 +00:00
|
|
|
voice_thread_start = false;
|
|
|
|
voice_codec_loaded = false;
|
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
track_changed = false;
|
|
|
|
current_fd = -1;
|
|
|
|
track_buffer_callback = NULL;
|
|
|
|
track_unbuffer_callback = NULL;
|
|
|
|
track_changed_callback = NULL;
|
2006-10-26 12:13:28 +00:00
|
|
|
track_ridx = 0; /* Just to prevent CUR_TI from being anything random. */
|
|
|
|
prev_ti = &tracks[MAX_TRACK-1]; /* And prevent prev_ti being random too */
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-11-01 19:19:36 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
mutex_init(&mutex_codecthread);
|
2006-11-01 19:19:36 +00:00
|
|
|
#endif
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
queue_init(&audio_queue, true);
|
|
|
|
queue_init(&codec_queue, true);
|
|
|
|
/* create a private queue */
|
|
|
|
queue_init(&codec_callback_queue, false);
|
2006-08-31 10:01:07 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
create_thread(audio_thread, audio_stack, sizeof(audio_stack),
|
2006-09-16 16:18:11 +00:00
|
|
|
audio_thread_name IF_PRIO(, PRIORITY_BUFFERING));
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2006-08-31 10:01:07 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_init(void)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("audio > audio Q_AUDIO_POSTINIT");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_POSTINIT, 0);
|
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void voice_init(void)
|
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!filebuf)
|
|
|
|
return; /* Audio buffers not yet set up */
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
if (voice_thread_p)
|
2006-09-28 15:17:59 +00:00
|
|
|
return;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!talk_voice_required())
|
|
|
|
return;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("Starting voice codec");
|
2006-09-16 16:18:11 +00:00
|
|
|
queue_init(&voice_queue, true);
|
|
|
|
voice_thread_p = create_thread(voice_thread, voice_stack,
|
|
|
|
sizeof(voice_stack), voice_thread_name
|
|
|
|
IF_PRIO(, PRIORITY_PLAYBACK));
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
while (!voice_codec_loaded)
|
|
|
|
yield();
|
2006-09-17 08:34:42 +00:00
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
} /* voice_init */
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void voice_stop(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Messages should not be posted to voice codec queue unless it is the
|
2006-11-06 18:07:30 +00:00
|
|
|
current codec or deadlocks happen. */
|
2006-09-01 07:59:31 +00:00
|
|
|
if (current_codec != CODEC_IDX_VOICE)
|
|
|
|
return;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-26 07:19:43 +00:00
|
|
|
LOGFQUEUE("mp3 > voice Q_VOICE_STOP");
|
|
|
|
queue_post(&voice_queue, Q_VOICE_STOP, 0);
|
2006-11-13 09:07:18 +00:00
|
|
|
while (voice_is_playing || !queue_empty(&voice_queue))
|
2006-09-26 07:19:43 +00:00
|
|
|
yield();
|
|
|
|
if (!playing)
|
|
|
|
pcmbuf_play_stop();
|
2006-09-17 08:34:42 +00:00
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
} /* voice_stop */
|
2006-03-30 05:56:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* --- Routines called from multiple threads --- */
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
static void swap_codec(void)
|
2006-04-13 14:17:12 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
int my_codec = current_codec;
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("swapping out codec:%d", my_codec);
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Save our current IRAM and DRAM */
|
2006-11-18 02:18:29 +00:00
|
|
|
#ifdef IRAM_STEAL
|
|
|
|
if (voice_iram_stolen)
|
|
|
|
{
|
|
|
|
logf("swap: iram restore");
|
|
|
|
voice_iram_stolen = false;
|
|
|
|
/* Don't swap trashed data into buffer - _should_ always be the case
|
|
|
|
if voice_iram_stolen is true since the voice has been swapped in
|
|
|
|
before hand */
|
|
|
|
if (my_codec == CODEC_IDX_VOICE)
|
|
|
|
goto skip_iram_swap;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
memcpy(iram_buf[my_codec], (unsigned char *)CODEC_IRAM_ORIGIN,
|
|
|
|
CODEC_IRAM_SIZE);
|
2006-11-18 02:18:29 +00:00
|
|
|
|
|
|
|
#ifdef IRAM_STEAL
|
|
|
|
skip_iram_swap:
|
|
|
|
#endif
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
memcpy(dram_buf[my_codec], codecbuf, CODEC_SIZE);
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
/* Release my semaphore */
|
|
|
|
mutex_unlock(&mutex_codecthread);
|
|
|
|
|
|
|
|
/* Loop until the other codec has locked and run */
|
2006-09-01 07:59:31 +00:00
|
|
|
do {
|
|
|
|
/* Release my semaphore and force a task switch. */
|
|
|
|
yield();
|
|
|
|
} while (my_codec == current_codec);
|
2006-09-17 08:34:42 +00:00
|
|
|
|
|
|
|
/* Wait for other codec to unlock */
|
|
|
|
mutex_lock(&mutex_codecthread);
|
|
|
|
|
|
|
|
/* Take control */
|
2006-09-01 07:59:31 +00:00
|
|
|
current_codec = my_codec;
|
|
|
|
|
|
|
|
/* Reload our IRAM and DRAM */
|
|
|
|
memcpy((unsigned char *)CODEC_IRAM_ORIGIN, iram_buf[my_codec],
|
|
|
|
CODEC_IRAM_SIZE);
|
|
|
|
invalidate_icache();
|
|
|
|
memcpy(codecbuf, dram_buf[my_codec], CODEC_SIZE);
|
|
|
|
|
|
|
|
logf("resuming codec:%d", my_codec);
|
2006-04-13 14:17:12 +00:00
|
|
|
}
|
2006-09-17 08:34:42 +00:00
|
|
|
#endif
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void set_filebuf_watermark(int seconds)
|
2006-04-11 03:55:58 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
size_t bytes;
|
2006-04-14 17:31:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (current_codec == CODEC_IDX_VOICE)
|
|
|
|
return;
|
2006-04-17 17:05:05 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!filebuf)
|
|
|
|
return; /* Audio buffers not yet set up */
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
bytes = MAX(CUR_TI->id3.bitrate * seconds * (1000/8), conf_watermark);
|
2006-09-01 07:59:31 +00:00
|
|
|
bytes = MIN(bytes, filebuflen / 2);
|
|
|
|
conf_watermark = bytes;
|
2006-04-11 03:55:58 +00:00
|
|
|
}
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
static const char * get_codec_filename(int cod_spec)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
const char *fname;
|
2006-11-06 18:07:30 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
/* Can choose decoder or encoder if one available */
|
|
|
|
int type = cod_spec & CODEC_TYPE_MASK;
|
|
|
|
int afmt = cod_spec & CODEC_AFMT_MASK;
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if ((unsigned)afmt >= AFMT_NUM_CODECS)
|
|
|
|
type = AFMT_UNKNOWN | (type & CODEC_TYPE_MASK);
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
fname = (type == CODEC_TYPE_ENCODER) ?
|
|
|
|
audio_formats[afmt].codec_enc_root_fn :
|
|
|
|
audio_formats[afmt].codec_root_fn;
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("%s: %d - %s",
|
|
|
|
(type == CODEC_TYPE_ENCODER) ? "Encoder" : "Decoder",
|
|
|
|
afmt, fname ? fname : "<unknown>");
|
2006-11-06 18:07:30 +00:00
|
|
|
#else /* !HAVE_RECORDING */
|
|
|
|
/* Always decoder */
|
|
|
|
if ((unsigned)cod_spec >= AFMT_NUM_CODECS)
|
|
|
|
cod_spec = AFMT_UNKNOWN;
|
|
|
|
fname = audio_formats[cod_spec].codec_root_fn;
|
|
|
|
logf("Codec: %d - %s", cod_spec, fname ? fname : "<unknown>");
|
|
|
|
#endif /* HAVE_RECORDING */
|
2006-04-13 17:30:54 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return fname;
|
|
|
|
} /* get_codec_filename */
|
2006-04-11 03:55:58 +00:00
|
|
|
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* --- Voice thread --- */
|
2006-04-11 03:55:58 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool voice_pcmbuf_insert_split_callback(
|
|
|
|
const void *ch1, const void *ch2, size_t length)
|
2006-04-11 03:55:58 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
const char* src[2];
|
|
|
|
char *dest;
|
|
|
|
long input_size;
|
|
|
|
size_t output_size;
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
src[0] = ch1;
|
|
|
|
src[1] = ch2;
|
|
|
|
|
|
|
|
if (dsp_stereo_mode() == STEREO_NONINTERLEAVED)
|
|
|
|
length *= 2; /* Length is per channel */
|
|
|
|
|
|
|
|
while (length)
|
2006-04-18 18:33:09 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
long est_output_size = dsp_output_size(length);
|
|
|
|
|
|
|
|
while ((dest = pcmbuf_request_voice_buffer(est_output_size,
|
|
|
|
&output_size, playing)) == NULL)
|
2006-04-18 18:33:09 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
if (playing && audio_codec_loaded)
|
2006-09-01 07:59:31 +00:00
|
|
|
swap_codec();
|
|
|
|
else
|
|
|
|
yield();
|
2006-04-18 18:33:09 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* Get the real input_size for output_size bytes, guarding
|
|
|
|
* against resampling buffer overflows. */
|
|
|
|
input_size = dsp_input_size(output_size);
|
|
|
|
|
|
|
|
if (input_size <= 0)
|
2006-04-18 18:33:09 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
DEBUGF("Error: dsp_input_size(%ld=dsp_output_size(%ld))=%ld<=0\n",
|
|
|
|
output_size, length, input_size);
|
|
|
|
/* If this happens, there are samples of codec data that don't
|
|
|
|
* become a number of pcm samples, and something is broken */
|
|
|
|
return false;
|
2006-04-18 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Input size has grown, no error, just don't write more than length */
|
|
|
|
if ((size_t)input_size > length)
|
|
|
|
input_size = length;
|
2006-04-26 04:01:35 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
output_size = dsp_process(dest, src, input_size);
|
|
|
|
|
|
|
|
if (playing)
|
2006-04-14 14:19:56 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
pcmbuf_mix_voice(output_size);
|
2006-09-17 08:34:42 +00:00
|
|
|
if ((pcmbuf_usage() < 10 || pcmbuf_mix_free() < 30) && audio_codec_loaded)
|
2006-09-01 07:59:31 +00:00
|
|
|
swap_codec();
|
2006-04-14 14:19:56 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
else
|
|
|
|
pcmbuf_write_complete(output_size);
|
|
|
|
|
|
|
|
length -= input_size;
|
2006-04-13 14:17:12 +00:00
|
|
|
}
|
2006-06-07 20:43:34 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return true;
|
|
|
|
} /* voice_pcmbuf_insert_split_callback */
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool voice_pcmbuf_insert_callback(const char *buf, size_t length)
|
|
|
|
{
|
|
|
|
/* TODO: The audiobuffer API should probably be updated, and be based on
|
|
|
|
* pcmbuf_insert_split(). */
|
|
|
|
long real_length = length;
|
2006-04-26 04:01:35 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (dsp_stereo_mode() == STEREO_NONINTERLEAVED)
|
|
|
|
length /= 2; /* Length is per channel */
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Second channel is only used for non-interleaved stereo. */
|
|
|
|
return voice_pcmbuf_insert_split_callback(buf, buf + (real_length / 2),
|
|
|
|
length);
|
|
|
|
}
|
2006-04-11 03:55:58 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void* voice_get_memory_callback(size_t *size)
|
|
|
|
{
|
|
|
|
*size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-06-03 17:23:20 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void voice_set_elapsed_callback(unsigned int value)
|
|
|
|
{
|
|
|
|
(void)value;
|
|
|
|
}
|
2006-04-11 03:55:58 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void voice_set_offset_callback(size_t value)
|
|
|
|
{
|
|
|
|
(void)value;
|
|
|
|
}
|
2006-04-18 18:33:09 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static size_t voice_filebuf_callback(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
(void)ptr;
|
|
|
|
(void)size;
|
2006-04-26 04:01:35 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-04-18 18:33:09 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void* voice_request_buffer_callback(size_t *realsize, size_t reqsize)
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
if (ci_voice.new_track)
|
2006-04-13 14:17:12 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
2006-04-13 14:17:12 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
while (1)
|
2006-04-11 03:55:58 +00:00
|
|
|
{
|
2006-09-26 07:19:43 +00:00
|
|
|
if (voice_is_playing || playing)
|
2006-09-01 07:59:31 +00:00
|
|
|
queue_wait_w_tmo(&voice_queue, &ev, 0);
|
2006-09-26 07:19:43 +00:00
|
|
|
else
|
|
|
|
queue_wait(&voice_queue, &ev);
|
|
|
|
if (!voice_is_playing)
|
2006-04-13 14:17:12 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
if (ev.id == SYS_TIMEOUT)
|
|
|
|
ev.id = Q_AUDIO_PLAY;
|
2006-04-13 14:17:12 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
switch (ev.id) {
|
|
|
|
case Q_AUDIO_PLAY:
|
|
|
|
LOGFQUEUE("voice < Q_AUDIO_PLAY");
|
|
|
|
if (playing)
|
2006-09-26 07:19:43 +00:00
|
|
|
{
|
|
|
|
if (audio_codec_loaded)
|
|
|
|
swap_codec();
|
|
|
|
yield();
|
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
break;
|
2006-04-11 03:55:58 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_ENCODER_RECORD:
|
|
|
|
LOGFQUEUE("voice < Q_ENCODER_RECORD");
|
2006-09-17 18:52:31 +00:00
|
|
|
swap_codec();
|
2006-09-01 07:59:31 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_VOICE_STOP:
|
|
|
|
LOGFQUEUE("voice < Q_VOICE_STOP");
|
2006-10-25 06:19:27 +00:00
|
|
|
if (ev.data == (void *)1 && !playing && pcm_is_playing())
|
2006-11-01 19:19:36 +00:00
|
|
|
{
|
2006-10-25 06:19:27 +00:00
|
|
|
/* Aborting: Slight hack - flush PCM buffer if
|
|
|
|
only being used for voice */
|
|
|
|
pcmbuf_play_stop();
|
2006-11-01 19:19:36 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
if (voice_is_playing)
|
|
|
|
{
|
|
|
|
/* Clear the current buffer */
|
|
|
|
voice_is_playing = false;
|
|
|
|
voice_getmore = NULL;
|
|
|
|
voice_remaining = 0;
|
|
|
|
voicebuf = NULL;
|
2006-09-28 16:45:23 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Force the codec to think it's changing tracks */
|
2006-09-28 16:45:23 +00:00
|
|
|
ci_voice.new_track = 1;
|
2006-09-01 07:59:31 +00:00
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2006-04-09 15:30:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
LOGFQUEUE("voice < SYS_USB_CONNECTED");
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
if (audio_codec_loaded)
|
|
|
|
swap_codec();
|
|
|
|
usb_wait_for_disconnect(&voice_queue);
|
|
|
|
break;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_VOICE_PLAY:
|
|
|
|
LOGFQUEUE("voice < Q_VOICE_PLAY");
|
2006-09-17 08:34:42 +00:00
|
|
|
if (!voice_is_playing)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-09-28 16:45:23 +00:00
|
|
|
/* Set up new voice data */
|
2006-09-01 07:59:31 +00:00
|
|
|
struct voice_info *voice_data;
|
2006-11-18 02:18:29 +00:00
|
|
|
#ifdef IRAM_STEAL
|
|
|
|
if (voice_iram_stolen)
|
|
|
|
{
|
|
|
|
logf("voice: iram restore");
|
|
|
|
memcpy((void*)CODEC_IRAM_ORIGIN,
|
|
|
|
iram_buf[CODEC_IDX_VOICE],
|
|
|
|
CODEC_IRAM_SIZE);
|
|
|
|
voice_iram_stolen = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* must reset the buffer before any playback
|
|
|
|
begins if needed */
|
|
|
|
if (buffer_state == BUFFER_STATE_TRASHED)
|
|
|
|
audio_reset_buffer(pcmbuf_get_bufsize());
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
voice_is_playing = true;
|
2006-10-15 11:57:52 +00:00
|
|
|
trigger_cpu_boost();
|
2006-09-01 07:59:31 +00:00
|
|
|
voice_data = ev.data;
|
|
|
|
voice_remaining = voice_data->size;
|
|
|
|
voicebuf = voice_data->buf;
|
|
|
|
voice_getmore = voice_data->callback;
|
|
|
|
}
|
2006-09-17 08:34:42 +00:00
|
|
|
goto voice_play_clip;
|
2006-04-09 15:30:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case SYS_TIMEOUT:
|
2006-11-06 18:07:30 +00:00
|
|
|
LOGFQUEUE_SYS_TIMEOUT("voice < SYS_TIMEOUT");
|
2006-09-01 07:59:31 +00:00
|
|
|
goto voice_play_clip;
|
2006-04-09 15:30:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
default:
|
|
|
|
LOGFQUEUE("voice < default");
|
|
|
|
}
|
|
|
|
}
|
2006-04-09 15:30:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
voice_play_clip:
|
2006-04-11 17:08:11 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (voice_remaining == 0 || voicebuf == NULL)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
if (voice_getmore)
|
|
|
|
voice_getmore((unsigned char **)&voicebuf, (int *)&voice_remaining);
|
2006-04-09 15:30:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If this clip is done */
|
2006-09-17 08:34:42 +00:00
|
|
|
if (voice_remaining == 0)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
LOGFQUEUE("voice > voice Q_VOICE_STOP");
|
|
|
|
queue_post(&voice_queue, Q_VOICE_STOP, 0);
|
|
|
|
/* Force pcm playback. */
|
|
|
|
if (!pcm_is_playing())
|
|
|
|
pcmbuf_play_start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*realsize = MIN(voice_remaining, reqsize);
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (*realsize == 0)
|
|
|
|
return NULL;
|
2006-04-09 15:30:28 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return voicebuf;
|
|
|
|
} /* voice_request_buffer_callback */
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
static void voice_advance_buffer_callback(size_t amount)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-04-22 14:40:13 +00:00
|
|
|
amount = MIN(amount, voice_remaining);
|
|
|
|
voicebuf += amount;
|
|
|
|
voice_remaining -= amount;
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void voice_advance_buffer_loc_callback(void *ptr)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
size_t amount = (size_t)ptr - (size_t)voicebuf;
|
|
|
|
|
|
|
|
voice_advance_buffer_callback(amount);
|
|
|
|
}
|
2005-12-01 19:38:08 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static off_t voice_mp3_get_filepos_callback(int newtime)
|
|
|
|
{
|
|
|
|
(void)newtime;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void voice_do_nothing(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
return;
|
2006-04-22 14:40:13 +00:00
|
|
|
}
|
2006-04-17 17:05:05 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool voice_seek_buffer_callback(size_t newpos)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
(void)newpos;
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return false;
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool voice_request_next_track_callback(void)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
ci_voice.new_track = 0;
|
|
|
|
return true;
|
2006-04-22 14:40:13 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void voice_thread(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
logf("Loading voice codec");
|
|
|
|
voice_codec_loaded = true;
|
|
|
|
mutex_lock(&mutex_codecthread);
|
|
|
|
current_codec = CODEC_IDX_VOICE;
|
|
|
|
dsp_configure(DSP_RESET, 0);
|
|
|
|
voice_remaining = 0;
|
|
|
|
voice_getmore = NULL;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
codec_load_file(get_codec_filename(AFMT_MPA_L3), &ci_voice);
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("Voice codec finished");
|
|
|
|
voice_codec_loaded = false;
|
|
|
|
mutex_unlock(&mutex_codecthread);
|
2006-04-08 12:58:39 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
} /* voice_thread */
|
2005-08-21 18:12:31 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
#endif /* PLAYBACK_VOICE */
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* --- Codec thread --- */
|
|
|
|
|
|
|
|
static bool codec_pcmbuf_insert_split_callback(
|
|
|
|
const void *ch1, const void *ch2, size_t length)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
const char* src[2];
|
|
|
|
char *dest;
|
|
|
|
long input_size;
|
|
|
|
size_t output_size;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
src[0] = ch1;
|
|
|
|
src[1] = ch2;
|
2006-08-31 10:01:07 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (dsp_stereo_mode() == STEREO_NONINTERLEAVED)
|
|
|
|
length *= 2; /* Length is per channel */
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
while (length)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
long est_output_size = dsp_output_size(length);
|
|
|
|
/* Prevent audio from a previous track from playing */
|
|
|
|
if (ci.new_track || ci.stop_codec)
|
|
|
|
return true;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
while ((dest = pcmbuf_request_buffer(est_output_size,
|
|
|
|
&output_size)) == NULL)
|
2006-04-14 14:03:43 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
sleep(1);
|
|
|
|
if (ci.seek_time || ci.new_track || ci.stop_codec)
|
2006-04-14 14:03:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Get the real input_size for output_size bytes, guarding
|
|
|
|
* against resampling buffer overflows. */
|
|
|
|
input_size = dsp_input_size(output_size);
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (input_size <= 0)
|
|
|
|
{
|
|
|
|
DEBUGF("Error: dsp_input_size(%ld=dsp_output_size(%ld))=%ld<=0\n",
|
|
|
|
output_size, length, input_size);
|
|
|
|
/* If this happens, there are samples of codec data that don't
|
|
|
|
* become a number of pcm samples, and something is broken */
|
|
|
|
return false;
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Input size has grown, no error, just don't write more than length */
|
|
|
|
if ((size_t)input_size > length)
|
|
|
|
input_size = length;
|
2005-07-21 11:44:00 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
output_size = dsp_process(dest, src, input_size);
|
2005-10-16 08:01:02 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
pcmbuf_write_complete(output_size);
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
if ((voice_is_playing || voice_thread_start)
|
|
|
|
&& pcm_is_playing() && voice_codec_loaded &&
|
|
|
|
pcmbuf_usage() > 30 && pcmbuf_mix_free() > 80)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
voice_thread_start = false;
|
2006-09-01 07:59:31 +00:00
|
|
|
swap_codec();
|
|
|
|
}
|
2006-09-17 08:34:42 +00:00
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
length -= input_size;
|
|
|
|
}
|
2005-07-21 11:44:00 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return true;
|
|
|
|
} /* codec_pcmbuf_insert_split_callback */
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool codec_pcmbuf_insert_callback(const char *buf, size_t length)
|
|
|
|
{
|
|
|
|
/* TODO: The audiobuffer API should probably be updated, and be based on
|
|
|
|
* pcmbuf_insert_split(). */
|
|
|
|
long real_length = length;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (dsp_stereo_mode() == STEREO_NONINTERLEAVED)
|
|
|
|
length /= 2; /* Length is per channel */
|
2006-08-30 15:47:18 +00:00
|
|
|
|
2006-09-01 07:59:31 +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-10 15:02:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void* codec_get_memory_callback(size_t *size)
|
2005-07-02 16:52:30 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
*size = MALLOC_BUFSIZE;
|
|
|
|
return &audiobuf[talk_get_bufsize()];
|
2005-07-02 16:52:30 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_pcmbuf_position_callback(size_t size) ICODE_ATTR;
|
|
|
|
static void codec_pcmbuf_position_callback(size_t size)
|
2005-07-02 16:52:30 +00:00
|
|
|
{
|
2006-10-26 12:13:28 +00:00
|
|
|
/* This is called from an ISR, so be quick */
|
2006-09-01 07:59:31 +00:00
|
|
|
unsigned int time = size * 1000 / 4 / NATIVE_FREQUENCY +
|
|
|
|
prev_ti->id3.elapsed;
|
|
|
|
|
|
|
|
if (time >= prev_ti->id3.length)
|
|
|
|
{
|
|
|
|
pcmbuf_set_position_callback(NULL);
|
|
|
|
prev_ti->id3.elapsed = prev_ti->id3.length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prev_ti->id3.elapsed = time;
|
2005-07-02 16:52:30 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_set_elapsed_callback(unsigned int value)
|
2005-06-29 21:36:30 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
unsigned int latency;
|
|
|
|
if (ci.seek_time)
|
|
|
|
return;
|
2005-06-29 21:36:30 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
#ifdef AB_REPEAT_ENABLE
|
|
|
|
ab_position_report(value);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
latency = pcmbuf_get_latency();
|
|
|
|
if (value < latency)
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->id3.elapsed = 0;
|
|
|
|
else if (value - latency > CUR_TI->id3.elapsed ||
|
|
|
|
value - latency < CUR_TI->id3.elapsed - 2)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->id3.elapsed = value - latency;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2005-06-15 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_set_offset_callback(size_t value)
|
2006-02-07 20:38:55 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
unsigned int latency;
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (ci.seek_time)
|
|
|
|
return;
|
2006-04-06 21:06:37 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
latency = pcmbuf_get_latency() * CUR_TI->id3.bitrate / 8;
|
2006-09-01 07:59:31 +00:00
|
|
|
if (value < latency)
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->id3.offset = 0;
|
2006-09-01 07:59:31 +00:00
|
|
|
else
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->id3.offset = value - latency;
|
2005-06-07 06:34:54 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_advance_buffer_counters(size_t amount)
|
2005-07-07 09:53:02 +00:00
|
|
|
{
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_ridx = RINGBUF_ADD(buf_ridx, amount);
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.curpos += amount;
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->available -= amount;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Start buffer filling as necessary. */
|
|
|
|
if (!pcmbuf_is_lowdata() && !filling)
|
2006-04-14 04:42:11 +00:00
|
|
|
{
|
2006-10-27 15:42:34 +00:00
|
|
|
if (FILEBUFUSED < conf_watermark && playing && !playlist_end)
|
2005-07-07 09:53:02 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_FILL_BUFFER");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_FILL_BUFFER, 0);
|
2005-07-07 09:53:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* copy up-to size bytes into ptr and return the actual size copied */
|
|
|
|
static size_t codec_filebuf_callback(void *ptr, size_t size)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
char *buf = (char *)ptr;
|
2006-03-24 13:43:15 +00:00
|
|
|
size_t copy_n;
|
2006-09-01 07:59:31 +00:00
|
|
|
size_t part_n;
|
2006-08-30 15:47:18 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (ci.stop_codec || !playing)
|
|
|
|
return 0;
|
2006-04-06 16:21:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* The ammount to copy is the lesser of the requested amount and the
|
|
|
|
* amount left of the current track (both on disk and already loaded) */
|
2006-10-12 15:54:36 +00:00
|
|
|
copy_n = MIN(size, CUR_TI->available + CUR_TI->filerem);
|
2006-04-14 17:31:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Nothing requested OR nothing left */
|
|
|
|
if (copy_n == 0)
|
|
|
|
return 0;
|
2006-04-06 16:21:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Let the disk buffer catch fill until enough data is available */
|
2006-10-12 15:54:36 +00:00
|
|
|
while (copy_n > CUR_TI->available)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
if (!filling)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_FILL_BUFFER");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_FILL_BUFFER, 0);
|
2006-08-30 15:47:18 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
sleep(1);
|
|
|
|
if (ci.stop_codec || ci.new_track)
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-07 21:32:30 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Copy as much as possible without wrapping */
|
|
|
|
part_n = MIN(copy_n, filebuflen - buf_ridx);
|
|
|
|
memcpy(buf, &filebuf[buf_ridx], part_n);
|
|
|
|
/* Copy the rest in the case of a wrap */
|
|
|
|
if (part_n < copy_n) {
|
|
|
|
memcpy(&buf[part_n], &filebuf[0], copy_n - part_n);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2006-01-18 14:20:34 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Update read and other position pointers */
|
|
|
|
codec_advance_buffer_counters(copy_n);
|
2006-04-13 20:42:11 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Return the actual amount of data copied to the buffer */
|
|
|
|
return copy_n;
|
|
|
|
} /* codec_filebuf_callback */
|
2006-04-11 20:41:04 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void* codec_request_buffer_callback(size_t *realsize, size_t reqsize)
|
|
|
|
{
|
|
|
|
size_t short_n, copy_n, buf_rem;
|
|
|
|
|
|
|
|
if (!playing)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
2006-04-11 23:08:21 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
copy_n = MIN(reqsize, CUR_TI->available + CUR_TI->filerem);
|
2006-09-01 07:59:31 +00:00
|
|
|
if (copy_n == 0)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
2006-04-11 12:29:46 +00:00
|
|
|
}
|
2006-07-31 06:12:53 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
while (copy_n > CUR_TI->available)
|
2006-04-13 17:30:54 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!filling)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_FILL_BUFFER");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_FILL_BUFFER, 0);
|
|
|
|
}
|
2006-07-31 06:12:53 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
sleep(1);
|
|
|
|
if (ci.stop_codec || ci.new_track)
|
|
|
|
{
|
|
|
|
*realsize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* How much is left at the end of the file buffer before wrap? */
|
|
|
|
buf_rem = filebuflen - buf_ridx;
|
|
|
|
|
|
|
|
/* If we can't satisfy the request without wrapping */
|
|
|
|
if (buf_rem < copy_n)
|
|
|
|
{
|
|
|
|
/* How short are we? */
|
|
|
|
short_n = copy_n - buf_rem;
|
2006-07-31 06:12:53 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If we can fudge it with the guardbuf */
|
|
|
|
if (short_n < GUARD_BUFSIZE)
|
|
|
|
memcpy(&filebuf[filebuflen], &filebuf[0], short_n);
|
|
|
|
else
|
|
|
|
copy_n = buf_rem;
|
2006-04-13 17:30:54 +00:00
|
|
|
}
|
2005-12-01 18:44:11 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
*realsize = copy_n;
|
|
|
|
|
|
|
|
return (char *)&filebuf[buf_ridx];
|
|
|
|
} /* codec_request_buffer_callback */
|
|
|
|
|
|
|
|
static int get_codec_base_type(int type)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
switch (type) {
|
|
|
|
case AFMT_MPA_L1:
|
|
|
|
case AFMT_MPA_L2:
|
|
|
|
case AFMT_MPA_L3:
|
|
|
|
return AFMT_MPA_L3;
|
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return type;
|
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_advance_buffer_callback(size_t amount)
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
if (amount > CUR_TI->available + CUR_TI->filerem)
|
|
|
|
amount = CUR_TI->available + CUR_TI->filerem;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
while (amount > CUR_TI->available && filling)
|
2006-09-01 07:59:31 +00:00
|
|
|
sleep(1);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
if (amount > CUR_TI->available)
|
2006-04-06 16:21:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_REBUFFER_SEEK");
|
|
|
|
queue_post(&audio_queue,
|
|
|
|
Q_AUDIO_REBUFFER_SEEK, (void *)(ci.curpos + amount));
|
|
|
|
|
|
|
|
queue_wait(&codec_callback_queue, &ev);
|
|
|
|
switch (ev.id)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_CODEC_REQUEST_FAILED:
|
|
|
|
LOGFQUEUE("codec < Q_CODEC_REQUEST_FAILED");
|
|
|
|
ci.stop_codec = true;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Q_CODEC_REQUEST_COMPLETE:
|
|
|
|
LOGFQUEUE("codec < Q_CODEC_REQUEST_COMPLETE");
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOGFQUEUE("codec < default");
|
|
|
|
ci.stop_codec = true;
|
|
|
|
return;
|
2006-04-06 16:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
codec_advance_buffer_counters(amount);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
codec_set_offset_callback(ci.curpos);
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_advance_buffer_loc_callback(void *ptr)
|
|
|
|
{
|
|
|
|
size_t amount = (size_t)ptr - (size_t)&filebuf[buf_ridx];
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
codec_advance_buffer_callback(amount);
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Copied from mpeg.c. Should be moved somewhere else. */
|
|
|
|
static int codec_get_file_pos(void)
|
|
|
|
{
|
|
|
|
int pos = -1;
|
|
|
|
struct mp3entry *id3 = audio_current_track();
|
|
|
|
|
|
|
|
if (id3->vbr)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
if (id3->has_toc)
|
|
|
|
{
|
|
|
|
/* Use the TOC to find the new position */
|
|
|
|
unsigned int percent, remainder;
|
|
|
|
int curtoc, nexttoc, plen;
|
2006-04-11 03:55:58 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
percent = (id3->elapsed*100)/id3->length;
|
|
|
|
if (percent > 99)
|
|
|
|
percent = 99;
|
2006-04-07 21:32:30 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
curtoc = id3->toc[percent];
|
2006-04-06 16:21:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (percent < 99)
|
|
|
|
nexttoc = id3->toc[percent+1];
|
|
|
|
else
|
|
|
|
nexttoc = 256;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
pos = (id3->filesize/256)*curtoc;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (id3->bitrate)
|
|
|
|
pos = id3->elapsed * (id3->bitrate / 8);
|
|
|
|
else
|
|
|
|
return -1;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-10-30 20:19:21 +00:00
|
|
|
pos += id3->first_frame_offset;
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Don't seek right to the end of the file so that we can
|
|
|
|
transition properly to the next song */
|
|
|
|
if (pos >= (int)(id3->filesize - id3->id3v1len))
|
|
|
|
pos = id3->filesize - id3->id3v1len - 1;
|
2005-07-03 18:03:20 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return pos;
|
|
|
|
}
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static off_t codec_mp3_get_filepos_callback(int newtime)
|
|
|
|
{
|
|
|
|
off_t newpos;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->id3.elapsed = newtime;
|
2006-09-01 07:59:31 +00:00
|
|
|
newpos = codec_get_file_pos();
|
2005-07-03 18:03:20 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return newpos;
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_seek_complete_callback(void)
|
|
|
|
{
|
|
|
|
logf("seek_complete");
|
|
|
|
if (pcm_is_paused())
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If this is not a seamless seek, clear the buffer */
|
|
|
|
pcmbuf_play_stop();
|
2006-11-26 12:02:47 +00:00
|
|
|
dsp_configure(DSP_FLUSH, NULL);
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If playback was not 'deliberately' paused, unpause now */
|
|
|
|
if (!paused)
|
|
|
|
pcmbuf_pause(false);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.seek_time = 0;
|
2005-07-03 18:03:20 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool codec_seek_buffer_callback(size_t newpos)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
int difference;
|
2005-06-29 14:46:27 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("codec_seek_buffer_callback");
|
2005-07-05 13:34:52 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
if (newpos >= CUR_TI->filesize)
|
|
|
|
newpos = CUR_TI->filesize - 1;
|
2005-07-05 13:34:52 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
difference = newpos - ci.curpos;
|
|
|
|
if (difference >= 0)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Seeking forward */
|
|
|
|
logf("seek: +%d", difference);
|
|
|
|
codec_advance_buffer_callback(difference);
|
|
|
|
return true;
|
2005-07-30 17:00:06 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Seeking backward */
|
|
|
|
difference = -difference;
|
|
|
|
if (ci.curpos - difference < 0)
|
|
|
|
difference = ci.curpos;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* We need to reload the song. */
|
2006-10-12 15:54:36 +00:00
|
|
|
if (newpos < CUR_TI->start_pos)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
struct event ev;
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_REBUFFER_SEEK");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_REBUFFER_SEEK, (void *)newpos);
|
|
|
|
|
|
|
|
queue_wait(&codec_callback_queue, &ev);
|
|
|
|
switch (ev.id)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_CODEC_REQUEST_COMPLETE:
|
|
|
|
LOGFQUEUE("codec < Q_CODEC_REQUEST_COMPLETE");
|
|
|
|
return true;
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_CODEC_REQUEST_FAILED:
|
|
|
|
LOGFQUEUE("codec < Q_CODEC_REQUEST_FAILED");
|
|
|
|
ci.stop_codec = true;
|
|
|
|
return false;
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
default:
|
|
|
|
LOGFQUEUE("codec < default");
|
|
|
|
return false;
|
2005-12-01 18:44:11 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Seeking inside buffer space. */
|
|
|
|
logf("seek: -%d", difference);
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->available += difference;
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_ridx = RINGBUF_SUB(buf_ridx, (unsigned)difference);
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.curpos -= difference;
|
2005-12-01 18:44:11 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2005-06-14 22:27:57 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_configure_callback(int setting, void *value)
|
|
|
|
{
|
|
|
|
switch (setting) {
|
|
|
|
case CODEC_SET_FILEBUF_WATERMARK:
|
|
|
|
conf_watermark = (unsigned long)value;
|
|
|
|
set_filebuf_watermark(buffer_margin);
|
|
|
|
break;
|
2005-07-07 07:15:05 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case CODEC_SET_FILEBUF_CHUNKSIZE:
|
|
|
|
conf_filechunk = (unsigned long)value;
|
|
|
|
break;
|
2006-06-04 15:04:03 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case CODEC_SET_FILEBUF_PRESEEK:
|
|
|
|
conf_preseek = (unsigned long)value;
|
|
|
|
break;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
default:
|
|
|
|
if (!dsp_configure(setting, value)) { logf("Illegal key:%d", setting); }
|
2006-04-07 16:31:20 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2006-04-26 04:01:35 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_track_changed(void)
|
|
|
|
{
|
|
|
|
automatic_skip = false;
|
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_TRACK_CHANGED");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_TRACK_CHANGED, 0);
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_pcmbuf_track_changed_callback(void)
|
|
|
|
{
|
|
|
|
pcmbuf_set_position_callback(NULL);
|
|
|
|
codec_track_changed();
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_discard_codec_callback(void)
|
2005-06-13 22:09:12 +00:00
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
if (CUR_TI->has_codec)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->has_codec = false;
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_ridx = RINGBUF_ADD(buf_ridx, CUR_TI->codecsize);
|
2005-07-03 18:36:24 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
#if 0
|
|
|
|
/* Check if a buffer desync has happened, log it and stop playback. */
|
2006-10-12 15:54:36 +00:00
|
|
|
if (buf_ridx != CUR_TI->buf_idx)
|
2006-04-09 02:15:35 +00:00
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
int offset = CUR_TI->buf_idx - buf_ridx;
|
2006-10-12 16:51:22 +00:00
|
|
|
size_t new_used = FILEBUFUSED - offset;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
logf("Buf off :%d=%d-%d", offset, CUR_TI->buf_idx, buf_ridx);
|
2006-10-12 16:51:22 +00:00
|
|
|
logf("Used off:%d",FILEBUFUSED - new_used);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* This is a fatal internal error and it's not safe to
|
|
|
|
* continue playback. */
|
|
|
|
ci.stop_codec = true;
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_STOP, 0);
|
2005-06-13 22:09:12 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
#endif
|
2005-06-13 22:09:12 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_track_skip_done(bool was_manual)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-01-20 22:02:44 +00:00
|
|
|
/* Manual track change (always crossfade or flush audio). */
|
2006-04-13 14:17:12 +00:00
|
|
|
if (was_manual)
|
2006-01-20 22:02:44 +00:00
|
|
|
{
|
2006-01-27 16:25:44 +00:00
|
|
|
pcmbuf_crossfade_init(true);
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_TRACK_CHANGED");
|
2006-04-23 20:37:43 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_TRACK_CHANGED, 0);
|
2005-07-17 15:35:17 +00:00
|
|
|
}
|
2006-04-11 03:55:58 +00:00
|
|
|
/* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */
|
2006-03-30 05:56:19 +00:00
|
|
|
else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active()
|
|
|
|
&& global_settings.crossfade != 2)
|
2006-01-20 22:02:44 +00:00
|
|
|
{
|
2006-04-24 12:57:41 +00:00
|
|
|
pcmbuf_crossfade_init(false);
|
|
|
|
codec_track_changed();
|
2006-01-20 22:02:44 +00:00
|
|
|
}
|
|
|
|
/* Gapless playback. */
|
|
|
|
else
|
2006-04-13 14:17:12 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
pcmbuf_set_position_callback(codec_pcmbuf_position_callback);
|
|
|
|
pcmbuf_set_event_handler(codec_pcmbuf_track_changed_callback);
|
2006-04-13 14:17:12 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-08-31 10:01:07 +00:00
|
|
|
static bool codec_load_next_track(void)
|
2006-07-29 19:34:12 +00:00
|
|
|
{
|
2006-04-14 13:05:08 +00:00
|
|
|
struct event ev;
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
prev_track_elapsed = CUR_TI->id3.elapsed;
|
|
|
|
|
2006-04-18 23:43:54 +00:00
|
|
|
if (ci.seek_time)
|
|
|
|
codec_seek_complete_callback();
|
|
|
|
|
2006-04-13 14:17:12 +00:00
|
|
|
#ifdef AB_REPEAT_ENABLE
|
|
|
|
ab_end_of_track_report();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
logf("Request new track");
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci.new_track == 0)
|
2005-12-01 18:44:11 +00:00
|
|
|
{
|
2006-04-15 02:03:11 +00:00
|
|
|
ci.new_track++;
|
2006-04-23 22:30:52 +00:00
|
|
|
automatic_skip = true;
|
2005-07-02 17:03:19 +00:00
|
|
|
}
|
2006-04-13 14:17:12 +00:00
|
|
|
|
2006-10-15 11:57:52 +00:00
|
|
|
trigger_cpu_boost();
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_CHECK_NEW_TRACK");
|
2006-04-17 17:05:05 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_CHECK_NEW_TRACK, 0);
|
2006-07-29 19:34:12 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2006-04-19 00:23:08 +00:00
|
|
|
queue_wait(&codec_callback_queue, &ev);
|
|
|
|
if (ev.id == Q_CODEC_REQUEST_PENDING)
|
2006-04-23 22:30:52 +00:00
|
|
|
{
|
|
|
|
if (!automatic_skip)
|
2006-04-23 22:21:12 +00:00
|
|
|
pcmbuf_play_stop();
|
2006-04-23 22:30:52 +00:00
|
|
|
}
|
2006-04-19 00:23:08 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2006-10-15 11:57:52 +00:00
|
|
|
|
2006-04-14 14:03:43 +00:00
|
|
|
switch (ev.id)
|
|
|
|
{
|
|
|
|
case Q_CODEC_REQUEST_COMPLETE:
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec < Q_CODEC_REQUEST_COMPLETE");
|
|
|
|
codec_track_skip_done(!automatic_skip);
|
2006-04-14 14:03:43 +00:00
|
|
|
return true;
|
2006-08-31 10:01:07 +00:00
|
|
|
|
2006-04-14 14:03:43 +00:00
|
|
|
case Q_CODEC_REQUEST_FAILED:
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec < Q_CODEC_REQUEST_FAILED");
|
2006-04-18 19:52:10 +00:00
|
|
|
ci.new_track = 0;
|
2006-04-14 14:03:43 +00:00
|
|
|
ci.stop_codec = true;
|
|
|
|
return false;
|
2006-08-31 10:01:07 +00:00
|
|
|
|
2006-04-14 14:03:43 +00:00
|
|
|
default:
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec < default");
|
2006-04-14 14:03:43 +00:00
|
|
|
ci.stop_codec = true;
|
|
|
|
return false;
|
|
|
|
}
|
2005-12-01 18:44:11 +00:00
|
|
|
}
|
2005-11-23 19:13:25 +00:00
|
|
|
|
2006-04-13 14:17:12 +00:00
|
|
|
static bool codec_request_next_track_callback(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-04-17 17:05:05 +00:00
|
|
|
int prev_codectype;
|
|
|
|
|
2005-07-10 06:58:02 +00:00
|
|
|
if (ci.stop_codec || !playing)
|
2005-06-05 23:05:10 +00:00
|
|
|
return false;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
prev_codectype = get_codec_base_type(CUR_TI->id3.codectype);
|
2006-04-17 17:05:05 +00:00
|
|
|
|
2006-08-31 10:01:07 +00:00
|
|
|
if (!codec_load_next_track())
|
2006-04-13 14:17:12 +00:00
|
|
|
return false;
|
2005-07-24 04:17:34 +00:00
|
|
|
|
2005-07-10 16:33:03 +00:00
|
|
|
/* Check if the next codec is the same file. */
|
2006-10-12 15:54:36 +00:00
|
|
|
if (prev_codectype == get_codec_base_type(CUR_TI->id3.codectype))
|
2006-04-11 03:55:58 +00:00
|
|
|
{
|
|
|
|
logf("New track loaded");
|
2006-04-11 12:29:46 +00:00
|
|
|
codec_discard_codec_callback();
|
2006-04-11 03:55:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-04-11 12:29:46 +00:00
|
|
|
else
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
logf("New codec:%d/%d", CUR_TI->id3.codectype, prev_codectype);
|
2006-04-11 12:29:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void codec_thread(void)
|
2005-06-13 22:09:12 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
struct event ev;
|
|
|
|
int status;
|
|
|
|
size_t wrap;
|
2006-04-09 02:15:35 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
while (1) {
|
|
|
|
status = 0;
|
|
|
|
queue_wait(&codec_queue, &ev);
|
2006-04-09 02:15:35 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
switch (ev.id) {
|
|
|
|
case Q_CODEC_LOAD_DISK:
|
|
|
|
LOGFQUEUE("codec < Q_CODEC_LOAD_DISK");
|
|
|
|
audio_codec_loaded = true;
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-17 18:52:31 +00:00
|
|
|
/* Don't sent messages to voice codec if it's not current */
|
|
|
|
if (voice_codec_loaded && current_codec == CODEC_IDX_VOICE)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
LOGFQUEUE("codec > voice Q_AUDIO_PLAY");
|
|
|
|
queue_post(&voice_queue, Q_AUDIO_PLAY, 0);
|
|
|
|
}
|
|
|
|
mutex_lock(&mutex_codecthread);
|
2006-11-01 19:19:36 +00:00
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
ci.stop_codec = false;
|
|
|
|
status = codec_load_file((const char *)ev.data, &ci);
|
2006-11-01 19:19:36 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
mutex_unlock(&mutex_codecthread);
|
2006-11-01 19:19:36 +00:00
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
break ;
|
2005-06-13 22:09:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_CODEC_LOAD:
|
|
|
|
LOGFQUEUE("codec < Q_CODEC_LOAD");
|
2006-10-12 15:54:36 +00:00
|
|
|
if (!CUR_TI->has_codec) {
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("Codec slot is empty!");
|
|
|
|
/* Wait for the pcm buffer to go empty */
|
|
|
|
while (pcm_is_playing())
|
|
|
|
yield();
|
|
|
|
/* This must be set to prevent an infinite loop */
|
|
|
|
ci.stop_codec = true;
|
|
|
|
LOGFQUEUE("codec > codec Q_AUDIO_PLAY");
|
|
|
|
queue_post(&codec_queue, Q_AUDIO_PLAY, 0);
|
|
|
|
break ;
|
|
|
|
}
|
2006-04-26 04:01:35 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_codec_loaded = true;
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-17 18:52:31 +00:00
|
|
|
if (voice_codec_loaded && current_codec == CODEC_IDX_VOICE)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
LOGFQUEUE("codec > voice Q_AUDIO_PLAY");
|
|
|
|
queue_post(&voice_queue, Q_AUDIO_PLAY, 0);
|
|
|
|
}
|
|
|
|
mutex_lock(&mutex_codecthread);
|
2006-11-01 19:19:36 +00:00
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
ci.stop_codec = false;
|
2006-10-12 15:54:36 +00:00
|
|
|
wrap = (size_t)&filebuf[filebuflen] - (size_t)CUR_TI->codecbuf;
|
|
|
|
status = codec_load_ram(CUR_TI->codecbuf, CUR_TI->codecsize,
|
2006-09-01 07:59:31 +00:00
|
|
|
&filebuf[0], wrap, &ci);
|
2006-11-01 19:19:36 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-09-01 07:59:31 +00:00
|
|
|
mutex_unlock(&mutex_codecthread);
|
2006-11-01 19:19:36 +00:00
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
break ;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
2006-08-28 22:38:41 +00:00
|
|
|
case Q_ENCODER_LOAD_DISK:
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK");
|
2006-09-17 18:52:31 +00:00
|
|
|
audio_codec_loaded = false; /* Not audio codec! */
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-08-28 22:38:41 +00:00
|
|
|
if (voice_codec_loaded && current_codec == CODEC_IDX_VOICE)
|
2006-08-31 10:01:07 +00:00
|
|
|
{
|
|
|
|
LOGFQUEUE("codec > voice Q_ENCODER_RECORD");
|
2006-09-01 07:59:31 +00:00
|
|
|
queue_post(&voice_queue, Q_ENCODER_RECORD, NULL);
|
2006-08-31 10:01:07 +00:00
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
mutex_lock(&mutex_codecthread);
|
2006-11-13 09:07:18 +00:00
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
logf("loading encoder");
|
2006-08-28 22:38:41 +00:00
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
ci.stop_codec = false;
|
|
|
|
status = codec_load_file((const char *)ev.data, &ci);
|
2006-11-13 09:07:18 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-08-28 22:38:41 +00:00
|
|
|
mutex_unlock(&mutex_codecthread);
|
2006-11-13 09:07:18 +00:00
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
logf("encoder stopped");
|
2006-08-28 22:38:41 +00:00
|
|
|
break;
|
2006-11-06 18:07:30 +00:00
|
|
|
#endif /* AUDIO_HAVE_RECORDING */
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-03-30 05:56:19 +00:00
|
|
|
#ifndef SIMULATOR
|
2006-09-16 16:18:11 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec < SYS_USB_CONNECTED");
|
2006-04-14 16:42:14 +00:00
|
|
|
queue_clear(&codec_queue);
|
2005-06-05 23:05:10 +00:00
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&codec_queue);
|
2006-08-31 10:01:07 +00:00
|
|
|
break;
|
2005-06-05 23:05:10 +00:00
|
|
|
#endif
|
2006-08-31 10:01:07 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
LOGFQUEUE("codec < default");
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2005-06-29 20:50:58 +00:00
|
|
|
|
2006-04-09 16:12:12 +00:00
|
|
|
if (audio_codec_loaded)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-04-15 09:19:49 +00:00
|
|
|
if (ci.stop_codec)
|
|
|
|
{
|
|
|
|
status = CODEC_OK;
|
|
|
|
if (!playing)
|
|
|
|
pcmbuf_play_stop();
|
2006-11-01 19:19:36 +00:00
|
|
|
|
2006-04-15 09:19:49 +00:00
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
audio_codec_loaded = false;
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2005-06-05 23:05:10 +00:00
|
|
|
switch (ev.id) {
|
2006-04-11 03:55:58 +00:00
|
|
|
case Q_CODEC_LOAD_DISK:
|
|
|
|
case Q_CODEC_LOAD:
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec < Q_CODEC_LOAD");
|
2006-07-29 17:25:31 +00:00
|
|
|
if (playing)
|
|
|
|
{
|
|
|
|
if (ci.new_track || status != CODEC_OK)
|
|
|
|
{
|
|
|
|
if (!ci.new_track)
|
|
|
|
{
|
2006-04-17 17:05:05 +00:00
|
|
|
logf("Codec failure");
|
|
|
|
gui_syncsplash(HZ*2, true, "Codec failure");
|
|
|
|
}
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-08-31 10:01:07 +00:00
|
|
|
if (!codec_load_next_track())
|
2006-04-17 17:05:05 +00:00
|
|
|
{
|
2006-08-28 22:38:41 +00:00
|
|
|
// queue_post(&codec_queue, Q_AUDIO_STOP, 0);
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_STOP");
|
2006-08-28 22:38:41 +00:00
|
|
|
queue_post(&audio_queue, Q_AUDIO_STOP, 0);
|
2006-04-17 17:05:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-07-29 17:25:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-17 17:05:05 +00:00
|
|
|
logf("Codec finished");
|
2006-04-11 03:55:58 +00:00
|
|
|
if (ci.stop_codec)
|
2006-04-17 17:05:05 +00:00
|
|
|
{
|
2006-04-22 14:40:13 +00:00
|
|
|
/* Wait for the audio to stop playing before
|
|
|
|
* triggering the WPS exit */
|
|
|
|
while(pcm_is_playing())
|
2006-10-19 07:43:07 +00:00
|
|
|
{
|
|
|
|
CUR_TI->id3.elapsed = CUR_TI->id3.length - pcmbuf_get_latency();
|
2006-10-19 08:55:24 +00:00
|
|
|
sleep(1);
|
2006-10-19 07:43:07 +00:00
|
|
|
}
|
2006-11-02 07:53:55 +00:00
|
|
|
LOGFQUEUE("codec > audio Q_AUDIO_STOP");
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_STOP, 0);
|
2006-04-17 17:05:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-29 17:25:31 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
if (CUR_TI->has_codec)
|
2006-08-31 10:01:07 +00:00
|
|
|
{
|
|
|
|
LOGFQUEUE("codec > codec Q_CODEC_LOAD");
|
2006-04-17 17:05:05 +00:00
|
|
|
queue_post(&codec_queue, Q_CODEC_LOAD, 0);
|
2006-08-31 10:01:07 +00:00
|
|
|
}
|
2006-04-17 17:05:05 +00:00
|
|
|
else
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
const char *codec_fn = get_codec_filename(CUR_TI->id3.codectype);
|
2006-08-31 10:01:07 +00:00
|
|
|
LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK");
|
2006-08-28 22:38:41 +00:00
|
|
|
queue_post(&codec_queue, Q_CODEC_LOAD_DISK,
|
|
|
|
(void *)codec_fn);
|
2006-04-11 03:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-31 10:01:07 +00:00
|
|
|
break;
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
|
|
|
case Q_ENCODER_LOAD_DISK:
|
|
|
|
LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK");
|
|
|
|
|
|
|
|
if (status == CODEC_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
logf("Encoder failure");
|
|
|
|
gui_syncsplash(HZ*2, true, "Encoder failure");
|
|
|
|
|
|
|
|
if (ci.enc_codec_loaded < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
logf("Encoder failed to load");
|
|
|
|
ci.enc_codec_loaded = -1;
|
|
|
|
break;
|
|
|
|
#endif /* AUDIO_HAVE_RECORDING */
|
|
|
|
|
2006-08-31 10:01:07 +00:00
|
|
|
default:
|
|
|
|
LOGFQUEUE("codec < default");
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
} /* end switch */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- Audio thread --- */
|
|
|
|
|
|
|
|
static bool audio_filebuf_is_lowdata(void)
|
|
|
|
{
|
2006-10-12 16:51:22 +00:00
|
|
|
return FILEBUFUSED < AUDIO_FILEBUF_CRITICAL;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool audio_have_tracks(void)
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
return track_ridx != track_widx || CUR_TI->filesize;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool audio_have_free_tracks(void)
|
|
|
|
{
|
|
|
|
if (track_widx < track_ridx)
|
|
|
|
return track_widx + 1 < track_ridx;
|
|
|
|
else if (track_ridx == 0)
|
|
|
|
return track_widx < MAX_TRACK - 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_track_count(void)
|
|
|
|
{
|
|
|
|
if (audio_have_tracks())
|
|
|
|
{
|
|
|
|
int relative_track_widx = track_widx;
|
|
|
|
|
|
|
|
if (track_ridx > track_widx)
|
|
|
|
relative_track_widx += MAX_TRACK;
|
|
|
|
|
|
|
|
return relative_track_widx - track_ridx + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-12 16:51:22 +00:00
|
|
|
long audio_filebufused(void)
|
|
|
|
{
|
|
|
|
return (long) FILEBUFUSED;
|
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* Count the data BETWEEN the selected tracks */
|
|
|
|
static size_t audio_buffer_count_tracks(int from_track, int to_track)
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
bool need_wrap = to_track < from_track;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (++from_track >= MAX_TRACK)
|
|
|
|
{
|
|
|
|
from_track -= MAX_TRACK;
|
|
|
|
need_wrap = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (from_track >= to_track && !need_wrap)
|
|
|
|
break;
|
|
|
|
|
|
|
|
amount += tracks[from_track].codecsize + tracks[from_track].filesize;
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool audio_buffer_wind_forward(int new_track_ridx, int old_track_ridx)
|
|
|
|
{
|
|
|
|
size_t amount;
|
|
|
|
|
|
|
|
/* Start with the remainder of the previously playing track */
|
|
|
|
amount = tracks[old_track_ridx].filesize - ci.curpos;
|
|
|
|
/* Then collect all data from tracks in between them */
|
|
|
|
amount += audio_buffer_count_tracks(old_track_ridx, new_track_ridx);
|
2006-10-15 11:03:26 +00:00
|
|
|
logf("bwf:%ldB", (long) amount);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-12 16:51:22 +00:00
|
|
|
if (amount > FILEBUFUSED)
|
2006-09-01 07:59:31 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Wind the buffer to the beginning of the target track or its codec */
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_ridx = RINGBUF_ADD(buf_ridx, amount);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool audio_buffer_wind_backward(int new_track_ridx, int old_track_ridx)
|
|
|
|
{
|
|
|
|
/* Available buffer data */
|
|
|
|
size_t buf_back;
|
|
|
|
/* Start with the previously playing track's data and our data */
|
|
|
|
size_t amount;
|
|
|
|
|
|
|
|
amount = ci.curpos;
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_back = RINGBUF_SUB(buf_ridx, buf_widx);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* If we're not just resetting the current track */
|
|
|
|
if (new_track_ridx != old_track_ridx)
|
|
|
|
{
|
|
|
|
/* Need to wind to before the old track's codec and our filesize */
|
|
|
|
amount += tracks[old_track_ridx].codecsize;
|
|
|
|
amount += tracks[new_track_ridx].filesize;
|
|
|
|
|
|
|
|
/* Rewind the old track to its beginning */
|
|
|
|
tracks[old_track_ridx].available =
|
|
|
|
tracks[old_track_ridx].filesize - tracks[old_track_ridx].filerem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the codec was ever buffered */
|
|
|
|
if (tracks[new_track_ridx].codecsize)
|
|
|
|
{
|
|
|
|
/* Add the codec to the needed size */
|
|
|
|
amount += tracks[new_track_ridx].codecsize;
|
|
|
|
tracks[new_track_ridx].has_codec = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then collect all data from tracks between new and old */
|
|
|
|
amount += audio_buffer_count_tracks(new_track_ridx, old_track_ridx);
|
|
|
|
|
|
|
|
/* Do we have space to make this skip? */
|
|
|
|
if (amount > buf_back)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
logf("bwb:%ldB",amount);
|
|
|
|
|
|
|
|
/* Rewind the buffer to the beginning of the target track or its codec */
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_ridx = RINGBUF_SUB(buf_ridx, amount);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* Reset to the beginning of the new track */
|
|
|
|
tracks[new_track_ridx].available = tracks[new_track_ridx].filesize;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_update_trackinfo(void)
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
ci.filesize = CUR_TI->filesize;
|
|
|
|
CUR_TI->id3.elapsed = 0;
|
|
|
|
CUR_TI->id3.offset = 0;
|
|
|
|
ci.id3 = &CUR_TI->id3;
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.curpos = 0;
|
2006-10-12 15:54:36 +00:00
|
|
|
ci.taginfo_ready = &CUR_TI->taginfo_ready;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Yield to codecs for as long as possible if they are in need of data
|
|
|
|
* return true if the caller should break to let the audio thread process
|
|
|
|
* new events */
|
|
|
|
static bool audio_yield_codecs(void)
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
if (!queue_empty(&audio_queue))
|
|
|
|
return true;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
while ((pcmbuf_is_crossfade_active() || pcmbuf_is_lowdata())
|
|
|
|
&& !ci.stop_codec && playing && !audio_filebuf_is_lowdata())
|
|
|
|
{
|
2006-11-11 05:33:24 +00:00
|
|
|
if (filling)
|
|
|
|
yield();
|
|
|
|
else
|
|
|
|
sleep(2);
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
if (!queue_empty(&audio_queue))
|
|
|
|
return true;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
static void audio_clear_track_entries(bool clear_unbuffered)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
int cur_idx = track_widx;
|
|
|
|
int last_idx = -1;
|
|
|
|
|
|
|
|
logf("Clearing tracks:%d/%d, %d", track_ridx, track_widx, clear_unbuffered);
|
|
|
|
|
|
|
|
/* Loop over all tracks from write-to-read */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
cur_idx++;
|
|
|
|
cur_idx &= MAX_TRACK_MASK;
|
|
|
|
|
|
|
|
if (cur_idx == track_ridx)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* If the track is buffered, conditionally clear/notify,
|
|
|
|
* otherwise clear the track if that option is selected */
|
|
|
|
if (tracks[cur_idx].event_sent)
|
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
if (last_idx >= 0)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
/* If there is an unbuffer callback, call it, otherwise,
|
|
|
|
* just clear the track */
|
|
|
|
if (track_unbuffer_callback)
|
|
|
|
track_unbuffer_callback(&tracks[last_idx].id3, false);
|
|
|
|
|
|
|
|
memset(&tracks[last_idx], 0, sizeof(struct track_info));
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2006-10-17 12:56:22 +00:00
|
|
|
last_idx = cur_idx;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
else if (clear_unbuffered)
|
|
|
|
memset(&tracks[cur_idx], 0, sizeof(struct track_info));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We clear the previous instance of a buffered track throughout
|
|
|
|
* the above loop to facilitate 'last' detection. Clear/notify
|
|
|
|
* the last track here */
|
|
|
|
if (last_idx >= 0)
|
|
|
|
{
|
|
|
|
if (track_unbuffer_callback)
|
|
|
|
track_unbuffer_callback(&tracks[last_idx].id3, true);
|
|
|
|
memset(&tracks[last_idx], 0, sizeof(struct track_info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: This code should be made more generic and move to metadata.c */
|
2006-11-14 15:48:20 +00:00
|
|
|
static void audio_strip_tags(void)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static const unsigned char tag[] = "TAG";
|
2006-11-14 15:48:20 +00:00
|
|
|
static const unsigned char apetag[] = "APETAGEX";
|
2006-09-01 07:59:31 +00:00
|
|
|
size_t tag_idx;
|
|
|
|
size_t cur_idx;
|
2006-11-14 15:48:20 +00:00
|
|
|
size_t len, version;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-13 09:16:36 +00:00
|
|
|
tag_idx = RINGBUF_SUB(buf_widx, 128);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-12 16:51:22 +00:00
|
|
|
if (FILEBUFUSED > 128 && tag_idx > buf_ridx)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
cur_idx = tag_idx;
|
|
|
|
for(i = 0;i < 3;i++)
|
|
|
|
{
|
|
|
|
if(filebuf[cur_idx] != tag[i])
|
2006-11-14 15:48:20 +00:00
|
|
|
goto strip_ape_tag;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-13 09:16:36 +00:00
|
|
|
cur_idx = RINGBUF_ADD(cur_idx, 1);
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip id3v1 tag */
|
|
|
|
logf("Skipping ID3v1 tag");
|
|
|
|
buf_widx = tag_idx;
|
|
|
|
tracks[track_widx].available -= 128;
|
|
|
|
tracks[track_widx].filesize -= 128;
|
|
|
|
}
|
2006-11-14 15:48:20 +00:00
|
|
|
|
|
|
|
strip_ape_tag:
|
|
|
|
/* Check for APE tag (look for the APE tag footer) */
|
|
|
|
tag_idx = RINGBUF_SUB(buf_widx, 32);
|
|
|
|
|
|
|
|
if (FILEBUFUSED > 32 && tag_idx > buf_ridx)
|
|
|
|
{
|
|
|
|
cur_idx = tag_idx;
|
|
|
|
for(i = 0;i < 8;i++)
|
|
|
|
{
|
|
|
|
if(filebuf[cur_idx] != apetag[i])
|
|
|
|
return;
|
|
|
|
|
|
|
|
cur_idx = RINGBUF_ADD(cur_idx, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the version and length from the footer */
|
2006-11-16 21:04:04 +00:00
|
|
|
version = filebuf[tag_idx+8] | (filebuf[tag_idx+9] << 8) |
|
|
|
|
(filebuf[tag_idx+10] << 16) | (filebuf[tag_idx+11] << 24);
|
|
|
|
len = filebuf[tag_idx+12] | (filebuf[tag_idx+13] << 8) |
|
|
|
|
(filebuf[tag_idx+14] << 16) | (filebuf[tag_idx+15] << 24);
|
2006-11-14 15:48:20 +00:00
|
|
|
if (version == 2000)
|
|
|
|
len += 32; /* APEv2 has a 32 byte header */
|
|
|
|
|
|
|
|
/* Skip APE tag */
|
|
|
|
if (FILEBUFUSED > len)
|
|
|
|
{
|
|
|
|
logf("Skipping APE tag (%dB)", len);
|
|
|
|
buf_widx = RINGBUF_SUB(buf_widx, len);
|
|
|
|
tracks[track_widx].available -= len;
|
|
|
|
tracks[track_widx].filesize -= len;
|
|
|
|
}
|
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
/* Returns true if a whole file is read, false otherwise */
|
|
|
|
static bool audio_read_file(size_t minimum)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
bool ret_val = false;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* If we're called and no file is open, this is an error */
|
|
|
|
if (current_fd < 0)
|
|
|
|
{
|
|
|
|
logf("Bad fd in arf");
|
|
|
|
/* Give some hope of miraculous recovery by forcing a track reload */
|
|
|
|
tracks[track_widx].filesize = 0;
|
2006-10-17 12:56:22 +00:00
|
|
|
/* Stop this buffering run */
|
|
|
|
return ret_val;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
2006-10-15 11:57:52 +00:00
|
|
|
trigger_cpu_boost();
|
2006-09-01 07:59:31 +00:00
|
|
|
while (tracks[track_widx].filerem > 0)
|
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
size_t copy_n;
|
2006-09-01 07:59:31 +00:00
|
|
|
int overlap;
|
2006-10-17 12:56:22 +00:00
|
|
|
int rc;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* copy_n is the largest chunk that is safe to read */
|
|
|
|
copy_n = MIN(conf_filechunk, filebuflen - buf_widx);
|
2006-10-17 12:56:22 +00:00
|
|
|
|
2006-10-22 21:56:42 +00:00
|
|
|
/* buf_widx == buf_ridx is defined as buffer empty, not buffer full */
|
2006-10-17 12:56:22 +00:00
|
|
|
if (RINGBUF_ADD_CROSS(buf_widx,copy_n,buf_ridx) >= 0)
|
|
|
|
break;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* rc is the actual amount read */
|
|
|
|
rc = read(current_fd, &filebuf[buf_widx], copy_n);
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
if (rc < 0)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
logf("File ended %dB early", tracks[track_widx].filerem);
|
|
|
|
tracks[track_widx].filesize -= tracks[track_widx].filerem;
|
2006-09-01 07:59:31 +00:00
|
|
|
tracks[track_widx].filerem = 0;
|
2006-10-17 12:56:22 +00:00
|
|
|
break;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
/* How much of the playing track did we overwrite */
|
2006-10-22 21:56:42 +00:00
|
|
|
if (buf_widx == CUR_TI->buf_idx)
|
|
|
|
{
|
|
|
|
/* Special handling; zero or full overlap? */
|
2006-10-23 12:44:11 +00:00
|
|
|
if (track_widx == track_ridx && CUR_TI->available == 0)
|
|
|
|
overlap = 0;
|
2006-10-22 21:56:42 +00:00
|
|
|
else
|
2006-10-23 12:44:11 +00:00
|
|
|
overlap = rc;
|
2006-10-22 21:56:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
overlap = RINGBUF_ADD_CROSS(buf_widx,rc,CUR_TI->buf_idx);
|
2006-10-17 12:56:22 +00:00
|
|
|
|
2006-10-23 16:47:48 +00:00
|
|
|
if ((unsigned)rc > tracks[track_widx].filerem)
|
|
|
|
{
|
|
|
|
logf("Bad: rc-filerem=%d, fixing", rc-tracks[track_widx].filerem);
|
|
|
|
tracks[track_widx].filesize += rc - tracks[track_widx].filerem;
|
|
|
|
tracks[track_widx].filerem = rc;
|
|
|
|
}
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
/* Advance buffer */
|
|
|
|
buf_widx = RINGBUF_ADD(buf_widx, rc);
|
2006-10-23 12:44:11 +00:00
|
|
|
tracks[track_widx].available += rc;
|
|
|
|
tracks[track_widx].filerem -= rc;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
/* If we write into the playing track, adjust it's buffer info */
|
2006-10-23 12:44:11 +00:00
|
|
|
if (overlap > 0)
|
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
CUR_TI->buf_idx += overlap;
|
|
|
|
CUR_TI->start_pos += overlap;
|
|
|
|
}
|
2006-10-23 12:44:11 +00:00
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
/* For a rebuffer, fill at least this minimum */
|
|
|
|
if (minimum > (unsigned)rc)
|
|
|
|
minimum -= rc;
|
|
|
|
/* Let the codec process up to the watermark */
|
|
|
|
/* Break immediately if this is a quick buffer, or there is an event */
|
|
|
|
else if (minimum || audio_yield_codecs())
|
|
|
|
{
|
|
|
|
/* Exit quickly, but don't stop the overall buffering process */
|
|
|
|
ret_val = true;
|
2006-09-01 07:59:31 +00:00
|
|
|
break;
|
2006-10-17 12:56:22 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tracks[track_widx].filerem == 0)
|
|
|
|
{
|
|
|
|
logf("Finished buf:%dB", tracks[track_widx].filesize);
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
2006-11-14 15:48:20 +00:00
|
|
|
audio_strip_tags();
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
track_widx++;
|
|
|
|
track_widx &= MAX_TRACK_MASK;
|
|
|
|
|
|
|
|
tracks[track_widx].filesize = 0;
|
2006-10-17 12:56:22 +00:00
|
|
|
return true;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
logf("%s buf:%dB", ret_val?"Quick":"Partially",
|
2006-09-01 07:59:31 +00:00
|
|
|
tracks[track_widx].filesize - tracks[track_widx].filerem);
|
2006-10-17 12:56:22 +00:00
|
|
|
return ret_val;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool audio_loadcodec(bool start_play)
|
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
size_t size = 0;
|
2006-09-01 07:59:31 +00:00
|
|
|
int fd;
|
|
|
|
int rc;
|
|
|
|
size_t copy_n;
|
|
|
|
int prev_track;
|
|
|
|
char codec_path[MAX_PATH]; /* Full path to codec */
|
|
|
|
|
|
|
|
const char * codec_fn = get_codec_filename(tracks[track_widx].id3.codectype);
|
|
|
|
if (codec_fn == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tracks[track_widx].has_codec = false;
|
|
|
|
|
|
|
|
if (start_play)
|
|
|
|
{
|
|
|
|
/* Load the codec directly from disk and save some memory. */
|
|
|
|
track_ridx = track_widx;
|
2006-10-12 15:54:36 +00:00
|
|
|
ci.filesize = CUR_TI->filesize;
|
|
|
|
ci.id3 = &CUR_TI->id3;
|
|
|
|
ci.taginfo_ready = &CUR_TI->taginfo_ready;
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.curpos = 0;
|
|
|
|
LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK");
|
|
|
|
queue_post(&codec_queue, Q_CODEC_LOAD_DISK, (void *)codec_fn);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If we already have another track than this one buffered */
|
|
|
|
if (track_widx != track_ridx)
|
|
|
|
{
|
|
|
|
prev_track = (track_widx - 1) & MAX_TRACK_MASK;
|
|
|
|
|
|
|
|
/* If the previous codec is the same as this one, there is no need
|
|
|
|
* to put another copy of it on the file buffer */
|
|
|
|
if (get_codec_base_type(tracks[track_widx].id3.codectype) ==
|
|
|
|
get_codec_base_type(tracks[prev_track].id3.codectype)
|
|
|
|
&& audio_codec_loaded)
|
|
|
|
{
|
|
|
|
logf("Reusing prev. codec");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
codec_get_full_path(codec_path, codec_fn);
|
|
|
|
|
|
|
|
fd = open(codec_path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("Codec doesn't exist!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
tracks[track_widx].codecsize = filesize(fd);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* Never load a partial codec */
|
2006-10-17 12:56:22 +00:00
|
|
|
if (RINGBUF_ADD_CROSS(buf_widx,tracks[track_widx].codecsize,buf_ridx) >= 0)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
logf("Not enough space");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
while (size < tracks[track_widx].codecsize)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
|
|
|
copy_n = MIN(conf_filechunk, filebuflen - buf_widx);
|
|
|
|
rc = read(fd, &filebuf[buf_widx], copy_n);
|
|
|
|
if (rc < 0)
|
2006-10-13 09:16:36 +00:00
|
|
|
{
|
|
|
|
close(fd);
|
2006-10-17 12:56:22 +00:00
|
|
|
/* This is an error condition, likely the codec file is corrupt */
|
|
|
|
logf("Partial codec loaded");
|
|
|
|
/* Must undo the buffer write of the partial codec */
|
|
|
|
buf_widx = RINGBUF_SUB(buf_widx, size);
|
|
|
|
tracks[track_widx].codecsize = 0;
|
2006-09-01 07:59:31 +00:00
|
|
|
return false;
|
2006-10-13 09:16:36 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_widx = RINGBUF_ADD(buf_widx, rc);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
size += rc;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tracks[track_widx].has_codec = true;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
logf("Done: %dB", size);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Copied from mpeg.c. Should be moved somewhere else. */
|
|
|
|
static void audio_set_elapsed(struct mp3entry* id3)
|
|
|
|
{
|
2006-10-30 20:19:21 +00:00
|
|
|
unsigned long offset = id3->offset > id3->first_frame_offset ?
|
|
|
|
id3->offset - id3->first_frame_offset : 0;
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
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++ )
|
2006-10-30 20:19:21 +00:00
|
|
|
if ( offset < id3->toc[i] * (id3->filesize / 256) )
|
2006-09-01 07:59:31 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
i--;
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
relpos = id3->toc[i];
|
|
|
|
|
|
|
|
if (i < 99)
|
|
|
|
nextpos = id3->toc[i+1];
|
|
|
|
else
|
|
|
|
nextpos = 256;
|
|
|
|
|
2006-10-30 20:19:21 +00:00
|
|
|
remainder = offset - (relpos * (id3->filesize / 256));
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* 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 */
|
2006-10-30 20:19:21 +00:00
|
|
|
int tpk = id3->length /
|
|
|
|
((id3->filesize - id3->first_frame_offset - id3->id3v1len) /
|
|
|
|
1024);
|
|
|
|
id3->elapsed = offset / 1024 * tpk;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* constant bitrate, use exact calculation */
|
|
|
|
if (id3->bitrate != 0)
|
2006-10-30 20:19:21 +00:00
|
|
|
id3->elapsed = offset / (id3->bitrate / 8);
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool audio_load_track(int offset, bool start_play, bool rebuffer)
|
|
|
|
{
|
|
|
|
char *trackname;
|
|
|
|
off_t size;
|
|
|
|
char msgbuf[80];
|
|
|
|
|
|
|
|
/* 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 (!audio_have_free_tracks())
|
|
|
|
{
|
|
|
|
logf("No free tracks");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_fd >= 0)
|
|
|
|
{
|
|
|
|
logf("Nonzero fd in alt");
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_peek_offset++;
|
|
|
|
peek_again:
|
|
|
|
logf("Buffering track:%d/%d", track_widx, track_ridx);
|
|
|
|
/* Get track name from current playlist read position. */
|
|
|
|
while ((trackname = playlist_peek(last_peek_offset)) != NULL)
|
|
|
|
{
|
|
|
|
/* Handle broken playlists. */
|
|
|
|
current_fd = open(trackname, O_RDONLY);
|
|
|
|
if (current_fd < 0)
|
|
|
|
{
|
|
|
|
logf("Open failed");
|
|
|
|
/* Skip invalid entry from playlist. */
|
|
|
|
playlist_skip_entry(NULL, last_peek_offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!trackname)
|
|
|
|
{
|
|
|
|
logf("End-of-playlist");
|
|
|
|
playlist_end = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize track entry. */
|
|
|
|
size = filesize(current_fd);
|
|
|
|
tracks[track_widx].filerem = size;
|
|
|
|
tracks[track_widx].filesize = size;
|
|
|
|
tracks[track_widx].available = 0;
|
|
|
|
|
|
|
|
/* Set default values */
|
|
|
|
if (start_play)
|
|
|
|
{
|
|
|
|
int last_codec = current_codec;
|
|
|
|
|
|
|
|
current_codec = CODEC_IDX_AUDIO;
|
|
|
|
conf_watermark = AUDIO_DEFAULT_WATERMARK;
|
|
|
|
conf_filechunk = AUDIO_DEFAULT_FILECHUNK;
|
|
|
|
conf_preseek = AUDIO_REBUFFER_GUESS_SIZE;
|
|
|
|
dsp_configure(DSP_RESET, 0);
|
|
|
|
current_codec = last_codec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get track metadata if we don't already have it. */
|
|
|
|
if (!tracks[track_widx].taginfo_ready)
|
|
|
|
{
|
|
|
|
if (get_metadata(&tracks[track_widx],current_fd,trackname,v1first))
|
|
|
|
{
|
|
|
|
if (start_play)
|
|
|
|
{
|
|
|
|
track_changed = true;
|
|
|
|
playlist_update_resume_info(audio_current_track());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf("mde:%s!",trackname);
|
|
|
|
|
|
|
|
/* Set filesize to zero to indicate no file was loaded. */
|
|
|
|
tracks[track_widx].filesize = 0;
|
|
|
|
tracks[track_widx].filerem = 0;
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
|
|
|
|
/* Skip invalid entry from playlist. */
|
|
|
|
playlist_skip_entry(NULL, last_peek_offset);
|
|
|
|
tracks[track_widx].taginfo_ready = false;
|
|
|
|
goto peek_again;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the codec. */
|
|
|
|
tracks[track_widx].codecbuf = &filebuf[buf_widx];
|
|
|
|
if (!audio_loadcodec(start_play))
|
|
|
|
{
|
|
|
|
/* Set filesize to zero to indicate no file was loaded. */
|
|
|
|
tracks[track_widx].filesize = 0;
|
|
|
|
tracks[track_widx].filerem = 0;
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
if (tracks[track_widx].codecsize)
|
2006-09-01 07:59:31 +00:00
|
|
|
{
|
2006-10-17 12:56:22 +00:00
|
|
|
/* No space for codec on buffer, not an error */
|
|
|
|
tracks[track_widx].codecsize = 0;
|
|
|
|
return false;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2006-10-17 12:56:22 +00:00
|
|
|
|
|
|
|
/* This is an error condition, either no codec was found, or reading
|
|
|
|
* the codec file failed part way through, either way, skip the track */
|
|
|
|
snprintf(msgbuf, sizeof(msgbuf)-1, "No codec for: %s", trackname);
|
|
|
|
/* We should not use gui_syncplash from audio thread! */
|
|
|
|
gui_syncsplash(HZ*2, true, msgbuf);
|
|
|
|
/* Skip invalid entry from playlist. */
|
|
|
|
playlist_skip_entry(NULL, last_peek_offset);
|
|
|
|
tracks[track_widx].taginfo_ready = false;
|
|
|
|
goto peek_again;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tracks[track_widx].start_pos = 0;
|
|
|
|
set_filebuf_watermark(buffer_margin);
|
|
|
|
tracks[track_widx].id3.elapsed = 0;
|
|
|
|
|
|
|
|
if (offset > 0)
|
|
|
|
{
|
|
|
|
switch (tracks[track_widx].id3.codectype) {
|
|
|
|
case AFMT_MPA_L1:
|
|
|
|
case AFMT_MPA_L2:
|
|
|
|
case AFMT_MPA_L3:
|
|
|
|
lseek(current_fd, offset, SEEK_SET);
|
|
|
|
tracks[track_widx].id3.offset = offset;
|
|
|
|
audio_set_elapsed(&tracks[track_widx].id3);
|
|
|
|
tracks[track_widx].filerem = size - offset;
|
|
|
|
ci.curpos = offset;
|
|
|
|
tracks[track_widx].start_pos = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AFMT_WAVPACK:
|
|
|
|
lseek(current_fd, offset, SEEK_SET);
|
|
|
|
tracks[track_widx].id3.offset = offset;
|
|
|
|
tracks[track_widx].id3.elapsed =
|
|
|
|
tracks[track_widx].id3.length / 2;
|
|
|
|
tracks[track_widx].filerem = size - offset;
|
|
|
|
ci.curpos = offset;
|
|
|
|
tracks[track_widx].start_pos = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AFMT_OGG_VORBIS:
|
|
|
|
case AFMT_FLAC:
|
|
|
|
case AFMT_PCM_WAV:
|
|
|
|
case AFMT_A52:
|
|
|
|
case AFMT_AAC:
|
|
|
|
tracks[track_widx].id3.offset = offset;
|
|
|
|
break;
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
logf("alt:%s", trackname);
|
|
|
|
tracks[track_widx].buf_idx = buf_widx;
|
|
|
|
|
2006-10-17 12:56:22 +00:00
|
|
|
return audio_read_file(rebuffer);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool audio_read_next_metadata(void)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
int fd;
|
|
|
|
char *trackname;
|
|
|
|
int next_idx;
|
|
|
|
int status;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
next_idx = track_widx;
|
|
|
|
if (tracks[next_idx].taginfo_ready)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
next_idx++;
|
|
|
|
next_idx &= MAX_TRACK_MASK;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (tracks[next_idx].taginfo_ready)
|
|
|
|
return true;
|
2006-08-15 18:01:42 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
trackname = playlist_peek(last_peek_offset + 1);
|
|
|
|
if (!trackname)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fd = open(trackname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
status = get_metadata(&tracks[next_idx],fd,trackname,v1first);
|
|
|
|
/* Preload the glyphs in the tags */
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
if (tracks[next_idx].id3.title)
|
|
|
|
lcd_getstringsize(tracks[next_idx].id3.title, NULL, NULL);
|
|
|
|
if (tracks[next_idx].id3.artist)
|
|
|
|
lcd_getstringsize(tracks[next_idx].id3.artist, NULL, NULL);
|
|
|
|
if (tracks[next_idx].id3.album)
|
|
|
|
lcd_getstringsize(tracks[next_idx].id3.album, NULL, NULL);
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
close(fd);
|
2006-03-23 16:18:17 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
return status;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Send callback events to notify about new tracks. */
|
|
|
|
static void audio_generate_postbuffer_events(void)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
int cur_idx;
|
|
|
|
int last_idx = -1;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("Postbuffer:%d/%d",track_ridx,track_widx);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (audio_have_tracks())
|
|
|
|
{
|
|
|
|
cur_idx = track_ridx;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (!tracks[cur_idx].event_sent)
|
|
|
|
{
|
|
|
|
if (last_idx >= 0 && !tracks[last_idx].event_sent)
|
|
|
|
{
|
|
|
|
/* Mark the event 'sent' even if we don't really send one */
|
|
|
|
tracks[last_idx].event_sent = true;
|
|
|
|
if (track_buffer_callback)
|
|
|
|
track_buffer_callback(&tracks[last_idx].id3, false);
|
|
|
|
}
|
|
|
|
last_idx = cur_idx;
|
|
|
|
}
|
|
|
|
if (cur_idx == track_widx)
|
|
|
|
break;
|
|
|
|
cur_idx++;
|
|
|
|
cur_idx &= MAX_TRACK_MASK;
|
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (last_idx >= 0 && !tracks[last_idx].event_sent)
|
|
|
|
{
|
|
|
|
tracks[last_idx].event_sent = true;
|
|
|
|
if (track_buffer_callback)
|
|
|
|
track_buffer_callback(&tracks[last_idx].id3, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Force WPS reload. */
|
|
|
|
track_changed = true;
|
|
|
|
}
|
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static bool audio_initialize_buffer_fill(bool clear_tracks)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Don't initialize if we're already initialized */
|
|
|
|
if (filling)
|
|
|
|
return true;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("Starting buffer fill");
|
|
|
|
|
|
|
|
/* Set the filling flag true before calling audio_clear_tracks as that
|
|
|
|
* function can yield and we start looping. */
|
|
|
|
filling = true;
|
|
|
|
|
|
|
|
if (clear_tracks)
|
2006-10-17 12:56:22 +00:00
|
|
|
audio_clear_track_entries(false);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* Save the current resume position once. */
|
|
|
|
playlist_update_resume_info(audio_current_track());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_fill_file_buffer(
|
|
|
|
bool start_play, bool rebuffer, size_t offset)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
bool had_next_track = audio_next_track() != NULL;
|
2006-10-17 12:56:22 +00:00
|
|
|
bool continue_buffering;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
2006-11-18 02:18:29 +00:00
|
|
|
/* must reset the buffer before use if trashed */
|
|
|
|
if (buffer_state != BUFFER_STATE_NORMAL)
|
|
|
|
audio_reset_buffer(pcmbuf_get_bufsize());
|
|
|
|
|
2006-10-26 17:33:54 +00:00
|
|
|
if (!audio_initialize_buffer_fill(!start_play))
|
2006-09-01 07:59:31 +00:00
|
|
|
return ;
|
|
|
|
|
|
|
|
/* If we have a partially buffered track, continue loading,
|
|
|
|
* otherwise load a new track */
|
|
|
|
if (tracks[track_widx].filesize > 0)
|
2006-10-17 12:56:22 +00:00
|
|
|
continue_buffering = audio_read_file(rebuffer);
|
|
|
|
else
|
|
|
|
continue_buffering = audio_load_track(offset, start_play, rebuffer);
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
if (!had_next_track && audio_next_track())
|
|
|
|
track_changed = true;
|
|
|
|
|
|
|
|
/* If we're done buffering */
|
2006-10-17 12:56:22 +00:00
|
|
|
if (!continue_buffering)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_read_next_metadata();
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_generate_postbuffer_events();
|
|
|
|
filling = false;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
2006-11-08 02:23:01 +00:00
|
|
|
#ifndef SIMULATOR
|
2006-11-08 01:55:26 +00:00
|
|
|
ata_sleep();
|
2006-11-08 02:23:01 +00:00
|
|
|
#endif
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_rebuffer(void)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
logf("Forcing rebuffer");
|
|
|
|
|
|
|
|
/* Notify the codec that this will take a while */
|
|
|
|
/* Currently this can cause some problems (logf in reverse order):
|
|
|
|
* Codec load error:-1
|
|
|
|
* Codec load disk
|
|
|
|
* Codec: Unsupported
|
|
|
|
* Codec finished
|
|
|
|
* New codec:0/3
|
|
|
|
* Clearing tracks:7/7, 1
|
|
|
|
* Forcing rebuffer
|
|
|
|
* Check new track buffer
|
|
|
|
* Request new track
|
|
|
|
* Clearing tracks:5/5, 0
|
|
|
|
* Starting buffer fill
|
|
|
|
* Clearing tracks:5/5, 1
|
|
|
|
* Re-buffering song w/seek
|
|
|
|
*/
|
|
|
|
//if (!filling)
|
|
|
|
// queue_post(&codec_callback_queue, Q_CODEC_REQUEST_PENDING, 0);
|
|
|
|
|
|
|
|
/* Stop in progress fill, and clear open file descriptor */
|
|
|
|
if (current_fd >= 0)
|
2005-08-20 11:13:19 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
filling = false;
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Reset buffer and track pointers */
|
2006-10-13 09:16:36 +00:00
|
|
|
CUR_TI->buf_idx = buf_ridx = buf_widx = 0;
|
2006-09-01 07:59:31 +00:00
|
|
|
track_widx = track_ridx;
|
2006-10-17 12:56:22 +00:00
|
|
|
audio_clear_track_entries(true);
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->available = 0;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Fill the buffer */
|
|
|
|
last_peek_offset = -1;
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->filesize = 0;
|
|
|
|
CUR_TI->start_pos = 0;
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.curpos = 0;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
if (!CUR_TI->taginfo_ready)
|
|
|
|
memset(&CUR_TI->id3, 0, sizeof(struct mp3entry));
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_fill_file_buffer(false, true, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_check_new_track(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
int track_count = audio_track_count();
|
|
|
|
int old_track_ridx = track_ridx;
|
|
|
|
bool forward;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (dir_skip)
|
|
|
|
{
|
|
|
|
dir_skip = false;
|
|
|
|
if (playlist_next_dir(ci.new_track))
|
|
|
|
{
|
|
|
|
ci.new_track = 0;
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->taginfo_ready = false;
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_rebuffer();
|
|
|
|
goto skip_done;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGFQUEUE("audio > codec Q_CODEC_REQUEST_FAILED");
|
|
|
|
queue_post(&codec_callback_queue, Q_CODEC_REQUEST_FAILED, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-11-17 19:31:29 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (new_playlist)
|
|
|
|
ci.new_track = 0;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If the playlist isn't that big */
|
|
|
|
if (!playlist_check(ci.new_track))
|
|
|
|
{
|
|
|
|
if (ci.new_track >= 0)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("audio > codec Q_CODEC_REQUEST_FAILED");
|
|
|
|
queue_post(&codec_callback_queue, Q_CODEC_REQUEST_FAILED, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Find the beginning backward if the user over-skips it */
|
|
|
|
while (!playlist_check(++ci.new_track))
|
|
|
|
if (ci.new_track >= 0)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("audio > codec Q_CODEC_REQUEST_FAILED");
|
|
|
|
queue_post(&codec_callback_queue, Q_CODEC_REQUEST_FAILED, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Update the playlist */
|
|
|
|
last_peek_offset -= ci.new_track;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (playlist_next(ci.new_track) < 0)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("audio > codec Q_CODEC_REQUEST_FAILED");
|
|
|
|
queue_post(&codec_callback_queue, Q_CODEC_REQUEST_FAILED, 0);
|
|
|
|
return;
|
|
|
|
}
|
2006-01-19 18:21:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (new_playlist)
|
|
|
|
{
|
|
|
|
ci.new_track = 1;
|
|
|
|
new_playlist = false;
|
|
|
|
}
|
2006-01-19 18:21:12 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Save the old track */
|
2006-10-12 15:54:36 +00:00
|
|
|
prev_ti = CUR_TI;
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Move to the new track */
|
2006-10-12 15:54:36 +00:00
|
|
|
track_ridx += ci.new_track;
|
|
|
|
track_ridx &= MAX_TRACK_MASK;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (automatic_skip)
|
|
|
|
playlist_end = false;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
track_changed = !automatic_skip;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If it is not safe to even skip this many track entries */
|
|
|
|
if (ci.new_track >= track_count || ci.new_track <= track_count - MAX_TRACK)
|
|
|
|
{
|
|
|
|
ci.new_track = 0;
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->taginfo_ready = false;
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_rebuffer();
|
|
|
|
goto skip_done;
|
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
forward = ci.new_track > 0;
|
|
|
|
ci.new_track = 0;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If the target track is clearly not in memory */
|
2006-10-12 15:54:36 +00:00
|
|
|
if (CUR_TI->filesize == 0 || !CUR_TI->taginfo_ready)
|
2006-07-29 17:25:31 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_rebuffer();
|
|
|
|
goto skip_done;
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* The track may be in memory, see if it really is */
|
|
|
|
if (forward)
|
2006-08-31 10:01:07 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
if (!audio_buffer_wind_forward(track_ridx, old_track_ridx))
|
|
|
|
audio_rebuffer();
|
2006-08-31 10:01:07 +00:00
|
|
|
}
|
2006-04-26 04:01:35 +00:00
|
|
|
else
|
2006-01-20 22:02:44 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
int cur_idx = track_ridx;
|
|
|
|
bool taginfo_ready = true;
|
|
|
|
bool wrap = track_ridx > old_track_ridx;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
cur_idx++;
|
|
|
|
cur_idx &= MAX_TRACK_MASK;
|
|
|
|
if (!(wrap || cur_idx < old_track_ridx))
|
|
|
|
break;
|
2006-05-02 16:18:21 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If we hit a track in between without valid tag info, bail */
|
|
|
|
if (!tracks[cur_idx].taginfo_ready)
|
|
|
|
{
|
|
|
|
taginfo_ready = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracks[cur_idx].available = tracks[cur_idx].filesize;
|
|
|
|
if (tracks[cur_idx].codecsize)
|
|
|
|
tracks[cur_idx].has_codec = true;
|
|
|
|
}
|
|
|
|
if (taginfo_ready)
|
|
|
|
{
|
|
|
|
if (!audio_buffer_wind_backward(track_ridx, old_track_ridx))
|
|
|
|
audio_rebuffer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->taginfo_ready = false;
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_rebuffer();
|
|
|
|
}
|
2006-01-20 22:02:44 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
skip_done:
|
|
|
|
audio_update_trackinfo();
|
|
|
|
LOGFQUEUE("audio > codec Q_CODEC_REQUEST_COMPLETE");
|
|
|
|
queue_post(&codec_callback_queue, Q_CODEC_REQUEST_COMPLETE, 0);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_rebuffer_and_seek(size_t newpos)
|
2005-07-20 01:58:27 +00:00
|
|
|
{
|
2006-10-22 21:56:42 +00:00
|
|
|
size_t real_preseek;
|
2006-09-01 07:59:31 +00:00
|
|
|
int fd;
|
|
|
|
char *trackname;
|
2005-07-20 01:58:27 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* (Re-)open current track's file handle. */
|
2006-10-15 11:03:26 +00:00
|
|
|
trackname = playlist_peek(0);
|
2006-09-01 07:59:31 +00:00
|
|
|
fd = open(trackname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("audio > codec Q_CODEC_REQUEST_FAILED");
|
|
|
|
queue_post(&codec_callback_queue, Q_CODEC_REQUEST_FAILED, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_fd >= 0)
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = fd;
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
playlist_end = false;
|
|
|
|
|
|
|
|
ci.curpos = newpos;
|
|
|
|
|
|
|
|
/* Clear codec buffer. */
|
|
|
|
track_widx = track_ridx;
|
|
|
|
tracks[track_widx].buf_idx = buf_widx = buf_ridx = 0;
|
|
|
|
|
|
|
|
last_peek_offset = 0;
|
|
|
|
filling = false;
|
|
|
|
audio_initialize_buffer_fill(true);
|
|
|
|
|
2006-10-23 16:47:48 +00:00
|
|
|
/* This may have been tweaked by the id3v1 code */
|
|
|
|
CUR_TI->filesize=filesize(fd);
|
2006-10-17 12:56:22 +00:00
|
|
|
if (newpos > conf_preseek)
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->start_pos = newpos - conf_preseek;
|
2006-10-23 16:47:48 +00:00
|
|
|
lseek(current_fd, CUR_TI->start_pos, SEEK_SET);
|
2006-10-17 12:56:22 +00:00
|
|
|
CUR_TI->filerem = CUR_TI->filesize - CUR_TI->start_pos;
|
2006-10-22 21:56:42 +00:00
|
|
|
real_preseek = conf_preseek;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->start_pos = 0;
|
2006-10-17 12:56:22 +00:00
|
|
|
CUR_TI->filerem = CUR_TI->filesize;
|
2006-10-22 21:56:42 +00:00
|
|
|
real_preseek = newpos;
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
2005-11-17 19:31:29 +00:00
|
|
|
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->available = 0;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-10-22 21:56:42 +00:00
|
|
|
audio_read_file(real_preseek);
|
2006-10-17 12:56:22 +00:00
|
|
|
|
|
|
|
/* Account for the data we just read that is 'behind' us now */
|
2006-10-22 21:56:42 +00:00
|
|
|
CUR_TI->available -= real_preseek;
|
2006-10-17 12:56:22 +00:00
|
|
|
|
2006-10-22 21:56:42 +00:00
|
|
|
buf_ridx = RINGBUF_ADD(buf_ridx, real_preseek);
|
2006-10-17 12:56:22 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
LOGFQUEUE("audio > codec Q_CODEC_REQUEST_COMPLETE");
|
|
|
|
queue_post(&codec_callback_queue, Q_CODEC_REQUEST_COMPLETE, 0);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_set_track_buffer_event(void (*handler)(struct mp3entry *id3,
|
|
|
|
bool last_track))
|
2005-11-02 22:32:04 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
track_buffer_callback = handler;
|
2005-11-02 22:32:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_set_track_unbuffer_event(void (*handler)(struct mp3entry *id3,
|
|
|
|
bool last_track))
|
2005-11-02 22:32:04 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
track_unbuffer_callback = handler;
|
2005-11-02 22:32:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
void audio_set_track_changed_event(void (*handler)(struct mp3entry *id3))
|
2006-04-09 16:12:12 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
track_changed_callback = handler;
|
2006-04-06 04:07:06 +00:00
|
|
|
}
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
unsigned long audio_prev_elapsed(void)
|
|
|
|
{
|
|
|
|
return prev_track_elapsed;
|
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_stop_codec_flush(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.stop_codec = true;
|
|
|
|
pcmbuf_pause(true);
|
|
|
|
while (audio_codec_loaded)
|
|
|
|
yield();
|
|
|
|
/* If the audio codec is not loaded any more, and the audio is still
|
|
|
|
* playing, it is now and _only_ now safe to call this function from the
|
|
|
|
* audio thread */
|
|
|
|
if (pcm_is_playing())
|
|
|
|
pcmbuf_play_stop();
|
|
|
|
pcmbuf_pause(paused);
|
2006-01-21 22:58:08 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_stop_playback(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If we were playing, save resume information */
|
|
|
|
if (playing)
|
|
|
|
{
|
|
|
|
/* Save the current playing spot, or NULL if the playlist has ended */
|
|
|
|
playlist_update_resume_info(
|
|
|
|
(playlist_end && ci.stop_codec)?NULL:audio_current_track());
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
filling = false;
|
|
|
|
paused = false;
|
|
|
|
audio_stop_codec_flush();
|
2006-10-13 13:08:05 +00:00
|
|
|
playing = false;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
if (current_fd >= 0)
|
|
|
|
{
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark all entries null. */
|
2006-10-17 12:56:22 +00:00
|
|
|
audio_clear_track_entries(false);
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_play_start(size_t offset)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
#if defined(HAVE_RECORDING) || defined(CONFIG_TUNER)
|
|
|
|
rec_set_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
|
|
|
|
#endif
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Wait for any previously playing audio to flush - TODO: Not necessary? */
|
2006-09-17 08:34:42 +00:00
|
|
|
audio_stop_codec_flush();
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
track_changed = true;
|
|
|
|
playlist_end = false;
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
playing = true;
|
|
|
|
ci.new_track = 0;
|
|
|
|
ci.seek_time = 0;
|
2006-09-17 08:34:42 +00:00
|
|
|
wps_offset = 0;
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
if (current_fd >= 0)
|
|
|
|
{
|
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
sound_set_volume(global_settings.volume);
|
|
|
|
track_widx = track_ridx = 0;
|
|
|
|
buf_ridx = buf_widx = 0;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Mark all entries null. */
|
|
|
|
memset(tracks, 0, sizeof(struct track_info) * MAX_TRACK);
|
|
|
|
|
|
|
|
last_peek_offset = -1;
|
|
|
|
|
|
|
|
audio_fill_file_buffer(true, false, offset);
|
2005-06-06 00:35:21 +00:00
|
|
|
}
|
|
|
|
|
2005-06-07 06:34:54 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Invalidates all but currently playing track. */
|
2006-10-25 06:19:27 +00:00
|
|
|
static void audio_invalidate_tracks(void)
|
2005-06-08 10:33:01 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
if (audio_have_tracks()) {
|
|
|
|
last_peek_offset = 0;
|
2005-06-08 10:33:01 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
playlist_end = false;
|
|
|
|
track_widx = track_ridx;
|
2006-10-17 12:56:22 +00:00
|
|
|
/* Mark all other entries null (also buffered wrong metadata). */
|
|
|
|
audio_clear_track_entries(true);
|
2005-06-08 10:33:01 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* If the current track is fully buffered, advance the write pointer */
|
|
|
|
if (tracks[track_widx].filerem == 0)
|
|
|
|
track_widx = (track_widx + 1) & MAX_TRACK_MASK;
|
2005-06-08 10:33:01 +00:00
|
|
|
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_widx = RINGBUF_ADD(buf_ridx, CUR_TI->available);
|
2005-06-08 10:33:01 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_read_next_metadata();
|
2006-04-02 20:19:00 +00:00
|
|
|
}
|
2005-06-08 10:33:01 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_new_playlist(void)
|
2005-06-07 06:34:54 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Prepare to start a new fill from the beginning of the playlist */
|
|
|
|
last_peek_offset = -1;
|
|
|
|
if (audio_have_tracks()) {
|
|
|
|
playlist_end = false;
|
|
|
|
track_widx = track_ridx;
|
2006-10-17 12:56:22 +00:00
|
|
|
audio_clear_track_entries(true);
|
2005-06-07 06:34:54 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
track_widx++;
|
|
|
|
track_widx &= MAX_TRACK_MASK;
|
2006-04-11 20:41:04 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Stop reading the current track */
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->filerem = 0;
|
2006-09-01 07:59:31 +00:00
|
|
|
close(current_fd);
|
|
|
|
current_fd = -1;
|
2005-06-07 06:34:54 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Mark the current track as invalid to prevent skipping back to it */
|
2006-10-12 15:54:36 +00:00
|
|
|
CUR_TI->taginfo_ready = false;
|
2006-09-01 07:59:31 +00:00
|
|
|
|
|
|
|
/* Invalidate the buffer other than the playing track */
|
2006-10-13 09:16:36 +00:00
|
|
|
buf_widx = RINGBUF_ADD(buf_ridx, CUR_TI->available);
|
2006-09-01 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Signal the codec to initiate a track change forward */
|
|
|
|
new_playlist = true;
|
|
|
|
ci.new_track = 1;
|
|
|
|
audio_fill_file_buffer(false, true, 0);
|
2006-04-22 14:40:13 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_initiate_track_change(long direction)
|
2006-04-22 14:40:13 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
playlist_end = false;
|
|
|
|
ci.new_track += direction;
|
|
|
|
wps_offset -= direction;
|
2005-08-20 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_initiate_dir_change(long direction)
|
2005-06-06 00:35:21 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
playlist_end = false;
|
|
|
|
dir_skip = true;
|
|
|
|
ci.new_track = direction;
|
2005-07-21 11:44:00 +00:00
|
|
|
}
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/*
|
|
|
|
* Layout audio buffer as follows:
|
|
|
|
* [|TALK]|MALLOC|FILE|GUARD|PCM|AUDIOCODEC|[VOICECODEC|]
|
|
|
|
*/
|
|
|
|
static void audio_reset_buffer(size_t pcmbufsize)
|
2005-07-21 11:44:00 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
/* see audio_get_recording_buffer if this is modified */
|
2006-09-01 07:59:31 +00:00
|
|
|
size_t offset;
|
2005-07-21 11:44:00 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
logf("audio_reset_buffer");
|
|
|
|
logf(" size:%08X", pcmbufsize);
|
|
|
|
|
|
|
|
/* Initially set up file buffer as all space available */
|
|
|
|
filebuf = audiobuf + MALLOC_BUFSIZE + talk_get_bufsize();
|
|
|
|
filebuflen = audiobufend - filebuf;
|
2005-10-16 08:01:02 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* Allow for codec(s) at end of audio buffer */
|
2006-09-01 07:59:31 +00:00
|
|
|
if (talk_voice_required())
|
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2006-11-18 02:18:29 +00:00
|
|
|
#ifdef IRAM_STEAL
|
|
|
|
filebuflen -= CODEC_IRAM_SIZE + 2*CODEC_SIZE;
|
|
|
|
#else
|
|
|
|
filebuflen -= 2*(CODEC_IRAM_SIZE + CODEC_SIZE);
|
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
/* Allow 2 codecs at end of audio buffer */
|
2006-11-18 02:18:29 +00:00
|
|
|
/* If using IRAM for plugins voice IRAM swap buffer must be dedicated
|
|
|
|
and out of the way of buffer usage or else a call to audio_get_buffer
|
|
|
|
and subsequent buffer use might trash the swap space. A plugin
|
|
|
|
initializing IRAM after getting the full buffer would present similar
|
|
|
|
problem. Options include: failing the request if the other buffer
|
|
|
|
has been obtained already or never allowing use of the voice IRAM
|
|
|
|
buffer within the audio buffer. Using buffer_alloc basically
|
|
|
|
implements the second in a more convenient way. */
|
2006-11-06 18:07:30 +00:00
|
|
|
iram_buf[CODEC_IDX_AUDIO] = filebuf + filebuflen;
|
|
|
|
dram_buf[CODEC_IDX_AUDIO] = iram_buf[CODEC_IDX_AUDIO] + CODEC_IRAM_SIZE;
|
2006-11-18 02:18:29 +00:00
|
|
|
|
|
|
|
#ifdef IRAM_STEAL
|
|
|
|
/* Allocate voice IRAM swap buffer once */
|
|
|
|
if (iram_buf[CODEC_IDX_VOICE] == NULL)
|
|
|
|
iram_buf[CODEC_IDX_VOICE] = buffer_alloc(CODEC_IRAM_SIZE);
|
|
|
|
dram_buf[CODEC_IDX_VOICE] = dram_buf[CODEC_IDX_AUDIO] + CODEC_SIZE;
|
|
|
|
#else
|
2006-11-06 18:07:30 +00:00
|
|
|
iram_buf[CODEC_IDX_VOICE] = dram_buf[CODEC_IDX_AUDIO] + CODEC_SIZE;
|
|
|
|
dram_buf[CODEC_IDX_VOICE] = iram_buf[CODEC_IDX_VOICE] + CODEC_IRAM_SIZE;
|
2006-11-18 02:18:29 +00:00
|
|
|
#endif /* IRAM_STEAL */
|
|
|
|
|
|
|
|
#endif /* PLAYBACK_VOICE */
|
2006-04-17 17:05:05 +00:00
|
|
|
}
|
2006-09-01 07:59:31 +00:00
|
|
|
else
|
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
|
|
|
/* Allow for 1 codec at end of audio buffer */
|
2006-09-01 07:59:31 +00:00
|
|
|
filebuflen -= CODEC_IRAM_SIZE + CODEC_SIZE;
|
2006-04-17 17:05:05 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
iram_buf[CODEC_IDX_AUDIO] = filebuf + filebuflen;
|
|
|
|
dram_buf[CODEC_IDX_AUDIO] = iram_buf[CODEC_IDX_AUDIO] + CODEC_IRAM_SIZE;
|
|
|
|
iram_buf[CODEC_IDX_VOICE] = NULL;
|
|
|
|
dram_buf[CODEC_IDX_VOICE] = NULL;
|
2006-11-01 19:19:36 +00:00
|
|
|
#endif
|
2005-07-22 16:57:08 +00:00
|
|
|
}
|
2005-06-06 00:35:21 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
filebuflen -= pcmbuf_init(pcmbufsize, filebuf + filebuflen) + GUARD_BUFSIZE;
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
/* Ensure that file buffer is aligned */
|
2006-11-06 18:07:30 +00:00
|
|
|
offset = -(size_t)filebuf & 3;
|
2006-09-01 07:59:31 +00:00
|
|
|
filebuf += offset;
|
|
|
|
filebuflen -= offset;
|
|
|
|
filebuflen &= ~3;
|
2006-11-01 19:19:36 +00:00
|
|
|
|
|
|
|
/* Clear any references to the file buffer */
|
2006-11-06 18:07:30 +00:00
|
|
|
buffer_state = BUFFER_STATE_NORMAL;
|
2005-06-06 00:35:21 +00:00
|
|
|
}
|
|
|
|
|
2006-05-25 21:08:38 +00:00
|
|
|
#ifdef ROCKBOX_HAS_LOGF
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_test_track_changed_event(struct mp3entry *id3)
|
2006-04-17 19:59:16 +00:00
|
|
|
{
|
|
|
|
(void)id3;
|
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
logf("tce:%s", id3->path);
|
2006-04-17 19:59:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_playback_init(void)
|
2005-06-05 23:05:10 +00:00
|
|
|
{
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2005-08-20 11:13:19 +00:00
|
|
|
static bool voicetagtrue = true;
|
2006-09-17 08:34:42 +00:00
|
|
|
static struct mp3entry id3_voice;
|
|
|
|
#endif
|
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
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
|
|
|
rec_set_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
|
2005-12-20 21:49:47 +00:00
|
|
|
#endif
|
2006-01-27 11:39:46 +00:00
|
|
|
|
2006-05-25 21:08:38 +00:00
|
|
|
#ifdef ROCKBOX_HAS_LOGF
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_set_track_changed_event(audio_test_track_changed_event);
|
2006-04-17 19:59:16 +00:00
|
|
|
#endif
|
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;
|
2006-04-22 14:40:13 +00:00
|
|
|
ci.pcmbuf_insert = codec_pcmbuf_insert_callback;
|
2005-07-13 12:48:22 +00:00
|
|
|
ci.pcmbuf_insert_split = codec_pcmbuf_insert_split_callback;
|
2006-09-01 07:59:31 +00:00
|
|
|
ci.get_codec_memory = codec_get_memory_callback;
|
2005-06-05 23:05:10 +00:00
|
|
|
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;
|
2006-04-11 03:55:58 +00:00
|
|
|
ci.discard_codec = codec_discard_codec_callback;
|
2005-07-01 18:22:04 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
/* Initialize voice codec api. */
|
2006-09-17 08:34:42 +00:00
|
|
|
#ifdef PLAYBACK_VOICE
|
2005-08-20 11:13:19 +00:00
|
|
|
memcpy(&ci_voice, &ci, sizeof(struct codec_api));
|
|
|
|
memset(&id3_voice, 0, sizeof(struct mp3entry));
|
2006-04-22 14:40:13 +00:00
|
|
|
ci_voice.read_filebuf = voice_filebuf_callback;
|
|
|
|
ci_voice.pcmbuf_insert = voice_pcmbuf_insert_callback;
|
|
|
|
ci_voice.pcmbuf_insert_split = voice_pcmbuf_insert_split_callback;
|
2006-09-01 07:59:31 +00:00
|
|
|
ci_voice.get_codec_memory = voice_get_memory_callback;
|
2006-04-22 14:40:13 +00:00
|
|
|
ci_voice.request_buffer = voice_request_buffer_callback;
|
|
|
|
ci_voice.advance_buffer = voice_advance_buffer_callback;
|
|
|
|
ci_voice.advance_buffer_loc = voice_advance_buffer_loc_callback;
|
|
|
|
ci_voice.request_next_track = voice_request_next_track_callback;
|
|
|
|
ci_voice.mp3_get_filepos = voice_mp3_get_filepos_callback;
|
|
|
|
ci_voice.seek_buffer = voice_seek_buffer_callback;
|
|
|
|
ci_voice.seek_complete = voice_do_nothing;
|
|
|
|
ci_voice.set_elapsed = voice_set_elapsed_callback;
|
|
|
|
ci_voice.set_offset = voice_set_offset_callback;
|
|
|
|
ci_voice.discard_codec = voice_do_nothing;
|
2005-08-20 11:13:19 +00:00
|
|
|
ci_voice.taginfo_ready = &voicetagtrue;
|
|
|
|
ci_voice.id3 = &id3_voice;
|
|
|
|
id3_voice.frequency = 11200;
|
|
|
|
id3_voice.length = 1000000L;
|
2006-09-17 08:34:42 +00:00
|
|
|
#endif
|
2006-01-27 11:39:46 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
codec_thread_p = create_thread(codec_thread, codec_stack,
|
|
|
|
sizeof(codec_stack),
|
|
|
|
codec_thread_name IF_PRIO(, PRIORITY_PLAYBACK));
|
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-03-30 05:56:19 +00:00
|
|
|
|
2006-02-24 10:27:23 +00:00
|
|
|
#ifndef SIMULATOR
|
|
|
|
if (ev.id == SYS_USB_CONNECTED)
|
|
|
|
{
|
|
|
|
logf("USB: Audio preinit");
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&audio_queue);
|
|
|
|
}
|
|
|
|
#endif
|
2006-01-27 11:39:46 +00:00
|
|
|
}
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* initialize the buffer */
|
|
|
|
filebuf = audiobuf; /* must be non-NULL for audio_set_crossfade */
|
|
|
|
buffer_state = BUFFER_STATE_TRASHED; /* force it */
|
2005-10-16 12:05:58 +00:00
|
|
|
audio_set_crossfade(global_settings.crossfade);
|
2006-03-30 05:56:19 +00:00
|
|
|
|
2006-05-01 18:18:54 +00:00
|
|
|
audio_is_initialized = true;
|
|
|
|
|
2006-01-27 11:39:46 +00:00
|
|
|
sound_settings_apply();
|
2005-06-05 23:05:10 +00:00
|
|
|
}
|
2006-11-06 14:24:18 +00:00
|
|
|
#if MEM > 8
|
|
|
|
/* we dont want this rebuffering on targets with little ram
|
|
|
|
because the disk may never spin down */
|
|
|
|
bool ata_fillbuffer_callback(void)
|
|
|
|
{
|
|
|
|
queue_post(&audio_queue, Q_AUDIO_FILL_BUFFER_IF_ACTIVE_ATA, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2005-06-05 23:05:10 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
static void audio_thread(void)
|
2006-01-27 11:39:46 +00:00
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
struct event ev;
|
2006-11-06 14:24:18 +00:00
|
|
|
#if MEM > 8
|
|
|
|
size_t high_watermark;
|
|
|
|
#endif
|
2006-09-01 07:59:31 +00:00
|
|
|
/* At first initialize audio system in background. */
|
|
|
|
audio_playback_init();
|
2006-11-06 14:24:18 +00:00
|
|
|
#if MEM > 8
|
|
|
|
high_watermark = (3*filebuflen)/4;
|
|
|
|
#endif
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2006-09-01 07:59:31 +00:00
|
|
|
if (filling)
|
|
|
|
{
|
|
|
|
queue_wait_w_tmo(&audio_queue, &ev, 0);
|
|
|
|
if (ev.id == SYS_TIMEOUT)
|
|
|
|
ev.id = Q_AUDIO_FILL_BUFFER;
|
|
|
|
}
|
|
|
|
else
|
2006-11-06 14:24:18 +00:00
|
|
|
{
|
2006-09-16 16:18:11 +00:00
|
|
|
queue_wait_w_tmo(&audio_queue, &ev, HZ/2);
|
2006-12-06 16:21:04 +00:00
|
|
|
#if MEM > 8
|
2006-12-06 12:11:57 +00:00
|
|
|
if (playing && (ev.id == SYS_TIMEOUT) &&
|
2006-11-06 14:24:18 +00:00
|
|
|
(FILEBUFUSED < high_watermark))
|
|
|
|
register_ata_idle_func(ata_fillbuffer_callback);
|
|
|
|
#endif
|
2006-12-06 16:21:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
switch (ev.id) {
|
2006-11-06 14:24:18 +00:00
|
|
|
#if MEM > 8
|
|
|
|
case Q_AUDIO_FILL_BUFFER_IF_ACTIVE_ATA:
|
|
|
|
/* only fill if the disk is still spining */
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
if (!ata_disk_is_active())
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#endif /* MEM > 8 */
|
|
|
|
/* else fall through to Q_AUDIO_FILL_BUFFER */
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_AUDIO_FILL_BUFFER:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_FILL_BUFFER");
|
|
|
|
if (!filling)
|
|
|
|
if (!playing || playlist_end || ci.stop_codec)
|
|
|
|
break;
|
|
|
|
audio_fill_file_buffer(false, false, 0);
|
|
|
|
break;
|
2006-01-27 11:39:46 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_AUDIO_PLAY:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_PLAY");
|
2006-10-17 12:56:22 +00:00
|
|
|
audio_clear_track_entries(false);
|
2006-09-01 07:59:31 +00:00
|
|
|
audio_play_start((size_t)ev.data);
|
|
|
|
break ;
|
2006-01-27 11:39:46 +00:00
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
case Q_AUDIO_STOP:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_STOP");
|
|
|
|
audio_stop_playback();
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case Q_AUDIO_PAUSE:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_PAUSE");
|
|
|
|
pcmbuf_pause((bool)ev.data);
|
|
|
|
paused = (bool)ev.data;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case Q_AUDIO_SKIP:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_SKIP");
|
|
|
|
audio_initiate_track_change((long)ev.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_AUDIO_PRE_FF_REWIND:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_PRE_FF_REWIND");
|
|
|
|
if (!playing)
|
|
|
|
break;
|
|
|
|
pcmbuf_pause(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_AUDIO_FF_REWIND:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_FF_REWIND");
|
|
|
|
if (!playing)
|
|
|
|
break ;
|
|
|
|
ci.seek_time = (long)ev.data+1;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case Q_AUDIO_REBUFFER_SEEK:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_REBUFFER_SEEK");
|
|
|
|
audio_rebuffer_and_seek((size_t)ev.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_AUDIO_CHECK_NEW_TRACK:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_CHECK_NEW_TRACK");
|
|
|
|
audio_check_new_track();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_AUDIO_DIR_SKIP:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_DIR_SKIP");
|
|
|
|
playlist_end = false;
|
|
|
|
audio_initiate_dir_change((long)ev.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_AUDIO_NEW_PLAYLIST:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_NEW_PLAYLIST");
|
|
|
|
audio_new_playlist();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_AUDIO_FLUSH:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_FLUSH");
|
|
|
|
audio_invalidate_tracks();
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case Q_AUDIO_TRACK_CHANGED:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_TRACK_CHANGED");
|
|
|
|
if (track_changed_callback)
|
2006-10-12 15:54:36 +00:00
|
|
|
track_changed_callback(&CUR_TI->id3);
|
2006-09-01 07:59:31 +00:00
|
|
|
track_changed = true;
|
|
|
|
playlist_update_resume_info(audio_current_track());
|
|
|
|
break ;
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef AUDIO_HAVE_RECORDING
|
|
|
|
case Q_AUDIO_LOAD_ENCODER:
|
|
|
|
LOGFQUEUE("audio < Q_AUDIO_LOAD_ENCODER");
|
|
|
|
LOGFQUEUE("audio > codec Q_ENCODER_LOAD_DISK");
|
|
|
|
queue_post(&codec_queue, Q_ENCODER_LOAD_DISK, ev.data);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2006-09-01 07:59:31 +00:00
|
|
|
#ifndef SIMULATOR
|
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
LOGFQUEUE("audio < SYS_USB_CONNECTED");
|
|
|
|
audio_stop_playback();
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&audio_queue);
|
|
|
|
break ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case SYS_TIMEOUT:
|
2006-11-06 18:07:30 +00:00
|
|
|
LOGFQUEUE_SYS_TIMEOUT("audio < SYS_TIMEOUT");
|
2006-09-01 07:59:31 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOGFQUEUE("audio < default");
|
|
|
|
}
|
|
|
|
}
|
2006-01-27 11:39:46 +00:00
|
|
|
}
|
2005-06-05 23:05:10 +00:00
|
|
|
|