/apps/plugins/

15' href='#n415'>415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 Dave Chapman
 *
 * 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

/* All swcodec targets have BUTTON_SELECT apart from the H10 and M3 */

#if CONFIG_KEYPAD == IRIVER_H10_PAD
#define TESTCODEC_EXITBUTTON BUTTON_RIGHT
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define TESTCODEC_EXITBUTTON BUTTON_RC_PLAY
#elif CONFIG_KEYPAD == COWOND2_PAD || CONFIG_KEYPAD == ONDAVX747_PAD
#define TESTCODEC_EXITBUTTON BUTTON_POWER
#else
#define TESTCODEC_EXITBUTTON BUTTON_SELECT
#endif

/* Log functions copied from test_disk.c */
static int line = 0;
static int max_line = 0;
static int log_fd = -1;
static char logfilename[MAX_PATH];

static bool log_init(bool use_logfile)
{
    int h;

    rb->lcd_getstringsize("A", NULL, &h);
    max_line = LCD_HEIGHT / h;
    line = 0;
    rb->lcd_clear_display();
    rb->lcd_update();

    if (use_logfile) {
        rb->create_numbered_filename(logfilename, "/", "test_codec_log_", ".txt",
                                     2 IF_CNFN_NUM_(, NULL));
        log_fd = rb->open(logfilename, O_RDWR|O_CREAT|O_TRUNC);
        return log_fd >= 0;
    }

    return true;
}

static void log_text(char *text, bool advance)
{
    rb->lcd_puts(0, line, text);
    rb->lcd_update();
    if (advance)
    {
        if (++line >= max_line)
            line = 0;
        if (log_fd >= 0)
            rb->fdprintf(log_fd, "%s\n", text);
    }
}

static void log_close(void)
{
    if (log_fd >= 0)
        rb->close(log_fd);
}

struct wavinfo_t
{
  int fd;
  int samplerate;
  int channels;
  int sampledepth;
  int stereomode;
  int totalsamples;
};

static void* audiobuf;
static void* codec_mallocbuf;
static size_t audiosize;
static char str[MAX_PATH];

/* Our local implementation of the codec API */
static struct codec_api ci;

struct test_track_info {
    struct mp3entry id3;       /* TAG metadata */
    size_t filesize;           /* File total length */
};

static struct test_track_info track;
static bool taginfo_ready = true;

static volatile unsigned int elapsed;
static volatile bool codec_playing;
static volatile long endtick;
struct wavinfo_t wavinfo;

static unsigned char wav_header[44] =
{
    'R','I','F','F',     //  0 - ChunkID
     0,0,0,0,            //  4 - ChunkSize (filesize-8)
     'W','A','V','E',    //  8 - Format
     'f','m','t',' ',    // 12 - SubChunkID
     16,0,0,0,           // 16 - SubChunk1ID  // 16 for PCM
     1,0,                // 20 - AudioFormat (1=16-bit)
     0,0,                // 22 - NumChannels
     0,0,0,0,            // 24 - SampleRate in Hz
     0,0,0,0,            // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
     0,0,                // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
     16,0,               // 34 - BitsPerSample
     'd','a','t','a',    // 36 - Subchunk2ID
     0,0,0,0             // 40 - Subchunk2Size
};

static inline void int2le32(unsigned char* buf, int32_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
  buf[2] = (x & 0xff0000) >> 16;
  buf[3] = (x & 0xff000000) >>24;
}

static inline void int2le24(unsigned char* buf, int32_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
  buf[2] = (x & 0xff0000) >> 16;
}

static inline void int2le16(unsigned char* buf, int16_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
}

void init_wav(char* filename)
{
    wavinfo.totalsamples = 0;

    wavinfo.fd = rb->creat(filename);

    if (wavinfo.fd >= 0)
    {
        /* Write WAV header - we go back and fill in the details at the end */
        rb->write(wavinfo.fd, wav_header, sizeof(wav_header));
    }
}


void close_wav(void) {
  int filesize = rb->filesize(wavinfo.fd);
  int channels = (wavinfo.stereomode == STEREO_MONO) ? 1 : 2;
  int bps = 16; /* TODO */

  /* We assume 16-bit, Stereo */

  rb->lseek(wavinfo.fd,0,SEEK_SET);

  int2le32(wav_header+4, filesize-8); /* ChunkSize */

  int2le16(wav_header+22, channels);

  int2le32(wav_header+24, wavinfo.samplerate);

  int2le32(wav_header+28, wavinfo.samplerate * channels * (bps / 8)); /* ByteRate */

  int2le16(wav_header+32, channels * (bps / 8));

  int2le32(wav_header+40, filesize - 44);  /* Subchunk2Size */

  rb->write(wavinfo.fd, wav_header, sizeof(wav_header));

  rb->close(wavinfo.fd);
}

/* Returns buffer to malloc array. Only codeclib should need this. */
static void* codec_get_buffer(size_t *size)
{
   DEBUGF("codec_get_buffer(%d)\n",(int)size);
   *size = CODEC_SIZE;
   return codec_mallocbuf;
}

/* Null output */
static bool pcmbuf_insert_null(const void *ch1, const void *ch2, int count)
{
    /* Always successful - just discard data */
    (void)ch1;
    (void)ch2;
    (void)count;

    /* Prevent idle poweroff */
    rb->reset_poweroff_timer();

    return true;
}

/* 64KB should be enough */
static unsigned char wavbuffer[64*1024];

static inline int32_t clip_sample(int32_t sample)
{
    if ((int16_t)sample != sample)
        sample = 0x7fff ^ (sample >> 31);

    return sample;
}


/* WAV output */
static bool pcmbuf_insert_wav(const void *ch1, const void *ch2, int count)
{
    const int16_t* data1_16;
    const int16_t* data2_16;
    const int32_t* data1_32;
    const int32_t* data2_32;
    unsigned char* p = wavbuffer;
    const int scale = wavinfo.sampledepth - 15;
    const int dc_bias = 1 << (scale - 1);

    /* Prevent idle poweroff */
    rb->reset_poweroff_timer();

    if (wavinfo.sampledepth <= 16) {
        data1_16 = ch1;
        data2_16 = ch2;

        switch(wavinfo.stereomode)
        {
            case STEREO_INTERLEAVED:
                while (count--) {
                    int2le16(p,*data1_16++);
                    p += 2;
                    int2le16(p,*data1_16++);
                    p += 2;
                }
                break;
 
            case STEREO_NONINTERLEAVED:
                while (count--) {
                    int2le16(p,*data1_16++);
                    p += 2;
                    int2le16(p,*data2_16++);
                    p += 2;
                }

                break;
     
            case STEREO_MONO:
                while (count--) {
                    int2le16(p,*data1_16++);
                    p += 2;
                }
                break;
        }
    } else {
        data1_32 = ch1;
        data2_32 = ch2;

        switch(wavinfo.stereomode)
        {
            case STEREO_INTERLEAVED:
                while (count--) {
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                }
                break;
 
            case STEREO_NONINTERLEAVED:
                while (count--) {
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                    int2le16(p, clip_sample((*data2_32++ + dc_bias) >> scale));
                    p += 2;
                }

                break;
     
            case STEREO_MONO:
                while (count--) {
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                }
                break;
        }
    }

    wavinfo.totalsamples += count;
    rb->write(wavinfo.fd, wavbuffer, p - wavbuffer);

    return true;
}


/* Set song position in WPS (value in ms). */
static void set_elapsed(unsigned int value)
{
    elapsed = value;
}


/* Read next <size> amount bytes from file buffer to <ptr>.
   Will return number of bytes read or 0 if end of file. */
static size_t read_filebuf(void *ptr, size_t size)
{
   if (ci.curpos > (off_t)track.filesize)
   {
       return 0;
   } else {
       /* TODO: Don't read beyond end of buffer */
       rb->memcpy(ptr, audiobuf + ci.curpos, size);
       ci.curpos += size;
       return size;
   }
}