summaryrefslogtreecommitdiff
path: root/apps/plugins/wav2wv.c
blob: 935c0ed4c63fc91302856ba44eaf652a706d95ab (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/***************************************************************************
 *             __________               __   ___.
 *   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"

#include <codecs/libwavpack/wavpack.h>

#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 */
    long ckSize;
    char formType [4];

    char fmt_ckID [4];                 /* format chunk header */
    long fmt_ckSize;
    ushort FormatTag, NumChannels;
    ulong SampleRate, BytesPerSecond;
    ushort BlockAlign, BitsPerSample;

    char data_ckID [4];                 /* data chunk header */
    long data_ckSize;
} raw_header, native_header;

#define WAV_HEADER_FORMAT "4L44LSSLLSS4L"

static void wvupdate (long start_tick,
                      long sample_rate,
                      ulong total_samples,
                      ulong samples_converted,
                      ulong bytes_read,
                      ulong bytes_written)
{
    long elapsed_ticks = *rb->current_tick - start_tick;
    int compression = 0, progress = 0, realtime = 0;
    char buf[32];

    if (total_samples)
        progress = (int)(((long long) samples_converted * 100 +
            (total_samples/2)) / total_samples);

    if (elapsed_ticks)
        realtime = (int)(((long long) samples_converted * 100 * HZ /
            sample_rate + (elapsed_ticks/2)) / elapsed_ticks);

    if (bytes_read)
        compression = (int)(((long long)(bytes_read - bytes_written) * 100 +
            (bytes_read/2)) / bytes_read);

    rb->snprintf(buf, 32, "elapsed time: %d secs", (elapsed_ticks + (HZ/2)) / HZ);
    rb->lcd_puts(0, 2, buf);

    rb->snprintf(buf, 32, "progress: %d%%", progress);
    rb->lcd_puts(0, 4, buf);

    rb->snprintf(buf, 32, "realtime: %d%%  ", realtime);
    rb->lcd_puts(0, 6, buf);

    rb->snprintf(buf, 32, "compression: %d%%  ", compression);
    rb->lcd_puts(0, 8, buf);

#ifdef HAVE_LCD_BITMAP
    rb->lcd_update();
#endif
}

#define TEMP_SAMPLES 4096

static long temp_buffer [TEMP_SAMPLES] IDATA_ATTR;

static int wav2wv (char *filename)
{
    int in_fd, out_fd, num_chans, error = false, last_buttons;
    unsigned long total_bytes_read = 0, total_bytes_written = 0;
    unsigned long total_samples, samples_remaining;
    long *input_buffer = (long *) audiobuf;
    unsigned char *output_buffer = audiobuf + 0x100000;
    char *extension, save_a;
    WavpackConfig config;
    WavpackContext *wpc;
    long start_tick;

    rb->lcd_clear_display();
    rb->lcd_puts_scroll(0, 0, filename);
#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")) {
        rb->splash(HZ*2, true, "only for wav files!");
        return 1;
    }

    in_fd = rb->open(filename, O_RDONLY);

    if (in_fd < 0) {
        rb->splash(HZ*2, true, "could not open file!");
        return true;
    }

    if (rb->read (in_fd, &raw_header, sizeof (raw_header)) != sizeof (raw_header)) {
        rb->splash(HZ*2, true, "could not read file!");
        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) {
            rb->splash(HZ*2, true, "incompatible wav file!");
            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;

//  config.flags |= CONFIG_HIGH_FLAG;

    if (!WavpackSetConfiguration (wpc, &config, total_samples)) {
        rb->splash(HZ*2, true, "internal error!");
        rb->close (in_fd);
        return true;
    }

    WavpackAddWrapper (wpc, &raw_header, sizeof (raw_header));
    save_a = extension [1];
    extension [1] = extension [2];
    extension [2] = 0;

    out_fd = rb->creat (filename, O_WRONLY);

    extension [2] = extension [1];
    extension [1] = save_a;

    if (out_fd < 0) {
        rb->splash(HZ*2, true, "could not create file!");
        rb->close (in_fd);
        return true;
    }

    wvupdate (start_tick, native_header.SampleRate, total_samples, 0, 0, 0);

    for (samples_remaining = total_samples; samples_remaining;) {
        unsigned long samples_count, samples_to_pack, bytes_count;
        int cnt, buttons;
        long value, *lp;
        char *cp;
 
        samples_count = SAMPLES_PER_BLOCK;

        if (samples_count > samples_remaining)
            samples_count = samples_remaining;

        bytes_count = samples_count * num_chans * 2;

        if (rb->read (in_fd, input_buffer, bytes_count) != (long) bytes_count) {
            rb->splash(HZ*2, true, "could not read file!");
            error = true;
            break;
        }

        total_bytes_read += bytes_count;
        WavpackStartBlock (wpc, output_buffer, output_buffer + 0x100000);
        samples_to_pack = samples_count;
        cp = (char *) input_buffer;

        while (samples_to_pack) {
            unsigned long samples_this_pass = TEMP_SAMPLES / num_chans;

            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)) {
                rb->splash(HZ*2, true, "internal error!");
                error = true;
                break;
            }

            samples_to_pack -= samples_this_pass;
        }

        if (error)
            break;

        bytes_count = WavpackFinishBlock (wpc);

        if (rb->write (out_fd, output_buffer, bytes_count) != (long) bytes_count) {
            rb->splash(HZ*2, true, "could not write file!");
            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) {
            rb->splash(HZ*2, true, "operation aborted!");
            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
        rb->splash(HZ*3, true, "operation successful");

    return error;
}

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

    rb = api;

    if (!parameter)
        return PLUGIN_ERROR;

    audiobuf = rb->plugin_get_audio_buffer(&audiobuflen);

    if (audiobuflen < 0x200000) {
        rb->splash(HZ*2, true, "not enough memory!");
        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
    /* Return PLUGIN_USB_CONNECTED to force a file-tree refresh */
    return PLUGIN_USB_CONNECTED;
}