Remove plugin-specific defines from the pdbox makefile, and add them to m_pd.h, which...
[kugel-rb.git] / apps / plugins / wav2wv.c
blob3211203db9ee908c73feeb6bf1008dd49d17e5a1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 David Bryant
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #ifdef RB_PROFILE
23 #include "lib/profile_plugin.h"
24 #endif
26 #include <codecs/libwavpack/wavpack.h>
28 PLUGIN_HEADER
30 #define SAMPLES_PER_BLOCK 22050
32 static char *audiobuf;
33 static ssize_t audiobuflen;
35 static struct wav_header {
36 char ckID [4]; /* RIFF chuck header */
37 int32_t ckSize;
38 char formType [4];
40 char fmt_ckID [4]; /* format chunk header */
41 int32_t fmt_ckSize;
42 ushort FormatTag, NumChannels;
43 uint32_t SampleRate, BytesPerSecond;
44 ushort BlockAlign, BitsPerSample;
46 char data_ckID [4]; /* data chunk header */
47 int32_t data_ckSize;
48 } raw_header, native_header;
50 #define WAV_HEADER_FORMAT "4L44LSSLLSS4L"
52 static void wvupdate (int32_t start_tick,
53 int32_t sample_rate,
54 uint32_t total_samples,
55 uint32_t samples_converted,
56 uint32_t bytes_read,
57 uint32_t bytes_written)
59 int32_t elapsed_ticks = *rb->current_tick - start_tick;
60 int compression = 0, progress = 0, realtime = 0;
61 char buf[32];
63 if (total_samples)
64 progress = (int)(((int64_t) samples_converted * 100 +
65 (total_samples/2)) / total_samples);
67 if (elapsed_ticks)
68 realtime = (int)(((int64_t) samples_converted * 100 * HZ /
69 sample_rate + (elapsed_ticks/2)) / elapsed_ticks);
71 if (bytes_read)
72 compression = (int)(((int64_t)(bytes_read - bytes_written) * 100 +
73 (bytes_read/2)) / bytes_read);
75 rb->snprintf(buf, 32, "elapsed time: %ld secs",
76 (long)(elapsed_ticks + (HZ/2)) / HZ);
77 rb->lcd_puts(0, 2, (unsigned char *)buf);
79 rb->snprintf(buf, 32, "progress: %d%%", progress);
80 rb->lcd_puts(0, 4, (unsigned char *)buf);
82 rb->snprintf(buf, 32, "realtime: %d%% ", realtime);
83 rb->lcd_puts(0, 6, (unsigned char *)buf);
85 rb->snprintf(buf, 32, "compression: %d%% ", compression);
86 rb->lcd_puts(0, 8, (unsigned char *)buf);
88 #ifdef HAVE_LCD_BITMAP
89 rb->lcd_update();
90 #endif
93 #define TEMP_SAMPLES 4096
95 static int32_t temp_buffer [TEMP_SAMPLES] IDATA_ATTR;
97 static int wav2wv(const char *infile)
99 int in_fd, out_fd, num_chans, error = false, last_buttons;
100 uint32_t total_bytes_read = 0, total_bytes_written = 0;
101 uint32_t total_samples, samples_remaining;
102 int32_t *input_buffer = (int32_t *) audiobuf;
103 unsigned char *output_buffer = (unsigned char *)(audiobuf + 0x100000);
104 char outfile[MAX_PATH];
105 const char *inextension;
106 char *outextension;
107 WavpackConfig config;
108 WavpackContext *wpc;
109 int32_t start_tick;
111 rb->lcd_clear_display();
112 rb->lcd_puts_scroll(0, 0, (unsigned char *)infile);
113 #ifdef HAVE_LCD_BITMAP
114 rb->lcd_update();
115 #endif
117 last_buttons = rb->button_status ();
118 start_tick = *rb->current_tick;
119 inextension = infile + rb->strlen(infile) - 3;
120 if (rb->strcasecmp (inextension, "wav")) {
121 rb->splash(HZ*2, "only for wav files!");
122 return 1;
125 in_fd = rb->open(infile, O_RDONLY);
127 if (in_fd < 0) {
128 rb->splash(HZ*2, "could not open file!");
129 return true;
132 if (rb->read (in_fd, &raw_header, sizeof (raw_header)) != sizeof (raw_header)) {
133 rb->splash(HZ*2, "could not read file!");
134 return true;
137 total_bytes_read += sizeof (raw_header);
138 rb->memcpy (&native_header, &raw_header, sizeof (raw_header));
139 little_endian_to_native (&native_header, WAV_HEADER_FORMAT);
141 if (rb->strncmp (native_header.ckID, "RIFF", 4) ||
142 rb->strncmp (native_header.fmt_ckID, "fmt ", 4) ||
143 rb->strncmp (native_header.data_ckID, "data", 4) ||
144 native_header.FormatTag != 1 || native_header.BitsPerSample != 16) {
145 rb->splash(HZ*2, "incompatible wav file!");
146 return true;
149 wpc = WavpackOpenFileOutput ();
151 rb->memset (&config, 0, sizeof (config));
152 config.bits_per_sample = 16;
153 config.bytes_per_sample = 2;
154 config.sample_rate = native_header.SampleRate;
155 num_chans = config.num_channels = native_header.NumChannels;
156 total_samples = native_header.data_ckSize / native_header.BlockAlign;
158 /* config.flags |= CONFIG_HIGH_FLAG; */
160 if (!WavpackSetConfiguration (wpc, &config, total_samples)) {
161 rb->splash(HZ*2, "internal error!");
162 rb->close (in_fd);
163 return true;
166 WavpackAddWrapper (wpc, &raw_header, sizeof (raw_header));
168 rb->strcpy(outfile, infile);
169 outextension = outfile + rb->strlen(outfile) - 3;
170 outextension[1] = outextension[2];
171 outextension[2] = 0;
172 out_fd = rb->creat(outfile, 0666);
174 if (out_fd < 0) {
175 rb->splash(HZ*2, "could not create file!");
176 rb->close (in_fd);
177 return true;
180 wvupdate (start_tick, native_header.SampleRate, total_samples, 0, 0, 0);
182 for (samples_remaining = total_samples; samples_remaining;) {
183 uint32_t samples_count, samples_to_pack, bytes_count;
184 int cnt, buttons;
185 int32_t value, *lp;
186 signed char *cp;
188 samples_count = SAMPLES_PER_BLOCK;
190 if (samples_count > samples_remaining)
191 samples_count = samples_remaining;
193 bytes_count = samples_count * num_chans * 2;
195 if (rb->read (in_fd, input_buffer, bytes_count) != (int32_t) bytes_count) {
196 rb->splash(HZ*2, "could not read file!");
197 error = true;
198 break;
201 total_bytes_read += bytes_count;
202 WavpackStartBlock (wpc, output_buffer, output_buffer + 0x100000);
203 samples_to_pack = samples_count;
204 cp = (signed char *) input_buffer;
206 while (samples_to_pack) {
207 uint32_t samples_this_pass = TEMP_SAMPLES / num_chans;
209 if (samples_this_pass > samples_to_pack)
210 samples_this_pass = samples_to_pack;
212 lp = temp_buffer;
213 cnt = samples_this_pass;
215 if (num_chans == 2)
216 while (cnt--) {
217 value = *cp++ & 0xff;
218 value += *cp++ << 8;
219 *lp++ = value;
220 value = *cp++ & 0xff;
221 value += *cp++ << 8;
222 *lp++ = value;
224 else
225 while (cnt--) {
226 value = *cp++ & 0xff;
227 value += *cp++ << 8;
228 *lp++ = value;
231 if (!WavpackPackSamples (wpc, temp_buffer, samples_this_pass)) {
232 rb->splash(HZ*2, "internal error!");
233 error = true;
234 break;
237 samples_to_pack -= samples_this_pass;
240 if (error)
241 break;
243 bytes_count = WavpackFinishBlock (wpc);
245 if (rb->write (out_fd, output_buffer, bytes_count) != (int32_t) bytes_count) {
246 rb->splash(HZ*2, "could not write file!");
247 error = true;
248 break;
251 total_bytes_written += bytes_count;
252 samples_remaining -= samples_count;
254 wvupdate (start_tick, native_header.SampleRate, total_samples,
255 total_samples - samples_remaining, total_bytes_read, total_bytes_written);
257 buttons = rb->button_status ();
259 if (last_buttons == BUTTON_NONE && buttons != BUTTON_NONE) {
260 rb->splash(HZ*2, "operation aborted!");
261 error = true;
262 break;
264 else
265 last_buttons = buttons;
268 rb->close (out_fd);
269 rb->close (in_fd);
271 if (error) {
272 rb->remove (outfile);
274 else
275 rb->splash(HZ*3, "operation successful");
277 return error;
280 enum plugin_status plugin_start(const void *parameter)
282 if (!parameter)
283 return PLUGIN_ERROR;
285 audiobuf = rb->plugin_get_audio_buffer((size_t *)&audiobuflen);
287 if (audiobuflen < 0x200000) {
288 rb->splash(HZ*2, "not enough memory!");
289 return PLUGIN_ERROR;
292 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
293 rb->cpu_boost(true);
294 #endif
296 wav2wv (parameter);
298 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
299 rb->cpu_boost(false);
300 #endif
301 /* Return PLUGIN_USB_CONNECTED to force a file-tree refresh */
302 return PLUGIN_USB_CONNECTED;