enable downloading of the manual. It will get saved to the players root folder for...
[Rockbox.git] / apps / abrepeat.c
blobf1a430b8966e3f9a462ec170e267956cf8082701
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $$
10 * Copyright (C) 2005 Ray Lambert
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "abrepeat.h"
22 #ifdef AB_REPEAT_ENABLE
24 unsigned int ab_A_marker IDATA_ATTR = AB_MARKER_NONE;
25 unsigned int ab_B_marker IDATA_ATTR = AB_MARKER_NONE;
27 #if (CONFIG_CODEC == SWCODEC)
28 void ab_end_of_track_report(void)
30 if ( ab_A_marker_set() && ! ab_B_marker_set() )
32 ab_jump_to_A_marker();
35 #else
36 static int ab_audio_event_handler(unsigned short event, unsigned long data)
38 int rc = AUDIO_EVENT_RC_IGNORED;
39 if ( ab_repeat_mode_enabled() )
41 switch(event)
43 case AUDIO_EVENT_POS_REPORT:
45 if ( ! (audio_status() & AUDIO_STATUS_PAUSE) &&
46 ab_reached_B_marker(data) )
48 ab_jump_to_A_marker();
49 rc = AUDIO_EVENT_RC_HANDLED;
51 break;
53 case AUDIO_EVENT_END_OF_TRACK:
55 if ( ab_A_marker_set() && ! ab_B_marker_set() )
57 ab_jump_to_A_marker();
58 rc = AUDIO_EVENT_RC_HANDLED;
60 break;
64 return rc;
66 #endif
68 void ab_repeat_init(void)
70 static bool ab_initialized = false;
71 if ( ! ab_initialized )
73 ab_initialized = true;
74 #if (CONFIG_CODEC != SWCODEC)
75 audio_register_event_handler(ab_audio_event_handler,
76 AUDIO_EVENT_POS_REPORT | AUDIO_EVENT_END_OF_TRACK );
77 #endif
81 #if 0 /* Currently unused */
82 unsigned int ab_get_A_marker(void)
84 return ab_A_marker;
87 unsigned int ab_get_B_marker(void)
89 return ab_B_marker;
91 #endif /* if 0 */
93 /* determines if the given song position is earlier than the A mark;
94 intended for use in handling the jump NEXT and PREV commands */
95 bool ab_before_A_marker(unsigned int song_position)
97 return (ab_A_marker != AB_MARKER_NONE)
98 && (song_position < ab_A_marker);
101 /* determines if the given song position is later than the A mark;
102 intended for use in handling the jump PREV command */
103 bool ab_after_A_marker(unsigned int song_position)
105 /* following is the size of the virtual A marker; we pretend that the A marker
106 is larger than a single instant in order to give the user time to hit PREV again
107 to jump back to the start of the song; it should be large enough to allow a
108 reasonable amount of time for the typical user to react */
109 #define A_MARKER_VIRTUAL_SIZE 1000
110 return (ab_A_marker != AB_MARKER_NONE)
111 && (song_position > (ab_A_marker+A_MARKER_VIRTUAL_SIZE));
114 void ab_jump_to_A_marker(void)
116 #if (CONFIG_CODEC != SWCODEC)
117 bool paused = (audio_status() & AUDIO_STATUS_PAUSE) != 0;
118 if ( ! paused )
119 audio_pause();
120 #endif
121 audio_ff_rewind(ab_A_marker);
122 #if (CONFIG_CODEC != SWCODEC)
123 if ( ! paused )
124 audio_resume();
125 #endif
128 void ab_reset_markers(void)
130 ab_A_marker = AB_MARKER_NONE;
131 ab_B_marker = AB_MARKER_NONE;
134 /* following is a fudge factor to help overcome the latency between
135 the time the user hears the passage they want to mark and the time
136 they actually press the button; the actual song position is adjusted
137 by this fudge factor when setting a mark */
138 #define EAR_TO_HAND_LATENCY_FUDGE 200
140 void ab_set_A_marker(unsigned int song_position)
142 ab_A_marker = song_position;
143 ab_A_marker = (ab_A_marker >= EAR_TO_HAND_LATENCY_FUDGE)
144 ? (ab_A_marker - EAR_TO_HAND_LATENCY_FUDGE) : 0;
145 /* check if markers are out of order */
146 if ( (ab_B_marker != AB_MARKER_NONE) && (ab_A_marker > ab_B_marker) )
147 ab_B_marker = AB_MARKER_NONE;
150 void ab_set_B_marker(unsigned int song_position)
152 ab_B_marker = song_position;
153 ab_B_marker = (ab_B_marker >= EAR_TO_HAND_LATENCY_FUDGE)
154 ? (ab_B_marker - EAR_TO_HAND_LATENCY_FUDGE) : 0;
155 /* check if markers are out of order */
156 if ( (ab_A_marker != AB_MARKER_NONE) && (ab_B_marker < ab_A_marker) )
157 ab_A_marker = AB_MARKER_NONE;
160 #ifdef HAVE_LCD_BITMAP
162 static inline int ab_calc_mark_x_pos(int mark, int capacity,
163 int offset, int size)
165 int w = size - offset;
166 return offset + ( (w * mark) / capacity );
169 static inline void ab_draw_vertical_line_mark(struct screen * screen,
170 int x, int y, int h)
172 screen->set_drawmode(DRMODE_COMPLEMENT);
173 screen->vline(x, y, y+h-1);
176 #define DIRECTION_RIGHT 1
177 #define DIRECTION_LEFT -1
179 static inline void ab_draw_arrow_mark(struct screen * screen,
180 int x, int y, int h, int direction)
182 /* draw lines in decreasing size until a height of zero is reached */
183 screen->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
184 while( h > 0 )
186 screen->vline(x, y, y+h-1);
187 h -= 2;
188 y++;
189 x += direction;
190 screen->set_drawmode(DRMODE_COMPLEMENT);
194 void ab_draw_markers(struct screen * screen, int capacity,
195 int x0, int x1, int y, int h)
197 /* if both markers are set, determine if they're far enough apart
198 to draw arrows */
199 if ( ab_A_marker_set() && ab_B_marker_set() )
201 int xa = ab_calc_mark_x_pos(ab_A_marker, capacity, x0, x1);
202 int xb = ab_calc_mark_x_pos(ab_B_marker, capacity, x0, x1);
203 int arrow_width = (h+1) / 2;
204 if ( (xb-xa) < (arrow_width*2) )
206 ab_draw_vertical_line_mark(screen, xa, y, h);
207 ab_draw_vertical_line_mark(screen, xb, y, h);
209 else
211 ab_draw_arrow_mark(screen, xa, y, h, DIRECTION_RIGHT);
212 ab_draw_arrow_mark(screen, xb, y, h, DIRECTION_LEFT);
215 else
217 if (ab_A_marker_set())
219 int xa = ab_calc_mark_x_pos(ab_A_marker, capacity, x0, x1);
220 ab_draw_arrow_mark(screen, xa, y, h, DIRECTION_RIGHT);
222 if (ab_B_marker_set())
224 int xb = ab_calc_mark_x_pos(ab_B_marker, capacity, x0, x1);
225 ab_draw_arrow_mark(screen, xb, y, h, DIRECTION_LEFT);
230 #endif /* HAVE_LCD_BITMAP */
232 #endif /* AB_REPEAT_ENABLE */