summaryrefslogtreecommitdiff
path: root/apps/plugins/Makefile
blob: 4f518a2c1caec366aae4cee6b9ecac5ca4762cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#

INCLUDES = $(TARGET_INC) -I$(FIRMDIR)/include -I$(FIRMDIR)/export -I$(FIRMDIR)/common \
 -I$(FIRMDIR)/drivers -I$(APPSDIR) -Ilib -I$(BUILDDIR) \
 -I$(BUILDDIR)/pluginbitmaps
CFLAGS = $(INCLUDES) $(GCCOPTS) $(TARGET) $(EXTRA_DEFINES)	\
 -DTARGET_ID=$(TARGET_ID) -DMEM=${MEMORYSIZE} -DPLUGIN

ifdef APPEXTRA
   INCLUDES += $(patsubst %,-I$(APPSDIR)/%,$(subst :, ,$(APPEXTRA)))
endif

ifdef SOFTWARECODECS
   CODECLIBS = -lmad -la52 -lffmpegFLAC -lTremor -lwavpack -lmusepack
endif

# Set up the bitmap libraries
BITMAPLIBS =
LINKBITMAPS =
ifneq ($(strip $(BMP2RB_MONO)),)
  BITMAPLIBS += $(BUILDDIR)/libpluginbitmapsmono.a
  LINKBITMAPS += -lpluginbitmapsmono
endif
ifneq ($(strip $(BMP2RB_NATIVE)),)
  BITMAPLIBS += $(BUILDDIR)/libpluginbitmapsnative.a
  LINKBITMAPS += -lpluginbitmapsnative
endif
ifneq ($(strip $(BMP2RB_REMOTEMONO)),)
  BITMAPLIBS += $(BUILDDIR)/libpluginbitmapsremotemono.a
  LINKBITMAPS += -lpluginbitmapsremotemono
endif
ifneq ($(strip $(BMP2RB_REMOTENATIVE)),)
  BITMAPLIBS += $(BUILDDIR)/libpluginbitmapsremotenative.a
  LINKBITMAPS += -lpluginbitmapsremotenative
endif

LDS := plugin.lds
LINKFILE := $(OBJDIR)/pluginlink.lds
DEPFILE = $(OBJDIR)/dep-plugins

# This sets up 'SRC' based on the files mentioned in SOURCES
include $(TOOLSDIR)/makesrc.inc

# This sets up 'SUBDIRS' based on the directories mentioned in SUBDIRS
include $(TOOLSDIR)/makesubdirs.inc

ROCKS := $(SRC:%.c=$(OBJDIR)/%.rock)
SOURCES = $(SRC)
ELFS := $(SRC:%.c=$(OBJDIR)/%.elf)
OBJS := $(SRC:%.c=$(OBJDIR)/%.o)
# as created by the cross-compiler for win32:
DEFS := $(SRC:%.c=$(OBJDIR)/%.def)
DIRS = .

.PHONY: $(SUBDIRS)
all: $(BUILDDIR)/libplugin.a $(ROCKS) $(SUBDIRS) $(DEPFILE)

$(DEPFILE): $(BITMAPLIBS)

dep: $(DEPFILE)

$(BUILDDIR)/credits.raw: $(DOCSDIR)/CREDITS
	$(call PRINTS,create credits.raw)perl credits.pl < $< > $@

$(OBJDIR)/credits.o: credits.c $(BUILDDIR)/credits.raw
	$(SILENT)mkdir -p $(dir $@)
	$(call PRINTS,CC $(<F))$(CC) $(CFLAGS) -I$(OBJDIR) -c $< -o $@

build-bitmapsmono:
	$(call PRINTS,MAKE in plugins/bitmaps/mono)$(MAKE) -C bitmaps/mono OBJDIR=$(OBJDIR)/bitmaps/mono

build-bitmapsnative:
	$(call PRINTS,MAKE in plugins/bitmaps/native)$(MAKE) -C bitmaps/native OBJDIR=$(OBJDIR)/bitmaps/native

build-bitmapsremotemono:
	$(call PRINTS,MAKE in plugins/bitmaps/remote_mono)$(MAKE) -C bitmaps/remote_mono OBJDIR=$(OBJDIR)/bitmaps/remote_mono

build-bitmapsremotenative:
	$(call PRINTS,MAKE in plugins/bitmaps/remote_native)$(MAKE) -C bitmaps/remote_native OBJDIR=$(OBJDIR)/bitmaps/remote_native

$(BITMAPLIBS): $(BUILDDIR)/libplugin%.a: build-%

ifndef SIMVER
$(OBJDIR)/%.elf: $(OBJDIR)/%.o $(LINKFILE) $(BUILDDIR)/libplugin.a $(BITMAPLIBS)
	$(call PRINTS,LD $(@F))$(CC) $(CFLAGS) -o $@ $< -L$(BUILDDIR) $(CODECLIBS) -lplugin $(LINKBITMAPS) -lgcc \
			-T$(LINKFILE) -Wl,--gc-sections -Wl,-Map,$(OBJDIR)/$*.map

$(OBJDIR)/%.rock : $(OBJDIR)/%.elf
	$(call PRINTS,OBJCOPY $(@F))$(OC) -O binary $< $@
else

ifeq ($(SIMVER), x11)
###################################################
# This is the X11 simulator version

$(OBJDIR)/%.rock : $(OBJDIR)/%.o  $(BUILDDIR)/libplugin.a $(BITMAPLIBS)
	$(call PRINTS,LD $(@F))$(CC) $(CFLAGS) $(SHARED_FLAG) $< -L$(BUILDDIR)  $(CODECLIBS) -lplugin $(LINKBITMAPS) -o $@
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
# 'x' must be kept or you'll have "Win32 error 5"
#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
#         #define ERROR_ACCESS_DENIED 5L
else
	$(SILENT)chmod -x $@
endif

else # end of x11-simulator
ifeq ($(SIMVER), sdl)
###################################################
# This is the SDL simulator version

$(OBJDIR)/%.rock : $(OBJDIR)/%.o  $(BUILDDIR)/libplugin.a $(BITMAPLIBS)
	$(call PRINTS,LD $(@F))$(CC) $(CFLAGS) $(SHARED_FLAG) $< -L$(BUILDDIR)  $(CODECLIBS) -lplugin $(LINKBITMAPS) -o $@
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
# 'x' must be kept or you'll have "Win32 error 5"
#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
#         #define ERROR_ACCESS_DENIED 5L
else
	$(SILENT)chmod -x $@
endif

else # end of sdl-simulator
###################################################
# This is the win32 simulator version
DLLTOOLFLAGS = --export-all
DLLWRAPFLAGS = -s --entry _DllMain@12 --target=i386-mingw32 -mno-cygwin

$(OBJDIR)/%.rock : $(OBJDIR)/%.o  $(BUILDDIR)/libplugin.a $(BITMAPLIBS)
	$(call PRINTS,DLL $(@F))$(DLLTOOL) $(DLLTOOLFLAGS) -z $(OBJDIR)/$*.def $<
	$(SILENT)$(DLLWRAP) $(DLLWRAPFLAGS) --def $(OBJDIR)/$*.def $< $(BUILDDIR)/libplugin.a $(BITMAPLIBS) \
                $(patsubst -l%,$(BUILDDIR)/lib%.a,$(CODECLIBS)) -o $@
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
# 'x' must be kept or you'll have "Win32 error 5"
#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
#         #define ERROR_ACCESS_DENIED 5L
else
	$(SILENT)chmod -x $@
endif
endif # end of win32-simulator
endif

endif # end of simulator section

include $(TOOLSDIR)/make.inc

$(BUILDDIR)/libplugin.a:
	$(SILENT)mkdir -p $(OBJDIR)/lib
	$(call PRINTS,MAKE in plugin/lib)$(MAKE) -C lib OBJDIR=$(OBJDIR)/lib

$(LINKFILE): $(LDS)
	$(call PRINTS,build $(@F))cat $< | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) $(TARGET) $(DEFINES) -E -P - >$@

$(SUBDIRS): $(BITMAPLIBS)
	$(SILENT)mkdir -p $(OBJDIR)/$@
	$(call PRINTS,MAKE in $@)$(MAKE) -C $@ OUTDIR=$(OBJDIR) OBJDIR=$(OBJDIR)/$@ \
                               LINKBITMAPS="$(LINKBITMAPS)" BITMAPLIBS="$(BITMAPLIBS)"

clean:
	$(call PRINTS,cleaning plugins)rm -f $(ROCKS) $(LINKFILE) $(OBJDIR)/*.rock $(DEPFILE) $(ELFS) \
	$(BUILDDIR)/credits.raw $(OBJS) $(DEFS)
	$(SILENT)$(MAKE) -C lib clean OBJDIR=$(OBJDIR)/lib
	$(SILENT)$(MAKE) -C bitmaps/mono clean OBJDIR=$(OBJDIR)/bitmaps/mono
	$(SILENT)$(MAKE) -C bitmaps/native clean OBJDIR=$(OBJDIR)/bitmaps/native
	$(SILENT)$(MAKE) -C bitmaps/remote_mono clean OBJDIR=$(OBJDIR)/bitmaps/remote_mono
	$(SILENT)$(MAKE) -C bitmaps/remote_native clean OBJDIR=$(OBJDIR)/bitmaps/remote_native
	$(SILENT)$(MAKE) -C rockboy clean OBJDIR=$(OBJDIR)/rockboy
	$(SILENT)$(MAKE) -C searchengine clean OBJDIR=$(OBJDIR)/searchengine
	@rm -rf $(BUILDDIR)/pluginbitmaps

-include $(DEPFILE)
/a> 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Philipp Pertermann
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"

#ifndef SIMULATOR
#ifdef HAVE_LCD_BITMAP

PLUGIN_HEADER

/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define SPLITEDIT_QUIT BUTTON_OFF
#define SPLITEDIT_PLAY BUTTON_PLAY
#define SPLITEDIT_SAVE BUTTON_F1
#define SPLITEDIT_LOOP_MODE BUTTON_F2
#define SPLITEDIT_SCALE BUTTON_F3
#define SPLITEDIT_SPEED50 (BUTTON_ON | BUTTON_LEFT)
#define SPLITEDIT_SPEED100 (BUTTON_ON | BUTTON_PLAY)
#define SPLITEDIT_SPEED150 (BUTTON_ON | BUTTON_RIGHT)
#define SPLITEDIT_MENU_RUN BUTTON_PLAY

#elif CONFIG_KEYPAD == ONDIO_PAD
#define SPLITEDIT_QUIT BUTTON_OFF
#define SPLITEDIT_PLAY_PRE BUTTON_MENU
#define SPLITEDIT_PLAY (BUTTON_MENU | BUTTON_REL)
#define SPLITEDIT_SAVE (BUTTON_MENU | BUTTON_LEFT)
#define SPLITEDIT_LOOP_MODE (BUTTON_MENU | BUTTON_UP)
#define SPLITEDIT_SCALE (BUTTON_MENU | BUTTON_RIGHT)
#define SPLITEDIT_MENU_RUN BUTTON_RIGHT

#elif CONFIG_KEYPAD == IRIVER_H100_PAD
#define SPLITEDIT_QUIT BUTTON_OFF
#define SPLITEDIT_PLAY BUTTON_ON
#define SPLITEDIT_SAVE BUTTON_SELECT
#define SPLITEDIT_LOOP_MODE BUTTON_MODE
#define SPLITEDIT_SCALE (BUTTON_REC | BUTTON_UP)
#define SPLITEDIT_SPEED50 (BUTTON_REC | BUTTON_LEFT)
#define SPLITEDIT_SPEED100 (BUTTON_REC | BUTTON_DOWN)
#define SPLITEDIT_SPEED150 (BUTTON_REC | BUTTON_RIGHT)
#define SPLITEDIT_MENU_RUN BUTTON_RIGHT

#define SPLITEDIT_RC_QUIT BUTTON_RC_STOP
#endif

#define BMPHEIGHT 7
#define BMPWIDTH 13
unsigned char LOOP_BMP[][13] =
{
  {0xfc,0x00,0x10,0x11,0x93,0x7f,0x13,0x11,0x7c,0x38,0x10,0x00,0x7c}, /*ALL */
  {0x81,0x03,0x7f,0x03,0x91,0x10,0x10,0x10,0x7c,0x38,0x10,0x00,0x7c}, /*FROM*/
  {0xfc,0x00,0x10,0x10,0x90,0x10,0x7c,0x38,0x11,0x03,0x7f,0x03,0x01}, /*TO  */
  {0x80,0x10,0x10,0x11,0x93,0x7f,0x13,0x11,0x10,0x7c,0x38,0x10,0x00}, /*FREE*/
};

unsigned char CUT_BMP[] =
{
  0xc1,0x63,0x63,0x36,0xb6,0x1c,0x1c,0x36,0x77,0x55,0x55,0x55,0x32,
};

unsigned char SCALE_BMP[][13] =
{
  {0x80,0x06,0x49,0x66,0xb0,0x18,0x0c,0x06,0x33,0x49,0x30,0x00,0x00}, /*lin*/
  {0x80,0x30,0x78,0x48,0xff,0x7f,0x00,0x7f,0x7f,0x48,0x78,0x30,0x00}, /*db*/
};

#define TIMEBAR_Y 9
#define TIMEBAR_HEIGHT 4

#define OSCI_X 0
#define OSCI_Y (TIMEBAR_Y + TIMEBAR_HEIGHT + 1)
#define OSCI_WIDTH LCD_WIDTH
#define OSCI_HEIGHT (LCD_HEIGHT - BMPHEIGHT - OSCI_Y - 1)

/* Indices of the menu items in the save editor, see save_editor */
#define SE_PART1_SAVE 0
#define SE_PART1_NAME 1
#define SE_PART2_SAVE 2
#define SE_PART2_NAME 3
#define SE_SAVE 4
#define SE_COUNT 5

/* the global api pointer */
static struct plugin_api* rb;

/* contains the file name of the song that is to be split */
static char path_mp3[MAX_PATH];

/* Exit code of this plugin */
static enum plugin_status splitedit_exit_code = PLUGIN_OK;

/* The range in time that the displayed aerea comprises */
static unsigned int range_start = 0;
static unsigned int range_end = 0;

/* The range in time that is being looped */
static unsigned int play_start = 0;
static unsigned int play_end = 0;

/* Point in time (pixel) at which the split mark is set */
static int split_x = OSCI_X + (OSCI_WIDTH / 2);

/* Contains the peak values */
static unsigned char osci_buffer[OSCI_WIDTH];

/* if true peak values from a previous loop are only overwritten
   if the new value is greater than the old value */
static bool osci_valid = false;

/**
 * point in time from which on the osci_buffer is invalid
 * if set to ~(unsigned int)0 the entire osci_buffer is invalid
 */
static unsigned int validation_start = ~(unsigned int)0;

/* all the visible aerea is looped */
#define LOOP_MODE_ALL  0

/* loop starts at split point, ends at right visible border */
#define LOOP_MODE_FROM 1

/* loop start at left visible border, ends at split point */
#define LOOP_MODE_TO   2

/* let the song play without looping */
#define LOOP_MODE_FREE 3

/* see LOOP_MODE_XXX constants vor valid values */
static int loop_mode = LOOP_MODE_FREE;

/* minimal allowed timespan (ms) of the visible (and looped) aerea*/
#define MIN_RANGE_SIZE 1000

/* Format time into buf.
 *
 * buf      - buffer to format to.
 * buf_size - size of buffer.
 * time     - time to format, in milliseconds.
 */
static void format_time_ms(char* buf, int buf_size, int time)
{
    rb->snprintf(buf, buf_size, "%d:%02d:%03d", time / 60000,
        time % 60000 / 1000, (time % 60000) % 1000);
}

/**
 * converts screen coordinate (pixel) to time (ms)
 */
static int xpos_to_time(int xpos)
{
    int retval = 0;
    int range = range_end - range_start;
    retval = range_start + (((xpos - OSCI_X) * range) / OSCI_WIDTH);
    return retval;
}

/**
 * Converts time (ms) to screen coordinates (pixel).
 */
static int time_to_xpos(unsigned int time)
{
    int retval = OSCI_X;

    /* clip the range */
    if (time < range_start)
    {
        retval = OSCI_X;
    }
    else
    if (time >= range_end)
    {
        retval = OSCI_X + OSCI_WIDTH;
    }

    /* do the calculation */
    else
    {
        int range = range_end - range_start;
        retval = OSCI_X + ((time - range_start) * OSCI_WIDTH) / range ;
    }
    return retval;
}

/**
 * Updates the display of the textual data only.
 */
static void update_data(void)
{
    char buf[20];
    char timebuf[10];
    int w, h;

    /* split point */
    format_time_ms(timebuf, sizeof buf, xpos_to_time(split_x));
    rb->snprintf(buf, sizeof buf, "Split at: %s", timebuf);

    rb->lcd_getstringsize(buf, &w, &h);

    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(0, 0, LCD_WIDTH, h);
    rb->lcd_set_drawmode(DRMODE_SOLID);
    rb->lcd_puts(0, 0, buf);
    rb->lcd_update_rect(0, 0, LCD_WIDTH, h);
}

/**
 * Displays which part of the song is visible
 * in the osci.
 */
static void update_timebar(struct mp3entry *mp3)
{
    rb->gui_scrollbar_draw
    (
        rb->screens[SCREEN_MAIN],0, TIMEBAR_Y, LCD_WIDTH, TIMEBAR_HEIGHT,
        mp3->length, range_start, range_end,
        HORIZONTAL
    );
    rb->lcd_update_rect(0, TIMEBAR_Y, LCD_WIDTH, TIMEBAR_HEIGHT);
}

/**
 * Marks the entire area of the osci buffer invalid.
 * It will be drawn with new values in the next loop.
 */
void splitedit_invalidate_osci(void)
{
    osci_valid = false;
    validation_start = ~(unsigned int)0;
}

/**
 * Returns the loop mode. See the LOOP_MODE_XXX constants above.
 */
int splitedit_get_loop_mode(void)
{
    return loop_mode;
}

/**
 * Updates the icons that display the Fn key hints.
 */
static void update_icons(void)
{
    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(0, LCD_HEIGHT - BMPHEIGHT, LCD_WIDTH, BMPHEIGHT);
    rb->lcd_set_drawmode(DRMODE_SOLID);

    /* The CUT icon */
    rb->lcd_mono_bitmap(CUT_BMP,
        LCD_WIDTH / 3 / 2 - BMPWIDTH / 2, LCD_HEIGHT - BMPHEIGHT,
        BMPWIDTH, BMPHEIGHT);

    /* The loop mode icon */
    rb->lcd_mono_bitmap(LOOP_BMP[splitedit_get_loop_mode()],
        LCD_WIDTH/3 + LCD_WIDTH/3 / 2 - BMPWIDTH/2, LCD_HEIGHT - BMPHEIGHT,
        BMPWIDTH, BMPHEIGHT);

#if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
    /* The scale icon */
    rb->lcd_mono_bitmap(SCALE_BMP[rb->peak_meter_get_use_dbfs() ? 1 : 0],
        2 *LCD_WIDTH/3 + LCD_WIDTH/3 / 2 - BMPWIDTH/2, LCD_HEIGHT - BMPHEIGHT,
        BMPWIDTH, BMPHEIGHT);
#else
    {
        static int idx;
        if (idx < 0 || idx > 1) idx = 0;
        idx = 1 - idx;
        rb->lcd_mono_bitmap(SCALE_BMP[idx],
            2 *LCD_WIDTH/3 + LCD_WIDTH/3 / 2 - BMPWIDTH/2, LCD_HEIGHT - BMPHEIGHT,
            BMPWIDTH, BMPHEIGHT);
    }
#endif

    rb->lcd_update_rect(0, LCD_HEIGHT - BMPHEIGHT, LCD_WIDTH, BMPHEIGHT);
}

/**
 * Sets the loop mode. See the LOOP_MODE_XXX constants above.
 */
void splitedit_set_loop_mode(int mode)
{
    int old_loop_mode = loop_mode;
    /* range restriction */
    loop_mode = mode % (LOOP_MODE_FREE + 1);
    switch (loop_mode)
    {
        case LOOP_MODE_ALL:
            play_start = range_start;
            play_end = range_end;
            break;

        case LOOP_MODE_FROM:
            play_start = xpos_to_time(split_x);
            play_end = range_end;
            break;

        case LOOP_MODE_TO:
            play_start = range_start;
            play_end = xpos_to_time(split_x);
            break;

        case LOOP_MODE_FREE:
            /* play_start is used when the song plays beyond its end */
            play_start = range_start;
            play_end = range_end;
            break;
    }

    if (loop_mode != old_loop_mode)
    {
        update_icons();
    }
}

/**
 * Readraws the osci without clear.
 */
static void redraw_osci(void)
{
    int x;
    for (x = 0; x < OSCI_WIDTH; x++)
    {
        if (osci_buffer[x] > 0)
        {
            rb->lcd_drawline
            (
                OSCI_X + x, OSCI_Y + OSCI_HEIGHT - 1,
                OSCI_X + x, OSCI_Y + OSCI_HEIGHT - osci_buffer[x] - 1
            );
        }
    }
}

/**
 * Sets the range of time in which the user can finetune the split
 * point. The split point is the center of the time range.
 */
static void set_range_by_time(
    struct mp3entry *mp3,
    unsigned int split_time,
    unsigned int range)
{
    if (mp3 != NULL)
    {
        if (range < MIN_RANGE_SIZE)
        {
            range = MIN_RANGE_SIZE;
        }
        range_start = (split_time > range / 2) ? (split_time - range / 2) : 0;
        range_end = MIN(range_start + range, mp3->length);
        split_x = time_to_xpos(split_time);

        splitedit_invalidate_osci();

        /* this sets the play_start / play_end */
        splitedit_set_loop_mode(splitedit_get_loop_mode());

        update_data();
        update_timebar(mp3);
    }
}

/**
 * Set the split point in screen coordinates
 */
void splitedit_set_split_x(int newx)
{
    int minx = split_x - 2 > 0 ? split_x - 2: 0;

    /* remove old split point from screen, only if moved */
    if (split_x != newx)
    {
        rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
        rb->lcd_fillrect(minx, OSCI_Y, 5, 1);
        rb->lcd_fillrect(split_x-1 > 0 ? split_x - 1: 0, OSCI_Y + 1, 3, 1);
        rb->lcd_fillrect(split_x, OSCI_Y + 2, 1, OSCI_HEIGHT - 2);
        rb->lcd_set_drawmode(DRMODE_SOLID);
        rb->lcd_update_rect(minx, OSCI_Y, 5, OSCI_HEIGHT);
    }

    if (newx >= OSCI_X && newx < OSCI_X + OSCI_WIDTH)
    {
        split_x = newx;
        /* in LOOP_FROM / LOOP_TO modes play_start /play_end must be updated */
        splitedit_set_loop_mode(splitedit_get_loop_mode());

        /* display new split time  */
        update_data();
    }

    /* display new split point */
    minx = split_x - 2 > 0 ? split_x - 2: 0;
    rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
    rb->lcd_fillrect(minx, OSCI_Y, 5, 1);
    rb->lcd_fillrect(split_x - 1 > 0 ? split_x - 1: 0, OSCI_Y + 1, 3, 1);
    rb->lcd_fillrect(split_x, OSCI_Y + 2, 1, OSCI_HEIGHT - 2);
    rb->lcd_set_drawmode(DRMODE_SOLID);
    rb->lcd_update_rect(minx, OSCI_Y, 5, OSCI_HEIGHT);
}

/**
 * returns the split point in screen coordinates
 */
int splitedit_get_split_x(void)
{
    return split_x;
}

/**
 * Clears the osci area and redraws it
 */
static void update_osci(void)
{
    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(OSCI_X, OSCI_Y, OSCI_WIDTH, OSCI_HEIGHT);
    rb->lcd_set_drawmode(DRMODE_SOLID);
    redraw_osci();
    splitedit_set_split_x(splitedit_get_split_x());
    rb->lcd_update_rect(OSCI_X, OSCI_Y, OSCI_WIDTH, OSCI_HEIGHT);
}

/**
 * Zooms the visable and loopable range by the factor
 * (counter / denominator). The split point is used as
 * center point of the new selected range.
 */
static void zoom(struct mp3entry *mp3, int counter, int denominator)
{
    unsigned char oldbuf[OSCI_WIDTH];
    int oldrange = range_end - range_start;
    int range = oldrange * counter / denominator;
    int i;
    int oldindex;
    int oldsplitx;
    int splitx;
    int split;

    /* for stretching / shrinking a second buffer is needed */
    rb->memcpy(&oldbuf, &osci_buffer, sizeof osci_buffer);

    /* recalculate the new range and split point */
    oldsplitx = split_x;
    split = xpos_to_time(split_x);

    set_range_by_time(mp3, split, range);
    range = range_end - range_start;

    splitx = time_to_xpos(split);

    /* strech / shrink the existing osci buffer */
    for (i = 0; i < OSCI_WIDTH; i++)
    {
        /* oldindex = (i + OSCI_X - splitx) * range / oldrange + oldsplitx ;*/
        oldindex = (i*range / oldrange) + oldsplitx - (splitx*range /oldrange);
        if (oldindex >= 0 && oldindex < OSCI_WIDTH)
        {
            osci_buffer[i] = oldbuf[oldindex];
        }
        else
        {
            osci_buffer[i] = 0;
        }
    }

    splitx = time_to_xpos(split);
    splitedit_set_split_x(splitx);
    splitedit_invalidate_osci();

}

static void scroll(struct mp3entry *mp3)
{
    zoom(mp3, 1, 1);
    rb->lcd_update_rect(OSCI_X, OSCI_Y, LCD_WIDTH, OSCI_HEIGHT);
    update_osci();
    update_data();
}

/**
 * Zooms in by 3/4
 */
void splitedit_zoom_in(struct mp3entry *mp3)
{
    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(OSCI_X, OSCI_Y, OSCI_WIDTH, OSCI_HEIGHT);
    rb->lcd_set_drawmode(DRMODE_SOLID);
    zoom(mp3, 3, 4);
    rb->lcd_update_rect(OSCI_X, OSCI_Y, LCD_WIDTH, OSCI_HEIGHT);
    update_osci();
    update_data();
}

/**
 * Zooms out by 4/3
 */
void splitedit_zoom_out(struct mp3entry *mp3)
{
    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(OSCI_X, OSCI_Y, LCD_WIDTH, OSCI_HEIGHT);
    rb->lcd_set_drawmode(DRMODE_SOLID);
    zoom(mp3, 4, 3);
    rb->lcd_update_rect(OSCI_X, OSCI_Y, LCD_WIDTH, OSCI_HEIGHT);
    update_osci();
    update_data();
}

/**
 * Append part_no to the file name.
 */
static void generateFileName(char* file_name, int part_no)
{
    if (rb->strlen(file_name) <MAX_PATH)
    {
        int len = rb->strlen(file_name);
        int ext_len = rb->strlen(".mp3");
        if (rb->strcasecmp(
            &file_name[len - ext_len],
            ".mp3") == 0)
        {
            int i = 0;
            /* shift the extension one position to the right*/
            for (i = len; i > len - ext_len; i--)
            {
                file_name[i] = file_name[i - 1];
            }
            file_name[len - ext_len] = '0' + part_no;
        }
        else
        {
            rb->splash(0, "wrong extension");
            rb->button_get(true);
            rb->button_get(true);
        }
    }
    else
    {
        rb->splash(0, "name too long");
        rb->button_get(true);
        rb->button_get(true);

    }

}

/**
 * Copy bytes from src to dest while displaying a progressbar.
 * The files must be already open.
 */
static int copy_file(
    int dest,
    int src,
    unsigned int bytes,
    int prg_y,
    int prg_h)
{
    long button;
    unsigned char *buffer;
    unsigned int i = 0;
    ssize_t bytes_read = 1; /* ensure the for loop is executed */
    size_t buffer_size;
    buffer = rb->plugin_get_buffer(&buffer_size);

    for (i = 0; i < bytes && bytes_read > 0; i += bytes_read)
    {
        ssize_t bytes_written;
        unsigned int bytes_to_read =
            bytes - i > buffer_size ? buffer_size : bytes - i;
        bytes_read = rb->read(src, buffer, bytes_to_read);
        bytes_written = rb->write(dest, buffer, bytes_read);

        if (bytes_written < 0) {
            rb->splash(0, "Write failed in copy.");
            rb->button_get(true);
            rb->button_get(true);
            return -1;
        }

        button = rb->button_get(false);

        if (button == SPLITEDIT_QUIT
#ifdef SPLITEDIT_RC_QUIT
            || button == SPLITEDIT_RC_QUIT:
#endif
        ) {
            rb->splash(0, "Aborting copy.");
            rb->button_get(true);
            rb->button_get(true);
            return -1;
        }

        rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],0, prg_y, LCD_WIDTH,
                                 prg_h, bytes, 0, i, HORIZONTAL);
        rb->lcd_update_rect(0, prg_y, LCD_WIDTH, prg_h);
    }

    return 0;
}

/**
 * Save the files, if the file_name is not NULL
 */
static int save(
    struct mp3entry *mp3,
    char *file_name1,
    char *file_name2,
    int splittime)
{
    int file1, file2, src_file;
    unsigned int end = 0;
    int retval = 0;

    /* Verify that file 1 doesn't exit yet */
    if (file_name1 != NULL)
    {
        file1 = rb->open(file_name1, O_RDONLY);
        if (file1 >= 0)
        {
            rb->close(file1);
            rb->splash(0, "File 1 exists. Please rename.");
            rb->button_get(true);
            rb->button_get(true);
            return -1;
        }
    }

    /* Verify that file 2 doesn't exit yet */
    if (file_name2 != NULL)
    {
        file2 = rb->open(file_name2, O_RDONLY);
        if (file2 >= 0)
        {
            rb->close(file2);
            rb->splash(0, "File 2 exists. Please rename.");
            rb->button_get(true);
            rb->button_get(true);
            return -2;
        }
    }

    /* find the file position of the split point */
    rb->audio_pause();
    rb->audio_ff_rewind(splittime);
    rb->yield();
    rb->yield();
    end = rb->audio_get_file_pos();

    /* open the source file */
    src_file = rb->open(mp3->path, O_RDONLY);
    if (src_file >= 0)
    {
        int close_stat = 0;
        int x, y;
        long offset;
        unsigned long last_header = rb->mpeg_get_last_header();

        rb->lcd_getstringsize("M", &x, &y);

        /* Find the next frame boundary */
        rb->lseek(src_file, end, SEEK_SET);
        rb->find_next_frame(src_file, &offset, 8000, last_header);
        rb->lseek(src_file, 0, SEEK_SET);
        end += offset;
        
        /* write the file 1 */
        if (file_name1 != NULL)
        {
            file1 = rb->open (file_name1, O_WRONLY | O_CREAT);
            if (file1 >= 0)
            {
                int rc = copy_file(file1, src_file, end, y*2 + 1, y -1);
                close_stat = rb->close(file1);

                if (close_stat != 0)
                {
                    rb->splash(0, "failed closing file1: error %d", close_stat);
                    rb->button_get(true);
                    rb->button_get(true);
                } else {
                    /* If there was an error, cleanup */
                    if (rc) {
                        rb->remove(file_name1);
                    }
                }
            }
            else
            {
                rb->splash(0, "Can't write File1: error %d", file1);
                rb->button_get(true);
                rb->button_get(true);
                retval = -1;
            }
        }
        /* if file1 hasn't been written we're not at the split point yet */
        else
        {
            if (rb->lseek(src_file, end, SEEK_SET) < (off_t)end)
            {
                rb->splash(0, "Src file to short: error %d", src_file);
                rb->button_get(true);
                rb->button_get(true);
            }
        }

        if (file_name2 != NULL)
        {
            /* write file 2 */
            file2 = rb->open (file_name2, O_WRONLY | O_CREAT);
            if (file2 >= 0)
            {
                end = mp3->filesize - end;
                int rc = copy_file(file2, src_file, end, y * 5 + 1, y -1);
                close_stat = rb->close(file2);

                if (close_stat != 0)
                {
                    rb->splash(0, "failed: closing file2: error %d",
                               close_stat);
                    rb->button_get(true);
                    rb->button_get(true);
                } else {
                    /* If there was an error, cleanup */
                    if (rc) {
                        rb->remove(file_name2);
                    }
                }
            }
            else
            {
                rb->splash(0, "Can't write File2: error %d", file2);
                rb->button_get(true);
                rb->button_get(true);
                retval = -2;
            }
        }

        close_stat = rb->close(src_file);
        if (close_stat != 0)
        {
            rb->splash(0, "failed: closing src: error %d", close_stat);
            rb->button_get(true);
            rb->button_get(true);
        }
    }
    else
    {
        rb->splash(0, "Source file not found");
        rb->button_get(true);
        rb->button_get(true);
        retval = -3;
    }

    rb->audio_resume();

    return retval;
}

/**
 * Let the user choose which file to save with which name
 */
static void save_editor(struct mp3entry *mp3, int splittime)
{
    bool exit_request = false;
    int choice = 0;
    int button = BUTTON_NONE;
    char part1_name [MAX_PATH];
    char part2_name [MAX_PATH];
    bool part1_save = true;
    bool part2_save = true;

    /* file name for left part */
    rb->strncpy(part1_name, mp3->path, MAX_PATH);
    generateFileName(part1_name, 1);

    /* file name for right part */
    rb->strncpy(part2_name, mp3->path, MAX_PATH);
    generateFileName(part2_name, 2);

    while (!exit_request)
    {
        int pos;
        rb->lcd_clear_display();

        /* Save file1? */
        rb->lcd_puts_style(0, 0, "Save part 1?", choice == SE_PART1_SAVE);
        rb->lcd_puts(13, 0, part1_save?"yes":"no");

        /* trim to display the filename without path */
        for (pos = rb->strlen(part1_name); pos > 0; pos--)
        {
            if (part1_name[pos] == '/')
                break;
        }
        pos++;

        /* File name 1 */
        rb->lcd_puts_scroll_style(0, 1,
            &part1_name[pos], choice == SE_PART1_NAME);

        /* Save file2? */
        rb->lcd_puts_style(0, 3, "Save part 2?", choice == SE_PART2_SAVE);
        rb->lcd_puts(13, 3, part2_save?"yes":"no");

        /* trim to display the filename without path */
        for (pos = rb->strlen(part2_name); pos > 0; pos --)
        {
            if (part2_name[pos] == '/')
                break;
        }
        pos++;

        /* File name 2 */
        rb->lcd_puts_scroll_style(0, 4,
            &part2_name[pos], choice == SE_PART2_NAME);

        /* Save */
        rb->lcd_puts_style(0, 6, "Save", choice == SE_SAVE);

        rb->lcd_update();


        button = rb->button_get(true);
        switch (button)
        {
        case BUTTON_UP:
            choice = (choice + SE_COUNT - 1) % SE_COUNT;
            break;

        case BUTTON_DOWN:
            choice = (choice + 1) % SE_COUNT;
            break;

        case SPLITEDIT_MENU_RUN:
            switch (choice)
            {
            int saved;

            case SE_PART1_SAVE:
                part1_save = !part1_save;
                break;

            case SE_PART1_NAME:
                rb->kbd_input(part1_name, MAX_PATH);
                break;

            case SE_PART2_SAVE:
                part2_save = !part2_save;
                break;

            case SE_PART2_NAME:
                rb->kbd_input(part2_name, MAX_PATH);
                break;

            case SE_SAVE:
                rb->lcd_stop_scroll();
                rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
                rb->lcd_fillrect(0, 6*8, LCD_WIDTH, LCD_HEIGHT);
                rb->lcd_set_drawmode(DRMODE_SOLID);
                saved = save
                (
                    mp3,
                    part1_save?part1_name:NULL,
                    part2_save?part2_name:NULL,
                    splittime
                );

                /* if something failed the user may go on choosing */
                if (saved >= 0)
                {
                    exit_request = true;
                }
                break;
            }
            break;
#ifdef SPLITEDIT_RC_QUIT
        case SPLITEDIT_RC_QUIT:
#endif
        case SPLITEDIT_QUIT:
            exit_request = true;
            break;

        default:
            if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
            {
                splitedit_exit_code = PLUGIN_USB_CONNECTED;
                exit_request = true;
            }
            break;
        }
    }
}

/**
 * The main loop of the editor
 */
unsigned long splitedit_editor(struct mp3entry * mp3_to_split,
                          unsigned int split_time,
                          unsigned int range)
{
    int button = BUTTON_NONE;
    int lastbutton = BUTTON_NONE;
    struct mp3entry *mp3 = mp3_to_split;
    unsigned int last_elapsed = 0;
    int lastx = OSCI_X + (OSCI_WIDTH / 2);
    int retval = -1;

    if (mp3 != NULL)
    {
        /*unsigned short scheme = SCHEME_SPLIT_EDITOR;*/
        bool exit_request = false;
        set_range_by_time(mp3, split_time, range);
        splitedit_set_loop_mode(LOOP_MODE_ALL);
        update_icons();

        /*while (scheme != SCHEME_RETURN) {*/
        while (!exit_request)
        {
            unsigned int elapsed ;
            int x ;

            /* get position */
            elapsed = mp3->elapsed;
            x = time_to_xpos(elapsed);

            /* are we still in the zoomed range? */
            if (elapsed > play_start && elapsed < play_end)
            {
                /* read volume info */
                unsigned short volume;
#if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
                volume = rb->mas_codec_readreg(0x0c);
                volume += rb->mas_codec_readreg(0x0d);
                volume = volume / 2;
                volume = rb->peak_meter_scale_value(volume, OSCI_HEIGHT);
#else
                volume = OSCI_HEIGHT / 2;
#endif

                /* update osci_buffer */
                if (osci_valid || lastx == x)
                {
                    int index = x - OSCI_X;
                    osci_buffer[index] = MAX(osci_buffer[index], volume);
                }
                else
                {
                    int i;
                    osci_buffer[x - OSCI_X] = volume;
                    for (i = lastx + 1; i < x; i++)
                    {
                        osci_buffer[i - OSCI_X] = 0;
                    }
                }

                /* make room */
                rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
                rb->lcd_fillrect(lastx + 1, OSCI_Y, x - lastx, OSCI_HEIGHT);
                rb->lcd_set_drawmode(DRMODE_SOLID);
                /* draw a value */
                if (osci_buffer[x - OSCI_X] > 0)
                {
                    int i;
                    for (i = lastx +1; i <= x; i++)
                    {
                        rb->lcd_drawline
                            (
                            i, OSCI_Y + OSCI_HEIGHT - 1,
                            i, OSCI_Y + OSCI_HEIGHT - osci_buffer[i - OSCI_X]-1
                            );
                    }
                }

                /* mark the current position */
                if (lastx != x)
                {
                    rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
                    rb->lcd_fillrect(lastx, OSCI_Y, 1, OSCI_HEIGHT);
                    rb->lcd_fillrect(x, OSCI_Y, 1, OSCI_HEIGHT);
                    rb->lcd_set_drawmode(DRMODE_SOLID);
                }

                /* mark the split point */
                if ((x > split_x - 2) && (lastx < split_x + 3))
                {
                    if ((lastx < split_x) && (x >= split_x))
                    {
                        rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
                        rb->lcd_fillrect
                        (
                            split_x, OSCI_Y + 2,
                            1, OSCI_HEIGHT - 2
                        );
                        rb->lcd_set_drawmode(DRMODE_SOLID);
                    }
                    rb->lcd_drawline(split_x -2, OSCI_Y, split_x + 2, OSCI_Y);
                    rb->lcd_drawline(split_x-1, OSCI_Y+1, split_x +1,OSCI_Y+1);
                }

                /* make visible */
                if (lastx <= x)
                {
                    rb->lcd_update_rect(lastx, OSCI_Y, x-lastx+1, OSCI_HEIGHT);
                }
                else
                {
                    rb->lcd_update_rect
                    (
                        lastx, OSCI_Y,
                        OSCI_X + OSCI_WIDTH - lastx, OSCI_HEIGHT
                    );
                    rb->lcd_update_rect(0, OSCI_Y, x + 1, OSCI_HEIGHT);
                }

                lastx = x;
            }

            /* we're not in the zoom range -> rewind */
            else
            {
                if (elapsed >= play_end)
                {
                    switch (splitedit_get_loop_mode())
                    {
                    unsigned int range_width;

                    case LOOP_MODE_ALL:
                    case LOOP_MODE_TO:
                        rb->audio_pause();
                        rb->audio_ff_rewind(range_start);
#ifdef HAVE_MMC
/* MMC is slow - wait some time to allow track reload to finish */
                        rb->sleep(HZ/20);
                        if (mp3->elapsed > play_end) /* reload in progress */
                            rb->splash(10*HZ, "Wait - reloading");
#endif
                        rb->audio_resume();
                        break;

                    case LOOP_MODE_FROM:
                        rb->audio_pause();
                        rb->audio_ff_rewind(xpos_to_time(split_x));
#ifdef HAVE_MMC
/* MMC is slow - wait some time to allow track reload to finish */
                        rb->sleep(HZ/20);
                        if (mp3->elapsed > play_end) /* reload in progress */
                            rb->splash(10*HZ, "Wait - reloading");
#endif
                        rb->audio_resume();
                        break;

                    case LOOP_MODE_FREE:
                        range_width = range_end - range_start;
                        set_range_by_time(mp3,
                            range_end + range_width / 2, range_width);

                        /* play_end und play_start anpassen */
                        splitedit_set_loop_mode(LOOP_MODE_FREE);
                        rb->memset(osci_buffer, 0, sizeof osci_buffer);
                        update_osci();
                        rb->lcd_update();
                        break;
                    }
                }
            }

            button = rb->button_get(false);
            rb->yield();

            /* here the evaluation of the key scheme starts.
            All functions the user triggers are called from
            within execute_scheme */
            /* key_scheme_execute(button, &scheme); */
            switch (button)
            {
            case SPLITEDIT_PLAY:
#ifdef SPLITEDIT_PLAY_PRE
                if (lastbutton != SPLITEDIT_PLAY_PRE)
                    break;
#endif
                rb->audio_pause();
                rb->audio_ff_rewind(xpos_to_time(split_x));
                rb->audio_resume();
                break;

            case BUTTON_UP:
                splitedit_zoom_in(mp3);
                lastx = time_to_xpos(mp3->elapsed);
                break;

            case BUTTON_DOWN:
                splitedit_zoom_out(mp3);
                lastx = time_to_xpos(mp3->elapsed);
                break;

#if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
#ifdef SPLITEDIT_SPEED100
            case SPLITEDIT_SPEED150:
                rb->sound_set_pitch(1500);
                splitedit_invalidate_osci();
                break;

            case SPLITEDIT_SPEED100:
                rb->sound_set_pitch(1000);
                splitedit_invalidate_osci();
                break;

            case SPLITEDIT_SPEED50:
                rb->sound_set_pitch(500);
                splitedit_invalidate_osci();
                break;
#endif
#endif

            case BUTTON_LEFT:
            case BUTTON_LEFT | BUTTON_REPEAT:
                if (splitedit_get_split_x() > OSCI_X + 2)
                {
                    splitedit_set_split_x(splitedit_get_split_x() - 1);
                }
                else
                {
                    scroll(mp3);
                    lastx = time_to_xpos(mp3->elapsed);
                }
                break;

            case BUTTON_RIGHT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
                if (splitedit_get_split_x() < OSCI_X + OSCI_WIDTH-3)
                {
                    splitedit_set_split_x(splitedit_get_split_x() + 1);
                }
                else
                {
                    scroll(mp3);
                    lastx = time_to_xpos(mp3->elapsed);
                }
                break;

            case SPLITEDIT_SAVE:
                save_editor(mp3, xpos_to_time(split_x));
                rb->lcd_clear_display();
                update_osci();
                update_timebar(mp3);
                update_icons();
                break;

            case SPLITEDIT_LOOP_MODE:
                splitedit_set_loop_mode(splitedit_get_loop_mode() + 1);
                update_icons();
                break;

            case SPLITEDIT_SCALE:
#if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
                rb->peak_meter_set_use_dbfs(!rb->peak_meter_get_use_dbfs());
#endif
                splitedit_invalidate_osci();
                update_icons();
                break;

#ifdef SPLITEDIT_RC_QUIT
            case SPLITEDIT_RC_QUIT:
#endif
            case SPLITEDIT_QUIT:
                exit_request = true;
                break;

            default:
                if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                {
                    splitedit_exit_code = PLUGIN_USB_CONNECTED;
                    exit_request = true;
                }
                break;

            }
            if (button != BUTTON_NONE)
                lastbutton = button;

            if (validation_start == ~(unsigned int)0)
            {
                if (elapsed < range_end && elapsed > range_start)
                {
                    validation_start = elapsed;
                }
                else
                {
                    int endx = time_to_xpos(range_end);
                    validation_start = xpos_to_time(endx - 2);
                }
                last_elapsed = elapsed + 1;
            }
            else
            {
                if ((last_elapsed <= validation_start) &&
                    (elapsed > validation_start))
                {
                    osci_valid = true;
                }

                last_elapsed = elapsed;
            }
            update_data();

            if (mp3 != rb->audio_current_track())
            {
                struct mp3entry *new_mp3 = rb->audio_current_track();
                if (rb->strncasecmp(path_mp3, new_mp3->path,
                                    sizeof (path_mp3)))
                {
                    rb->splash(0, "Abort due to file change");
                    rb->button_get(true);
                    rb->button_get(true);
                    exit_request = true;
                }
                else
                {
                    mp3 = new_mp3;
                    rb->audio_pause();
                    rb->audio_flush_and_reload_tracks();
                    rb->audio_ff_rewind(range_start);
                    rb->audio_resume();
                }
            }
        }
#if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
#ifdef SPLITEDIT_SPEED100
        rb->sound_set_pitch(1000); /* make sure to reset pitch */
#endif
#endif

    }
    return retval;
}

enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    struct mp3entry* mp3;

    (void)parameter;
    rb = api;
    rb->lcd_clear_display();
    rb->lcd_update();
    mp3 = rb->audio_current_track();
    if (mp3 != NULL)
    {
        if (rb->audio_status() & AUDIO_STATUS_PAUSE)
        {
            rb->audio_resume();
        }
        splitedit_editor(mp3, mp3->elapsed, MIN_RANGE_SIZE * 8);
    }
    else
    {
        rb->splash(0, "Play or pause a mp3 file first.");
        rb->button_get(true);
        rb->button_get(true);
    }
    return splitedit_exit_code;
}
#endif
#endif