Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include <stdbool.h> |
Linus Nielsen Feltzing | 72d2535 | 2004-10-11 07:55:45 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Linus Nielsen Feltzing | c633ceb | 2002-06-07 14:41:26 +0000 | [diff] [blame] | 23 | #include "config.h" |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 24 | |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 25 | #if CONFIG_CODEC != SWCODEC |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 26 | |
Linus Nielsen Feltzing | cb30e54 | 2002-06-30 14:36:58 +0000 | [diff] [blame] | 27 | #include "debug.h" |
| 28 | #include "panic.h" |
Björn Stenberg | 51b45d5 | 2008-10-15 06:38:51 +0000 | [diff] [blame] | 29 | #include "metadata.h" |
Linus Nielsen Feltzing | cb30e54 | 2002-06-30 14:36:58 +0000 | [diff] [blame] | 30 | #include "mpeg.h" |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 31 | #include "audio.h" |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 32 | #include "storage.h" |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 33 | #include "string.h" |
Daniel Stenberg | f9b4490 | 2003-02-07 09:41:57 +0000 | [diff] [blame] | 34 | #include <kernel.h> |
Kjell Ericson | ddefd1e | 2003-01-10 09:19:10 +0000 | [diff] [blame] | 35 | #include "thread.h" |
Linus Nielsen Feltzing | c6db787 | 2003-06-19 12:08:22 +0000 | [diff] [blame] | 36 | #include "errno.h" |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 37 | #include "mp3data.h" |
Thomas Martitz | d0b72e2 | 2011-08-30 14:01:33 +0000 | [diff] [blame] | 38 | #include "core_alloc.h" |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 39 | #include "mp3_playback.h" |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 40 | #include "talk.h" |
Linus Nielsen Feltzing | 674eaca | 2005-04-01 13:41:03 +0000 | [diff] [blame] | 41 | #include "sound.h" |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 42 | #include "system.h" |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 43 | #include "appevents.h" |
Jens Arnold | 5bea40f | 2010-08-22 20:58:32 +0000 | [diff] [blame] | 44 | #include "playlist.h" |
Jonathan Gordon | 24b136f | 2009-07-20 05:18:18 +0000 | [diff] [blame] | 45 | #include "cuesheet.h" |
| 46 | #include "settings.h" |
Linus Nielsen Feltzing | cb30e54 | 2002-06-30 14:36:58 +0000 | [diff] [blame] | 47 | #ifndef SIMULATOR |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 48 | #include "i2c.h" |
Marcin Bukat | 56c4e9f | 2010-10-31 21:09:34 +0000 | [diff] [blame] | 49 | #include "mas35xx.h" |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 50 | #include "system.h" |
Linus Nielsen Feltzing | 392f385 | 2002-06-29 23:01:10 +0000 | [diff] [blame] | 51 | #include "usb.h" |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 52 | #include "file.h" |
Linus Nielsen Feltzing | 8e4a0e0 | 2002-11-10 18:24:40 +0000 | [diff] [blame] | 53 | #include "hwcompat.h" |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 54 | #endif /* !SIMULATOR */ |
Marcoen Hirschberg | b0fee17 | 2005-12-06 13:27:15 +0000 | [diff] [blame] | 55 | #ifdef HAVE_LCD_BITMAP |
| 56 | #include "lcd.h" |
Marcin Bukat | 56c4e9f | 2010-10-31 21:09:34 +0000 | [diff] [blame] | 57 | #endif /* CONFIG_CODEC != SWCODEC */ |
Linus Nielsen Feltzing | bfc8d0a | 2002-08-20 14:22:11 +0000 | [diff] [blame] | 58 | |
Jeffrey Goode | a052102 | 2009-11-16 22:02:06 +0000 | [diff] [blame] | 59 | #define MPEG_SWAP_CHUNKSIZE 0x2000 |
| 60 | #define MPEG_HIGH_WATER 2 /* We leave 2 bytes empty because otherwise we |
| 61 | wouldn't be able to see the difference between |
| 62 | an empty buffer and a full one. */ |
| 63 | #define MPEG_LOW_WATER 0x60000 |
| 64 | #define MPEG_RECORDING_LOW_WATER 0x80000 |
| 65 | #define MPEG_LOW_WATER_CHUNKSIZE 0x40000 |
| 66 | #define MPEG_LOW_WATER_SWAP_CHUNKSIZE 0x10000 |
| 67 | #if (CONFIG_STORAGE & STORAGE_MMC) |
| 68 | #define MPEG_PLAY_PENDING_THRESHOLD 0x20000 |
| 69 | #define MPEG_PLAY_PENDING_SWAPSIZE 0x20000 |
| 70 | #else |
| 71 | #define MPEG_PLAY_PENDING_THRESHOLD 0x10000 |
| 72 | #define MPEG_PLAY_PENDING_SWAPSIZE 0x10000 |
| 73 | #endif |
| 74 | |
| 75 | #define MPEG_MAX_PRERECORD_SECONDS 30 |
| 76 | |
| 77 | /* For ID3 info and VBR header */ |
| 78 | #define MPEG_RESERVED_HEADER_SPACE (4096 + 576) |
| 79 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 80 | #ifndef SIMULATOR |
| 81 | extern unsigned long mas_version_code; |
| 82 | #endif |
| 83 | |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 84 | #if CONFIG_CODEC == MAS3587F |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 85 | extern enum /* from mp3_playback.c */ |
| 86 | { |
| 87 | MPEG_DECODER, |
| 88 | MPEG_ENCODER |
| 89 | } mpeg_mode; |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 90 | #endif /* CONFIG_CODEC == MAS3587F */ |
Linus Nielsen Feltzing | 893095d | 2002-11-10 16:37:26 +0000 | [diff] [blame] | 91 | |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 92 | #define MPEG_PLAY 1 |
| 93 | #define MPEG_STOP 2 |
| 94 | #define MPEG_PAUSE 3 |
| 95 | #define MPEG_RESUME 4 |
| 96 | #define MPEG_NEXT 5 |
| 97 | #define MPEG_PREV 6 |
| 98 | #define MPEG_FF_REWIND 7 |
| 99 | #define MPEG_FLUSH_RELOAD 8 |
| 100 | #define MPEG_RECORD 9 |
| 101 | #define MPEG_INIT_RECORDING 10 |
| 102 | #define MPEG_INIT_PLAYBACK 11 |
| 103 | #define MPEG_NEW_FILE 12 |
| 104 | #define MPEG_PAUSE_RECORDING 13 |
| 105 | #define MPEG_RESUME_RECORDING 14 |
| 106 | #define MPEG_NEED_DATA 100 |
| 107 | #define MPEG_TRACK_CHANGE 101 |
| 108 | #define MPEG_SAVE_DATA 102 |
| 109 | #define MPEG_STOP_DONE 103 |
| 110 | #define MPEG_PRERECORDING_TICK 104 |
Linus Nielsen Feltzing | 893095d | 2002-11-10 16:37:26 +0000 | [diff] [blame] | 111 | |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 112 | /* indicator for MPEG_NEED_DATA */ |
Michael Sevakis | 4b90267 | 2006-12-19 16:50:07 +0000 | [diff] [blame] | 113 | #define GENERATE_UNBUFFER_EVENTS 1 |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 114 | |
Linus Nielsen Feltzing | cb30e54 | 2002-06-30 14:36:58 +0000 | [diff] [blame] | 115 | /* list of tracks in memory */ |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 116 | #define MAX_TRACK_ENTRIES (1<<4) /* Must be power of 2 */ |
| 117 | #define MAX_TRACK_ENTRIES_MASK (MAX_TRACK_ENTRIES - 1) |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 118 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 119 | struct trackdata |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 120 | { |
Linus Nielsen Feltzing | cb30e54 | 2002-06-30 14:36:58 +0000 | [diff] [blame] | 121 | struct mp3entry id3; |
| 122 | int mempos; |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 123 | int load_ahead_index; |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 126 | static struct trackdata trackdata[MAX_TRACK_ENTRIES]; |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 127 | |
Linus Nielsen Feltzing | e5792d6 | 2002-07-25 23:12:41 +0000 | [diff] [blame] | 128 | static unsigned int current_track_counter = 0; |
Linus Nielsen Feltzing | cb30e54 | 2002-06-30 14:36:58 +0000 | [diff] [blame] | 129 | |
| 130 | #ifndef SIMULATOR |
Thomas Martitz | 0c4c816 | 2011-08-30 14:32:16 +0000 | [diff] [blame] | 131 | static void stop_playing(void); |
Bertrik Sikken | f736159 | 2010-01-09 17:42:54 +0000 | [diff] [blame] | 132 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 133 | static int track_read_idx = 0; |
| 134 | static int track_write_idx = 0; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 135 | #endif /* !SIMULATOR */ |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 136 | |
Jonathan Gordon | 24b136f | 2009-07-20 05:18:18 +0000 | [diff] [blame] | 137 | /* Cuesheet support */ |
| 138 | static struct cuesheet *curr_cuesheet = NULL; |
| 139 | static bool checked_for_cuesheet = false; |
Jens Arnold | 509a96d | 2007-03-01 00:26:24 +0000 | [diff] [blame] | 140 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 141 | static const char mpeg_thread_name[] = "mpeg"; |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 142 | static unsigned int audio_thread_id; |
Jens Arnold | 0aa12a9 | 2011-10-09 16:19:51 +0000 | [diff] [blame] | 143 | static bool audio_is_initialized; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 144 | static unsigned int mpeg_errno; |
| 145 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 146 | static bool playing = false; /* We are playing an MP3 stream */ |
| 147 | static bool is_playing = false; /* We are (attempting to) playing MP3 files */ |
| 148 | static bool paused; /* playback is paused */ |
Thomas Martitz | d0b72e2 | 2011-08-30 14:01:33 +0000 | [diff] [blame] | 149 | static int audiobuf_handle; /* handle to the audio buffer */ |
| 150 | static char* mpeg_audiobuf; /* poiunter to the audio buffer */ |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 151 | static long audiobuflen; /* length of the audio buffer */ |
Thomas Martitz | 2a8eacd | 2011-11-17 17:55:02 +0000 | [diff] [blame] | 152 | #define AUDIO_BUFFER_RESERVE (256*1024) |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 153 | #ifdef SIMULATOR |
| 154 | static char mpeg_stack[DEFAULT_STACK_SIZE]; |
| 155 | static struct mp3entry taginfo; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 156 | #else /* !SIMULATOR */ |
Michael Sevakis | b15aa47 | 2011-02-14 11:27:45 +0000 | [diff] [blame] | 157 | static struct event_queue mpeg_queue SHAREDBSS_ATTR; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 158 | static long mpeg_stack[(DEFAULT_STACK_SIZE + 0x1000)/sizeof(long)]; |
| 159 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 160 | static int audiobuf_write; |
| 161 | static int audiobuf_swapwrite; |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 162 | static long audiobuf_read; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 163 | |
| 164 | static int mpeg_file; |
| 165 | |
| 166 | static bool play_pending; /* We are about to start playing */ |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 167 | static bool play_pending_track_change; /* When starting play we're starting a new file */ |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 168 | static bool filling; /* We are filling the buffer with data from disk */ |
| 169 | static bool dma_underrun; /* True when the DMA has stopped because of |
| 170 | slow disk reading (read error, shaking) */ |
| 171 | static bool mpeg_stop_done; |
| 172 | |
| 173 | static int last_dma_tick = 0; |
| 174 | static int last_dma_chunk_size; |
| 175 | |
| 176 | static long low_watermark; /* Dynamic low watermark level */ |
Nils Wallménius | 0bfa3e7 | 2007-08-01 08:50:44 +0000 | [diff] [blame] | 177 | static long low_watermark_margin = 0; /* Extra time in seconds for watermark */ |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 178 | static long lowest_watermark_level; /* Debug value to observe the buffer |
| 179 | usage */ |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 180 | |
| 181 | struct audio_resume_info |
| 182 | { |
| 183 | unsigned long elapsed; |
| 184 | unsigned long offset; |
| 185 | }; |
| 186 | |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 187 | #if CONFIG_CODEC == MAS3587F |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 188 | static char recording_filename[MAX_PATH]; /* argument to thread */ |
| 189 | static char delayed_filename[MAX_PATH]; /* internal copy of above */ |
| 190 | |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 191 | static char xing_buffer[MAX_XING_HEADER_SIZE]; |
| 192 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 193 | static bool init_recording_done; |
| 194 | static bool init_playback_done; |
| 195 | static bool prerecording; /* True if prerecording is enabled */ |
| 196 | static bool is_prerecording; /* True if we are prerecording */ |
| 197 | static bool is_recording; /* We are recording */ |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 198 | |
| 199 | static enum { |
| 200 | NOT_SAVING = 0, /* reasons to save data, sorted by importance */ |
| 201 | BUFFER_FULL, |
| 202 | NEW_FILE, |
| 203 | STOP_RECORDING |
| 204 | } saving_status; |
| 205 | |
| 206 | static int rec_frequency_index; /* For create_xing_header() calls */ |
| 207 | static int rec_version_index; /* For create_xing_header() calls */ |
| 208 | |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 209 | struct prerecord_info { |
| 210 | int mempos; |
| 211 | unsigned long framecount; |
| 212 | }; |
| 213 | |
| 214 | static struct prerecord_info prerecord_buffer[MPEG_MAX_PRERECORD_SECONDS]; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 215 | static int prerecord_index; /* Current index in the prerecord buffer */ |
| 216 | static int prerecording_max_seconds; /* Max number of seconds to store */ |
| 217 | static int prerecord_count; /* Number of seconds in the prerecord buffer */ |
| 218 | static int prerecord_timeout; /* The tick count of the next prerecord data |
| 219 | store */ |
| 220 | |
Bertrik Sikken | f736159 | 2010-01-09 17:42:54 +0000 | [diff] [blame] | 221 | static unsigned long record_start_time; /* Value of current_tick when recording |
| 222 | was started */ |
| 223 | static unsigned long pause_start_time; /* Value of current_tick when pause was |
| 224 | started */ |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 225 | static unsigned long last_rec_time; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 226 | static unsigned long num_rec_bytes; |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 227 | static unsigned long last_rec_bytes; |
| 228 | static unsigned long frame_count_start; |
| 229 | static unsigned long frame_count_end; |
| 230 | static unsigned long saved_header = 0; |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 231 | |
| 232 | /* Shadow MAS registers */ |
| 233 | unsigned long shadow_encoder_control = 0; |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 234 | #endif /* CONFIG_CODEC == MAS3587F */ |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 235 | |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 236 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 237 | unsigned long shadow_io_control_main = 0; |
| 238 | unsigned long shadow_soft_mute = 0; |
| 239 | unsigned shadow_codec_reg0; |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 240 | #endif /* (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */ |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 241 | |
| 242 | #ifdef HAVE_RECORDING |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 243 | static const unsigned char empty_id3_header[] = |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 244 | { |
| 245 | 'I', 'D', '3', 0x03, 0x00, 0x00, |
| 246 | 0x00, 0x00, 0x1f, 0x76 /* Size is 4096 minus 10 bytes for the header */ |
| 247 | }; |
| 248 | #endif /* HAVE_RECORDING */ |
| 249 | |
| 250 | |
| 251 | static int get_unplayed_space(void); |
| 252 | static int get_playable_space(void); |
| 253 | static int get_unswapped_space(void); |
| 254 | #endif /* !SIMULATOR */ |
| 255 | |
Jens Arnold | 8051a0b | 2005-11-06 23:12:11 +0000 | [diff] [blame] | 256 | #if (CONFIG_CODEC == MAS3587F) && !defined(SIMULATOR) |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 257 | static void init_recording(void); |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 258 | static void prepend_header(void); |
| 259 | static void update_header(void); |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 260 | static void start_prerecording(void); |
| 261 | static void start_recording(void); |
| 262 | static void stop_recording(void); |
| 263 | static int get_unsaved_space(void); |
| 264 | static void pause_recording(void); |
| 265 | static void resume_recording(void); |
Jens Arnold | 8051a0b | 2005-11-06 23:12:11 +0000 | [diff] [blame] | 266 | #endif /* (CONFIG_CODEC == MAS3587F) && !defined(SIMULATOR) */ |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 267 | |
Thomas Martitz | ee7c135 | 2013-12-23 12:48:26 +0100 | [diff] [blame] | 268 | static void audio_reset_buffer_noalloc(void* buf, size_t bufsize); |
| 269 | static void audio_reset_buffer(void); |
| 270 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 271 | |
| 272 | #ifndef SIMULATOR |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 273 | static int num_tracks_in_memory(void) |
| 274 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 275 | return (track_write_idx - track_read_idx) & MAX_TRACK_ENTRIES_MASK; |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 276 | } |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 277 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 278 | #ifdef DEBUG_TAGS |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 279 | static void debug_tags(void) |
| 280 | { |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 281 | int i; |
| 282 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 283 | for(i = 0;i < MAX_TRACK_ENTRIES;i++) |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 284 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 285 | DEBUGF("%d - %s\n", i, trackdata[i].id3.path); |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 286 | } |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 287 | DEBUGF("read: %d, write :%d\n", track_read_idx, track_write_idx); |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 288 | DEBUGF("num_tracks_in_memory: %d\n", num_tracks_in_memory()); |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 289 | } |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 290 | #else /* !DEBUG_TAGS */ |
| 291 | #define debug_tags() |
| 292 | #endif /* !DEBUG_TAGS */ |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 293 | |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 294 | static void remove_current_tag(void) |
| 295 | { |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 296 | if(num_tracks_in_memory() > 0) |
| 297 | { |
| 298 | /* First move the index, so nobody tries to access the tag */ |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 299 | track_read_idx = (track_read_idx+1) & MAX_TRACK_ENTRIES_MASK; |
Jonathan Gordon | 24b136f | 2009-07-20 05:18:18 +0000 | [diff] [blame] | 300 | checked_for_cuesheet = false; |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 301 | debug_tags(); |
| 302 | } |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 303 | else |
| 304 | { |
| 305 | DEBUGF("remove_current_tag: no tracks to remove\n"); |
| 306 | } |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 309 | static void remove_all_non_current_tags(void) |
| 310 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 311 | track_write_idx = (track_read_idx+1) & MAX_TRACK_ENTRIES_MASK; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 312 | debug_tags(); |
| 313 | } |
| 314 | |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 315 | static void remove_all_tags(void) |
| 316 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 317 | track_write_idx = track_read_idx; |
Hardeep Sidhu | d3d1b98 | 2002-12-19 00:58:11 +0000 | [diff] [blame] | 318 | |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 319 | debug_tags(); |
| 320 | } |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 321 | |
| 322 | static struct trackdata *get_trackdata(int offset) |
| 323 | { |
Henrik Backe | a670e34 | 2005-07-08 09:55:22 +0000 | [diff] [blame] | 324 | if(offset >= num_tracks_in_memory()) |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 325 | return NULL; |
| 326 | else |
| 327 | return &trackdata[(track_read_idx + offset) & MAX_TRACK_ENTRIES_MASK]; |
| 328 | } |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 329 | #endif /* !SIMULATOR */ |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 330 | |
Linus Nielsen Feltzing | 0ad617c | 2005-08-21 23:01:12 +0000 | [diff] [blame] | 331 | /***********************************************************************/ |
| 332 | /* audio event handling */ |
| 333 | |
| 334 | #define MAX_EVENT_HANDLERS 10 |
| 335 | struct event_handlers_table |
| 336 | { |
| 337 | AUDIO_EVENT_HANDLER handler; |
| 338 | unsigned short mask; |
| 339 | }; |
| 340 | static struct event_handlers_table event_handlers[MAX_EVENT_HANDLERS]; |
| 341 | static int event_handlers_count = 0; |
| 342 | |
| 343 | void audio_register_event_handler(AUDIO_EVENT_HANDLER handler, unsigned short mask) |
| 344 | { |
| 345 | if (event_handlers_count < MAX_EVENT_HANDLERS) |
| 346 | { |
| 347 | event_handlers[event_handlers_count].handler = handler; |
| 348 | event_handlers[event_handlers_count].mask = mask; |
| 349 | event_handlers_count++; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* dispatch calls each handler in the order registered and returns after some |
| 354 | handler actually handles the event (the event is assumed to no longer be valid |
| 355 | after this, due to the handler changing some condition); returns true if someone |
| 356 | handled the event, which is expected to cause the caller to skip its own handling |
| 357 | of the event */ |
| 358 | #ifndef SIMULATOR |
| 359 | static bool audio_dispatch_event(unsigned short event, unsigned long data) |
| 360 | { |
| 361 | int i = 0; |
| 362 | for(i=0; i < event_handlers_count; i++) |
| 363 | { |
| 364 | if ( event_handlers[i].mask & event ) |
| 365 | { |
| 366 | int rc = event_handlers[i].handler(event, data); |
| 367 | if ( rc == AUDIO_EVENT_RC_HANDLED ) |
| 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | return false; |
| 372 | } |
Michael Sevakis | 023f6b6 | 2013-07-12 12:06:38 -0400 | [diff] [blame] | 373 | |
| 374 | static void send_track_event(unsigned int id, struct mp3entry *id3) |
| 375 | { |
| 376 | struct mp3entry *cur_id3 = |
| 377 | &trackdata[track_read_idx & MAX_TRACK_ENTRIES_MASK].id3; |
| 378 | unsigned int flags = id3 == cur_id3 ? TEF_CURRENT : 0; |
| 379 | send_event(id, &(struct track_event){ .flags = flags, .id3 = id3 }); |
| 380 | } |
| 381 | #endif /* SIMULATOR */ |
Linus Nielsen Feltzing | 0ad617c | 2005-08-21 23:01:12 +0000 | [diff] [blame] | 382 | |
| 383 | /***********************************************************************/ |
| 384 | |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 385 | static void set_elapsed(struct mp3entry* id3) |
| 386 | { |
| 387 | if ( id3->vbr ) { |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 388 | if ( id3->has_toc ) { |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 389 | /* calculate elapsed time using TOC */ |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 390 | int i; |
| 391 | unsigned int remainder, plen, relpos, nextpos; |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 392 | |
| 393 | /* find wich percent we're at */ |
| 394 | for (i=0; i<100; i++ ) |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 395 | { |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 396 | if ( id3->offset < id3->toc[i] * (id3->filesize / 256) ) |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 397 | { |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 398 | break; |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 401 | |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 402 | i--; |
| 403 | if (i < 0) |
| 404 | i = 0; |
| 405 | |
| 406 | relpos = id3->toc[i]; |
| 407 | |
| 408 | if (i < 99) |
| 409 | { |
| 410 | nextpos = id3->toc[i+1]; |
| 411 | } |
| 412 | else |
| 413 | { |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 414 | nextpos = 256; |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | remainder = id3->offset - (relpos * (id3->filesize / 256)); |
| 418 | |
Björn Stenberg | 972f431 | 2003-10-17 15:11:09 +0000 | [diff] [blame] | 419 | /* set time for this percent (divide before multiply to prevent |
| 420 | overflow on long files. loss of precision is negligible on |
| 421 | short files) */ |
| 422 | id3->elapsed = i * (id3->length / 100); |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 423 | |
| 424 | /* calculate remainder time */ |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 425 | plen = (nextpos - relpos) * (id3->filesize / 256); |
Björn Stenberg | 972f431 | 2003-10-17 15:11:09 +0000 | [diff] [blame] | 426 | id3->elapsed += (((remainder * 100) / plen) * |
| 427 | (id3->length / 10000)); |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 428 | } |
| 429 | else { |
| 430 | /* no TOC exists. set a rough estimate using average bitrate */ |
Björn Stenberg | 989a3ec | 2003-01-09 01:02:40 +0000 | [diff] [blame] | 431 | int tpk = id3->length / (id3->filesize / 1024); |
Björn Stenberg | 972f431 | 2003-10-17 15:11:09 +0000 | [diff] [blame] | 432 | id3->elapsed = id3->offset / 1024 * tpk; |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | else |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 436 | /* constant bitrate, use exact calculation */ |
| 437 | id3->elapsed = id3->offset / (id3->bitrate / 8); |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 440 | static int audio_get_file_pos_int(struct mp3entry *id3) |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 441 | { |
| 442 | int pos = -1; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 443 | |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 444 | if (id3->vbr) |
| 445 | { |
| 446 | if (id3->has_toc) |
| 447 | { |
| 448 | /* Use the TOC to find the new position */ |
| 449 | unsigned int percent, remainder; |
| 450 | int curtoc, nexttoc, plen; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 451 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 452 | percent = (id3->elapsed*100)/id3->length; |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 453 | if (percent > 99) |
| 454 | percent = 99; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 455 | |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 456 | curtoc = id3->toc[percent]; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 457 | |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 458 | if (percent < 99) |
| 459 | nexttoc = id3->toc[percent+1]; |
| 460 | else |
| 461 | nexttoc = 256; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 462 | |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 463 | pos = (id3->filesize/256)*curtoc; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 464 | |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 465 | /* Use the remainder to get a more accurate position */ |
| 466 | remainder = (id3->elapsed*100)%id3->length; |
| 467 | remainder = (remainder*100)/id3->length; |
| 468 | plen = (nexttoc - curtoc)*(id3->filesize/256); |
| 469 | pos += (plen/100)*remainder; |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | /* No TOC exists, estimate the new position */ |
| 474 | pos = (id3->filesize / (id3->length / 1000)) * |
| 475 | (id3->elapsed / 1000); |
| 476 | } |
| 477 | } |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 478 | else if (id3->bitrate) |
| 479 | pos = id3->elapsed * (id3->bitrate / 8); |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 480 | else |
| 481 | { |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | if (pos >= (int)(id3->filesize - id3->id3v1len)) |
| 486 | { |
| 487 | /* Don't seek right to the end of the file so that we can |
| 488 | transition properly to the next song */ |
| 489 | pos = id3->filesize - id3->id3v1len - 1; |
| 490 | } |
| 491 | else if (pos < (int)id3->first_frame_offset) |
| 492 | { |
| 493 | /* skip past id3v2 tag and other leading garbage */ |
| 494 | pos = id3->first_frame_offset; |
| 495 | } |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 496 | return pos; |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 499 | int audio_get_file_pos(void) |
| 500 | { |
| 501 | struct mp3entry *id3 = audio_current_track(); |
| 502 | return id3 ? audio_get_file_pos_int(id3) : 0; |
| 503 | } |
| 504 | |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 505 | unsigned long mpeg_get_last_header(void) |
| 506 | { |
| 507 | #ifdef SIMULATOR |
| 508 | return 0; |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 509 | #else /* !SIMULATOR */ |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 510 | unsigned long tmp[2]; |
| 511 | |
| 512 | /* Read the frame data from the MAS and reconstruct it with the |
| 513 | frame sync and all */ |
Jens Arnold | eaa1f73 | 2004-09-29 19:51:41 +0000 | [diff] [blame] | 514 | mas_readmem(MAS_BANK_D0, MAS_D0_MPEG_STATUS_1, tmp, 2); |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 515 | return 0xffe00000 | ((tmp[0] & 0x7c00) << 6) | (tmp[1] & 0xffff); |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 516 | #endif /* !SIMULATOR */ |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 519 | static void do_stop(void) |
| 520 | { |
| 521 | is_playing = false; |
| 522 | paused = false; |
| 523 | |
| 524 | #ifndef SIMULATOR |
| 525 | if (playing) |
| 526 | playlist_update_resume_info(audio_current_track()); |
| 527 | |
| 528 | stop_playing(); |
| 529 | mpeg_stop_done = true; |
| 530 | #else |
| 531 | playing = false; |
| 532 | #endif |
| 533 | } |
| 534 | |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 535 | /* Buffer must not move. */ |
| 536 | static int shrink_callback(int handle, unsigned hints, void* start, size_t old_size) |
| 537 | { |
Thomas Martitz | 2a8eacd | 2011-11-17 17:55:02 +0000 | [diff] [blame] | 538 | ssize_t extradata_size = old_size - audiobuflen; |
| 539 | /* check what buflib requests */ |
| 540 | size_t wanted_size = (hints & BUFLIB_SHRINK_SIZE_MASK); |
| 541 | ssize_t size = (ssize_t)old_size - wanted_size; |
Thomas Martitz | ee7c135 | 2013-12-23 12:48:26 +0100 | [diff] [blame] | 542 | |
Thomas Martitz | 10d71bb | 2013-12-23 13:02:59 +0100 | [diff] [blame] | 543 | #if !defined(SIMULATOR) && (CONFIG_CODEC == MAS3587F) |
Thomas Martitz | ee7c135 | 2013-12-23 12:48:26 +0100 | [diff] [blame] | 544 | /* FIXME: Cannot give the buffer during recording yet */ |
| 545 | if (is_recording) |
| 546 | return BUFLIB_CB_CANNOT_SHRINK; |
| 547 | #endif |
| 548 | |
Thomas Martitz | 2a8eacd | 2011-11-17 17:55:02 +0000 | [diff] [blame] | 549 | /* keep at least 256K for the buffering */ |
| 550 | if ((size - extradata_size) < AUDIO_BUFFER_RESERVE) |
Thomas Martitz | 22e802e | 2013-05-30 11:24:16 +0200 | [diff] [blame] | 551 | { |
| 552 | /* check if buflib needs the memory really hard. if yes we give |
| 553 | * up playback for now, otherwise refuse to shrink to keep at least |
| 554 | * 256K for the buffering */ |
| 555 | if ((hints & BUFLIB_SHRINK_POS_MASK) != BUFLIB_SHRINK_POS_MASK) |
| 556 | return BUFLIB_CB_CANNOT_SHRINK; |
| 557 | } |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 558 | /* TODO: Do it without stopping playback, if possible */ |
Thomas Martitz | 2a8eacd | 2011-11-17 17:55:02 +0000 | [diff] [blame] | 559 | bool playing = (audio_status() & AUDIO_STATUS_PLAY) == AUDIO_STATUS_PLAY; |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 560 | struct mp3entry *id3 = audio_current_track(); |
| 561 | unsigned long elapsed = 0, offset = 0; |
| 562 | if (id3) |
| 563 | { |
| 564 | elapsed = id3->elapsed; |
| 565 | offset = id3->offset; |
| 566 | } |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 567 | /* don't call audio_hard_stop() as it frees this handle */ |
| 568 | if (thread_self() == audio_thread_id) |
| 569 | { /* inline case MPEG_STOP (audio_stop()) response |
| 570 | * if we're in the audio thread since audio_stop() otherwise deadlocks */ |
| 571 | do_stop(); |
| 572 | } |
| 573 | else |
| 574 | audio_stop(); |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 575 | |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 576 | switch (hints & BUFLIB_SHRINK_POS_MASK) |
| 577 | { |
| 578 | case BUFLIB_SHRINK_POS_BACK: |
| 579 | core_shrink(handle, start, size); |
| 580 | audio_reset_buffer_noalloc(start, size); |
| 581 | break; |
| 582 | case BUFLIB_SHRINK_POS_FRONT: |
| 583 | core_shrink(handle, start + wanted_size, size); |
| 584 | audio_reset_buffer_noalloc(start + wanted_size, size); |
| 585 | break; |
Thomas Martitz | 22e802e | 2013-05-30 11:24:16 +0200 | [diff] [blame] | 586 | case BUFLIB_SHRINK_POS_MASK: |
| 587 | audiobuf_handle = core_free(audiobuf_handle); |
| 588 | mpeg_audiobuf = NULL; |
| 589 | talk_buffer_set_policy(TALK_BUFFER_DEFAULT); |
| 590 | playing = false; |
| 591 | break; |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 592 | } |
Thomas Martitz | ca2b82b | 2011-11-06 12:01:43 +0000 | [diff] [blame] | 593 | if (playing) |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 594 | { /* safe to call even from the audio thread (due to queue_post()) */ |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 595 | audio_play(elapsed, offset); |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | return BUFLIB_CB_OK; |
| 599 | } |
| 600 | |
| 601 | static struct buflib_callbacks ops = { |
| 602 | .move_callback = NULL, |
| 603 | .shrink_callback = shrink_callback, |
| 604 | }; |
| 605 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 606 | #ifndef SIMULATOR |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 607 | /* Send callback events to notify about removing old tracks. */ |
| 608 | static void generate_unbuffer_events(void) |
| 609 | { |
| 610 | int i; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 611 | int numentries = MAX_TRACK_ENTRIES - num_tracks_in_memory(); |
| 612 | int cur_idx = track_write_idx; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 613 | |
| 614 | for (i = 0; i < numentries; i++) |
| 615 | { |
| 616 | /* Send an event to notify that track has finished. */ |
Michael Sevakis | 023f6b6 | 2013-07-12 12:06:38 -0400 | [diff] [blame] | 617 | send_track_event(PLAYBACK_EVENT_TRACK_FINISH, &trackdata[cur_idx].id3); |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 618 | cur_idx = (cur_idx + 1) & MAX_TRACK_ENTRIES_MASK; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /* Send callback events to notify about new tracks. */ |
| 623 | static void generate_postbuffer_events(void) |
| 624 | { |
| 625 | int i; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 626 | int numentries = num_tracks_in_memory(); |
| 627 | int cur_idx = track_read_idx; |
| 628 | |
| 629 | for (i = 0; i < numentries; i++) |
| 630 | { |
Michael Sevakis | 023f6b6 | 2013-07-12 12:06:38 -0400 | [diff] [blame] | 631 | send_track_event(PLAYBACK_EVENT_TRACK_BUFFER, &trackdata[cur_idx].id3); |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 632 | cur_idx = (cur_idx + 1) & MAX_TRACK_ENTRIES_MASK; |
| 633 | } |
| 634 | } |
| 635 | |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 636 | static void recalculate_watermark(int bitrate) |
| 637 | { |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 638 | int bytes_per_sec; |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 639 | int time = storage_spinup_time(); |
Linus Nielsen Feltzing | 672479c | 2002-12-05 21:56:53 +0000 | [diff] [blame] | 640 | |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 641 | /* A bitrate of 0 probably means empty VBR header. We play safe |
| 642 | and set a high threshold */ |
| 643 | if(bitrate == 0) |
Linus Nielsen Feltzing | 27539aa | 2003-03-24 11:40:59 +0000 | [diff] [blame] | 644 | bitrate = 320; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 645 | |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 646 | bytes_per_sec = bitrate * 1000 / 8; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 647 | |
Linus Nielsen Feltzing | 672479c | 2002-12-05 21:56:53 +0000 | [diff] [blame] | 648 | if(time) |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 649 | { |
Linus Nielsen Feltzing | c9feaaf | 2002-12-05 23:02:36 +0000 | [diff] [blame] | 650 | /* No drive spins up faster than 3.5s */ |
| 651 | if(time < 350) |
| 652 | time = 350; |
Linus Nielsen Feltzing | 672479c | 2002-12-05 21:56:53 +0000 | [diff] [blame] | 653 | |
| 654 | time = time * 3; |
| 655 | low_watermark = ((low_watermark_margin * HZ + time) * |
| 656 | bytes_per_sec) / HZ; |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 657 | } |
| 658 | else |
| 659 | { |
| 660 | low_watermark = MPEG_LOW_WATER; |
| 661 | } |
| 662 | } |
| 663 | |
Frank Gevaerts | 4657301 | 2008-10-07 19:37:33 +0000 | [diff] [blame] | 664 | #ifdef HAVE_DISK_STORAGE |
Jeffrey Goode | db82be4 | 2009-11-16 20:09:46 +0000 | [diff] [blame] | 665 | void audio_set_buffer_margin(int setting) |
Linus Nielsen Feltzing | b8ff5f8 | 2002-12-05 13:09:51 +0000 | [diff] [blame] | 666 | { |
Jeffrey Goode | db82be4 | 2009-11-16 20:09:46 +0000 | [diff] [blame] | 667 | low_watermark_margin = setting; /* in seconds */ |
Linus Nielsen Feltzing | b8ff5f8 | 2002-12-05 13:09:51 +0000 | [diff] [blame] | 668 | } |
Nils Wallménius | 0bfa3e7 | 2007-08-01 08:50:44 +0000 | [diff] [blame] | 669 | #endif |
Linus Nielsen Feltzing | b8ff5f8 | 2002-12-05 13:09:51 +0000 | [diff] [blame] | 670 | |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 671 | void audio_get_debugdata(struct audio_debug *dbgdata) |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 672 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 673 | dbgdata->audiobuflen = audiobuflen; |
| 674 | dbgdata->audiobuf_write = audiobuf_write; |
| 675 | dbgdata->audiobuf_swapwrite = audiobuf_swapwrite; |
| 676 | dbgdata->audiobuf_read = audiobuf_read; |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 677 | |
| 678 | dbgdata->last_dma_chunk_size = last_dma_chunk_size; |
| 679 | |
Daniel Stenberg | 301f53f | 2005-02-02 22:04:15 +0000 | [diff] [blame] | 680 | #if CONFIG_CPU == SH7034 |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 681 | dbgdata->dma_on = (SCR0 & 0x80) != 0; |
Daniel Stenberg | 301f53f | 2005-02-02 22:04:15 +0000 | [diff] [blame] | 682 | #endif |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 683 | dbgdata->playing = playing; |
| 684 | dbgdata->play_pending = play_pending; |
| 685 | dbgdata->is_playing = is_playing; |
| 686 | dbgdata->filling = filling; |
| 687 | dbgdata->dma_underrun = dma_underrun; |
| 688 | |
| 689 | dbgdata->unplayed_space = get_unplayed_space(); |
Linus Nielsen Feltzing | d29248d | 2002-12-05 14:11:48 +0000 | [diff] [blame] | 690 | dbgdata->playable_space = get_playable_space(); |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 691 | dbgdata->unswapped_space = get_unswapped_space(); |
Linus Nielsen Feltzing | bf303de | 2002-10-15 07:23:18 +0000 | [diff] [blame] | 692 | |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 693 | dbgdata->low_watermark_level = low_watermark; |
Linus Nielsen Feltzing | bf303de | 2002-10-15 07:23:18 +0000 | [diff] [blame] | 694 | dbgdata->lowest_watermark_level = lowest_watermark_level; |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Linus Nielsen Feltzing | 03c13a1 | 2002-10-08 07:43:52 +0000 | [diff] [blame] | 697 | #ifdef DEBUG |
| 698 | static void dbg_timer_start(void) |
| 699 | { |
| 700 | /* We are using timer 2 */ |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 701 | |
Linus Nielsen Feltzing | 03c13a1 | 2002-10-08 07:43:52 +0000 | [diff] [blame] | 702 | TSTR &= ~0x04; /* Stop the timer */ |
| 703 | TSNC &= ~0x04; /* No synchronization */ |
| 704 | TMDR &= ~0x44; /* Operate normally */ |
| 705 | |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 706 | TCNT2 = 0; /* Start counting at 0 */ |
| 707 | TCR2 = 0x03; /* Sysclock/8 */ |
Linus Nielsen Feltzing | 03c13a1 | 2002-10-08 07:43:52 +0000 | [diff] [blame] | 708 | |
| 709 | TSTR |= 0x04; /* Start timer 2 */ |
| 710 | } |
| 711 | |
| 712 | static int dbg_cnt2us(unsigned int cnt) |
| 713 | { |
| 714 | return (cnt * 10000) / (FREQ/800); |
| 715 | } |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 716 | #endif /* DEBUG */ |
Linus Nielsen Feltzing | 03c13a1 | 2002-10-08 07:43:52 +0000 | [diff] [blame] | 717 | |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 718 | static int get_unplayed_space(void) |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 719 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 720 | int space = audiobuf_write - audiobuf_read; |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 721 | if (space < 0) |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 722 | space += audiobuflen; |
Björn Stenberg | 749d87b | 2002-08-15 16:48:34 +0000 | [diff] [blame] | 723 | return space; |
| 724 | } |
| 725 | |
Linus Nielsen Feltzing | d29248d | 2002-12-05 14:11:48 +0000 | [diff] [blame] | 726 | static int get_playable_space(void) |
| 727 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 728 | int space = audiobuf_swapwrite - audiobuf_read; |
Linus Nielsen Feltzing | d29248d | 2002-12-05 14:11:48 +0000 | [diff] [blame] | 729 | if (space < 0) |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 730 | space += audiobuflen; |
Linus Nielsen Feltzing | d29248d | 2002-12-05 14:11:48 +0000 | [diff] [blame] | 731 | return space; |
| 732 | } |
| 733 | |
Björn Stenberg | 749d87b | 2002-08-15 16:48:34 +0000 | [diff] [blame] | 734 | static int get_unplayed_space_current_song(void) |
| 735 | { |
| 736 | int space; |
| 737 | |
| 738 | if (num_tracks_in_memory() > 1) |
| 739 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 740 | space = get_trackdata(1)->mempos - audiobuf_read; |
Björn Stenberg | 749d87b | 2002-08-15 16:48:34 +0000 | [diff] [blame] | 741 | } |
| 742 | else |
| 743 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 744 | space = audiobuf_write - audiobuf_read; |
Björn Stenberg | 749d87b | 2002-08-15 16:48:34 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | if (space < 0) |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 748 | space += audiobuflen; |
Björn Stenberg | 749d87b | 2002-08-15 16:48:34 +0000 | [diff] [blame] | 749 | |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 750 | return space; |
| 751 | } |
| 752 | |
Hardeep Sidhu | 67f95ec | 2002-10-03 07:28:28 +0000 | [diff] [blame] | 753 | static int get_unswapped_space(void) |
| 754 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 755 | int space = audiobuf_write - audiobuf_swapwrite; |
Hardeep Sidhu | 67f95ec | 2002-10-03 07:28:28 +0000 | [diff] [blame] | 756 | if (space < 0) |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 757 | space += audiobuflen; |
Hardeep Sidhu | 67f95ec | 2002-10-03 07:28:28 +0000 | [diff] [blame] | 758 | return space; |
| 759 | } |
| 760 | |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 761 | #if CONFIG_CODEC == MAS3587F |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 762 | static int get_unsaved_space(void) |
| 763 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 764 | int space = audiobuf_write - audiobuf_read; |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 765 | if (space < 0) |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 766 | space += audiobuflen; |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 767 | return space; |
| 768 | } |
Linus Nielsen Feltzing | 2c0b72f | 2002-11-25 00:02:54 +0000 | [diff] [blame] | 769 | |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 770 | static void drain_dma_buffer(void) |
Linus Nielsen Feltzing | 2c0b72f | 2002-11-25 00:02:54 +0000 | [diff] [blame] | 771 | { |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 772 | while (PBDRH & 0x40) |
| 773 | { |
| 774 | xor_b(0x08, &PADRH); |
Linus Nielsen Feltzing | 2c0b72f | 2002-11-25 00:02:54 +0000 | [diff] [blame] | 775 | |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 776 | while (PBDRH & 0x80); |
Linus Nielsen Feltzing | 2c0b72f | 2002-11-25 00:02:54 +0000 | [diff] [blame] | 777 | |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 778 | xor_b(0x08, &PADRH); |
Jens Arnold | 536dff1 | 2004-08-05 17:06:31 +0000 | [diff] [blame] | 779 | |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 780 | while (!(PBDRH & 0x80)); |
| 781 | } |
Linus Nielsen Feltzing | 2c0b72f | 2002-11-25 00:02:54 +0000 | [diff] [blame] | 782 | } |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 783 | |
Jens Arnold | 329caa8 | 2005-08-19 22:39:01 +0000 | [diff] [blame] | 784 | #ifdef DEBUG |
| 785 | static long timing_info_index = 0; |
| 786 | static long timing_info[1024]; |
| 787 | #endif /* DEBUG */ |
| 788 | |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 789 | void rec_tick (void) __attribute__ ((section (".icode"))); |
| 790 | void rec_tick(void) |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 791 | { |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 792 | int i; |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 793 | int delay; |
| 794 | char data; |
| 795 | |
| 796 | if(is_recording && (PBDRH & 0x40)) |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 797 | { |
Linus Nielsen Feltzing | 0dcd47e | 2002-11-19 09:50:19 +0000 | [diff] [blame] | 798 | #ifdef DEBUG |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 799 | timing_info[timing_info_index++] = current_tick; |
| 800 | TCNT2 = 0; |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 801 | #endif /* DEBUG */ |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 802 | /* Note: Although this loop is run in interrupt context, further |
| 803 | * optimisation will do no good. The MAS would then deliver bad |
| 804 | * frames occasionally, as observed in extended experiments. */ |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 805 | i = 0; |
| 806 | while (PBDRH & 0x40) /* We try to read as long as EOD is high */ |
| 807 | { |
| 808 | xor_b(0x08, &PADRH); /* Set PR active, independent of polarity */ |
Linus Nielsen Feltzing | c833a81 | 2002-11-24 00:23:49 +0000 | [diff] [blame] | 809 | |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 810 | delay = 100; |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 811 | while (PBDRH & 0x80) /* Wait until /RTW becomes active */ |
| 812 | { |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 813 | if (--delay <= 0) /* Bail out if we have to wait too long */ |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 814 | { /* i.e. the MAS doesn't want to talk to us */ |
| 815 | xor_b(0x08, &PADRH); /* Set PR inactive */ |
| 816 | goto transfer_end; /* and get out of here */ |
| 817 | } |
| 818 | } |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 819 | |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 820 | data = *(unsigned char *)0x04000000; /* read data byte */ |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 821 | |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 822 | xor_b(0x08, &PADRH); /* Set PR inactive */ |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 823 | |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 824 | mpeg_audiobuf[audiobuf_write++] = data; |
Jens Arnold | ff3add9 | 2005-05-23 18:56:15 +0000 | [diff] [blame] | 825 | |
| 826 | if (audiobuf_write >= audiobuflen) |
| 827 | audiobuf_write = 0; |
| 828 | |
| 829 | i++; |
| 830 | } |
| 831 | transfer_end: |
| 832 | |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 833 | #ifdef DEBUG |
| 834 | timing_info[timing_info_index++] = TCNT2 + (i << 16); |
| 835 | timing_info_index &= 0x3ff; |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 836 | #endif /* DEBUG */ |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 837 | |
| 838 | num_rec_bytes += i; |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 839 | |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 840 | if(is_prerecording) |
| 841 | { |
| 842 | if(TIME_AFTER(current_tick, prerecord_timeout)) |
| 843 | { |
| 844 | prerecord_timeout = current_tick + HZ; |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 845 | queue_post(&mpeg_queue, MPEG_PRERECORDING_TICK, 0); |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | else |
| 849 | { |
| 850 | /* Signal to save the data if we are running out of buffer |
| 851 | space */ |
Jens Arnold | 363bc49 | 2005-08-18 04:38:57 +0000 | [diff] [blame] | 852 | if (audiobuflen - get_unsaved_space() < MPEG_RECORDING_LOW_WATER |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 853 | && saving_status == NOT_SAVING) |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 854 | { |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 855 | saving_status = BUFFER_FULL; |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 856 | queue_post(&mpeg_queue, MPEG_SAVE_DATA, 0); |
Linus Nielsen Feltzing | 00b2aad | 2002-12-12 02:22:01 +0000 | [diff] [blame] | 857 | } |
Linus Nielsen Feltzing | 893095d | 2002-11-10 16:37:26 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 860 | } |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 861 | #endif /* CONFIG_CODEC == MAS3587F */ |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 862 | |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 863 | void playback_tick(void) |
| 864 | { |
Linus Nielsen Feltzing | 0ad617c | 2005-08-21 23:01:12 +0000 | [diff] [blame] | 865 | struct trackdata *ptd = get_trackdata(0); |
Linus Nielsen Feltzing | e54aa2a | 2005-08-30 22:50:56 +0000 | [diff] [blame] | 866 | if(ptd) |
| 867 | { |
| 868 | ptd->id3.elapsed += (current_tick - last_dma_tick) * 1000 / HZ; |
| 869 | last_dma_tick = current_tick; |
| 870 | audio_dispatch_event(AUDIO_EVENT_POS_REPORT, |
| 871 | (unsigned long)ptd->id3.elapsed); |
| 872 | } |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 875 | static void reset_mp3_buffer(void) |
| 876 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 877 | audiobuf_read = 0; |
| 878 | audiobuf_write = 0; |
| 879 | audiobuf_swapwrite = 0; |
| 880 | lowest_watermark_level = audiobuflen; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 883 | /* DMA transfer end interrupt callback */ |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 884 | static void transfer_end(const void** ppbuf, size_t* psize) |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 885 | { |
Björn Stenberg | 34486b7 | 2002-08-19 11:00:29 +0000 | [diff] [blame] | 886 | if(playing && !paused) |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 887 | { |
Linus Nielsen Feltzing | a46a5d3 | 2002-08-08 13:29:31 +0000 | [diff] [blame] | 888 | int unplayed_space_left; |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 889 | int space_until_end_of_buffer; |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 890 | int track_offset = 1; |
| 891 | struct trackdata *track; |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 892 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 893 | audiobuf_read += last_dma_chunk_size; |
| 894 | if(audiobuf_read >= audiobuflen) |
| 895 | audiobuf_read = 0; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 896 | |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 897 | /* First, check if we are on a track boundary */ |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 898 | if (num_tracks_in_memory() > 1) |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 899 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 900 | if (audiobuf_read == get_trackdata(track_offset)->mempos) |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 901 | { |
Linus Nielsen Feltzing | 0ad617c | 2005-08-21 23:01:12 +0000 | [diff] [blame] | 902 | if ( ! audio_dispatch_event(AUDIO_EVENT_END_OF_TRACK, 0) ) |
| 903 | { |
| 904 | queue_post(&mpeg_queue, MPEG_TRACK_CHANGE, 0); |
| 905 | track_offset++; |
| 906 | } |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 909 | |
Linus Nielsen Feltzing | a46a5d3 | 2002-08-08 13:29:31 +0000 | [diff] [blame] | 910 | unplayed_space_left = get_unplayed_space(); |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 911 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 912 | space_until_end_of_buffer = audiobuflen - audiobuf_read; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 913 | |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 914 | if(!filling && unplayed_space_left < low_watermark) |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 915 | { |
Linus Nielsen Feltzing | 3a25aa4 | 2002-05-29 12:25:21 +0000 | [diff] [blame] | 916 | filling = true; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 917 | queue_post(&mpeg_queue, MPEG_NEED_DATA, GENERATE_UNBUFFER_EVENTS); |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 918 | } |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 919 | |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 920 | if(unplayed_space_left) |
| 921 | { |
Linus Nielsen Feltzing | 9a9d9d0 | 2003-01-21 00:11:10 +0000 | [diff] [blame] | 922 | last_dma_chunk_size = MIN(0x2000, unplayed_space_left); |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 923 | last_dma_chunk_size = MIN(last_dma_chunk_size, |
| 924 | space_until_end_of_buffer); |
Björn Stenberg | bb9aaf5 | 2002-06-25 13:26:04 +0000 | [diff] [blame] | 925 | |
Björn Stenberg | 200d226 | 2002-06-26 12:05:06 +0000 | [diff] [blame] | 926 | /* several tracks loaded? */ |
Linus Nielsen Feltzing | 80bb281 | 2005-08-30 20:52:24 +0000 | [diff] [blame] | 927 | track = get_trackdata(track_offset); |
| 928 | if(track) |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 929 | { |
Björn Stenberg | 200d226 | 2002-06-26 12:05:06 +0000 | [diff] [blame] | 930 | /* will we move across the track boundary? */ |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 931 | if (( audiobuf_read < track->mempos ) && |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 932 | ((audiobuf_read+last_dma_chunk_size) > |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 933 | track->mempos )) |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 934 | { |
| 935 | /* Make sure that we end exactly on the boundary */ |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 936 | last_dma_chunk_size = track->mempos - audiobuf_read; |
Björn Stenberg | 200d226 | 2002-06-26 12:05:06 +0000 | [diff] [blame] | 937 | } |
Björn Stenberg | bb9aaf5 | 2002-06-25 13:26:04 +0000 | [diff] [blame] | 938 | } |
Linus Nielsen Feltzing | 4e2384e | 2002-07-25 11:12:54 +0000 | [diff] [blame] | 939 | |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 940 | *psize = last_dma_chunk_size & 0xffff; |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 941 | *ppbuf = mpeg_audiobuf + audiobuf_read; |
Linus Nielsen Feltzing | e54aa2a | 2005-08-30 22:50:56 +0000 | [diff] [blame] | 942 | track = get_trackdata(0); |
| 943 | if(track) |
| 944 | track->id3.offset += last_dma_chunk_size; |
Linus Nielsen Feltzing | d29248d | 2002-12-05 14:11:48 +0000 | [diff] [blame] | 945 | |
| 946 | /* Update the watermark debug level */ |
| 947 | if(unplayed_space_left < lowest_watermark_level) |
| 948 | lowest_watermark_level = unplayed_space_left; |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 949 | } |
| 950 | else |
| 951 | { |
Linus Nielsen Feltzing | 33060d0 | 2002-10-09 13:25:03 +0000 | [diff] [blame] | 952 | /* Check if the end of data is because of a hard disk error. |
| 953 | If there is an open file handle, we are still playing music. |
| 954 | If not, the last file has been loaded, and the file handle is |
| 955 | closed. */ |
| 956 | if(mpeg_file >= 0) |
| 957 | { |
Linus Nielsen Feltzing | d29248d | 2002-12-05 14:11:48 +0000 | [diff] [blame] | 958 | /* Update the watermark debug level */ |
| 959 | if(unplayed_space_left < lowest_watermark_level) |
| 960 | lowest_watermark_level = unplayed_space_left; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 961 | |
Linus Nielsen Feltzing | 33060d0 | 2002-10-09 13:25:03 +0000 | [diff] [blame] | 962 | DEBUGF("DMA underrun.\n"); |
| 963 | dma_underrun = true; |
| 964 | } |
Linus Nielsen Feltzing | 26e7ec4 | 2002-10-09 09:15:28 +0000 | [diff] [blame] | 965 | else |
| 966 | { |
Linus Nielsen Feltzing | 0ad617c | 2005-08-21 23:01:12 +0000 | [diff] [blame] | 967 | if ( ! audio_dispatch_event(AUDIO_EVENT_END_OF_TRACK, 0) ) |
| 968 | { |
| 969 | DEBUGF("No more MP3 data. Stopping.\n"); |
| 970 | queue_post(&mpeg_queue, MPEG_TRACK_CHANGE, 0); |
| 971 | playing = false; |
| 972 | } |
Linus Nielsen Feltzing | 26e7ec4 | 2002-10-09 09:15:28 +0000 | [diff] [blame] | 973 | } |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 974 | *psize = 0; /* no more transfer */ |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 979 | static struct trackdata *add_track_to_tag_list(const char *filename) |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 980 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 981 | struct trackdata *track; |
Jonathan Gordon | 843c7ef | 2009-04-06 00:39:43 +0000 | [diff] [blame] | 982 | bool send_nid3_event; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 983 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 984 | if(num_tracks_in_memory() >= MAX_TRACK_ENTRIES) |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 985 | { |
| 986 | DEBUGF("Tag memory is full\n"); |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 987 | return NULL; |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 988 | } |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 989 | |
| 990 | track = &trackdata[track_write_idx]; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 991 | |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 992 | /* grab id3 tag of new file and |
| 993 | remember where in memory it starts */ |
Thom Johansen | 294ec1d | 2007-09-19 10:40:55 +0000 | [diff] [blame] | 994 | if(mp3info(&track->id3, filename)) |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 995 | { |
| 996 | DEBUGF("Bad mp3\n"); |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 997 | return NULL; |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 998 | } |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 999 | track->mempos = audiobuf_write; |
| 1000 | track->id3.elapsed = 0; |
Marcoen Hirschberg | b0fee17 | 2005-12-06 13:27:15 +0000 | [diff] [blame] | 1001 | #ifdef HAVE_LCD_BITMAP |
Magnus Holmgren | 917f2c1 | 2005-12-22 21:42:00 +0000 | [diff] [blame] | 1002 | if (track->id3.title) |
| 1003 | lcd_getstringsize(track->id3.title, NULL, NULL); |
| 1004 | if (track->id3.artist) |
| 1005 | lcd_getstringsize(track->id3.artist, NULL, NULL); |
| 1006 | if (track->id3.album) |
| 1007 | lcd_getstringsize(track->id3.album, NULL, NULL); |
Marcoen Hirschberg | b0fee17 | 2005-12-06 13:27:15 +0000 | [diff] [blame] | 1008 | #endif |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 1009 | |
Jonathan Gordon | 843c7ef | 2009-04-06 00:39:43 +0000 | [diff] [blame] | 1010 | /* if this track is the next track then let the UI know it can get it */ |
| 1011 | send_nid3_event = (track_write_idx == track_read_idx + 1); |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1012 | track_write_idx = (track_write_idx+1) & MAX_TRACK_ENTRIES_MASK; |
Jonathan Gordon | 843c7ef | 2009-04-06 00:39:43 +0000 | [diff] [blame] | 1013 | if (send_nid3_event) |
Michael Sevakis | 023f6b6 | 2013-07-12 12:06:38 -0400 | [diff] [blame] | 1014 | send_track_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, &track->id3); |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 1015 | debug_tags(); |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1016 | return track; |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1019 | static int new_file(int steps) |
Björn Stenberg | fa8cd2c | 2002-05-31 12:12:23 +0000 | [diff] [blame] | 1020 | { |
Linus Nielsen Feltzing | 56e5d1a | 2002-10-09 13:47:38 +0000 | [diff] [blame] | 1021 | int max_steps = playlist_amount(); |
Linus Nielsen Feltzing | 685aeb5 | 2004-11-15 00:34:19 +0000 | [diff] [blame] | 1022 | int start = 0; |
| 1023 | int i; |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1024 | struct trackdata *track; |
Jens Arnold | 5bea40f | 2010-08-22 20:58:32 +0000 | [diff] [blame] | 1025 | char name_buf[MAX_PATH+1]; |
| 1026 | const char *trackname; |
Linus Nielsen Feltzing | 685aeb5 | 2004-11-15 00:34:19 +0000 | [diff] [blame] | 1027 | |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 1028 | /* Find out how many steps to advance. The load_ahead_index field tells |
| 1029 | us how many playlist entries it had to skip to get to a valid one. |
| 1030 | We add those together to find out where to start. */ |
| 1031 | if(steps > 0 && num_tracks_in_memory() > 1) |
Linus Nielsen Feltzing | 685aeb5 | 2004-11-15 00:34:19 +0000 | [diff] [blame] | 1032 | { |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 1033 | /* Begin with the song after the currently playing one */ |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1034 | i = 1; |
| 1035 | while((track = get_trackdata(i++))) |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 1036 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1037 | start += track->load_ahead_index; |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 1038 | } |
Linus Nielsen Feltzing | 685aeb5 | 2004-11-15 00:34:19 +0000 | [diff] [blame] | 1039 | } |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1040 | |
Björn Stenberg | f952ba7 | 2002-07-18 15:37:41 +0000 | [diff] [blame] | 1041 | do { |
Jens Arnold | 5bea40f | 2010-08-22 20:58:32 +0000 | [diff] [blame] | 1042 | trackname = playlist_peek(start + steps, name_buf, sizeof(name_buf)); |
Björn Stenberg | f952ba7 | 2002-07-18 15:37:41 +0000 | [diff] [blame] | 1043 | if ( !trackname ) |
| 1044 | return -1; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1045 | |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 1046 | DEBUGF("Loading %s\n", trackname); |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1047 | |
Björn Stenberg | f952ba7 | 2002-07-18 15:37:41 +0000 | [diff] [blame] | 1048 | mpeg_file = open(trackname, O_RDONLY); |
| 1049 | if(mpeg_file < 0) { |
| 1050 | DEBUGF("Couldn't open file: %s\n",trackname); |
Linus Nielsen Feltzing | 72d2535 | 2004-10-11 07:55:45 +0000 | [diff] [blame] | 1051 | if(steps < 0) |
| 1052 | steps--; |
| 1053 | else |
| 1054 | steps++; |
Björn Stenberg | f952ba7 | 2002-07-18 15:37:41 +0000 | [diff] [blame] | 1055 | } |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 1056 | else |
| 1057 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1058 | struct trackdata *track = add_track_to_tag_list(trackname); |
Linus Nielsen Feltzing | ccfef04 | 2002-08-28 10:21:32 +0000 | [diff] [blame] | 1059 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1060 | if(!track) |
Linus Nielsen Feltzing | 606b0f5 | 2002-08-28 11:43:49 +0000 | [diff] [blame] | 1061 | { |
| 1062 | /* Bad mp3 file */ |
Linus Nielsen Feltzing | 34145af | 2004-11-17 12:42:43 +0000 | [diff] [blame] | 1063 | if(steps < 0) |
| 1064 | steps--; |
| 1065 | else |
| 1066 | steps++; |
Linus Nielsen Feltzing | 606b0f5 | 2002-08-28 11:43:49 +0000 | [diff] [blame] | 1067 | close(mpeg_file); |
| 1068 | mpeg_file = -1; |
| 1069 | } |
| 1070 | else |
| 1071 | { |
Linus Nielsen Feltzing | 03c13a1 | 2002-10-08 07:43:52 +0000 | [diff] [blame] | 1072 | /* skip past id3v2 tag */ |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1073 | lseek(mpeg_file, |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1074 | track->id3.first_frame_offset, |
Linus Nielsen Feltzing | 606b0f5 | 2002-08-28 11:43:49 +0000 | [diff] [blame] | 1075 | SEEK_SET); |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1076 | track->id3.index = steps; |
| 1077 | track->load_ahead_index = steps; |
| 1078 | track->id3.offset = 0; |
Linus Nielsen Feltzing | ce882bb | 2002-12-05 15:23:46 +0000 | [diff] [blame] | 1079 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1080 | if(track->id3.vbr) |
Linus Nielsen Feltzing | 672479c | 2002-12-05 21:56:53 +0000 | [diff] [blame] | 1081 | /* Average bitrate * 1.5 */ |
Linus Nielsen Feltzing | ce882bb | 2002-12-05 15:23:46 +0000 | [diff] [blame] | 1082 | recalculate_watermark( |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1083 | (track->id3.bitrate * 3) / 2); |
Linus Nielsen Feltzing | ce882bb | 2002-12-05 15:23:46 +0000 | [diff] [blame] | 1084 | else |
| 1085 | recalculate_watermark( |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1086 | track->id3.bitrate); |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1087 | |
Linus Nielsen Feltzing | 606b0f5 | 2002-08-28 11:43:49 +0000 | [diff] [blame] | 1088 | } |
Björn Stenberg | f952ba7 | 2002-07-18 15:37:41 +0000 | [diff] [blame] | 1089 | } |
Linus Nielsen Feltzing | 319d5f7 | 2004-11-05 07:43:39 +0000 | [diff] [blame] | 1090 | |
| 1091 | /* Bail out if no file could be opened */ |
| 1092 | if(abs(steps) > max_steps) |
| 1093 | return -1; |
Björn Stenberg | f952ba7 | 2002-07-18 15:37:41 +0000 | [diff] [blame] | 1094 | } while ( mpeg_file < 0 ); |
Björn Stenberg | fa8cd2c | 2002-05-31 12:12:23 +0000 | [diff] [blame] | 1095 | |
Björn Stenberg | fa8cd2c | 2002-05-31 12:12:23 +0000 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
Linus Nielsen Feltzing | 392f385 | 2002-06-29 23:01:10 +0000 | [diff] [blame] | 1099 | static void stop_playing(void) |
| 1100 | { |
| 1101 | /* Stop the current stream */ |
Jörg Hohensohn | ec5d446 | 2004-01-10 15:39:56 +0000 | [diff] [blame] | 1102 | mp3_play_stop(); |
Linus Nielsen Feltzing | 392f385 | 2002-06-29 23:01:10 +0000 | [diff] [blame] | 1103 | playing = false; |
| 1104 | filling = false; |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1105 | |
Linus Nielsen Feltzing | 392f385 | 2002-06-29 23:01:10 +0000 | [diff] [blame] | 1106 | if(mpeg_file >= 0) |
Linus Nielsen Feltzing | 4e46ac7 | 2002-07-18 21:02:55 +0000 | [diff] [blame] | 1107 | close(mpeg_file); |
Linus Nielsen Feltzing | 392f385 | 2002-06-29 23:01:10 +0000 | [diff] [blame] | 1108 | mpeg_file = -1; |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 1109 | remove_all_tags(); |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1110 | generate_unbuffer_events(); |
Jörg Hohensohn | 30c338a | 2004-05-09 09:36:58 +0000 | [diff] [blame] | 1111 | reset_mp3_buffer(); |
Linus Nielsen Feltzing | 392f385 | 2002-06-29 23:01:10 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Michael Sevakis | 023f6b6 | 2013-07-12 12:06:38 -0400 | [diff] [blame] | 1114 | static void end_current_track(void) |
| 1115 | { |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1116 | play_pending = false; |
| 1117 | playing = false; |
| 1118 | mp3_play_pause(false); |
| 1119 | |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1120 | reset_mp3_buffer(); |
| 1121 | remove_all_tags(); |
| 1122 | generate_unbuffer_events(); |
| 1123 | |
| 1124 | if(mpeg_file >= 0) |
| 1125 | close(mpeg_file); |
| 1126 | } |
| 1127 | |
Hardeep Sidhu | 8fef62b | 2005-11-08 06:28:05 +0000 | [diff] [blame] | 1128 | /* Is this a really the end of playback or is a new playlist starting */ |
| 1129 | static void check_playlist_end(int direction) |
| 1130 | { |
Hardeep Sidhu | 1df2edb | 2005-11-08 06:33:36 +0000 | [diff] [blame] | 1131 | /* Use the largest possible step size to account for skipped tracks */ |
| 1132 | int steps = playlist_amount(); |
Hardeep Sidhu | 8fef62b | 2005-11-08 06:28:05 +0000 | [diff] [blame] | 1133 | |
Hardeep Sidhu | 1df2edb | 2005-11-08 06:33:36 +0000 | [diff] [blame] | 1134 | if (direction < 0) |
| 1135 | steps = -steps; |
Hardeep Sidhu | 8fef62b | 2005-11-08 06:28:05 +0000 | [diff] [blame] | 1136 | |
Hardeep Sidhu | 1df2edb | 2005-11-08 06:33:36 +0000 | [diff] [blame] | 1137 | if (playlist_next(steps) < 0) |
| 1138 | is_playing = false; |
Hardeep Sidhu | 8fef62b | 2005-11-08 06:28:05 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1141 | static void update_playlist(void) |
| 1142 | { |
Björn Stenberg | a6d0abb | 2002-10-04 08:48:20 +0000 | [diff] [blame] | 1143 | if (num_tracks_in_memory() > 0) |
| 1144 | { |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1145 | struct trackdata *track = get_trackdata(0); |
| 1146 | track->id3.index = playlist_next(track->id3.index); |
| 1147 | } |
| 1148 | else |
| 1149 | { |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1150 | /* End of playlist? */ |
Hardeep Sidhu | 1df2edb | 2005-11-08 06:33:36 +0000 | [diff] [blame] | 1151 | check_playlist_end(1); |
Björn Stenberg | a6d0abb | 2002-10-04 08:48:20 +0000 | [diff] [blame] | 1152 | } |
Hardeep Sidhu | 839dbca | 2005-07-04 22:50:57 +0000 | [diff] [blame] | 1153 | |
| 1154 | playlist_update_resume_info(audio_current_track()); |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1157 | static void track_change(void) |
| 1158 | { |
| 1159 | DEBUGF("Track change\n"); |
| 1160 | |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 1161 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1162 | /* Reset the AVC */ |
Jens Arnold | 65c1746 | 2005-11-07 20:35:24 +0000 | [diff] [blame] | 1163 | sound_set_avc(-1); |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 1164 | #endif /* (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */ |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1165 | |
Hardeep Sidhu | c8ce78a | 2005-07-01 12:06:30 +0000 | [diff] [blame] | 1166 | if (num_tracks_in_memory() > 0) |
| 1167 | { |
| 1168 | remove_current_tag(); |
| 1169 | update_playlist(); |
Jens Arnold | a2f92c9 | 2009-02-16 00:29:07 +0000 | [diff] [blame] | 1170 | if (is_playing) |
Michael Sevakis | 023f6b6 | 2013-07-12 12:06:38 -0400 | [diff] [blame] | 1171 | { |
| 1172 | send_track_event(PLAYBACK_EVENT_TRACK_CHANGE, |
| 1173 | audio_current_track()); |
| 1174 | } |
Hardeep Sidhu | c8ce78a | 2005-07-01 12:06:30 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Hardeep Sidhu | d3d1b98 | 2002-12-19 00:58:11 +0000 | [diff] [blame] | 1177 | current_track_counter++; |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Linus Nielsen Feltzing | b3bb076 | 2002-09-18 13:51:08 +0000 | [diff] [blame] | 1180 | #ifdef DEBUG |
Jens Arnold | c76c568 | 2004-08-16 23:37:23 +0000 | [diff] [blame] | 1181 | void hexdump(const unsigned char *buf, int len) |
Linus Nielsen Feltzing | b3bb076 | 2002-09-18 13:51:08 +0000 | [diff] [blame] | 1182 | { |
| 1183 | int i; |
| 1184 | |
| 1185 | for(i = 0;i < len;i++) |
| 1186 | { |
| 1187 | if(i && (i & 15) == 0) |
| 1188 | { |
| 1189 | DEBUGF("\n"); |
| 1190 | } |
| 1191 | DEBUGF("%02x ", buf[i]); |
| 1192 | } |
| 1193 | DEBUGF("\n"); |
| 1194 | } |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 1195 | #endif /* DEBUG */ |
Linus Nielsen Feltzing | b3bb076 | 2002-09-18 13:51:08 +0000 | [diff] [blame] | 1196 | |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1197 | static void start_playback_if_ready(void) |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1198 | { |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 1199 | int playable_space; |
| 1200 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1201 | playable_space = audiobuf_swapwrite - audiobuf_read; |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 1202 | if(playable_space < 0) |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1203 | playable_space += audiobuflen; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1204 | |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1205 | /* See if we have started playing yet. If not, do it. */ |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1206 | if(play_pending || dma_underrun) |
| 1207 | { |
| 1208 | /* If the filling has stopped, and we still haven't reached |
| 1209 | the watermark, the file must be smaller than the |
| 1210 | watermark. We must still play it. */ |
Linus Nielsen Feltzing | 7fed4a0 | 2002-12-05 17:14:35 +0000 | [diff] [blame] | 1211 | if((playable_space >= MPEG_PLAY_PENDING_THRESHOLD) || |
Linus Nielsen Feltzing | 8344437 | 2002-12-05 11:09:52 +0000 | [diff] [blame] | 1212 | !filling || dma_underrun) |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1213 | { |
| 1214 | DEBUGF("P\n"); |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1215 | if (play_pending) /* don't do this when recovering from DMA underrun */ |
| 1216 | { |
| 1217 | generate_postbuffer_events(); /* signal first track as buffered */ |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1218 | if (play_pending_track_change) |
| 1219 | { |
| 1220 | play_pending_track_change = false; |
Michael Sevakis | 023f6b6 | 2013-07-12 12:06:38 -0400 | [diff] [blame] | 1221 | send_track_event(PLAYBACK_EVENT_TRACK_CHANGE, |
| 1222 | audio_current_track()); |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1223 | } |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1224 | play_pending = false; |
| 1225 | } |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1226 | playing = true; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1227 | |
Linus Nielsen Feltzing | 66cda8c | 2004-04-09 21:03:09 +0000 | [diff] [blame] | 1228 | last_dma_chunk_size = MIN(0x2000, get_unplayed_space_current_song()); |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 1229 | mp3_play_data(mpeg_audiobuf + audiobuf_read, last_dma_chunk_size, transfer_end); |
Linus Nielsen Feltzing | 66cda8c | 2004-04-09 21:03:09 +0000 | [diff] [blame] | 1230 | dma_underrun = false; |
| 1231 | |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1232 | if (!paused) |
| 1233 | { |
| 1234 | last_dma_tick = current_tick; |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1235 | mp3_play_pause(true); |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /* Tell ourselves that we need more data */ |
| 1239 | queue_post(&mpeg_queue, MPEG_NEED_DATA, 0); |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1244 | static bool swap_one_chunk(void) |
| 1245 | { |
| 1246 | int free_space_left; |
| 1247 | int amount_to_swap; |
| 1248 | |
| 1249 | free_space_left = get_unswapped_space(); |
| 1250 | |
| 1251 | if(free_space_left == 0 && !play_pending) |
| 1252 | return false; |
| 1253 | |
| 1254 | /* Swap in larger chunks when the user is waiting for the playback |
Linus Nielsen Feltzing | d199e69 | 2002-12-05 13:36:08 +0000 | [diff] [blame] | 1255 | to start, or when there is dangerously little playable data left */ |
Linus Nielsen Feltzing | 7fed4a0 | 2002-12-05 17:14:35 +0000 | [diff] [blame] | 1256 | if(play_pending) |
| 1257 | amount_to_swap = MIN(MPEG_PLAY_PENDING_SWAPSIZE, free_space_left); |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1258 | else |
Linus Nielsen Feltzing | 7fed4a0 | 2002-12-05 17:14:35 +0000 | [diff] [blame] | 1259 | { |
| 1260 | if(get_playable_space() < low_watermark) |
| 1261 | amount_to_swap = MIN(MPEG_LOW_WATER_SWAP_CHUNKSIZE, |
| 1262 | free_space_left); |
| 1263 | else |
| 1264 | amount_to_swap = MIN(MPEG_SWAP_CHUNKSIZE, free_space_left); |
| 1265 | } |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1266 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1267 | if(audiobuf_write < audiobuf_swapwrite) |
| 1268 | amount_to_swap = MIN(audiobuflen - audiobuf_swapwrite, |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1269 | amount_to_swap); |
| 1270 | else |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1271 | amount_to_swap = MIN(audiobuf_write - audiobuf_swapwrite, |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1272 | amount_to_swap); |
Linus Nielsen Feltzing | 35c417f | 2003-04-12 09:38:33 +0000 | [diff] [blame] | 1273 | |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 1274 | bitswap(mpeg_audiobuf + audiobuf_swapwrite, amount_to_swap); |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1275 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1276 | audiobuf_swapwrite += amount_to_swap; |
| 1277 | if(audiobuf_swapwrite >= audiobuflen) |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1278 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1279 | audiobuf_swapwrite = 0; |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | return true; |
| 1283 | } |
Linus Nielsen Feltzing | a5fbdbb | 2005-02-28 09:04:59 +0000 | [diff] [blame] | 1284 | |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1285 | static void mpeg_thread(void) |
| 1286 | { |
Björn Stenberg | 0c2e9f2 | 2002-08-21 17:38:56 +0000 | [diff] [blame] | 1287 | static int pause_tick = 0; |
| 1288 | static unsigned int pause_track = 0; |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 1289 | struct queue_event ev; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1290 | int len; |
| 1291 | int free_space_left; |
Linus Nielsen Feltzing | 54a65f7 | 2002-07-21 07:12:39 +0000 | [diff] [blame] | 1292 | int unplayed_space_left; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1293 | int amount_to_read; |
Linus Nielsen Feltzing | 1d21ca9 | 2002-08-01 08:16:44 +0000 | [diff] [blame] | 1294 | int t1, t2; |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 1295 | unsigned long start_elapsed, start_offset; |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 1296 | #if CONFIG_CODEC == MAS3587F |
Linus Nielsen Feltzing | 893095d | 2002-11-10 16:37:26 +0000 | [diff] [blame] | 1297 | int amount_to_save; |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 1298 | int save_endpos = 0; |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 1299 | int rc; |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 1300 | int level; |
Jean-Philippe Bernardy | 3f65c3f | 2005-01-30 14:33:33 +0000 | [diff] [blame] | 1301 | long offset; |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 1302 | #endif /* CONFIG_CODEC == MAS3587F */ |
Jens Arnold | e03f402 | 2005-06-04 11:29:39 +0000 | [diff] [blame] | 1303 | |
Linus Nielsen Feltzing | ccfef04 | 2002-08-28 10:21:32 +0000 | [diff] [blame] | 1304 | is_playing = false; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1305 | play_pending = false; |
| 1306 | playing = false; |
Linus Nielsen Feltzing | 60ed5ee | 2002-06-19 06:19:00 +0000 | [diff] [blame] | 1307 | mpeg_file = -1; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1308 | |
| 1309 | while(1) |
| 1310 | { |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 1311 | #if CONFIG_CODEC == MAS3587F |
Linus Nielsen Feltzing | 893095d | 2002-11-10 16:37:26 +0000 | [diff] [blame] | 1312 | if(mpeg_mode == MPEG_DECODER) |
| 1313 | { |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 1314 | #endif /* CONFIG_CODEC == MAS3587F */ |
Linus Nielsen Feltzing | 4e46ac7 | 2002-07-18 21:02:55 +0000 | [diff] [blame] | 1315 | yield(); |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1316 | |
| 1317 | /* Swap if necessary, and don't block on the queue_wait() */ |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1318 | if(swap_one_chunk()) |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1319 | { |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1320 | queue_wait_w_tmo(&mpeg_queue, &ev, 0); |
| 1321 | } |
Hardeep Sidhu | 839dbca | 2005-07-04 22:50:57 +0000 | [diff] [blame] | 1322 | else if (playing) |
| 1323 | { |
| 1324 | /* periodically update resume info */ |
| 1325 | queue_wait_w_tmo(&mpeg_queue, &ev, HZ/2); |
| 1326 | } |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1327 | else |
| 1328 | { |
Linus Nielsen Feltzing | 647db26 | 2002-10-16 09:26:03 +0000 | [diff] [blame] | 1329 | DEBUGF("S R:%x W:%x SW:%x\n", |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1330 | audiobuf_read, audiobuf_write, audiobuf_swapwrite); |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1331 | queue_wait(&mpeg_queue, &ev); |
| 1332 | } |
Linus Nielsen Feltzing | c5e2993 | 2002-10-28 00:00:07 +0000 | [diff] [blame] | 1333 | |
| 1334 | start_playback_if_ready(); |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1335 | |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1336 | switch(ev.id) |
| 1337 | { |
| 1338 | case MPEG_PLAY: |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1339 | DEBUGF("MPEG_PLAY\n"); |
Linus Nielsen Feltzing | 0a12f4e | 2003-05-03 10:03:30 +0000 | [diff] [blame] | 1340 | |
Jonathan Gordon | 00d249a | 2007-02-18 03:25:40 +0000 | [diff] [blame] | 1341 | #if CONFIG_TUNER |
Linus Nielsen Feltzing | 0a12f4e | 2003-05-03 10:03:30 +0000 | [diff] [blame] | 1342 | /* Silence the A/D input, it may be on because the radio |
| 1343 | may be playing */ |
| 1344 | mas_codec_writereg(6, 0x0000); |
Jens Arnold | 1d38aa1 | 2005-08-19 06:43:50 +0000 | [diff] [blame] | 1345 | #endif /* CONFIG_TUNER */ |
Linus Nielsen Feltzing | 0a12f4e | 2003-05-03 10:03:30 +0000 | [diff] [blame] | 1346 | |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1347 | /* Stop the current stream */ |
Linus Nielsen Feltzing | 001226c | 2003-03-15 12:09:09 +0000 | [diff] [blame] | 1348 | paused = false; |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1349 | end_current_track(); |
Linus Nielsen Feltzing | 60ed5ee | 2002-06-19 06:19:00 +0000 | [diff] [blame] | 1350 | |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1351 | if ( new_file(0) == -1 ) |
Linus Nielsen Feltzing | ccfef04 | 2002-08-28 10:21:32 +0000 | [diff] [blame] | 1352 | { |
| 1353 | is_playing = false; |
Hardeep Sidhu | 96a6874 | 2003-01-04 07:16:36 +0000 | [diff] [blame] | 1354 | track_change(); |
Linus Nielsen Feltzing | ccfef04 | 2002-08-28 10:21:32 +0000 | [diff] [blame] | 1355 | break; |
| 1356 | } |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 1357 | |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 1358 | start_elapsed = ((struct audio_resume_info *)ev.data)->elapsed; |
| 1359 | start_offset = ((struct audio_resume_info *)ev.data)->offset; |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 1360 | |
| 1361 | /* mid-song resume? */ |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 1362 | if (!start_offset && start_elapsed) { |
| 1363 | struct mp3entry *id3 = &get_trackdata(0)->id3; |
| 1364 | id3->elapsed = start_elapsed; |
| 1365 | start_offset = audio_get_file_pos_int(id3); |
| 1366 | } |
| 1367 | |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1368 | if (start_offset) { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1369 | struct mp3entry* id3 = &get_trackdata(0)->id3; |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1370 | lseek(mpeg_file, start_offset, SEEK_SET); |
Björn Stenberg | 3a9e7b5 | 2002-08-20 20:38:50 +0000 | [diff] [blame] | 1371 | id3->offset = start_offset; |
| 1372 | set_elapsed(id3); |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1373 | } |
| 1374 | else { |
Linus Nielsen Feltzing | 03c13a1 | 2002-10-08 07:43:52 +0000 | [diff] [blame] | 1375 | /* skip past id3v2 tag */ |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1376 | lseek(mpeg_file, |
| 1377 | get_trackdata(0)->id3.first_frame_offset, |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1378 | SEEK_SET); |
| 1379 | |
| 1380 | } |
Björn Stenberg | bb9aaf5 | 2002-06-25 13:26:04 +0000 | [diff] [blame] | 1381 | |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1382 | /* Make it read more data */ |
| 1383 | filling = true; |
| 1384 | queue_post(&mpeg_queue, MPEG_NEED_DATA, 0); |
| 1385 | |
| 1386 | /* Tell the file loading code that we want to start playing |
| 1387 | as soon as we have some data */ |
| 1388 | play_pending = true; |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1389 | play_pending_track_change = true; |
Linus Nielsen Feltzing | e5792d6 | 2002-07-25 23:12:41 +0000 | [diff] [blame] | 1390 | |
Hardeep Sidhu | aa287bb | 2002-09-17 07:04:43 +0000 | [diff] [blame] | 1391 | update_playlist(); |
Hardeep Sidhu | d3d1b98 | 2002-12-19 00:58:11 +0000 | [diff] [blame] | 1392 | current_track_counter++; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | |
| 1395 | case MPEG_STOP: |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 1396 | do_stop(); |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1397 | break; |
| 1398 | |
| 1399 | case MPEG_PAUSE: |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 1400 | DEBUGF("MPEG_PAUSE\n"); |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1401 | /* Stop the current stream */ |
Hardeep Sidhu | 81b32e0 | 2005-07-07 16:57:10 +0000 | [diff] [blame] | 1402 | if (playing) |
| 1403 | playlist_update_resume_info(audio_current_track()); |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1404 | paused = true; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1405 | playing = false; |
Linus Nielsen Feltzing | d2018eb | 2002-07-25 22:09:12 +0000 | [diff] [blame] | 1406 | pause_tick = current_tick; |
Björn Stenberg | 0c2e9f2 | 2002-08-21 17:38:56 +0000 | [diff] [blame] | 1407 | pause_track = current_track_counter; |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1408 | mp3_play_pause(false); |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | |
| 1411 | case MPEG_RESUME: |
Björn Stenberg | 3c26077 | 2002-05-24 15:27:55 +0000 | [diff] [blame] | 1412 | DEBUGF("MPEG_RESUME\n"); |
Linus Nielsen Feltzing | d2018eb | 2002-07-25 22:09:12 +0000 | [diff] [blame] | 1413 | /* Continue the current stream */ |
Björn Stenberg | 34486b7 | 2002-08-19 11:00:29 +0000 | [diff] [blame] | 1414 | paused = false; |
| 1415 | if (!play_pending) |
| 1416 | { |
Björn Stenberg | 0c2e9f2 | 2002-08-21 17:38:56 +0000 | [diff] [blame] | 1417 | playing = true; |
| 1418 | if ( current_track_counter == pause_track ) |
| 1419 | last_dma_tick += current_tick - pause_tick; |
| 1420 | else |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1421 | last_dma_tick = current_tick; |
Björn Stenberg | 0c2e9f2 | 2002-08-21 17:38:56 +0000 | [diff] [blame] | 1422 | pause_tick = 0; |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1423 | mp3_play_pause(true); |
Björn Stenberg | 34486b7 | 2002-08-19 11:00:29 +0000 | [diff] [blame] | 1424 | } |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1427 | case MPEG_NEXT: |
| 1428 | DEBUGF("MPEG_NEXT\n"); |
Björn Stenberg | a6d0abb | 2002-10-04 08:48:20 +0000 | [diff] [blame] | 1429 | /* is next track in ram? */ |
| 1430 | if ( num_tracks_in_memory() > 1 ) { |
| 1431 | int unplayed_space_left, unswapped_space_left; |
| 1432 | |
Hardeep Sidhu | d3d1b98 | 2002-12-19 00:58:11 +0000 | [diff] [blame] | 1433 | /* stop the current stream */ |
| 1434 | play_pending = false; |
| 1435 | playing = false; |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1436 | mp3_play_pause(false); |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1437 | |
Hardeep Sidhu | 754651e | 2002-08-22 23:01:25 +0000 | [diff] [blame] | 1438 | track_change(); |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1439 | audiobuf_read = get_trackdata(0)->mempos; |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1440 | last_dma_chunk_size = MIN(0x2000, get_unplayed_space_current_song()); |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 1441 | mp3_play_data(mpeg_audiobuf + audiobuf_read, last_dma_chunk_size, transfer_end); |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1442 | dma_underrun = false; |
Linus Nielsen Feltzing | fe8a7e7 | 2002-08-08 14:51:58 +0000 | [diff] [blame] | 1443 | last_dma_tick = current_tick; |
Björn Stenberg | 235d9d2 | 2002-08-08 14:01:40 +0000 | [diff] [blame] | 1444 | |
Hardeep Sidhu | 754651e | 2002-08-22 23:01:25 +0000 | [diff] [blame] | 1445 | unplayed_space_left = get_unplayed_space(); |
Hardeep Sidhu | 67f95ec | 2002-10-03 07:28:28 +0000 | [diff] [blame] | 1446 | unswapped_space_left = get_unswapped_space(); |
Hardeep Sidhu | 754651e | 2002-08-22 23:01:25 +0000 | [diff] [blame] | 1447 | |
Björn Stenberg | 235d9d2 | 2002-08-08 14:01:40 +0000 | [diff] [blame] | 1448 | /* should we start reading more data? */ |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 1449 | if(!filling && (unplayed_space_left < low_watermark)) { |
Björn Stenberg | 235d9d2 | 2002-08-08 14:01:40 +0000 | [diff] [blame] | 1450 | filling = true; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1451 | queue_post(&mpeg_queue, MPEG_NEED_DATA, GENERATE_UNBUFFER_EVENTS); |
Linus Nielsen Feltzing | fe8a7e7 | 2002-08-08 14:51:58 +0000 | [diff] [blame] | 1452 | play_pending = true; |
Hardeep Sidhu | 754651e | 2002-08-22 23:01:25 +0000 | [diff] [blame] | 1453 | } else if(unswapped_space_left && |
| 1454 | unswapped_space_left > unplayed_space_left) { |
| 1455 | /* Stop swapping the data from the previous file */ |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1456 | audiobuf_swapwrite = audiobuf_read; |
Hardeep Sidhu | 754651e | 2002-08-22 23:01:25 +0000 | [diff] [blame] | 1457 | play_pending = true; |
Linus Nielsen Feltzing | fe8a7e7 | 2002-08-08 14:51:58 +0000 | [diff] [blame] | 1458 | } else { |
| 1459 | playing = true; |
Björn Stenberg | 6224cdb | 2002-08-16 14:41:47 +0000 | [diff] [blame] | 1460 | if (!paused) |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1461 | mp3_play_pause(true); |
Björn Stenberg | 235d9d2 | 2002-08-08 14:01:40 +0000 | [diff] [blame] | 1462 | } |
Björn Stenberg | 445f17e | 2002-08-07 22:44:43 +0000 | [diff] [blame] | 1463 | } |
| 1464 | else { |
Linus Nielsen Feltzing | 9d860e1 | 2003-04-11 00:29:15 +0000 | [diff] [blame] | 1465 | if (!playlist_check(1)) |
Björn Stenberg | a6d0abb | 2002-10-04 08:48:20 +0000 | [diff] [blame] | 1466 | break; |
| 1467 | |
| 1468 | /* stop the current stream */ |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1469 | end_current_track(); |
Björn Stenberg | a6d0abb | 2002-10-04 08:48:20 +0000 | [diff] [blame] | 1470 | |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1471 | if (new_file(1) < 0) { |
Björn Stenberg | 445f17e | 2002-08-07 22:44:43 +0000 | [diff] [blame] | 1472 | DEBUGF("No more files to play\n"); |
| 1473 | filling = false; |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1474 | |
Hardeep Sidhu | 8fef62b | 2005-11-08 06:28:05 +0000 | [diff] [blame] | 1475 | check_playlist_end(1); |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1476 | current_track_counter++; |
Björn Stenberg | 445f17e | 2002-08-07 22:44:43 +0000 | [diff] [blame] | 1477 | } else { |
| 1478 | /* Make it read more data */ |
| 1479 | filling = true; |
| 1480 | queue_post(&mpeg_queue, MPEG_NEED_DATA, 0); |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1481 | |
Linus Nielsen Feltzing | c833a81 | 2002-11-24 00:23:49 +0000 | [diff] [blame] | 1482 | /* Tell the file loading code that we want |
| 1483 | to start playing as soon as we have some data */ |
Björn Stenberg | 445f17e | 2002-08-07 22:44:43 +0000 | [diff] [blame] | 1484 | play_pending = true; |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1485 | play_pending_track_change = true; |
Linus Nielsen Feltzing | ab042e6 | 2002-07-27 00:31:38 +0000 | [diff] [blame] | 1486 | |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1487 | update_playlist(); |
Hardeep Sidhu | d3d1b98 | 2002-12-19 00:58:11 +0000 | [diff] [blame] | 1488 | current_track_counter++; |
Björn Stenberg | 445f17e | 2002-08-07 22:44:43 +0000 | [diff] [blame] | 1489 | } |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1490 | } |
| 1491 | break; |
| 1492 | |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1493 | case MPEG_PREV: { |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1494 | DEBUGF("MPEG_PREV\n"); |
Linus Nielsen Feltzing | 9d860e1 | 2003-04-11 00:29:15 +0000 | [diff] [blame] | 1495 | |
| 1496 | if (!playlist_check(-1)) |
Björn Stenberg | a6d0abb | 2002-10-04 08:48:20 +0000 | [diff] [blame] | 1497 | break; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1498 | |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1499 | /* stop the current stream */ |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1500 | end_current_track(); |
Linus Nielsen Feltzing | ab042e6 | 2002-07-27 00:31:38 +0000 | [diff] [blame] | 1501 | |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1502 | /* Open the next file */ |
Hardeep Sidhu | 8fd18e9 | 2003-01-03 22:54:02 +0000 | [diff] [blame] | 1503 | if (new_file(-1) < 0) { |
Linus Nielsen Feltzing | 42faf56 | 2002-07-26 14:32:24 +0000 | [diff] [blame] | 1504 | DEBUGF("No more files to play\n"); |
Linus Nielsen Feltzing | 80b285c | 2002-07-23 15:10:31 +0000 | [diff] [blame] | 1505 | filling = false; |
Hardeep Sidhu | 8fef62b | 2005-11-08 06:28:05 +0000 | [diff] [blame] | 1506 | |
Hardeep Sidhu | 1df2edb | 2005-11-08 06:33:36 +0000 | [diff] [blame] | 1507 | check_playlist_end(-1); |
| 1508 | current_track_counter++; |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1509 | } else { |
Linus Nielsen Feltzing | 80b285c | 2002-07-23 15:10:31 +0000 | [diff] [blame] | 1510 | /* Make it read more data */ |
| 1511 | filling = true; |
| 1512 | queue_post(&mpeg_queue, MPEG_NEED_DATA, 0); |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1513 | |
Linus Nielsen Feltzing | c833a81 | 2002-11-24 00:23:49 +0000 | [diff] [blame] | 1514 | /* Tell the file loading code that we want to |
| 1515 | start playing as soon as we have some data */ |
Linus Nielsen Feltzing | 80b285c | 2002-07-23 15:10:31 +0000 | [diff] [blame] | 1516 | play_pending = true; |
Robert Keevil | b8bb521 | 2007-08-23 18:53:17 +0000 | [diff] [blame] | 1517 | play_pending_track_change = true; |
Linus Nielsen Feltzing | e5792d6 | 2002-07-25 23:12:41 +0000 | [diff] [blame] | 1518 | |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1519 | update_playlist(); |
Hardeep Sidhu | d3d1b98 | 2002-12-19 00:58:11 +0000 | [diff] [blame] | 1520 | current_track_counter++; |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1521 | } |
Björn Stenberg | bcbc782 | 2002-08-08 12:02:17 +0000 | [diff] [blame] | 1522 | break; |
| 1523 | } |
Björn Stenberg | a61617f | 2002-06-26 21:27:17 +0000 | [diff] [blame] | 1524 | |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1525 | case MPEG_FF_REWIND: { |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 1526 | struct mp3entry *id3 = audio_current_track(); |
Hardeep Sidhu | 98cb636 | 2002-08-30 07:07:57 +0000 | [diff] [blame] | 1527 | unsigned int oldtime = id3->elapsed; |
Hardeep Sidhu | 4160b75 | 2003-08-29 04:36:35 +0000 | [diff] [blame] | 1528 | unsigned int newtime = (unsigned int)ev.data; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1529 | int curpos, newpos, diffpos; |
| 1530 | DEBUGF("MPEG_FF_REWIND\n"); |
| 1531 | |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 1532 | id3->elapsed = newtime; |
| 1533 | |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 1534 | newpos = audio_get_file_pos_int(id3); |
Linus Nielsen Feltzing | 0744c50 | 2004-07-29 13:58:16 +0000 | [diff] [blame] | 1535 | if(newpos < 0) |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1536 | { |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 1537 | id3->elapsed = oldtime; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1538 | break; |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 1539 | } |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1540 | |
Hardeep Sidhu | c538593 | 2002-10-04 16:39:02 +0000 | [diff] [blame] | 1541 | if (mpeg_file >= 0) |
| 1542 | curpos = lseek(mpeg_file, 0, SEEK_CUR); |
| 1543 | else |
| 1544 | curpos = id3->filesize; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1545 | |
| 1546 | if (num_tracks_in_memory() > 1) |
| 1547 | { |
| 1548 | /* We have started loading other tracks that need to be |
| 1549 | accounted for */ |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1550 | struct trackdata *track; |
| 1551 | int i = 0; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1552 | |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1553 | while((track = get_trackdata(i++))) |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1554 | { |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1555 | curpos += track->id3.filesize; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | diffpos = curpos - newpos; |
| 1560 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1561 | if(!filling && diffpos >= 0 && diffpos < audiobuflen) |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1562 | { |
Hardeep Sidhu | 67f95ec | 2002-10-03 07:28:28 +0000 | [diff] [blame] | 1563 | int unplayed_space_left, unswapped_space_left; |
| 1564 | |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1565 | /* We are changing to a position that's already in |
Linus Nielsen Feltzing | 35c417f | 2003-04-12 09:38:33 +0000 | [diff] [blame] | 1566 | memory, so we just move the DMA read pointer. */ |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1567 | audiobuf_read = audiobuf_write - diffpos; |
| 1568 | if (audiobuf_read < 0) |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1569 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1570 | audiobuf_read += audiobuflen; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Hardeep Sidhu | 67f95ec | 2002-10-03 07:28:28 +0000 | [diff] [blame] | 1573 | unplayed_space_left = get_unplayed_space(); |
| 1574 | unswapped_space_left = get_unswapped_space(); |
| 1575 | |
Linus Nielsen Feltzing | 35c417f | 2003-04-12 09:38:33 +0000 | [diff] [blame] | 1576 | /* If unswapped_space_left is larger than |
| 1577 | unplayed_space_left, it means that the swapwrite pointer |
| 1578 | hasn't yet advanced up to the new location of the read |
| 1579 | pointer. We just move it, there is no need to swap |
| 1580 | data that won't be played anyway. */ |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1581 | |
Linus Nielsen Feltzing | 35c417f | 2003-04-12 09:38:33 +0000 | [diff] [blame] | 1582 | if (unswapped_space_left > unplayed_space_left) |
| 1583 | { |
| 1584 | DEBUGF("Moved swapwrite\n"); |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1585 | audiobuf_swapwrite = audiobuf_read; |
Linus Nielsen Feltzing | 35c417f | 2003-04-12 09:38:33 +0000 | [diff] [blame] | 1586 | play_pending = true; |
| 1587 | } |
| 1588 | |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 1589 | if (mpeg_file>=0 && unplayed_space_left < low_watermark) |
Hardeep Sidhu | c674415 | 2002-08-25 05:11:25 +0000 | [diff] [blame] | 1590 | { |
| 1591 | /* We need to load more data before starting */ |
| 1592 | filling = true; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1593 | queue_post(&mpeg_queue, MPEG_NEED_DATA, GENERATE_UNBUFFER_EVENTS); |
Hardeep Sidhu | c674415 | 2002-08-25 05:11:25 +0000 | [diff] [blame] | 1594 | play_pending = true; |
| 1595 | } |
| 1596 | else |
| 1597 | { |
| 1598 | /* resume will start at new position */ |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1599 | last_dma_chunk_size = |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1600 | MIN(0x2000, get_unplayed_space_current_song()); |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1601 | mp3_play_data(mpeg_audiobuf + audiobuf_read, |
Jörg Hohensohn | f993365 | 2004-01-05 20:42:51 +0000 | [diff] [blame] | 1602 | last_dma_chunk_size, transfer_end); |
| 1603 | dma_underrun = false; |
Hardeep Sidhu | c674415 | 2002-08-25 05:11:25 +0000 | [diff] [blame] | 1604 | } |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1605 | } |
| 1606 | else |
| 1607 | { |
| 1608 | /* Move to the new position in the file and start |
| 1609 | loading data */ |
| 1610 | reset_mp3_buffer(); |
| 1611 | |
| 1612 | if (num_tracks_in_memory() > 1) |
| 1613 | { |
| 1614 | /* We have to reload the current track */ |
| 1615 | close(mpeg_file); |
| 1616 | remove_all_non_current_tags(); |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1617 | generate_unbuffer_events(); |
Hardeep Sidhu | c538593 | 2002-10-04 16:39:02 +0000 | [diff] [blame] | 1618 | mpeg_file = -1; |
| 1619 | } |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1620 | |
Hardeep Sidhu | c538593 | 2002-10-04 16:39:02 +0000 | [diff] [blame] | 1621 | if (mpeg_file < 0) |
| 1622 | { |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1623 | mpeg_file = open(id3->path, O_RDONLY); |
| 1624 | if (mpeg_file < 0) |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 1625 | { |
| 1626 | id3->elapsed = oldtime; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1627 | break; |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 1628 | } |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | if(-1 == lseek(mpeg_file, newpos, SEEK_SET)) |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 1632 | { |
| 1633 | id3->elapsed = oldtime; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1634 | break; |
Hardeep Sidhu | 441f48f | 2002-08-27 07:19:25 +0000 | [diff] [blame] | 1635 | } |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1636 | |
| 1637 | filling = true; |
| 1638 | queue_post(&mpeg_queue, MPEG_NEED_DATA, 0); |
| 1639 | |
| 1640 | /* Tell the file loading code that we want to start playing |
| 1641 | as soon as we have some data */ |
| 1642 | play_pending = true; |
| 1643 | } |
| 1644 | |
Björn Stenberg | 34486b7 | 2002-08-19 11:00:29 +0000 | [diff] [blame] | 1645 | id3->offset = newpos; |
Björn Stenberg | 0570497 | 2002-08-14 19:23:34 +0000 | [diff] [blame] | 1646 | |
| 1647 | break; |
| 1648 | } |
| 1649 | |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1650 | case MPEG_FLUSH_RELOAD: { |
| 1651 | int numtracks = num_tracks_in_memory(); |
| 1652 | bool reload_track = false; |
| 1653 | |
| 1654 | if (numtracks > 1) |
| 1655 | { |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1656 | /* Reset the buffer */ |
Linus Nielsen Feltzing | 5f8241a | 2005-04-07 22:28:26 +0000 | [diff] [blame] | 1657 | audiobuf_write = get_trackdata(1)->mempos; |
Hardeep Sidhu | df34e6b | 2003-06-25 15:17:04 +0000 | [diff] [blame] | 1658 | |
| 1659 | /* Reset swapwrite unless we're still swapping current |
| 1660 | track */ |
| 1661 | if (get_unplayed_space() <= get_playable_space()) |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1662 | audiobuf_swapwrite = audiobuf_write; |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1663 | |
| 1664 | close(mpeg_file); |
| 1665 | remove_all_non_current_tags(); |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1666 | generate_unbuffer_events(); |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1667 | mpeg_file = -1; |
| 1668 | reload_track = true; |
| 1669 | } |
| 1670 | else if (numtracks == 1 && mpeg_file < 0) |
| 1671 | { |
| 1672 | reload_track = true; |
| 1673 | } |
| 1674 | |
| 1675 | if(reload_track && new_file(1) >= 0) |
| 1676 | { |
| 1677 | /* Tell ourselves that we want more data */ |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1678 | filling = true; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1679 | queue_post(&mpeg_queue, MPEG_NEED_DATA, 0); |
Hardeep Sidhu | 62bec9e | 2002-09-10 08:50:40 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | break; |
| 1683 | } |
| 1684 | |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1685 | case MPEG_NEED_DATA: |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1686 | free_space_left = audiobuf_read - audiobuf_write; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1687 | |
| 1688 | /* We interpret 0 as "empty buffer" */ |
| 1689 | if(free_space_left <= 0) |
Jens Arnold | ef32881 | 2005-05-27 21:46:44 +0000 | [diff] [blame] | 1690 | free_space_left += audiobuflen; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1691 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 1692 | unplayed_space_left = audiobuflen - free_space_left; |
Linus Nielsen Feltzing | 54a65f7 | 2002-07-21 07:12:39 +0000 | [diff] [blame] | 1693 | |
Linus Nielsen Feltzing | 4e46ac7 | 2002-07-18 21:02:55 +0000 | [diff] [blame] | 1694 | /* Make sure that we don't fill the entire buffer */ |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1695 | free_space_left -= MPEG_HIGH_WATER; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1696 | |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1697 | if (ev.data == GENERATE_UNBUFFER_EVENTS) |
| 1698 | generate_unbuffer_events(); |
Linus Nielsen Feltzing | 4e46ac7 | 2002-07-18 21:02:55 +0000 | [diff] [blame] | 1699 | |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 1700 | /* do we have any more buffer space to fill? */ |
Jens Arnold | ef32881 | 2005-05-27 21:46:44 +0000 | [diff] [blame] | 1701 | if(free_space_left <= 0) |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1702 | { |
| 1703 | DEBUGF("0\n"); |
| 1704 | filling = false; |
Jens Arnold | dece414 | 2005-08-21 21:15:32 +0000 | [diff] [blame] | 1705 | generate_postbuffer_events(); |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 1706 | storage_sleep(); |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 1707 | break; |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1708 | } |
Linus Nielsen Feltzing | 4e46ac7 | 2002-07-18 21:02:55 +0000 | [diff] [blame] | 1709 | |
Linus Nielsen Feltzing | b605b2e | 2002-07-31 06:33:48 +0000 | [diff] [blame] | 1710 | /* Read small chunks while we are below the low water mark */ |
Linus Nielsen Feltzing | 2df262c | 2002-12-05 12:18:22 +0000 | [diff] [blame] | 1711 | if(unplayed_space_left < low_watermark) |
Linus Nielsen Feltzing | b605b2e | 2002-07-31 06:33:48 +0000 | [diff] [blame] | 1712 | amount_to_read = MIN(MPEG_LOW_WATER_CHUNKSIZE, |
| 1713 | free_space_left); |
Linus Nielsen Feltzing | 4e46ac7 | 2002-07-18 21:02:55 +0000 | [diff] [blame] | 1714 | else |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1715 | amount_to_read = free_space_left; |
Linus Nielsen Feltzing | b605b2e | 2002-07-31 06:33:48 +0000 | [diff] [blame] | 1716 | |
| 1717 | /* Don't read more than until the end of the buffer */ |
Jens Arnold | ef32881 | 2005-05-27 21:46:44 +0000 | [diff] [blame] | 1718 | amount_to_read = MIN(audiobuflen - audiobuf_write, |
| 1719 | amount_to_read); |
Frank Gevaerts | 19d1cac | 2008-10-31 21:25:04 +0000 | [diff] [blame] | 1720 | #if (CONFIG_STORAGE & STORAGE_MMC) |
| 1721 | /* MMC is slow, so don't read too large chunks */ |
Jens Arnold | 5ae37f0 | 2004-10-09 12:42:56 +0000 | [diff] [blame] | 1722 | amount_to_read = MIN(0x40000, amount_to_read); |
Andree Buschmann | 5d849a9 | 2011-02-02 17:43:32 +0000 | [diff] [blame] | 1723 | #elif MEMORYSIZE == 8 |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 1724 | amount_to_read = MIN(0x100000, amount_to_read); |
| 1725 | #endif |
Linus Nielsen Feltzing | 402502f | 2002-10-14 14:13:13 +0000 | [diff] [blame] | 1726 | |
| 1727 | /* Read as much mpeg data as we can fit in the buffer */ |
Linus Nielsen Feltzing | 3a25aa4 | 2002-05-29 12:25:21 +0000 | [diff] [blame] | 1728 | if(mpeg_file >= 0) |
Björn Stenberg | 1ac4600 | 2002-05-24 12:22:14 +0000 | [diff] [blame] | 1729 | { |
Linus Nielsen Feltzing | f779e26 | 2002-05-29 10:26:06 +0000 | [diff] [blame] | 1730 | DEBUGF("R\n"); |
Linus Nielsen Feltzing | 1d21ca9 | 2002-08-01 08:16:44 +0000 | [diff] [blame] | 1731 | t1 = current_tick; |
Thomas Martitz | d1322b7 | 2011-08-14 15:13:00 +0000 | [diff] [blame] | 1732 | len = read(mpeg_file, mpeg_audiobuf + audiobuf_write, |
Jens Arnold | ef32881 | 2005-05-27 21:46:44 +0000 | [diff] [blame] | 1733 | amount_to_read); |
Linus Nielsen Feltzing | c7e3675 | 2002-06-05 14:34:05 +0000 | [diff] [blame] | 1734 | if(len > 0) |
Linus Nielsen Feltzing | f779e26 | 2002-05-29 10:26:06 +0000 | [diff] [blame] | 1735 | { |
Linus Nielsen Feltzing | 1d21ca9 | 2002-08-01 08:16:44 +0000 | [diff] [blame] | 1736 | t2 = current_tick; |
| 1737 | DEBUGF("time: %d\n", t2 - t1); |
Linus Nielsen Feltzing | 80b285c | 2002-07-23 15:10:31 +0000 | [diff] [blame] | 1738 | DEBUGF("R: %x\n", len); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 1739 | |
Linus Nielsen Feltzing | cc136c5 | 2003-03-02 03:58:54 +0000 | [diff] [blame] | 1740 | /* Now make sure that we don't feed the MAS with ID3V1 |
| 1741 | data */ |
| 1742 | if (len < amount_to_read) |
| 1743 | { |
Linus Nielsen Feltzing | cc136c5 | 2003-03-02 03:58:54 +0000 | [diff] [blame] | 1744 | int i; |
Jens Arnold | ef32881 | 2005-05-27 21:46:44 +0000 | [diff] [blame] | 1745 | static const unsigned char tag[] = "TAG"; |
Linus Nielsen Feltzing | cc136c5 | 2003-03-02 03:58:54 +0000 | [diff] [blame] | 1746 | int taglen = 128; |
Jens Arnold | ef32881 | 2005-05-27 21:46:44 +0000 | [diff] [blame] | 1747 | int tagptr = audiobuf_write + len - 128; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [blame] | 1748 | |
Jens Arnold | ef32881 | 2005-05-27 21:46:44 +0000 | [diff] [blame] | 1749 | /* Really rare case: entire potential tag wasn't |
| 1750 | read in this call AND audiobuf_write < 128 */ |
| 1751 | if (tagptr < 0) |
| 1752 | tagptr += audiobuflen; |
Michael Sevakis | ffa8626 | 2013-07-12 12:03:20 -0400 | [diff] [ |