1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005-2007 Miika Pekkarinen
11 * Copyright (C) 2007-2008 Nicolas Pennequin
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 /* TODO: Pause should be handled in here, rather than PCMBUF so that voice can
24 * play whilst audio is paused */
46 #include "buffering.h"
47 #include "appevents.h"
48 #include "voice_thread.h"
49 #include "mp3_playback.h"
63 #ifdef HAVE_LCD_BITMAP
65 #include "peakmeter.h"
75 #include "ata_idle_notify.h"
78 #include "recording.h"
79 #include "pcm_record.h"
82 #ifdef IPOD_ACCESSORY_PROTOCOL
86 #define PLAYBACK_VOICE
88 /* amount of guess-space to allow for codecs that must hunt and peck
89 * for their correct seeek target, 32k seems a good size */
90 #define AUDIO_REBUFFER_GUESS_SIZE (1024*32)
92 /* Define LOGF_ENABLE to enable logf output in this file */
93 /*#define LOGF_ENABLE*/
96 /* macros to enable logf for queues
97 logging on SYS_TIMEOUT can be disabled */
99 /* Define this for logf output of all queuing except SYS_TIMEOUT */
100 #define PLAYBACK_LOGQUEUES
101 /* Define this to logf SYS_TIMEOUT messages */
102 /*#define PLAYBACK_LOGQUEUES_SYS_TIMEOUT*/
105 #ifdef PLAYBACK_LOGQUEUES
106 #define LOGFQUEUE logf
108 #define LOGFQUEUE(...)
111 #ifdef PLAYBACK_LOGQUEUES_SYS_TIMEOUT
112 #define LOGFQUEUE_SYS_TIMEOUT logf
114 #define LOGFQUEUE_SYS_TIMEOUT(...)
118 /* Define one constant that includes recording related functionality */
119 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
120 #define AUDIO_HAVE_RECORDING
129 Q_AUDIO_PRE_FF_REWIND
,
131 Q_AUDIO_CHECK_NEW_TRACK
,
133 Q_AUDIO_TRACK_CHANGED
,
138 Q_CODEC_REQUEST_COMPLETE
,
139 Q_CODEC_REQUEST_FAILED
,
144 #ifdef AUDIO_HAVE_RECORDING
153 STATE_IDLE
, /* audio is stopped: nothing to do */
154 STATE_FILLING
, /* adding tracks to the buffer */
155 STATE_FULL
, /* can't add any more tracks */
156 STATE_END_OF_PLAYLIST
, /* all remaining tracks have been added */
157 STATE_FINISHED
, /* all remaining tracks are fully buffered */
160 #define MAX_TRACK 128
162 #define MAX_TRACK_MASK (MAX_TRACK-1)
164 /* As defined in plugins/lib/xxx2wav.h */
165 #define GUARD_BUFSIZE (32*1024)
167 bool audio_is_initialized
= false;
168 static bool audio_thread_ready SHAREDBSS_ATTR
= false;
170 /* Variables are commented with the threads that use them: *
171 * A=audio, C=codec, V=voice. A suffix of - indicates that *
172 * the variable is read but not updated on that thread. */
173 /* TBD: Split out "audio" and "playback" (ie. calling) threads */
175 /* Main state control */
176 static volatile bool audio_codec_loaded SHAREDBSS_ATTR
= false; /* Codec loaded? (C/A-) */
177 static volatile bool playing SHAREDBSS_ATTR
= false; /* Is audio playing? (A) */
178 static volatile bool paused SHAREDBSS_ATTR
= false; /* Is audio paused? (A/C-) */
180 /* Ring buffer where compressed audio and codecs are loaded */
181 static unsigned char *filebuf
= NULL
; /* Start of buffer (A/C-) */
182 static unsigned char *malloc_buf
= NULL
; /* Start of malloc buffer (A/C-) */
183 /* FIXME: make filebuflen static */
184 size_t filebuflen
= 0; /* Size of buffer (A/C-) */
185 /* FIXME: make buf_ridx (C/A-) */
187 /* Possible arrangements of the buffer */
188 static int buffer_state
= AUDIOBUF_STATE_TRASHED
; /* Buffer state */
190 /* These are used to store the current and next (or prev if the current is the last)
191 * mp3entry's in a round-robin system. This guarentees that the pointer returned
192 * by audio_current/next_track will be valid for the full duration of the
193 * currently playing track */
194 static struct mp3entry mp3entry_buf
[2];
195 static struct mp3entry
*thistrack_id3
, /* the currently playing track */
196 *othertrack_id3
; /* prev track during track-change-transition, or end of playlist,
197 * next track otherwise */
198 static struct mp3entry unbuffered_id3
; /* the id3 for the first unbuffered track */
200 /* for cuesheet support */
201 static struct cuesheet
*curr_cue
= NULL
;
203 /* Track info structure about songs in the file buffer (A/C-) */
205 int audio_hid
; /* The ID for the track's buffer handle */
206 int id3_hid
; /* The ID for the track's metadata handle */
207 int codec_hid
; /* The ID for the track's codec handle */
209 int aa_hid
; /* The ID for the track's album art handle */
211 int cuesheet_hid
; /* The ID for the track's parsed cueesheet handle */
213 size_t filesize
; /* File total length */
215 bool taginfo_ready
; /* Is metadata read */
218 static struct track_info tracks
[MAX_TRACK
];
219 static volatile int track_ridx
= 0; /* Track being decoded (A/C-) */
220 static int track_widx
= 0; /* Track being buffered (A) */
222 #define CUR_TI (&tracks[track_ridx]) /* Playing track info pointer (A/C-) */
223 static struct track_info
*prev_ti
= NULL
; /* Pointer to the previously played
226 /* Information used only for filling the buffer */
227 /* Playlist steps from playing track to next track to be buffered (A) */
228 static int last_peek_offset
= 0;
230 /* Scrobbler support */
231 static unsigned long prev_track_elapsed
= 0; /* Previous track elapsed time (C/A-)*/
233 static enum filling_state filling
;
235 /* Track change controls */
236 static bool automatic_skip
= false; /* Who initiated in-progress skip? (C/A-) */
237 static bool dir_skip
= false; /* Is a directory skip pending? (A) */
238 static bool new_playlist
= false; /* Are we starting a new playlist? (A) */
239 static int wps_offset
= 0; /* Pending track change offset, to keep WPS responsive (A) */
240 static bool skipped_during_pause
= false; /* Do we need to clear the PCM buffer when playback resumes (A) */
242 static bool start_play_g
= false; /* Used by audio_load_track to notify
243 audio_finish_load_track about start_play */
245 /* True when a track load is in progress, i.e. audio_load_track() has returned
246 * but audio_finish_load_track() hasn't been called yet. Used to avoid allowing
247 * audio_load_track() to get called twice in a row, which would cause problems.
249 static bool track_load_started
= false;
251 /* Set to true if the codec thread should send an audio stop request
252 * (typically because the end of the playlist has been reached).
254 static bool codec_requested_stop
= false;
256 #ifdef HAVE_DISK_STORAGE
257 static size_t buffer_margin
= 5; /* Buffer margin aka anti-skip buffer (A/C-) */
260 /* Multiple threads */
261 /* Set the watermark to trigger buffer fill (A/C) */
262 static void set_filebuf_watermark(void);
265 static struct event_queue audio_queue SHAREDBSS_ATTR
;
266 static struct queue_sender_list audio_queue_sender_list SHAREDBSS_ATTR
;
267 static long audio_stack
[(DEFAULT_STACK_SIZE
+ 0x1000)/sizeof(long)];
268 static const char audio_thread_name
[] = "audio";
270 static void audio_thread(void);
271 static void audio_initiate_track_change(long direction
);
272 static bool audio_have_tracks(void);
273 static void audio_reset_buffer(void);
274 static void audio_stop_playback(void);
277 extern struct codec_api ci
;
278 static struct event_queue codec_queue SHAREDBSS_ATTR
;
279 static struct queue_sender_list codec_queue_sender_list
;
280 static long codec_stack
[(DEFAULT_STACK_SIZE
+ 0x2000)/sizeof(long)]
282 static const char codec_thread_name
[] = "codec";
283 unsigned int codec_thread_id
; /* For modifying thread priority later. */
285 /* PCM buffer messaging */
286 static struct event_queue pcmbuf_queue SHAREDBSS_ATTR
;
288 /* Function to be called by pcm buffer callbacks.
289 * Permissible Context(s): Audio interrupt
291 static void pcmbuf_callback_queue_post(long id
, intptr_t data
)
293 /* No lock since we're already in audio interrupt context */
294 queue_post(&pcmbuf_queue
, id
, data
);
297 /* Scan the pcmbuf queue and return true if a message pulled.
298 * Permissible Context(s): Thread
300 static bool pcmbuf_queue_scan(struct queue_event
*ev
)
302 if (!queue_empty(&pcmbuf_queue
))
304 /* Transfer message to audio queue */
306 /* Pull message - never, ever any blocking call! */
307 queue_wait_w_tmo(&pcmbuf_queue
, ev
, 0);
315 /* Clear the pcmbuf queue of messages
316 * Permissible Context(s): Thread
318 static void pcmbuf_queue_clear(void)
321 queue_clear(&pcmbuf_queue
);
325 /* --- Helper functions --- */
327 static struct mp3entry
*bufgetid3(int handle_id
)
332 struct mp3entry
*id3
;
333 ssize_t ret
= bufgetdata(handle_id
, 0, (void *)&id3
);
335 if (ret
< 0 || ret
!= sizeof(struct mp3entry
))
341 static bool clear_track_info(struct track_info
*track
)
343 /* bufclose returns true if the handle is not found, or if it is closed
344 * successfully, so these checks are safe on non-existant handles */
348 if (track
->codec_hid
>= 0) {
349 if (bufclose(track
->codec_hid
))
350 track
->codec_hid
= -1;
355 if (track
->id3_hid
>= 0) {
356 if (bufclose(track
->id3_hid
))
362 if (track
->audio_hid
>= 0) {
363 if (bufclose(track
->audio_hid
))
364 track
->audio_hid
= -1;
370 if (track
->aa_hid
>= 0) {
371 if (bufclose(track
->aa_hid
))
378 if (track
->cuesheet_hid
>= 0) {
379 if (bufclose(track
->cuesheet_hid
))
380 track
->cuesheet_hid
= -1;
386 track
->taginfo_ready
= false;
391 /* --- External interfaces --- */
393 /* This sends a stop message and the audio thread will dump all it's
394 subsequenct messages */
395 void audio_hard_stop(void)
398 LOGFQUEUE("audio >| audio Q_AUDIO_STOP: 1");
399 queue_send(&audio_queue
, Q_AUDIO_STOP
, 1);
400 #ifdef PLAYBACK_VOICE
405 bool audio_restore_playback(int type
)
409 case AUDIO_WANT_PLAYBACK
:
410 if (buffer_state
!= AUDIOBUF_STATE_INITIALIZED
)
411 audio_reset_buffer();
413 case AUDIO_WANT_VOICE
:
414 if (buffer_state
== AUDIOBUF_STATE_TRASHED
)
415 audio_reset_buffer();
422 unsigned char *audio_get_buffer(bool talk_buf
, size_t *buffer_size
)
424 unsigned char *buf
, *end
;
426 if (audio_is_initialized
)
430 /* else buffer_state will be AUDIOBUF_STATE_TRASHED at this point */
432 /* Reset the buffering thread so that it doesn't try to use the data */
433 buffering_reset(filebuf
, filebuflen
);
435 if (buffer_size
== NULL
)
437 /* Special case for talk_init to use since it already knows it's
439 buffer_state
= AUDIOBUF_STATE_TRASHED
;
443 if (talk_buf
|| buffer_state
== AUDIOBUF_STATE_TRASHED
444 || !talk_voice_required())
446 logf("get buffer: talk, audio");
447 /* Ok to use everything from audiobuf to audiobufend - voice is loaded,
448 the talk buffer is not needed because voice isn't being used, or
449 could be AUDIOBUF_STATE_TRASHED already. If state is
450 AUDIOBUF_STATE_VOICED_ONLY, no problem as long as memory isn't written
451 without the caller knowing what's going on. Changing certain settings
452 may move it to a worse condition but the memory in use by something
453 else will remain undisturbed.
455 if (buffer_state
!= AUDIOBUF_STATE_TRASHED
)
458 buffer_state
= AUDIOBUF_STATE_TRASHED
;
466 /* Safe to just return this if already AUDIOBUF_STATE_VOICED_ONLY or
467 still AUDIOBUF_STATE_INITIALIZED */
468 /* Skip talk buffer and move pcm buffer to end to maximize available
469 contiguous memory - no audio running means voice will not need the
471 logf("get buffer: audio");
472 buf
= audiobuf
+ talk_get_bufsize();
473 end
= audiobufend
- pcmbuf_init(audiobufend
);
474 buffer_state
= AUDIOBUF_STATE_VOICED_ONLY
;
477 *buffer_size
= end
- buf
;
482 int audio_buffer_state(void)
487 #ifdef HAVE_RECORDING
488 unsigned char *audio_get_recording_buffer(size_t *buffer_size
)
490 /* Stop audio, voice and obtain all available buffer space */
494 unsigned char *end
= audiobufend
;
495 buffer_state
= AUDIOBUF_STATE_TRASHED
;
496 *buffer_size
= end
- audiobuf
;
498 return (unsigned char *)audiobuf
;
501 bool audio_load_encoder(int afmt
)
504 const char *enc_fn
= get_codec_filename(afmt
| CODEC_TYPE_ENCODER
);
508 audio_remove_encoder();
509 ci
.enc_codec_loaded
= 0; /* clear any previous error condition */
511 LOGFQUEUE("codec > Q_ENCODER_LOAD_DISK");
512 queue_post(&codec_queue
, Q_ENCODER_LOAD_DISK
, (intptr_t)enc_fn
);
514 while (ci
.enc_codec_loaded
== 0)
517 logf("codec loaded: %d", ci
.enc_codec_loaded
);
519 return ci
.enc_codec_loaded
> 0;
524 } /* audio_load_encoder */
526 void audio_remove_encoder(void)
529 /* force encoder codec unload (if currently loaded) */
530 if (ci
.enc_codec_loaded
<= 0)
533 ci
.stop_encoder
= true;
534 while (ci
.enc_codec_loaded
> 0)
537 } /* audio_remove_encoder */
539 #endif /* HAVE_RECORDING */
542 int audio_current_aa_hid(void)
545 int offset
= ci
.new_track
+ wps_offset
;
547 cur_idx
= track_ridx
+ offset
;
548 cur_idx
&= MAX_TRACK_MASK
;
550 return tracks
[cur_idx
].aa_hid
;
554 struct mp3entry
* audio_current_track(void)
556 const char *filename
;
557 struct playlist_track_info trackinfo
;
559 int offset
= ci
.new_track
+ wps_offset
;
560 struct mp3entry
*write_id3
;
562 cur_idx
= (track_ridx
+ offset
) & MAX_TRACK_MASK
;
564 if (cur_idx
== track_ridx
&& *thistrack_id3
->path
)
567 if (tracks
[cur_idx
].cuesheet_hid
>= 0 && !thistrack_id3
->cuesheet
)
569 bufread(tracks
[cur_idx
].cuesheet_hid
, sizeof(struct cuesheet
), curr_cue
);
570 thistrack_id3
->cuesheet
= curr_cue
;
571 cue_spoof_id3(thistrack_id3
->cuesheet
, thistrack_id3
);
573 return thistrack_id3
;
575 else if (automatic_skip
&& offset
== -1 && *othertrack_id3
->path
)
577 /* We're in a track transition. The codec has moved on to the next track,
578 but the audio being played is still the same (now previous) track.
579 othertrack_id3.elapsed is being updated in an ISR by
580 codec_pcmbuf_position_callback */
581 if (tracks
[cur_idx
].cuesheet_hid
>= 0 && !thistrack_id3
->cuesheet
)
583 bufread(tracks
[cur_idx
].cuesheet_hid
, sizeof(struct cuesheet
), curr_cue
);
584 othertrack_id3
->cuesheet
= curr_cue
;
585 cue_spoof_id3(othertrack_id3
->cuesheet
, othertrack_id3
);
587 return othertrack_id3
;
592 /* Codec may be using thistrack_id3, so it must not be overwritten.
593 If this is a manual skip, othertrack_id3 will become
594 thistrack_id3 in audio_check_new_track().
595 FIXME: If this is an automatic skip, it probably means multiple
596 short tracks fit in the PCM buffer. Overwriting othertrack_id3
597 can lead to an incorrect value later.
598 Note that othertrack_id3 may also be used for next track.
600 write_id3
= othertrack_id3
;
604 write_id3
= thistrack_id3
;
607 if (tracks
[cur_idx
].id3_hid
>= 0)
609 /* The current track's info has been buffered but not read yet, so get it */
610 if (bufread(tracks
[cur_idx
].id3_hid
, sizeof(struct mp3entry
), write_id3
)
611 == sizeof(struct mp3entry
))
615 /* We didn't find the ID3 metadata, so we fill temp_id3 with the little info
616 we have and return that. */
618 memset(write_id3
, 0, sizeof(struct mp3entry
));
620 playlist_get_track_info(NULL
, playlist_next(0)+wps_offset
, &trackinfo
);
621 filename
= trackinfo
.filename
;
623 filename
= "No file!";
625 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
626 if (tagcache_fill_tags(write_id3
, filename
))
630 strlcpy(write_id3
->path
, filename
, sizeof(write_id3
->path
));
631 write_id3
->title
= strrchr(write_id3
->path
, '/');
632 if (!write_id3
->title
)
633 write_id3
->title
= &write_id3
->path
[0];
640 struct mp3entry
* audio_next_track(void)
643 int offset
= ci
.new_track
+ wps_offset
;
645 if (!audio_have_tracks())
648 if (wps_offset
== -1 && *thistrack_id3
->path
)
650 /* We're in a track transition. The next track for the WPS is the one
651 currently being decoded. */
652 return thistrack_id3
;
655 next_idx
= (track_ridx
+ offset
+ 1) & MAX_TRACK_MASK
;
657 if (tracks
[next_idx
].id3_hid
>= 0)
659 if (bufread(tracks
[next_idx
].id3_hid
, sizeof(struct mp3entry
), othertrack_id3
)
660 == sizeof(struct mp3entry
))
661 return othertrack_id3
;
666 if (next_idx
== track_widx
)
668 /* The next track hasn't been buffered yet, so we return the static
669 version of its metadata. */
670 return &unbuffered_id3
;
676 void audio_play(long offset
)
680 #ifdef PLAYBACK_VOICE
681 /* Truncate any existing voice output so we don't have spelling
682 * etc. over the first part of the played track */
687 LOGFQUEUE("audio >| audio Q_AUDIO_PLAY: %ld", offset
);
688 /* Don't return until playback has actually started */
689 queue_send(&audio_queue
, Q_AUDIO_PLAY
, offset
);
692 void audio_stop(void)
695 LOGFQUEUE("audio >| audio Q_AUDIO_STOP");
696 /* Don't return until playback has actually stopped */
697 queue_send(&audio_queue
, Q_AUDIO_STOP
, 0);
700 void audio_pause(void)
702 LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE");
703 /* Don't return until playback has actually paused */
704 queue_send(&audio_queue
, Q_AUDIO_PAUSE
, true);
707 void audio_resume(void)
709 LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE resume");
710 /* Don't return until playback has actually resumed */
711 queue_send(&audio_queue
, Q_AUDIO_PAUSE
, false);
714 void audio_skip(int direction
)
716 if (playlist_check(ci
.new_track
+ wps_offset
+ direction
))
718 if (global_settings
.beep
)
719 pcmbuf_beep(2000, 100, 2500*global_settings
.beep
);
721 LOGFQUEUE("audio > audio Q_AUDIO_SKIP %d", direction
);
722 queue_post(&audio_queue
, Q_AUDIO_SKIP
, direction
);
723 /* Update wps while our message travels inside deep playback queues. */
724 wps_offset
+= direction
;
728 /* No more tracks. */
729 if (global_settings
.beep
)
730 pcmbuf_beep(1000, 100, 1500*global_settings
.beep
);
734 void audio_next(void)
739 void audio_prev(void)
744 void audio_next_dir(void)
746 LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP 1");
747 queue_post(&audio_queue
, Q_AUDIO_DIR_SKIP
, 1);
750 void audio_prev_dir(void)
752 LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP -1");
753 queue_post(&audio_queue
, Q_AUDIO_DIR_SKIP
, -1);
756 void audio_pre_ff_rewind(void)
758 LOGFQUEUE("audio > audio Q_AUDIO_PRE_FF_REWIND");
759 queue_post(&audio_queue
, Q_AUDIO_PRE_FF_REWIND
, 0);
762 void audio_ff_rewind(long newpos
)
764 LOGFQUEUE("audio > audio Q_AUDIO_FF_REWIND");
765 queue_post(&audio_queue
, Q_AUDIO_FF_REWIND
, newpos
);
768 void audio_flush_and_reload_tracks(void)
770 LOGFQUEUE("audio > audio Q_AUDIO_FLUSH");
771 queue_post(&audio_queue
, Q_AUDIO_FLUSH
, 0);
774 void audio_error_clear(void)
776 #ifdef AUDIO_HAVE_RECORDING
777 pcm_rec_error_clear();
781 int audio_status(void)
786 ret
|= AUDIO_STATUS_PLAY
;
789 ret
|= AUDIO_STATUS_PAUSE
;
791 #ifdef HAVE_RECORDING
792 /* Do this here for constitency with mpeg.c version */
793 ret
|= pcm_rec_status();
799 int audio_get_file_pos(void)
804 #ifdef HAVE_DISK_STORAGE
805 void audio_set_buffer_margin(int setting
)
807 static const unsigned short lookup
[] = {5, 15, 30, 60, 120, 180, 300, 600};
808 buffer_margin
= lookup
[setting
];
809 logf("buffer margin: %ld", (long)buffer_margin
);
810 set_filebuf_watermark();
814 /* Take necessary steps to enable or disable the crossfade setting */
815 void audio_set_crossfade(int enable
)
821 /* Tell it the next setting to use */
822 pcmbuf_crossfade_enable(enable
);
824 /* Return if size hasn't changed or this is too early to determine
825 which in the second case there's no way we could be playing
827 if (pcmbuf_is_same_size())
829 /* This function is a copout and just syncs some variables -
830 to be removed at a later date */
831 pcmbuf_crossfade_enable_finished();
836 was_playing
= playing
;
838 /* Playback has to be stopped before changing the buffer size */
841 /* Store the track resume position */
842 offset
= thistrack_id3
->offset
;
845 /* Blast it - audio buffer will have to be setup again next time
847 audio_get_buffer(true, &size
);
849 /* Restart playback if audio was running previously */
854 /* --- Routines called from multiple threads --- */
856 static void set_filebuf_watermark(void)
859 return; /* Audio buffers not yet set up */
861 #ifdef HAVE_DISK_STORAGE
863 int spinup
= ata_spinup_time();
865 seconds
= (spinup
/ HZ
) + 1;
869 seconds
+= buffer_margin
;
875 /* bitrate of last track in buffer dictates watermark */
876 struct mp3entry
* id3
= NULL
;
877 if (tracks
[track_widx
].taginfo_ready
)
878 id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
880 id3
= bufgetid3(tracks
[track_widx
-1].id3_hid
);
882 logf("fwmark: No id3 for last track (r%d/w%d), aborting!", track_ridx
, track_widx
);
885 size_t bytes
= id3
->bitrate
* (1000/8) * seconds
;
886 buf_set_watermark(bytes
);
887 logf("fwmark: %d", bytes
);
890 const char *get_codec_filename(int cod_spec
)
894 #ifdef HAVE_RECORDING
895 /* Can choose decoder or encoder if one available */
896 int type
= cod_spec
& CODEC_TYPE_MASK
;
897 int afmt
= cod_spec
& CODEC_AFMT_MASK
;
899 if ((unsigned)afmt
>= AFMT_NUM_CODECS
)
900 type
= AFMT_UNKNOWN
| (type
& CODEC_TYPE_MASK
);
902 fname
= (type
== CODEC_TYPE_ENCODER
) ?
903 audio_formats
[afmt
].codec_enc_root_fn
:
904 audio_formats
[afmt
].codec_root_fn
;
907 (type
== CODEC_TYPE_ENCODER
) ? "Encoder" : "Decoder",
908 afmt
, fname
? fname
: "<unknown>");
909 #else /* !HAVE_RECORDING */
911 if ((unsigned)cod_spec
>= AFMT_NUM_CODECS
)
912 cod_spec
= AFMT_UNKNOWN
;
913 fname
= audio_formats
[cod_spec
].codec_root_fn
;
914 logf("Codec: %d - %s", cod_spec
, fname
? fname
: "<unknown>");
915 #endif /* HAVE_RECORDING */
918 } /* get_codec_filename */
920 /* --- Codec thread --- */
921 static bool codec_pcmbuf_insert_callback(
922 const void *ch1
, const void *ch2
, int count
)
924 const char *src
[2] = { ch1
, ch2
};
928 int out_count
= dsp_output_count(ci
.dsp
, count
);
932 /* Prevent audio from a previous track from playing */
933 if (ci
.new_track
|| ci
.stop_codec
)
936 while ((dest
= pcmbuf_request_buffer(&out_count
)) == NULL
)
940 if (ci
.seek_time
|| ci
.new_track
|| ci
.stop_codec
)
944 /* Get the real input_size for output_size bytes, guarding
945 * against resampling buffer overflows. */
946 inp_count
= dsp_input_count(ci
.dsp
, out_count
);
951 /* Input size has grown, no error, just don't write more than length */
952 if (inp_count
> count
)
955 out_count
= dsp_process(ci
.dsp
, dest
, src
, inp_count
);
960 pcmbuf_write_complete(out_count
);
966 } /* codec_pcmbuf_insert_callback */
968 static void* codec_get_buffer(size_t *size
)
970 if (codec_size
>= CODEC_SIZE
)
972 *size
= CODEC_SIZE
- codec_size
;
973 return &codecbuf
[codec_size
];
976 /* Between the codec and PCM track change, we need to keep updating the
977 "elapsed" value of the previous (to the codec, but current to the
978 user/PCM/WPS) track, so that the progressbar reaches the end.
979 During that transition, the WPS will display prevtrack_id3. */
980 static void codec_pcmbuf_position_callback(size_t size
) ICODE_ATTR
;
981 static void codec_pcmbuf_position_callback(size_t size
)
983 /* This is called from an ISR, so be quick */
984 unsigned int time
= size
* 1000 / 4 / NATIVE_FREQUENCY
+
985 othertrack_id3
->elapsed
;
987 if (time
>= othertrack_id3
->length
)
989 pcmbuf_set_position_callback(NULL
);
990 othertrack_id3
->elapsed
= othertrack_id3
->length
;
993 othertrack_id3
->elapsed
= time
;
996 static void codec_set_elapsed_callback(unsigned int value
)
998 unsigned int latency
;
1002 #ifdef AB_REPEAT_ENABLE
1003 ab_position_report(value
);
1006 latency
= pcmbuf_get_latency();
1007 if (value
< latency
)
1008 thistrack_id3
->elapsed
= 0;
1009 else if (value
- latency
> thistrack_id3
->elapsed
||
1010 value
- latency
< thistrack_id3
->elapsed
- 2)
1012 thistrack_id3
->elapsed
= value
- latency
;
1016 static void codec_set_offset_callback(size_t value
)
1018 unsigned int latency
;
1023 latency
= pcmbuf_get_latency() * thistrack_id3
->bitrate
/ 8;
1024 if (value
< latency
)
1025 thistrack_id3
->offset
= 0;
1027 thistrack_id3
->offset
= value
- latency
;
1030 static void codec_advance_buffer_counters(size_t amount
)
1032 bufadvance(CUR_TI
->audio_hid
, amount
);
1033 ci
.curpos
+= amount
;
1036 /* copy up-to size bytes into ptr and return the actual size copied */
1037 static size_t codec_filebuf_callback(void *ptr
, size_t size
)
1041 if (ci
.stop_codec
|| !playing
)
1044 copy_n
= bufread(CUR_TI
->audio_hid
, size
, ptr
);
1046 /* Nothing requested OR nothing left */
1050 /* Update read and other position pointers */
1051 codec_advance_buffer_counters(copy_n
);
1053 /* Return the actual amount of data copied to the buffer */
1055 } /* codec_filebuf_callback */
1057 static void* codec_request_buffer_callback(size_t *realsize
, size_t reqsize
)
1059 size_t copy_n
= reqsize
;
1069 ret
= bufgetdata(CUR_TI
->audio_hid
, reqsize
, &ptr
);
1071 copy_n
= MIN((size_t)ret
, reqsize
);
1082 } /* codec_request_buffer_callback */
1084 static int get_codec_base_type(int type
)
1096 static void codec_advance_buffer_callback(size_t amount
)
1098 codec_advance_buffer_counters(amount
);
1099 codec_set_offset_callback(ci
.curpos
);
1102 static void codec_advance_buffer_loc_callback(void *ptr
)
1104 size_t amount
= buf_get_offset(CUR_TI
->audio_hid
, ptr
);
1105 codec_advance_buffer_callback(amount
);
1108 static void codec_seek_complete_callback(void)
1110 logf("seek_complete");
1111 /* If seeking-while-playing, pcm playback is actually paused (pcm_is_paused())
1112 * but the paused flag is not set. If seeking-while-paused, the (paused) flag is
1113 * set, but pcm playback may have actually stopped due to a previous buffer clear.
1114 * The buffer clear below occurs with either condition. A seemless seek skips
1115 * this section and no buffer clear occurs.
1117 if (pcm_is_paused() || paused
)
1119 /* Clear the buffer */
1121 dsp_configure(ci
.dsp
, DSP_FLUSH
, 0);
1123 /* If seeking-while-playing, resume pcm playback */
1125 pcmbuf_pause(false);
1130 static bool codec_seek_buffer_callback(size_t newpos
)
1132 logf("codec_seek_buffer_callback");
1134 int ret
= bufseek(CUR_TI
->audio_hid
, newpos
);
1144 static void codec_configure_callback(int setting
, intptr_t value
)
1148 if (!dsp_configure(ci
.dsp
, setting
, value
))
1149 { logf("Illegal key:%d", setting
); }
1153 static void codec_track_changed(void)
1155 LOGFQUEUE("codec > audio Q_AUDIO_TRACK_CHANGED");
1156 queue_post(&audio_queue
, Q_AUDIO_TRACK_CHANGED
, 0);
1159 static void codec_pcmbuf_track_changed_callback(void)
1161 pcmbuf_set_position_callback(NULL
);
1162 pcmbuf_callback_queue_post(Q_AUDIO_TRACK_CHANGED
, 0);
1165 static void codec_discard_codec_callback(void)
1167 if (CUR_TI
->codec_hid
>= 0)
1169 bufclose(CUR_TI
->codec_hid
);
1170 CUR_TI
->codec_hid
= -1;
1174 static inline void codec_gapless_track_change(void)
1176 /* callback keeps the progress bar moving while the pcmbuf empties */
1177 pcmbuf_set_position_callback(codec_pcmbuf_position_callback
);
1178 /* set the pcmbuf callback for when the track really changes */
1179 pcmbuf_set_event_handler(codec_pcmbuf_track_changed_callback
);
1182 static inline void codec_crossfade_track_change(void)
1184 /* Initiate automatic crossfade mode */
1185 pcmbuf_crossfade_init(false);
1186 /* Notify the wps that the track change starts now */
1187 codec_track_changed();
1190 static void codec_track_skip_done(bool was_manual
)
1192 /* Manual track change (always crossfade or flush audio). */
1195 pcmbuf_crossfade_init(true);
1196 LOGFQUEUE("codec > audio Q_AUDIO_TRACK_CHANGED");
1197 queue_post(&audio_queue
, Q_AUDIO_TRACK_CHANGED
, 0);
1199 /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */
1200 else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active()
1201 && global_settings
.crossfade
!= CROSSFADE_ENABLE_TRACKSKIP
)
1203 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP
)
1205 if (global_settings
.playlist_shuffle
)
1206 /* shuffle mode is on, so crossfade: */
1207 codec_crossfade_track_change();
1209 /* shuffle mode is off, so do a gapless track change */
1210 codec_gapless_track_change();
1213 /* normal crossfade: */
1214 codec_crossfade_track_change();
1217 /* normal gapless playback. */
1218 codec_gapless_track_change();
1221 static bool codec_load_next_track(void)
1223 intptr_t result
= Q_CODEC_REQUEST_FAILED
;
1225 prev_track_elapsed
= thistrack_id3
->elapsed
;
1227 #ifdef AB_REPEAT_ENABLE
1228 ab_end_of_track_report();
1231 logf("Request new track");
1233 if (ci
.new_track
== 0)
1236 automatic_skip
= true;
1241 trigger_cpu_boost();
1242 LOGFQUEUE("codec >| audio Q_AUDIO_CHECK_NEW_TRACK");
1243 result
= queue_send(&audio_queue
, Q_AUDIO_CHECK_NEW_TRACK
, 0);
1248 case Q_CODEC_REQUEST_COMPLETE
:
1249 LOGFQUEUE("codec |< Q_CODEC_REQUEST_COMPLETE");
1250 codec_track_skip_done(!automatic_skip
);
1253 case Q_CODEC_REQUEST_FAILED
:
1254 LOGFQUEUE("codec |< Q_CODEC_REQUEST_FAILED");
1256 ci
.stop_codec
= true;
1257 codec_requested_stop
= true;
1261 LOGFQUEUE("codec |< default");
1262 ci
.stop_codec
= true;
1263 codec_requested_stop
= true;
1268 static bool codec_request_next_track_callback(void)
1272 if (ci
.stop_codec
|| !playing
)
1275 prev_codectype
= get_codec_base_type(thistrack_id3
->codectype
);
1276 if (!codec_load_next_track())
1279 /* Seek to the beginning of the new track because if the struct
1280 mp3entry was buffered, "elapsed" might not be zero (if the track has
1281 been played already but not unbuffered) */
1282 codec_seek_buffer_callback(thistrack_id3
->first_frame_offset
);
1283 /* Check if the next codec is the same file. */
1284 if (prev_codectype
== get_codec_base_type(thistrack_id3
->codectype
))
1286 logf("New track loaded");
1287 codec_discard_codec_callback();
1292 logf("New codec:%d/%d", thistrack_id3
->codectype
, prev_codectype
);
1297 static void codec_thread(void)
1299 struct queue_event ev
;
1305 if (!pcmbuf_is_crossfade_active()) {
1309 queue_wait(&codec_queue
, &ev
);
1310 codec_requested_stop
= false;
1313 case Q_CODEC_LOAD_DISK
:
1314 LOGFQUEUE("codec < Q_CODEC_LOAD_DISK");
1315 queue_reply(&codec_queue
, 1);
1316 audio_codec_loaded
= true;
1317 ci
.stop_codec
= false;
1318 status
= codec_load_file((const char *)ev
.data
, &ci
);
1319 LOGFQUEUE("codec_load_file %s %d\n", (const char *)ev
.data
, status
);
1323 LOGFQUEUE("codec < Q_CODEC_LOAD");
1324 if (CUR_TI
->codec_hid
< 0) {
1325 logf("Codec slot is empty!");
1326 /* Wait for the pcm buffer to go empty */
1327 while (pcm_is_playing())
1329 /* This must be set to prevent an infinite loop */
1330 ci
.stop_codec
= true;
1331 LOGFQUEUE("codec > codec Q_AUDIO_PLAY");
1332 queue_post(&codec_queue
, Q_AUDIO_PLAY
, 0);
1336 audio_codec_loaded
= true;
1337 ci
.stop_codec
= false;
1338 status
= codec_load_buf(CUR_TI
->codec_hid
, &ci
);
1339 LOGFQUEUE("codec_load_buf %d\n", status
);
1342 case Q_CODEC_DO_CALLBACK
:
1343 LOGFQUEUE("codec < Q_CODEC_DO_CALLBACK");
1344 queue_reply(&codec_queue
, 1);
1345 if ((void*)ev
.data
!= NULL
)
1347 cpucache_invalidate();
1348 ((void (*)(void))ev
.data
)();
1353 #ifdef AUDIO_HAVE_RECORDING
1354 case Q_ENCODER_LOAD_DISK
:
1355 LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK");
1356 audio_codec_loaded
= false; /* Not audio codec! */
1357 logf("loading encoder");
1358 ci
.stop_encoder
= false;
1359 status
= codec_load_file((const char *)ev
.data
, &ci
);
1360 logf("encoder stopped");
1362 #endif /* AUDIO_HAVE_RECORDING */
1365 LOGFQUEUE("codec < default");
1368 if (audio_codec_loaded
)
1377 audio_codec_loaded
= false;
1381 case Q_CODEC_LOAD_DISK
:
1383 LOGFQUEUE("codec < Q_CODEC_LOAD");
1386 if (ci
.new_track
|| status
!= CODEC_OK
)
1390 logf("Codec failure, %d %d", ci
.new_track
, status
);
1391 splash(HZ
*2, "Codec failure");
1394 if (!codec_load_next_track())
1396 LOGFQUEUE("codec > audio Q_AUDIO_STOP");
1397 /* End of playlist */
1398 queue_post(&audio_queue
, Q_AUDIO_STOP
, 0);
1404 logf("Codec finished");
1407 /* Wait for the audio to stop playing before
1408 * triggering the WPS exit */
1409 while(pcm_is_playing())
1411 /* There has been one too many struct pointer swaps by now
1412 * so even though it says othertrack_id3, its the correct one! */
1413 othertrack_id3
->elapsed
=
1414 othertrack_id3
->length
- pcmbuf_get_latency();
1418 if (codec_requested_stop
)
1420 LOGFQUEUE("codec > audio Q_AUDIO_STOP");
1421 queue_post(&audio_queue
, Q_AUDIO_STOP
, 0);
1427 if (CUR_TI
->codec_hid
>= 0)
1429 LOGFQUEUE("codec > codec Q_CODEC_LOAD");
1430 queue_post(&codec_queue
, Q_CODEC_LOAD
, 0);
1434 const char *codec_fn
=
1435 get_codec_filename(thistrack_id3
->codectype
);
1438 LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK");
1439 queue_post(&codec_queue
, Q_CODEC_LOAD_DISK
,
1440 (intptr_t)codec_fn
);
1446 #ifdef AUDIO_HAVE_RECORDING
1447 case Q_ENCODER_LOAD_DISK
:
1448 LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK");
1450 if (status
== CODEC_OK
)
1453 logf("Encoder failure");
1454 splash(HZ
*2, "Encoder failure");
1456 if (ci
.enc_codec_loaded
< 0)
1459 logf("Encoder failed to load");
1460 ci
.enc_codec_loaded
= -1;
1462 #endif /* AUDIO_HAVE_RECORDING */
1465 LOGFQUEUE("codec < default");
1471 /* Borrow the codec thread and return the ID */
1472 void codec_thread_do_callback(void (*fn
)(void), unsigned int *id
)
1474 /* Set id before telling thread to call something; it may be
1475 * needed before this function returns. */
1477 *id
= codec_thread_id
;
1479 /* Codec thread will signal just before entering callback */
1480 LOGFQUEUE("codec >| Q_CODEC_DO_CALLBACK");
1481 queue_send(&codec_queue
, Q_CODEC_DO_CALLBACK
, (intptr_t)fn
);
1484 /* --- Buffering callbacks --- */
1486 static void buffering_low_buffer_callback(void *data
)
1489 logf("low buffer callback");
1491 if (filling
== STATE_FULL
|| filling
== STATE_END_OF_PLAYLIST
) {
1492 /* force a refill */
1493 LOGFQUEUE("buffering > audio Q_AUDIO_FILL_BUFFER");
1494 queue_post(&audio_queue
, Q_AUDIO_FILL_BUFFER
, 0);
1498 static void buffering_handle_rebuffer_callback(void *data
)
1501 LOGFQUEUE("audio >| audio Q_AUDIO_FLUSH");
1502 queue_post(&audio_queue
, Q_AUDIO_FLUSH
, 0);
1505 static void buffering_handle_finished_callback(int *data
)
1507 logf("handle %d finished buffering", *data
);
1509 if (*data
== tracks
[track_widx
].id3_hid
)
1511 int offset
= ci
.new_track
+ wps_offset
;
1512 int next_idx
= (track_ridx
+ offset
+ 1) & MAX_TRACK_MASK
;
1513 /* The metadata handle for the last loaded track has been buffered.
1514 We can ask the audio thread to load the rest of the track's data. */
1515 LOGFQUEUE("audio >| audio Q_AUDIO_FINISH_LOAD");
1516 queue_post(&audio_queue
, Q_AUDIO_FINISH_LOAD
, 0);
1517 if (tracks
[next_idx
].id3_hid
== *data
)
1518 send_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE
, NULL
);
1522 /* This is most likely an audio handle, so we strip the useless
1523 trailing tags that are left. */
1526 if (*data
== tracks
[track_widx
-1].audio_hid
1527 && filling
== STATE_END_OF_PLAYLIST
)
1529 /* This was the last track in the playlist.
1530 We now have all the data we need. */
1531 logf("last track finished buffering");
1532 filling
= STATE_FINISHED
;
1538 /* --- Audio thread --- */
1540 static bool audio_have_tracks(void)
1542 return (audio_track_count() != 0);
1545 static int audio_free_track_count(void)
1547 /* Used tracks + free tracks adds up to MAX_TRACK - 1 */
1548 return MAX_TRACK
- 1 - audio_track_count();
1551 int audio_track_count(void)
1553 /* Calculate difference from track_ridx to track_widx
1554 * taking into account a possible wrap-around. */
1555 return (MAX_TRACK
+ track_widx
- track_ridx
) & MAX_TRACK_MASK
;
1558 long audio_filebufused(void)
1560 return (long) buf_used();
1563 /* Update track info after successful a codec track change */
1564 static void audio_update_trackinfo(void)
1566 /* Load the curent track's metadata into curtrack_id3 */
1567 if (CUR_TI
->id3_hid
>= 0)
1568 copy_mp3entry(thistrack_id3
, bufgetid3(CUR_TI
->id3_hid
));
1570 /* Reset current position */
1571 thistrack_id3
->elapsed
= 0;
1572 thistrack_id3
->offset
= 0;
1574 /* Update the codec API */
1575 ci
.filesize
= CUR_TI
->filesize
;
1576 ci
.id3
= thistrack_id3
;
1578 ci
.taginfo_ready
= &CUR_TI
->taginfo_ready
;
1581 /* Clear tracks between write and read, non inclusive */
1582 static void audio_clear_track_entries(void)
1584 int cur_idx
= track_widx
;
1586 logf("Clearing tracks:%d/%d", track_ridx
, track_widx
);
1588 /* Loop over all tracks from write-to-read */
1591 cur_idx
= (cur_idx
+ 1) & MAX_TRACK_MASK
;
1593 if (cur_idx
== track_ridx
)
1596 clear_track_info(&tracks
[cur_idx
]);
1600 /* Clear all tracks */
1601 static bool audio_release_tracks(void)
1605 logf("releasing all tracks");
1607 for(i
= 0; i
< MAX_TRACK
; i
++)
1609 cur_idx
= (track_ridx
+ i
) & MAX_TRACK_MASK
;
1610 if (!clear_track_info(&tracks
[cur_idx
]))
1617 static bool audio_loadcodec(bool start_play
)
1620 char codec_path
[MAX_PATH
]; /* Full path to codec */
1621 const struct mp3entry
*id3
, *prev_id3
;
1623 if (tracks
[track_widx
].id3_hid
< 0) {
1627 id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1631 const char *codec_fn
= get_codec_filename(id3
->codectype
);
1632 if (codec_fn
== NULL
)
1635 tracks
[track_widx
].codec_hid
= -1;
1639 /* Load the codec directly from disk and save some memory. */
1640 track_ridx
= track_widx
;
1641 ci
.filesize
= CUR_TI
->filesize
;
1642 ci
.id3
= thistrack_id3
;
1643 ci
.taginfo_ready
= &CUR_TI
->taginfo_ready
;
1645 LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK");
1646 queue_post(&codec_queue
, Q_CODEC_LOAD_DISK
, (intptr_t)codec_fn
);
1651 /* If we already have another track than this one buffered */
1652 if (track_widx
!= track_ridx
)
1654 prev_track
= (track_widx
- 1) & MAX_TRACK_MASK
;
1656 id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1657 prev_id3
= bufgetid3(tracks
[prev_track
].id3_hid
);
1659 /* If the previous codec is the same as this one, there is no need
1660 * to put another copy of it on the file buffer */
1661 if (id3
&& prev_id3
&&
1662 get_codec_base_type(id3
->codectype
) ==
1663 get_codec_base_type(prev_id3
->codectype
)
1664 && audio_codec_loaded
)
1666 logf("Reusing prev. codec");
1672 codec_get_full_path(codec_path
, codec_fn
);
1674 tracks
[track_widx
].codec_hid
= bufopen(codec_path
, 0, TYPE_CODEC
);
1675 if (tracks
[track_widx
].codec_hid
< 0)
1678 logf("Loaded codec");
1683 /* Load metadata for the next track (with bufopen). The rest of the track
1684 loading will be handled by audio_finish_load_track once the metadata has been
1685 actually loaded by the buffering thread. */
1686 static bool audio_load_track(size_t offset
, bool start_play
)
1688 const char *trackname
;
1691 if (track_load_started
) {
1692 /* There is already a track load in progress, so track_widx hasn't been
1693 incremented yet. Loading another track would overwrite the one that
1694 hasn't finished loading. */
1695 logf("audio_load_track(): a track load is already in progress");
1699 start_play_g
= start_play
; /* will be read by audio_finish_load_track */
1701 /* Stop buffer filling if there is no free track entries.
1702 Don't fill up the last track entry (we wan't to store next track
1704 if (!audio_free_track_count())
1706 logf("No free tracks");
1711 tracks
[track_widx
].taginfo_ready
= false;
1713 logf("Buffering track:%d/%d", track_widx
, track_ridx
);
1714 /* Get track name from current playlist read position. */
1715 while ((trackname
= playlist_peek(last_peek_offset
)) != NULL
)
1717 /* Handle broken playlists. */
1718 fd
= open(trackname
, O_RDONLY
);
1721 logf("Open failed");
1722 /* Skip invalid entry from playlist. */
1723 playlist_skip_entry(NULL
, last_peek_offset
);
1731 logf("End-of-playlist");
1732 memset(&unbuffered_id3
, 0, sizeof(struct mp3entry
));
1733 filling
= STATE_END_OF_PLAYLIST
;
1735 if (thistrack_id3
->length
== 0 && thistrack_id3
->filesize
== 0)
1737 /* Stop playback if no valid track was found. */
1738 audio_stop_playback();
1744 tracks
[track_widx
].filesize
= filesize(fd
);
1746 if (offset
> tracks
[track_widx
].filesize
)
1749 /* Set default values */
1752 buf_set_watermark(filebuflen
/2);
1753 dsp_configure(ci
.dsp
, DSP_RESET
, 0);
1754 playlist_update_resume_info(audio_current_track());
1757 /* Get track metadata if we don't already have it. */
1758 if (tracks
[track_widx
].id3_hid
< 0)
1760 tracks
[track_widx
].id3_hid
= bufopen(trackname
, 0, TYPE_ID3
);
1762 if (tracks
[track_widx
].id3_hid
< 0)
1764 /* Buffer is full. */
1765 get_metadata(&unbuffered_id3
, fd
, trackname
);
1768 logf("buffer is full for now");
1769 filling
= STATE_FULL
;
1773 if (track_widx
== track_ridx
)
1775 /* TODO: Superfluos buffering call? */
1776 buf_request_buffer_handle(tracks
[track_widx
].id3_hid
);
1777 struct mp3entry
*id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1780 copy_mp3entry(thistrack_id3
, id3
);
1781 thistrack_id3
->offset
= offset
;
1784 memset(thistrack_id3
, 0, sizeof(struct mp3entry
));
1789 playlist_update_resume_info(audio_current_track());
1794 track_load_started
= true; /* Remember that we've started loading a track */
1798 /* Second part of the track loading: We now have the metadata available, so we
1799 can load the codec, the album art and finally the audio data.
1800 This is called on the audio thread after the buffering thread calls the
1801 buffering_handle_finished_callback callback. */
1802 static void audio_finish_load_track(void)
1804 size_t file_offset
= 0;
1806 bool start_play
= start_play_g
;
1808 track_load_started
= false;
1810 if (tracks
[track_widx
].id3_hid
< 0) {
1811 logf("no metatdata");
1815 struct mp3entry
*track_id3
;
1817 if (track_widx
== track_ridx
)
1818 track_id3
= thistrack_id3
;
1820 track_id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1822 if (track_id3
->length
== 0 && track_id3
->filesize
== 0)
1824 logf("audio_finish_load_track: invalid metadata");
1826 /* Invalid metadata */
1827 bufclose(tracks
[track_widx
].id3_hid
);
1828 tracks
[track_widx
].id3_hid
= -1;
1830 /* Skip invalid entry from playlist. */
1831 playlist_skip_entry(NULL
, last_peek_offset
--);
1833 /* load next track */
1834 LOGFQUEUE("audio > audio Q_AUDIO_FILL_BUFFER %d", (int)start_play
);
1835 queue_post(&audio_queue
, Q_AUDIO_FILL_BUFFER
, start_play
);
1839 /* Try to load a cuesheet for the track */
1842 char cuepath
[MAX_PATH
];
1843 if (look_for_cuesheet_file(track_id3
->path
, cuepath
))
1846 tracks
[track_widx
].cuesheet_hid
=
1847 bufalloc(NULL
, sizeof(struct cuesheet
), TYPE_CUESHEET
);
1848 if (tracks
[track_widx
].cuesheet_hid
>= 0)
1850 bufgetdata(tracks
[track_widx
].cuesheet_hid
,
1851 sizeof(struct cuesheet
), &temp
);
1852 struct cuesheet
*cuesheet
= (struct cuesheet
*)temp
;
1853 if (!parse_cuesheet(cuepath
, cuesheet
))
1855 bufclose(tracks
[track_widx
].cuesheet_hid
);
1856 track_id3
->cuesheet
= NULL
;
1861 #ifdef HAVE_ALBUMART
1862 if (tracks
[track_widx
].aa_hid
< 0)
1864 char aa_path
[MAX_PATH
];
1865 /* find_albumart will error out if the wps doesn't have AA */
1866 if (find_albumart(track_id3
, aa_path
, sizeof(aa_path
)))
1868 tracks
[track_widx
].aa_hid
= bufopen(aa_path
, 0, TYPE_BITMAP
);
1870 if(tracks
[track_widx
].aa_hid
== ERR_BUFFER_FULL
)
1872 filling
= STATE_FULL
;
1873 logf("buffer is full for now");
1874 return; /* No space for track's album art, not an error */
1876 else if (tracks
[track_widx
].aa_hid
< 0)
1878 /* another error, ignore AlbumArt */
1879 logf("Album art loading failed");
1885 /* Load the codec. */
1886 if (!audio_loadcodec(start_play
))
1888 if (tracks
[track_widx
].codec_hid
== ERR_BUFFER_FULL
)
1890 /* No space for codec on buffer, not an error */
1891 filling
= STATE_FULL
;
1895 /* This is an error condition, either no codec was found, or reading
1896 * the codec file failed part way through, either way, skip the track */
1897 /* FIXME: We should not use splashf from audio thread! */
1898 splashf(HZ
*2, "No codec for: %s", track_id3
->path
);
1899 /* Skip invalid entry from playlist. */
1900 playlist_skip_entry(NULL
, last_peek_offset
);
1904 track_id3
->elapsed
= 0;
1905 offset
= track_id3
->offset
;
1907 enum data_type type
= TYPE_PACKET_AUDIO
;
1909 switch (track_id3
->codectype
) {
1914 file_offset
= offset
;
1915 track_id3
->offset
= offset
;
1921 file_offset
= offset
;
1922 track_id3
->offset
= offset
;
1923 track_id3
->elapsed
= track_id3
->length
/ 2;
1927 case AFMT_OGG_VORBIS
:
1937 track_id3
->offset
= offset
;
1943 logf("Loading atomic %d",track_id3
->codectype
);
1944 type
= TYPE_ATOMIC_AUDIO
;
1948 logf("alt:%s", track_id3
->path
);
1950 if (file_offset
> AUDIO_REBUFFER_GUESS_SIZE
)
1951 file_offset
-= AUDIO_REBUFFER_GUESS_SIZE
;
1952 else if (track_id3
->first_frame_offset
)
1953 file_offset
= track_id3
->first_frame_offset
;
1957 tracks
[track_widx
].audio_hid
= bufopen(track_id3
->path
, file_offset
, type
);
1959 /* No space left, not an error */
1960 if (tracks
[track_widx
].audio_hid
== ERR_BUFFER_FULL
)
1962 filling
= STATE_FULL
;
1963 logf("buffer is full for now");
1966 else if (tracks
[track_widx
].audio_hid
< 0)
1968 /* another error, do not continue either */
1969 logf("Could not add audio data handle");
1973 /* All required data is now available for the codec. */
1974 tracks
[track_widx
].taginfo_ready
= true;
1978 ci
.curpos
=file_offset
;
1979 buf_request_buffer_handle(tracks
[track_widx
].audio_hid
);
1982 track_widx
= (track_widx
+ 1) & MAX_TRACK_MASK
;
1984 send_event(PLAYBACK_EVENT_TRACK_BUFFER
, track_id3
);
1986 /* load next track */
1987 LOGFQUEUE("audio > audio Q_AUDIO_FILL_BUFFER");
1988 queue_post(&audio_queue
, Q_AUDIO_FILL_BUFFER
, 0);
1993 static void audio_fill_file_buffer(bool start_play
, size_t offset
)
1995 trigger_cpu_boost();
1997 /* No need to rebuffer if there are track skips pending,
1998 * however don't cancel buffering on skipping while filling. */
1999 if (ci
.new_track
!= 0 && filling
!= STATE_FILLING
)
2001 filling
= STATE_FILLING
;
2003 /* Must reset the buffer before use if trashed or voice only - voice
2004 file size shouldn't have changed so we can go straight from
2005 AUDIOBUF_STATE_VOICED_ONLY to AUDIOBUF_STATE_INITIALIZED */
2006 if (buffer_state
!= AUDIOBUF_STATE_INITIALIZED
)
2007 audio_reset_buffer();
2009 logf("Starting buffer fill");
2012 audio_clear_track_entries();
2014 /* Save the current resume position once. */
2015 playlist_update_resume_info(audio_current_track());
2017 audio_load_track(offset
, start_play
);
2020 static void audio_rebuffer(void)
2022 logf("Forcing rebuffer");
2024 clear_track_info(CUR_TI
);
2026 /* Reset track pointers */
2027 track_widx
= track_ridx
;
2028 audio_clear_track_entries();
2030 /* Reset a possibly interrupted track load */
2031 track_load_started
= false;
2033 /* Fill the buffer */
2034 last_peek_offset
= -1;
2037 if (!CUR_TI
->taginfo_ready
)
2038 memset(thistrack_id3
, 0, sizeof(struct mp3entry
));
2040 audio_fill_file_buffer(false, 0);
2043 /* Called on request from the codec to get a new track. This is the codec part
2044 of the track transition. */
2045 static int audio_check_new_track(void)
2047 int track_count
= audio_track_count();
2048 int old_track_ridx
= track_ridx
;
2051 struct mp3entry
*temp
= thistrack_id3
;
2053 /* Now it's good time to send track finish events. */
2054 send_event(PLAYBACK_EVENT_TRACK_FINISH
, thistrack_id3
);
2055 /* swap the mp3entry pointers */
2056 thistrack_id3
= othertrack_id3
;
2057 othertrack_id3
= temp
;
2058 ci
.id3
= thistrack_id3
;
2059 memset(thistrack_id3
, 0, sizeof(struct mp3entry
));
2064 /* regardless of the return value we need to rebuffer.
2065 if it fails the old playlist will resume, else the
2066 next dir will start playing */
2067 playlist_next_dir(ci
.new_track
);
2076 /* If the playlist isn't that big */
2079 while (!playlist_check(ci
.new_track
))
2081 if (ci
.new_track
>= 0)
2083 LOGFQUEUE("audio >|= codec Q_CODEC_REQUEST_FAILED");
2084 return Q_CODEC_REQUEST_FAILED
;
2090 /* Update the playlist */
2091 last_peek_offset
-= ci
.new_track
;
2093 if (playlist_next(ci
.new_track
) < 0)
2095 LOGFQUEUE("audio >|= codec Q_CODEC_REQUEST_FAILED");
2096 return Q_CODEC_REQUEST_FAILED
;
2102 new_playlist
= false;
2105 /* Save a pointer to the old track to allow later clearing */
2108 for (i
= 0; i
< ci
.new_track
; i
++)
2110 idx
= (track_ridx
+ i
) & MAX_TRACK_MASK
;
2111 struct mp3entry
*id3
= bufgetid3(tracks
[idx
].id3_hid
);
2112 ssize_t offset
= buf_handle_offset(tracks
[idx
].audio_hid
);
2113 if (!id3
|| offset
< 0 || (unsigned)offset
> id3
->first_frame_offset
)
2115 /* We don't have all the audio data for that track, so clear it,
2116 but keep the metadata. */
2117 if (tracks
[idx
].audio_hid
>= 0 && bufclose(tracks
[idx
].audio_hid
))
2119 tracks
[idx
].audio_hid
= -1;
2120 tracks
[idx
].filesize
= 0;
2125 /* Move to the new track */
2126 track_ridx
= (track_ridx
+ ci
.new_track
) & MAX_TRACK_MASK
;
2128 buf_set_base_handle(CUR_TI
->audio_hid
);
2132 wps_offset
= -ci
.new_track
;
2135 /* If it is not safe to even skip this many track entries */
2136 if (ci
.new_track
>= track_count
|| ci
.new_track
<= track_count
- MAX_TRACK
)
2143 forward
= ci
.new_track
> 0;
2146 /* If the target track is clearly not in memory */
2147 if (CUR_TI
->filesize
== 0 || !CUR_TI
->taginfo_ready
)
2153 /* When skipping backwards, it is possible that we've found a track that's
2154 * buffered, but which is around the track-wrap and therefore not the track
2155 * we are looking for */
2158 int cur_idx
= track_ridx
;
2159 bool taginfo_ready
= true;
2160 /* We've wrapped the buffer backwards if new > old */
2161 bool wrap
= track_ridx
> old_track_ridx
;
2165 cur_idx
= (cur_idx
+ 1) & MAX_TRACK_MASK
;
2167 /* if we've advanced past the wrap when cur_idx is zeroed */
2171 /* if we aren't still on the wrap and we've caught the old track */
2172 if (!(wrap
|| cur_idx
< old_track_ridx
))
2175 /* If we hit a track in between without valid tag info, bail */
2176 if (!tracks
[cur_idx
].taginfo_ready
)
2178 taginfo_ready
= false;
2189 audio_update_trackinfo();
2190 LOGFQUEUE("audio >|= codec Q_CODEC_REQUEST_COMPLETE");
2191 return Q_CODEC_REQUEST_COMPLETE
;
2194 unsigned long audio_prev_elapsed(void)
2196 return prev_track_elapsed
;
2199 static void audio_stop_codec_flush(void)
2201 ci
.stop_codec
= true;
2204 while (audio_codec_loaded
)
2207 /* If the audio codec is not loaded any more, and the audio is still
2208 * playing, it is now and _only_ now safe to call this function from the
2210 if (pcm_is_playing())
2213 pcmbuf_queue_clear();
2215 pcmbuf_pause(paused
);
2218 static void audio_stop_playback(void)
2222 /* If still actively playing here, play out the last samples in the track
2223 * before stopping. A manual stop is actually paused at this point, so
2224 * don't continue playback.
2227 pcmbuf_play_remainder();
2229 /* If we were playing, save resume information */
2230 struct mp3entry
*id3
= NULL
;
2234 /* Set this early, the outside code yields and may allow the codec
2235 to try to wait for a reply on a buffer wait */
2236 ci
.stop_codec
= true;
2237 id3
= audio_current_track();
2240 /* Save the current playing spot, or NULL if the playlist has ended */
2241 playlist_update_resume_info(id3
);
2243 /* TODO: Create auto bookmark too? */
2245 prev_track_elapsed
= othertrack_id3
->elapsed
;
2247 remove_event(BUFFER_EVENT_BUFFER_LOW
, buffering_low_buffer_callback
);
2250 audio_stop_codec_flush();
2253 track_load_started
= false;
2255 filling
= STATE_IDLE
;
2257 /* Mark all entries null. */
2258 audio_clear_track_entries();
2260 /* Close all tracks */
2261 audio_release_tracks();
2264 static void audio_play_start(size_t offset
)
2268 #if INPUT_SRC_CAPS != 0
2269 audio_set_input_source(AUDIO_SRC_PLAYBACK
, SRCF_PLAYBACK
);
2270 audio_set_output_source(AUDIO_SRC_PLAYBACK
);
2273 /* Wait for any previously playing audio to flush - TODO: Not necessary? */
2275 audio_stop_codec_flush();
2278 track_load_started
= false;
2284 sound_set_volume(global_settings
.volume
);
2285 track_widx
= track_ridx
= 0;
2287 /* Clear all track entries. */
2288 for (i
= 0; i
< MAX_TRACK
; i
++) {
2289 clear_track_info(&tracks
[i
]);
2292 last_peek_offset
= -1;
2294 /* Officially playing */
2295 queue_reply(&audio_queue
, 1);
2297 audio_fill_file_buffer(true, offset
);
2299 add_event(BUFFER_EVENT_BUFFER_LOW
, false, buffering_low_buffer_callback
);
2301 LOGFQUEUE("audio > audio Q_AUDIO_TRACK_CHANGED");
2302 queue_post(&audio_queue
, Q_AUDIO_TRACK_CHANGED
, 0);
2306 /* Invalidates all but currently playing track. */
2307 static void audio_invalidate_tracks(void)
2309 if (audio_have_tracks())
2311 last_peek_offset
= 0;
2312 track_widx
= track_ridx
;
2314 /* Mark all other entries null (also buffered wrong metadata). */
2315 audio_clear_track_entries();
2317 track_widx
= (track_widx
+ 1) & MAX_TRACK_MASK
;
2319 audio_fill_file_buffer(false, 0);
2323 static void audio_new_playlist(void)
2325 /* Prepare to start a new fill from the beginning of the playlist */
2326 last_peek_offset
= -1;
2327 if (audio_have_tracks())
2330 skipped_during_pause
= true;
2331 track_widx
= track_ridx
;
2332 audio_clear_track_entries();
2334 track_widx
= (track_widx
+ 1) & MAX_TRACK_MASK
;
2336 /* Mark the current track as invalid to prevent skipping back to it */
2337 CUR_TI
->taginfo_ready
= false;
2340 /* Signal the codec to initiate a track change forward */
2341 new_playlist
= true;
2344 /* Officially playing */
2345 queue_reply(&audio_queue
, 1);
2347 audio_fill_file_buffer(false, 0);
2350 /* Called on manual track skip */
2351 static void audio_initiate_track_change(long direction
)
2353 logf("audio_initiate_track_change(%ld)", direction
);
2355 ci
.new_track
+= direction
;
2356 wps_offset
-= direction
;
2358 skipped_during_pause
= true;
2361 /* Called on manual dir skip */
2362 static void audio_initiate_dir_change(long direction
)
2365 ci
.new_track
= direction
;
2367 skipped_during_pause
= true;
2370 /* Called when PCM track change is complete */
2371 static void audio_finalise_track_change(void)
2373 logf("audio_finalise_track_change");
2378 automatic_skip
= false;
2380 /* Invalidate prevtrack_id3 */
2381 memset(othertrack_id3
, 0, sizeof(struct mp3entry
));
2383 if (prev_ti
&& prev_ti
->audio_hid
< 0)
2385 /* No audio left so we clear all the track info. */
2386 clear_track_info(prev_ti
);
2389 send_event(PLAYBACK_EVENT_TRACK_CHANGE
, thistrack_id3
);
2390 playlist_update_resume_info(audio_current_track());
2394 * Layout audio buffer as follows - iram buffer depends on target:
2395 * [|SWAP:iram][|TALK]|FILE|GUARD|PCM|[SWAP:dram[|iram]|]
2397 static void audio_reset_buffer(void)
2399 /* see audio_get_recording_buffer if this is modified */
2400 logf("audio_reset_buffer");
2402 /* If the setup of anything allocated before the file buffer is
2403 changed, do check the adjustments after the buffer_alloc call
2404 as it will likely be affected and need sliding over */
2406 /* Initially set up file buffer as all space available */
2407 malloc_buf
= audiobuf
+ talk_get_bufsize();
2408 /* Align the malloc buf to line size. Especially important to cf
2409 targets that do line reads/writes. */
2410 malloc_buf
= (unsigned char *)(((uintptr_t)malloc_buf
+ 15) & ~15);
2411 filebuf
= malloc_buf
; /* filebuf line align implied */
2412 filebuflen
= audiobufend
- filebuf
;
2416 /* Subtract whatever the pcm buffer says it used plus the guard buffer */
2417 const size_t pcmbuf_size
= pcmbuf_init(filebuf
+ filebuflen
) +GUARD_BUFSIZE
;
2420 if(pcmbuf_size
> filebuflen
)
2421 panicf("Not enough memory for pcmbuf_init() : %d > %d",
2422 (int)pcmbuf_size
, (int)filebuflen
);
2425 filebuflen
-= pcmbuf_size
;
2427 /* Make sure filebuflen is a longword multiple after adjustment - filebuf
2428 will already be line aligned */
2431 buffering_reset(filebuf
, filebuflen
);
2433 /* Clear any references to the file buffer */
2434 buffer_state
= AUDIOBUF_STATE_INITIALIZED
;
2436 #if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE)
2437 /* Make sure everything adds up - yes, some info is a bit redundant but
2438 aids viewing and the sumation of certain variables should add up to
2439 the location of others. */
2442 const unsigned char *pcmbuf
= pcmbuf_get_meminfo(&pcmbufsize
);
2443 logf("mabuf: %08X", (unsigned)malloc_buf
);
2444 logf("fbuf: %08X", (unsigned)filebuf
);
2445 logf("fbufe: %08X", (unsigned)(filebuf
+ filebuflen
));
2446 logf("gbuf: %08X", (unsigned)(filebuf
+ filebuflen
));
2447 logf("gbufe: %08X", (unsigned)(filebuf
+ filebuflen
+ GUARD_BUFSIZE
));
2448 logf("pcmb: %08X", (unsigned)pcmbuf
);
2449 logf("pcmbe: %08X", (unsigned)(pcmbuf
+ pcmbufsize
));
2454 static void audio_thread(void)
2456 struct queue_event ev
;
2460 audio_thread_ready
= true;
2464 if (filling
!= STATE_FILLING
) {
2465 /* End of buffering, let's calculate the watermark and unboost */
2466 set_filebuf_watermark();
2470 if (!pcmbuf_queue_scan(&ev
))
2471 queue_wait_w_tmo(&audio_queue
, &ev
, HZ
/2);
2475 case Q_AUDIO_FILL_BUFFER
:
2476 LOGFQUEUE("audio < Q_AUDIO_FILL_BUFFER %d", (int)ev
.data
);
2477 audio_fill_file_buffer((bool)ev
.data
, 0);
2480 case Q_AUDIO_FINISH_LOAD
:
2481 LOGFQUEUE("audio < Q_AUDIO_FINISH_LOAD");
2482 audio_finish_load_track();
2486 LOGFQUEUE("audio < Q_AUDIO_PLAY");
2487 if (playing
&& ev
.data
<= 0)
2488 audio_new_playlist();
2491 audio_stop_playback();
2492 audio_play_start((size_t)ev
.data
);
2497 LOGFQUEUE("audio < Q_AUDIO_STOP");
2499 audio_stop_playback();
2501 queue_clear(&audio_queue
);
2505 LOGFQUEUE("audio < Q_AUDIO_PAUSE");
2506 if (!(bool) ev
.data
&& skipped_during_pause
&& !pcmbuf_is_crossfade_active())
2507 pcmbuf_play_stop(); /* Flush old track on resume after skip */
2508 skipped_during_pause
= false;
2511 pcmbuf_pause((bool)ev
.data
);
2512 paused
= (bool)ev
.data
;
2516 LOGFQUEUE("audio < Q_AUDIO_SKIP");
2517 audio_initiate_track_change((long)ev
.data
);
2520 case Q_AUDIO_PRE_FF_REWIND
:
2521 LOGFQUEUE("audio < Q_AUDIO_PRE_FF_REWIND");
2527 case Q_AUDIO_FF_REWIND
:
2528 LOGFQUEUE("audio < Q_AUDIO_FF_REWIND");
2533 /* An automatic track skip is in progress. Finalize it,
2534 then go back to the previous track */
2535 audio_finalise_track_change();
2538 ci
.seek_time
= (long)ev
.data
+1;
2541 case Q_AUDIO_CHECK_NEW_TRACK
:
2542 LOGFQUEUE("audio < Q_AUDIO_CHECK_NEW_TRACK");
2543 queue_reply(&audio_queue
, audio_check_new_track());
2546 case Q_AUDIO_DIR_SKIP
:
2547 LOGFQUEUE("audio < Q_AUDIO_DIR_SKIP");
2548 audio_initiate_dir_change(ev
.data
);
2552 LOGFQUEUE("audio < Q_AUDIO_FLUSH");
2553 audio_invalidate_tracks();
2556 case Q_AUDIO_TRACK_CHANGED
:
2557 /* PCM track change done */
2558 LOGFQUEUE("audio < Q_AUDIO_TRACK_CHANGED");
2559 audio_finalise_track_change();
2563 case SYS_USB_CONNECTED
:
2564 LOGFQUEUE("audio < SYS_USB_CONNECTED");
2566 audio_stop_playback();
2567 #ifdef PLAYBACK_VOICE
2570 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
2571 usb_wait_for_disconnect(&audio_queue
);
2573 /* Mark all entries null. */
2574 audio_clear_track_entries();
2576 /* release tracks to make sure all handles are closed */
2577 audio_release_tracks();
2582 LOGFQUEUE_SYS_TIMEOUT("audio < SYS_TIMEOUT");
2586 LOGFQUEUE("audio < default");
2592 /* Initialize the audio system - called from init() in main.c.
2593 * Last function because of all the references to internal symbols
2595 void audio_init(void)
2597 unsigned int audio_thread_id
;
2599 /* Can never do this twice */
2600 if (audio_is_initialized
)
2602 logf("audio: already initialized");
2606 logf("audio: initializing");
2608 /* Initialize queues before giving control elsewhere in case it likes
2609 to send messages. Thread creation will be delayed however so nothing
2610 starts running until ready if something yields such as talk_init. */
2611 queue_init(&audio_queue
, true);
2612 queue_init(&codec_queue
, false);
2613 queue_init(&pcmbuf_queue
, false);
2617 /* Initialize codec api. */
2618 ci
.read_filebuf
= codec_filebuf_callback
;
2619 ci
.pcmbuf_insert
= codec_pcmbuf_insert_callback
;
2620 ci
.codec_get_buffer
= codec_get_buffer
;
2621 ci
.request_buffer
= codec_request_buffer_callback
;
2622 ci
.advance_buffer
= codec_advance_buffer_callback
;
2623 ci
.advance_buffer_loc
= codec_advance_buffer_loc_callback
;
2624 ci
.request_next_track
= codec_request_next_track_callback
;
2625 ci
.seek_buffer
= codec_seek_buffer_callback
;
2626 ci
.seek_complete
= codec_seek_complete_callback
;
2627 ci
.set_elapsed
= codec_set_elapsed_callback
;
2628 ci
.set_offset
= codec_set_offset_callback
;
2629 ci
.configure
= codec_configure_callback
;
2630 ci
.discard_codec
= codec_discard_codec_callback
;
2631 ci
.dsp
= (struct dsp_config
*)dsp_configure(NULL
, DSP_MYDSP
,
2634 thistrack_id3
= &mp3entry_buf
[0];
2635 othertrack_id3
= &mp3entry_buf
[1];
2637 /* cuesheet support */
2638 if (global_settings
.cuesheet
)
2639 curr_cue
= (struct cuesheet
*)buffer_alloc(sizeof(struct cuesheet
));
2641 /* initialize the buffer */
2644 /* audio_reset_buffer must to know the size of voice buffer so init
2648 codec_thread_id
= create_thread(
2649 codec_thread
, codec_stack
, sizeof(codec_stack
),
2650 CREATE_THREAD_FROZEN
,
2651 codec_thread_name
IF_PRIO(, PRIORITY_PLAYBACK
)
2654 queue_enable_queue_send(&codec_queue
, &codec_queue_sender_list
,
2657 audio_thread_id
= create_thread(audio_thread
, audio_stack
,
2658 sizeof(audio_stack
), CREATE_THREAD_FROZEN
,
2659 audio_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
)
2662 queue_enable_queue_send(&audio_queue
, &audio_queue_sender_list
,
2665 #ifdef PLAYBACK_VOICE
2666 voice_thread_init();
2669 /* Set crossfade setting for next buffer init which should be about... */
2670 pcmbuf_crossfade_enable(global_settings
.crossfade
);
2672 /* initialize the buffering system */
2675 /* ...now! Set up the buffers */
2676 audio_reset_buffer();
2679 for(i
= 0; i
< MAX_TRACK
; i
++)
2681 tracks
[i
].audio_hid
= -1;
2682 tracks
[i
].id3_hid
= -1;
2683 tracks
[i
].codec_hid
= -1;
2684 #ifdef HAVE_ALBUMART
2685 tracks
[i
].aa_hid
= -1;
2687 tracks
[i
].cuesheet_hid
= -1;
2690 add_event(BUFFER_EVENT_REBUFFER
, false, buffering_handle_rebuffer_callback
);
2691 add_event(BUFFER_EVENT_FINISHED
, false, buffering_handle_finished_callback
);
2693 /* Probably safe to say */
2694 audio_is_initialized
= true;
2696 sound_settings_apply();
2697 #ifdef HAVE_DISK_STORAGE
2698 audio_set_buffer_margin(global_settings
.buffer_margin
);
2701 /* it's safe to let the threads run now */
2702 #ifdef PLAYBACK_VOICE
2703 voice_thread_resume();
2705 thread_thaw(codec_thread_id
);
2706 thread_thaw(audio_thread_id
);
2710 bool audio_is_thread_ready(void)
2712 return audio_thread_ready
;