2005-07-04 07:07:43 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 David Bryant
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2005-07-04 07:07:43 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "plugin.h"
|
2006-01-18 20:54:13 +00:00
|
|
|
#ifdef RB_PROFILE
|
|
|
|
#include "lib/profile_plugin.h"
|
|
|
|
#endif
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
#include <codecs/libwavpack/wavpack.h>
|
|
|
|
|
2006-01-15 18:20:18 +00:00
|
|
|
PLUGIN_HEADER
|
|
|
|
|
2005-07-04 07:07:43 +00:00
|
|
|
#define SAMPLES_PER_BLOCK 22050
|
|
|
|
|
|
|
|
static char *audiobuf;
|
2007-04-21 18:38:25 +00:00
|
|
|
static ssize_t audiobuflen;
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
static struct wav_header {
|
|
|
|
char ckID [4]; /* RIFF chuck header */
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t ckSize;
|
2005-07-04 07:07:43 +00:00
|
|
|
char formType [4];
|
|
|
|
|
|
|
|
char fmt_ckID [4]; /* format chunk header */
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t fmt_ckSize;
|
2005-07-04 07:07:43 +00:00
|
|
|
ushort FormatTag, NumChannels;
|
2006-03-26 22:54:15 +00:00
|
|
|
uint32_t SampleRate, BytesPerSecond;
|
2005-07-04 07:07:43 +00:00
|
|
|
ushort BlockAlign, BitsPerSample;
|
|
|
|
|
|
|
|
char data_ckID [4]; /* data chunk header */
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t data_ckSize;
|
2005-07-04 07:07:43 +00:00
|
|
|
} raw_header, native_header;
|
|
|
|
|
|
|
|
#define WAV_HEADER_FORMAT "4L44LSSLLSS4L"
|
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
static void wvupdate (int32_t start_tick,
|
|
|
|
int32_t sample_rate,
|
|
|
|
uint32_t total_samples,
|
|
|
|
uint32_t samples_converted,
|
|
|
|
uint32_t bytes_read,
|
|
|
|
uint32_t bytes_written)
|
2005-07-04 07:07:43 +00:00
|
|
|
{
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t elapsed_ticks = *rb->current_tick - start_tick;
|
2005-07-04 07:07:43 +00:00
|
|
|
int compression = 0, progress = 0, realtime = 0;
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
if (total_samples)
|
2006-03-26 22:54:15 +00:00
|
|
|
progress = (int)(((int64_t) samples_converted * 100 +
|
2005-07-04 07:07:43 +00:00
|
|
|
(total_samples/2)) / total_samples);
|
|
|
|
|
|
|
|
if (elapsed_ticks)
|
2006-03-26 22:54:15 +00:00
|
|
|
realtime = (int)(((int64_t) samples_converted * 100 * HZ /
|
2005-07-04 07:07:43 +00:00
|
|
|
sample_rate + (elapsed_ticks/2)) / elapsed_ticks);
|
|
|
|
|
|
|
|
if (bytes_read)
|
2006-03-26 22:54:15 +00:00
|
|
|
compression = (int)(((int64_t)(bytes_read - bytes_written) * 100 +
|
2005-07-04 07:07:43 +00:00
|
|
|
(bytes_read/2)) / bytes_read);
|
|
|
|
|
2007-03-17 10:50:58 +00:00
|
|
|
rb->snprintf(buf, 32, "elapsed time: %ld secs",
|
|
|
|
(long)(elapsed_ticks + (HZ/2)) / HZ);
|
2005-12-02 08:08:54 +00:00
|
|
|
rb->lcd_puts(0, 2, (unsigned char *)buf);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
rb->snprintf(buf, 32, "progress: %d%%", progress);
|
2005-12-02 08:08:54 +00:00
|
|
|
rb->lcd_puts(0, 4, (unsigned char *)buf);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
rb->snprintf(buf, 32, "realtime: %d%% ", realtime);
|
2005-12-02 08:08:54 +00:00
|
|
|
rb->lcd_puts(0, 6, (unsigned char *)buf);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
rb->snprintf(buf, 32, "compression: %d%% ", compression);
|
2005-12-02 08:08:54 +00:00
|
|
|
rb->lcd_puts(0, 8, (unsigned char *)buf);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
rb->lcd_update();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-07-09 23:14:41 +00:00
|
|
|
#define TEMP_SAMPLES 4096
|
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
static int32_t temp_buffer [TEMP_SAMPLES] IDATA_ATTR;
|
2005-07-09 23:14:41 +00:00
|
|
|
|
2008-05-13 09:57:56 +00:00
|
|
|
static int wav2wv(const char *infile)
|
2005-07-04 07:07:43 +00:00
|
|
|
{
|
|
|
|
int in_fd, out_fd, num_chans, error = false, last_buttons;
|
2010-05-06 21:04:40 +00:00
|
|
|
uint32_t total_bytes_read = 0, total_bytes_written = 0;
|
|
|
|
uint32_t total_samples, samples_remaining;
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t *input_buffer = (int32_t *) audiobuf;
|
2005-12-02 08:08:54 +00:00
|
|
|
unsigned char *output_buffer = (unsigned char *)(audiobuf + 0x100000);
|
2008-05-13 09:57:56 +00:00
|
|
|
char outfile[MAX_PATH];
|
|
|
|
const char *inextension;
|
|
|
|
char *outextension;
|
2005-07-04 07:07:43 +00:00
|
|
|
WavpackConfig config;
|
|
|
|
WavpackContext *wpc;
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t start_tick;
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
rb->lcd_clear_display();
|
2008-05-13 09:57:56 +00:00
|
|
|
rb->lcd_puts_scroll(0, 0, (unsigned char *)infile);
|
2005-07-04 07:07:43 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
rb->lcd_update();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
last_buttons = rb->button_status ();
|
|
|
|
start_tick = *rb->current_tick;
|
2008-05-13 09:57:56 +00:00
|
|
|
inextension = infile + rb->strlen(infile) - 3;
|
|
|
|
if (rb->strcasecmp (inextension, "wav")) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "only for wav files!");
|
2005-07-04 07:07:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-05-13 09:57:56 +00:00
|
|
|
in_fd = rb->open(infile, O_RDONLY);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
if (in_fd < 0) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "could not open file!");
|
2005-07-04 07:07:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb->read (in_fd, &raw_header, sizeof (raw_header)) != sizeof (raw_header)) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "could not read file!");
|
2005-07-04 07:07:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_bytes_read += sizeof (raw_header);
|
|
|
|
rb->memcpy (&native_header, &raw_header, sizeof (raw_header));
|
|
|
|
little_endian_to_native (&native_header, WAV_HEADER_FORMAT);
|
|
|
|
|
2010-02-07 18:38:47 +00:00
|
|
|
if (rb->strncmp (native_header.ckID, "RIFF", 4) ||
|
|
|
|
rb->strncmp (native_header.fmt_ckID, "fmt ", 4) ||
|
|
|
|
rb->strncmp (native_header.data_ckID, "data", 4) ||
|
2005-07-04 07:07:43 +00:00
|
|
|
native_header.FormatTag != 1 || native_header.BitsPerSample != 16) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "incompatible wav file!");
|
2005-07-04 07:07:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpc = WavpackOpenFileOutput ();
|
|
|
|
|
|
|
|
rb->memset (&config, 0, sizeof (config));
|
|
|
|
config.bits_per_sample = 16;
|
|
|
|
config.bytes_per_sample = 2;
|
|
|
|
config.sample_rate = native_header.SampleRate;
|
|
|
|
num_chans = config.num_channels = native_header.NumChannels;
|
2010-02-07 18:38:47 +00:00
|
|
|
total_samples = native_header.data_ckSize / native_header.BlockAlign;
|
2005-07-04 07:07:43 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* config.flags |= CONFIG_HIGH_FLAG; */
|
2005-07-09 23:14:41 +00:00
|
|
|
|
2005-07-04 07:07:43 +00:00
|
|
|
if (!WavpackSetConfiguration (wpc, &config, total_samples)) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "internal error!");
|
2005-07-04 07:07:43 +00:00
|
|
|
rb->close (in_fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WavpackAddWrapper (wpc, &raw_header, sizeof (raw_header));
|
|
|
|
|
2008-05-13 09:57:56 +00:00
|
|
|
rb->strcpy(outfile, infile);
|
|
|
|
outextension = outfile + rb->strlen(outfile) - 3;
|
|
|
|
outextension[1] = outextension[2];
|
|
|
|
outextension[2] = 0;
|
2010-05-06 17:35:04 +00:00
|
|
|
out_fd = rb->creat(outfile, 0666);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
if (out_fd < 0) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "could not create file!");
|
2005-07-04 07:07:43 +00:00
|
|
|
rb->close (in_fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
wvupdate (start_tick, native_header.SampleRate, total_samples, 0, 0, 0);
|
|
|
|
|
|
|
|
for (samples_remaining = total_samples; samples_remaining;) {
|
2010-05-06 21:04:40 +00:00
|
|
|
uint32_t samples_count, samples_to_pack, bytes_count;
|
2005-07-04 07:07:43 +00:00
|
|
|
int cnt, buttons;
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t value, *lp;
|
2005-12-19 03:12:20 +00:00
|
|
|
signed char *cp;
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
samples_count = SAMPLES_PER_BLOCK;
|
|
|
|
|
|
|
|
if (samples_count > samples_remaining)
|
|
|
|
samples_count = samples_remaining;
|
|
|
|
|
|
|
|
bytes_count = samples_count * num_chans * 2;
|
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
if (rb->read (in_fd, input_buffer, bytes_count) != (int32_t) bytes_count) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "could not read file!");
|
2005-07-04 07:07:43 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_bytes_read += bytes_count;
|
2005-07-09 23:14:41 +00:00
|
|
|
WavpackStartBlock (wpc, output_buffer, output_buffer + 0x100000);
|
|
|
|
samples_to_pack = samples_count;
|
2005-12-19 03:12:20 +00:00
|
|
|
cp = (signed char *) input_buffer;
|
2005-07-09 23:14:41 +00:00
|
|
|
|
|
|
|
while (samples_to_pack) {
|
2010-05-06 21:04:40 +00:00
|
|
|
uint32_t samples_this_pass = TEMP_SAMPLES / num_chans;
|
2005-07-09 23:14:41 +00:00
|
|
|
|
|
|
|
if (samples_this_pass > samples_to_pack)
|
|
|
|
samples_this_pass = samples_to_pack;
|
|
|
|
|
|
|
|
lp = temp_buffer;
|
|
|
|
cnt = samples_this_pass;
|
|
|
|
|
|
|
|
if (num_chans == 2)
|
|
|
|
while (cnt--) {
|
|
|
|
value = *cp++ & 0xff;
|
|
|
|
value += *cp++ << 8;
|
|
|
|
*lp++ = value;
|
|
|
|
value = *cp++ & 0xff;
|
|
|
|
value += *cp++ << 8;
|
|
|
|
*lp++ = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while (cnt--) {
|
|
|
|
value = *cp++ & 0xff;
|
|
|
|
value += *cp++ << 8;
|
|
|
|
*lp++ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WavpackPackSamples (wpc, temp_buffer, samples_this_pass)) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "internal error!");
|
2005-07-09 23:14:41 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
samples_to_pack -= samples_this_pass;
|
2005-07-04 07:07:43 +00:00
|
|
|
}
|
|
|
|
|
2005-07-09 23:14:41 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bytes_count = WavpackFinishBlock (wpc);
|
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
if (rb->write (out_fd, output_buffer, bytes_count) != (int32_t) bytes_count) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "could not write file!");
|
2005-07-04 07:07:43 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_bytes_written += bytes_count;
|
|
|
|
samples_remaining -= samples_count;
|
|
|
|
|
|
|
|
wvupdate (start_tick, native_header.SampleRate, total_samples,
|
|
|
|
total_samples - samples_remaining, total_bytes_read, total_bytes_written);
|
|
|
|
|
|
|
|
buttons = rb->button_status ();
|
|
|
|
|
|
|
|
if (last_buttons == BUTTON_NONE && buttons != BUTTON_NONE) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "operation aborted!");
|
2005-07-04 07:07:43 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
last_buttons = buttons;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb->close (out_fd);
|
|
|
|
rb->close (in_fd);
|
|
|
|
|
|
|
|
if (error) {
|
2008-05-13 09:57:56 +00:00
|
|
|
rb->remove (outfile);
|
2005-07-04 07:07:43 +00:00
|
|
|
}
|
|
|
|
else
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*3, "operation successful");
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-01-16 10:34:40 +00:00
|
|
|
enum plugin_status plugin_start(const void *parameter)
|
2005-07-04 07:07:43 +00:00
|
|
|
{
|
|
|
|
if (!parameter)
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
|
2007-04-21 19:07:15 +00:00
|
|
|
audiobuf = rb->plugin_get_audio_buffer((size_t *)&audiobuflen);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
if (audiobuflen < 0x200000) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "not enough memory!");
|
2005-07-04 07:07:43 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
wav2wv (parameter);
|
|
|
|
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(false);
|
|
|
|
#endif
|
2005-08-31 00:30:46 +00:00
|
|
|
/* Return PLUGIN_USB_CONNECTED to force a file-tree refresh */
|
|
|
|
return PLUGIN_USB_CONNECTED;
|
2005-07-04 07:07:43 +00:00
|
|
|
}
|