2005-07-04 07:07:43 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 David Bryant
|
|
|
|
*
|
|
|
|
* 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"
|
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 struct plugin_api* rb;
|
|
|
|
|
|
|
|
void *memset(void *s, int c, size_t n) {
|
|
|
|
return(rb->memset(s,c,n));
|
|
|
|
}
|
|
|
|
|
|
|
|
void *memcpy(void *dest, const void *src, size_t n) {
|
|
|
|
return(rb->memcpy(dest,src,n));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *audiobuf;
|
|
|
|
static int audiobuflen;
|
|
|
|
|
|
|
|
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
|
|
|
|
2005-07-04 07:07:43 +00:00
|
|
|
static int wav2wv (char *filename)
|
|
|
|
{
|
|
|
|
int in_fd, out_fd, num_chans, error = false, last_buttons;
|
2006-03-26 22:54:15 +00:00
|
|
|
unsigned int32_t total_bytes_read = 0, total_bytes_written = 0;
|
|
|
|
unsigned int32_t total_samples, samples_remaining;
|
|
|
|
int32_t *input_buffer = (int32_t *) audiobuf;
|
2005-12-02 08:08:54 +00:00
|
|
|
unsigned char *output_buffer = (unsigned char *)(audiobuf + 0x100000);
|
2005-07-04 07:07:43 +00:00
|
|
|
char *extension, save_a;
|
|
|
|
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();
|
2005-12-02 08:08:54 +00:00
|
|
|
rb->lcd_puts_scroll(0, 0, (unsigned char *)filename);
|
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;
|
|
|
|
extension = filename + rb->strlen (filename) - 3;
|
|
|
|
|
|
|
|
if (rb->strcasecmp (extension, "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;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_fd = rb->open(filename, O_RDONLY);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (rb->strncmp (native_header.ckID, "RIFF", 4) ||
|
|
|
|
rb->strncmp (native_header.fmt_ckID, "fmt ", 4) ||
|
|
|
|
rb->strncmp (native_header.data_ckID, "data", 4) ||
|
|
|
|
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;
|
|
|
|
total_samples = native_header.data_ckSize / native_header.BlockAlign;
|
|
|
|
|
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));
|
|
|
|
save_a = extension [1];
|
|
|
|
extension [1] = extension [2];
|
|
|
|
extension [2] = 0;
|
|
|
|
|
2007-02-01 23:08:15 +00:00
|
|
|
out_fd = rb->creat (filename);
|
2005-07-04 07:07:43 +00:00
|
|
|
|
|
|
|
extension [2] = extension [1];
|
|
|
|
extension [1] = save_a;
|
|
|
|
|
|
|
|
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;) {
|
2006-03-26 22:54:15 +00:00
|
|
|
unsigned int32_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) {
|
2006-03-26 22:54:15 +00:00
|
|
|
unsigned int32_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) {
|
|
|
|
save_a = extension [1];
|
|
|
|
extension [1] = extension [2];
|
|
|
|
extension [2] = 0;
|
|
|
|
rb->remove (filename);
|
|
|
|
extension [2] = extension [1];
|
|
|
|
extension [1] = save_a;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum plugin_status plugin_start(struct plugin_api* api, void *parameter)
|
|
|
|
{
|
2006-01-18 20:54:13 +00:00
|
|
|
#ifdef RB_PROFILE
|
|
|
|
/* This doesn't start profiling or anything, it just gives the
|
|
|
|
* profiling functions that are compiled in someplace to call,
|
|
|
|
* this is needed here to let this compile with profiling support
|
|
|
|
* since it calls code from a codec that is compiled with profiling
|
|
|
|
* support */
|
|
|
|
profile_init(api);
|
|
|
|
#endif
|
|
|
|
|
2005-07-04 07:07:43 +00:00
|
|
|
rb = api;
|
|
|
|
|
|
|
|
if (!parameter)
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
|
|
|
|
audiobuf = rb->plugin_get_audio_buffer(&audiobuflen);
|
|
|
|
|
|
|
|
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
|
|
|
}
|