--[[ __________ __ ___. Open \______ \ ____ ____ | | _\_ |__ _______ ___ Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ \/ \/ \/ \/ \/ $Id$ Example Lua script Copyright (C) 2009 by Maurus Cuelenaere This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. ]]-- require("actions") -- Contains rb.actions & rb.contexts --require("buttons") -- Contains rb.buttons; not needed in this example -- Example function which splashes a message for x seconds function sayhello(seconds) local message = string.format("Hello world from LUA for %d seconds", seconds) rb.splash(seconds * rb.HZ, message) end -- Helper function which draws a (transparent) image at the center of the screen function draw_image(img) local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2 local func = rb.lcd_bitmap_transparent_part or rb.lcd_bitmap_part -- Fallback version for grayscale targets or rb.lcd_mono_bitmap_part -- Fallback version for mono targets func(img, 0, 0, img:width(), x, y, img:width(), img:height()) rb.lcd_update() end -- Helper function that acts like a normal printf() would do local line = 0 function printf(...) local msg = string.format(...) local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI) if(w >= rb.LCD_WIDTH) then rb.lcd_puts_scroll(0, line, msg) else rb.lcd_puts(0, line, msg) end rb.lcd_update() line = line + 1 if(h * line >= rb.LCD_HEIGHT) then line = 0 end end -- Helper function which reads the contents of a file function file_get_contents(filename) local file = io.open(filename, "r") if not file then return nil end local contents = file:read("*all") -- See Lua manual for more information file:close() -- GC takes care of this if you would've forgotten it return contents end -- Helper function which saves contents to a file function file_put_contents(filename, contents) local file = io.open(filename, "w+") -- See Lua manual for more information if not file then return false end local ret = file:write(contents) == true file:close() -- GC takes care of this if you would've forgotten it return ret end -- Clear the screen rb.lcd_clear_display() -- Draw an X on the screen rb.lcd_drawline(0, 0, rb.LCD_WIDTH, rb.LCD_HEIGHT) rb.lcd_drawline(rb.LCD_WIDTH, 0, 0, rb.LCD_HEIGHT) if(rb.lcd_rgbpack ~= nil) then -- Only do this when we're on a color target, i.e. when LCD_RGBPACK is available local rectangle = rb.new_image(10, 15) -- Create a new image with width 10 and height 15 for i=1, 10 do for j=1, 15 do rectangle:set(i, j, rb.lcd_rgbpack(200, i*20, j*20)) -- Set the pixel at position i, j to the specified color end end -- rb.lcd_bitmap_part(src, src_x, src_y, stride, x, y, width, height) rb.lcd_bitmap_part(rectangle, 0, 0, 10, rb.LCD_WIDTH/2-5, rb.LCD_HEIGHT/10-1, 10, 10) -- Draws our rectangle at the top-center of the screen end -- Load a BMP file in the variable backdrop local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present... or rb.read_bmp_file("/.rockbox/icons/tango_small_viewers_mono.bmp") -- ... if not, try using the mono version... or rb.read_bmp_file("/.rockbox/icons/viewers.bmp") -- ... or try using the builtin version -- Draws the image using our own draw_image() function; see up draw_image(backdrop) -- Flush the contents from the framebuffer to the LCD rb.lcd_update() -- Sleep for 2 seconds local seconds = 2 rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second -- Call the function sayhello() with arguments seconds sayhello(seconds) -- Clear display rb.lcd_clear_display() -- Construct a pathname using the current path and a file name local pathname = rb.current_path() .. "test.txt" -- Put the string 'Hello from Lua!' in pathname file_put_contents(pathname, "Hello from Lua!") -- Splash the contents of pathname printf(file_get_contents(pathname)) line = line + 1 -- Add empty line -- Some examples of how bitlib works printf("1 | 2 = %d <> 7 & 3 = %d", bit.bor(1, 2), bit.band(7, 3)) printf("~8 = %d <> 5 ^ 1 = %d", bit.bnot(8), bit.bxor(5, 1)) line = line + 1 -- Add empty line -- Display some long lines for i=0, 4 do printf("This is a %slong line!", string.rep("very very very ", i)) rb.yield() -- Always try to yield to give other threads some breathing space! end -- Loop until the user exits the program repeat local action = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ) until action == rb.actions.ACTION_STD_CANCEL -- For a reference list of all functions available, see apps/plugins/lua/rocklib.c (and apps/plugin.h) 7'>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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2004 Linus Nielsen Feltzing
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

PLUGIN_HEADER

static char   *audiobuf;
static ssize_t audiobuflen;
unsigned char xingbuf[1500];
char tmpname[MAX_PATH];

static void xingupdate(int percent)
{
    char buf[32];

    rb->snprintf(buf, 32, "%d%%", percent);
    rb->lcd_puts(0, 1, buf);
    rb->lcd_update();
}

static int insert_data_in_file(const char *fname, int fpos, char *buf, int num_bytes)
{
    int readlen;
    int rc;
    int orig_fd, fd;
    
    rb->snprintf(tmpname, MAX_PATH, "%s.tmp", fname);

    orig_fd = rb->open(fname, O_RDONLY);
    if(orig_fd < 0) {
        return 10*orig_fd - 1;
    }

    fd = rb->creat(tmpname);
    if(fd < 0) {
        rb->close(orig_fd);
        return 10*fd - 2;
    }

    /* First, copy the initial portion (the ID3 tag) */
    if(fpos) {
        readlen = rb->read(orig_fd, audiobuf, fpos);
        if(readlen < 0) {
            rb->close(fd);
            rb->close(orig_fd);
            return 10*readlen - 3;
        }
        
        rc = rb->write(fd, audiobuf, readlen);
        if(rc < 0) {
            rb->close(fd);
            rb->close(orig_fd);
            return 10*rc - 4;
        }
    }
    
    /* Now insert the data into the file */
    rc = rb->write(fd, buf, num_bytes);
    if(rc < 0) {
        rb->close(orig_fd);
        rb->close(fd);
        return 10*rc - 5;
    }

    /* Copy the file */
    do {
        readlen = rb->read(orig_fd, audiobuf, audiobuflen);
        if(readlen < 0) {
            rb->close(fd);
            rb->close(orig_fd);
            return 10*readlen - 7;
        }

        rc = rb->write(fd, audiobuf, readlen);
        if(rc < 0) {
            rb->close(fd);
            rb->close(orig_fd);
            return 10*rc - 8;
        }
    } while(readlen > 0);
    
    rb->close(fd);
    rb->close(orig_fd);

    /* Remove the old file */
    rc = rb->remove(fname);
    if(rc < 0) {
        return 10*rc - 9;
    }

    /* Replace the old file with the new */
    rc = rb->rename(tmpname, fname);
    if(rc < 0) {
        return 10*rc - 9;
    }
    
    return 0;
}

static void fileerror(int rc)
{
    rb->splashf(HZ*2, "File error: %d", rc);
}

static const unsigned char empty_id3_header[] =
{
    'I', 'D', '3', 0x04, 0x00, 0x00,
    0x00, 0x00, 0x1f, 0x76 /* Size is 4096 minus 10 bytes for the header */
};

static bool vbr_fix(const char *selected_file)
{
    struct mp3entry entry;
    int fd;
    int rc;
    int flen;
    int num_frames;
    int numbytes;
    int framelen;
    int unused_space;

    rb->lcd_clear_display();
    rb->lcd_puts_scroll(0, 0, selected_file);
    rb->lcd_update();

    xingupdate(0);

    rc = rb->mp3info(&entry, selected_file);
    if(rc < 0) {
        fileerror(rc);
        return true;
    }
    
    fd = rb->open(selected_file, O_RDWR);
    if(fd < 0) {
        fileerror(fd);
        return true;
    }

    flen = rb->lseek(fd, 0, SEEK_END);

    xingupdate(0);

    num_frames = rb->count_mp3_frames(fd, entry.first_frame_offset,
                                      flen, xingupdate);

    if(num_frames) {
        /* Note: We don't need to pass a template header because it will be
           taken from the mpeg stream */
        framelen = rb->create_xing_header(fd, entry.first_frame_offset,
                                          flen, xingbuf, num_frames, 0,
                                          0, xingupdate, true);
        
        /* Try to fit the Xing header first in the stream. Replace the existing
           VBR header if there is one, else see if there is room between the
           ID3 tag and the first MP3 frame. */
        if(entry.first_frame_offset - entry.id3v2len >=
           (unsigned int)framelen) {
            DEBUGF("Using existing space between ID3 and first frame\n");

            /* Seek to the beginning of the unused space */
            rc = rb->lseek(fd, entry.id3v2len, SEEK_SET);
            if(rc < 0) {
                rb->close(fd);
                fileerror(rc);
                return true;
            }

            unused_space =
                entry.first_frame_offset - entry.id3v2len - framelen;
            
            /* Fill the unused space with 0's (using the MP3 buffer)
               and write it to the file */
            if(unused_space)
            {
                rb->memset(audiobuf, 0, unused_space);
                rc = rb->write(fd, audiobuf, unused_space);
                if(rc < 0) {
                    rb->close(fd);
                    fileerror(rc);
                    return true;
                }
            }

            /* Then write the Xing header */
            rc = rb->write(fd, xingbuf, framelen);
            if(rc < 0) {
                rb->close(fd);
                fileerror(rc);
                return true;
            }
            
            rb->close(fd);
        } else {
            /* If not, insert some space. If there is an ID3 tag in the
               file we only insert just enough to squeeze the Xing header
               in. If not, we insert an additional empty ID3 tag of 4K. */
            
            rb->close(fd);
            
            /* Nasty trick alert! The insert_data_in_file() function
               uses the MP3 buffer when copying the data. We assume
               that the ID3 tag isn't longer than 1MB so the xing
               buffer won't be overwritten. */
            
            if(entry.first_frame_offset) {
                DEBUGF("Inserting %d bytes\n", framelen);
                numbytes = framelen;
            } else {