Adapt the remaining plugins to put the greyscale isr on cop. Now they can be used...
[Rockbox.git] / apps / plugins / wav2wv.c
blobf3e191d30dfc770fe193b76b4981a626195f214b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 David Bryant
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #ifdef RB_PROFILE
21 #include "lib/profile_plugin.h"
22 #endif
24 #include <codecs/libwavpack/wavpack.h>
26 PLUGIN_HEADER
28 #define SAMPLES_PER_BLOCK 22050
30 static struct plugin_api* rb;
32 void *memset(void *s, int c, size_t n) {
33 return(rb->memset(s,c,n));
36 void *memcpy(void *dest, const void *src, size_t n) {
37 return(rb->memcpy(dest,src,n));
40 static char *audiobuf;
41 static ssize_t audiobuflen;
43 static struct wav_header {
44 char ckID [4]; /* RIFF chuck header */
45 int32_t ckSize;
46 char formType [4];
48 char fmt_ckID [4]; /* format chunk header */
49 int32_t fmt_ckSize;
50 ushort FormatTag, NumChannels;
51 uint32_t SampleRate, BytesPerSecond;
52 ushort BlockAlign, BitsPerSample;
54 char data_ckID [4]; /* data chunk header */
55 int32_t data_ckSize;
56 } raw_header, native_header;
58 #define WAV_HEADER_FORMAT "4L44LSSLLSS4L"
60 static void wvupdate (int32_t start_tick,
61 int32_t sample_rate,
62 uint32_t total_samples,
63 uint32_t samples_converted,
64 uint32_t bytes_read,
65 uint32_t bytes_written)
67 int32_t elapsed_ticks = *rb->current_tick - start_tick;
68 int compression = 0, progress = 0, realtime = 0;
69 char buf[32];
71 if (total_samples)
72 progress = (int)(((int64_t) samples_converted * 100 +
73 (total_samples/2)) / total_samples);
75 if (elapsed_ticks)
76 realtime = (int)(((int64_t) samples_converted * 100 * HZ /
77 sample_rate + (elapsed_ticks/2)) / elapsed_ticks);
79 if (bytes_read)
80 compression = (int)(((int64_t)(bytes_read - bytes_written) * 100 +
81 (bytes_read/2)) / bytes_read);
83 rb->snprintf(buf, 32, "elapsed time: %ld secs",
84 (long)(elapsed_ticks + (HZ/2)) / HZ);
85 rb->lcd_puts(0, 2, (unsigned char *)buf);
87 rb->snprintf(buf, 32, "progress: %d%%", progress);
88 rb->lcd_puts(0, 4, (unsigned char *)buf);
90 rb->snprintf(buf, 32, "realtime: %d%% ", realtime);
91 rb->lcd_puts(0, 6, (unsigned char *)buf);
93 rb->snprintf(buf, 32, "compression: %d%% ", compression);
94 rb->lcd_puts(0, 8, (unsigned char *)buf);
96 #ifdef HAVE_LCD_BITMAP
97 rb->lcd_update();
98 #endif
101 #define TEMP_SAMPLES 4096
103 static int32_t temp_buffer [TEMP_SAMPLES] IDATA_ATTR;
105 static int wav2wv (char *filename)
107 int in_fd, out_fd, num_chans, error = false, last_buttons;
108 unsigned int32_t total_bytes_read = 0, total_bytes_written = 0;
109 unsigned int32_t total_samples, samples_remaining;
110 int32_t *input_buffer = (int32_t *) audiobuf;
111 unsigned char *output_buffer = (unsigned char *)(audiobuf + 0x100000);
112 char *extension, save_a;
113 WavpackConfig config;
114 WavpackContext *wpc;
115 int32_t start_tick;
117 rb->lcd_clear_display();
118 rb->lcd_puts_scroll(0, 0, (unsigned char *)filename);
119 #ifdef HAVE_LCD_BITMAP
120 rb->lcd_update();
121 #endif
123 last_buttons = rb->button_status ();
124 start_tick = *rb->current_tick;
125 extension = filename + rb->strlen (filename) - 3;
127 if (rb->strcasecmp (extension, "wav")) {
128 rb->splash(HZ*2, "only for wav files!");
129 return 1;
132 in_fd = rb->open(filename, O_RDONLY);
134 if (in_fd < 0) {
135 rb->splash(HZ*2, "could not open file!");
136 return true;
139 if (rb->read (in_fd, &raw_header, sizeof (raw_header)) != sizeof (raw_header)) {
140 rb->splash(HZ*2, "could not read file!");
141 return true;
144 total_bytes_read += sizeof (raw_header);
145 rb->memcpy (&native_header, &raw_header, sizeof (raw_header));
146 little_endian_to_native (&native_header, WAV_HEADER_FORMAT);
148 if (rb->strncmp (native_header.ckID, "RIFF", 4) ||
149 rb->strncmp (native_header.fmt_ckID, "fmt ", 4) ||
150 rb->strncmp (native_header.data_ckID, "data", 4) ||
151 native_header.FormatTag != 1 || native_header.BitsPerSample != 16) {
152 rb->splash(HZ*2, "incompatible wav file!");
153 return true;
156 wpc = WavpackOpenFileOutput ();
158 rb->memset (&config, 0, sizeof (config));
159 config.bits_per_sample = 16;
160 config.bytes_per_sample = 2;
161 config.sample_rate = native_header.SampleRate;
162 num_chans = config.num_channels = native_header.NumChannels;
163 total_samples = native_header.data_ckSize / native_header.BlockAlign;
165 /* config.flags |= CONFIG_HIGH_FLAG; */
167 if (!WavpackSetConfiguration (wpc, &config, total_samples)) {
168 rb->splash(HZ*2, "internal error!");
169 rb->close (in_fd);
170 return true;
173 WavpackAddWrapper (wpc, &raw_header, sizeof (raw_header));
174 save_a = extension [1];
175 extension [1] = extension [2];
176 extension [2] = 0;
178 out_fd = rb->creat (filename);
180 extension [2] = extension [1];
181 extension [1] = save_a;
183 if (out_fd < 0) {
184 rb->splash(HZ*2, "could not create file!");
185 rb->close (in_fd);
186 return true;
189 wvupdate (start_tick, native_header.SampleRate, total_samples, 0, 0, 0);
191 for (samples_remaining = total_samples; samples_remaining;) {
192 unsigned int32_t samples_count, samples_to_pack, bytes_count;
193 int cnt, buttons;
194 int32_t value, *lp;
195 signed char *cp;
197 samples_count = SAMPLES_PER_BLOCK;
199 if (samples_count > samples_remaining)
200 samples_count = samples_remaining;
202 bytes_count = samples_count * num_chans * 2;
204 if (rb->read (in_fd, input_buffer, bytes_count) != (int32_t) bytes_count) {
205 rb->splash(HZ*2, "could not read file!");
206 error = true;
207 break;
210 total_bytes_read += bytes_count;
211 WavpackStartBlock (wpc, output_buffer, output_buffer + 0x100000);
212 samples_to_pack = samples_count;
213 cp = (signed char *) input_buffer;
215 while (samples_to_pack) {
216 unsigned int32_t samples_this_pass = TEMP_SAMPLES / num_chans;
218 if (samples_this_pass > samples_to_pack)
219 samples_this_pass = samples_to_pack;
221 lp = temp_buffer;
222 cnt = samples_this_pass;
224 if (num_chans == 2)
225 while (cnt--) {
226 value = *cp++ & 0xff;
227 value += *cp++ << 8;
228 *lp++ = value;
229 value = *cp++ & 0xff;
230 value += *cp++ << 8;
231 *lp++ = value;
233 else
234 while (cnt--) {
235 value = *cp++ & 0xff;
236 value += *cp++ << 8;
237 *lp++ = value;
240 if (!WavpackPackSamples (wpc, temp_buffer, samples_this_pass)) {
241 rb->splash(HZ*2, "internal error!");
242 error = true;
243 break;
246 samples_to_pack -= samples_this_pass;
249 if (error)
250 break;
252 bytes_count = WavpackFinishBlock (wpc);
254 if (rb->write (out_fd, output_buffer, bytes_count) != (int32_t) bytes_count) {
255 rb->splash(HZ*2, "could not write file!");
256 error = true;
257 break;
260 total_bytes_written += bytes_count;
261 samples_remaining -= samples_count;
263 wvupdate (start_tick, native_header.SampleRate, total_samples,
264 total_samples - samples_remaining, total_bytes_read, total_bytes_written);
266 buttons = rb->button_status ();
268 if (last_buttons == BUTTON_NONE && buttons != BUTTON_NONE) {
269 rb->splash(HZ*2, "operation aborted!");
270 error = true;
271 break;
273 else
274 last_buttons = buttons;
277 rb->close (out_fd);
278 rb->close (in_fd);
280 if (error) {
281 save_a = extension [1];
282 extension [1] = extension [2];
283 extension [2] = 0;
284 rb->remove (filename);
285 extension [2] = extension [1];
286 extension [1] = save_a;
288 else
289 rb->splash(HZ*3, "operation successful");
291 return error;
294 enum plugin_status plugin_start(struct plugin_api* api, void *parameter)
296 #ifdef RB_PROFILE
297 /* This doesn't start profiling or anything, it just gives the
298 * profiling functions that are compiled in someplace to call,
299 * this is needed here to let this compile with profiling support
300 * since it calls code from a codec that is compiled with profiling
301 * support */
302 profile_init(api);
303 #endif
305 rb = api;
307 if (!parameter)
308 return PLUGIN_ERROR;
310 audiobuf = rb->plugin_get_audio_buffer((size_t *)&audiobuflen);
312 if (audiobuflen < 0x200000) {
313 rb->splash(HZ*2, "not enough memory!");
314 return PLUGIN_ERROR;
317 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
318 rb->cpu_boost(true);
319 #endif
321 wav2wv (parameter);
323 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
324 rb->cpu_boost(false);
325 #endif
326 /* Return PLUGIN_USB_CONNECTED to force a file-tree refresh */
327 return PLUGIN_USB_CONNECTED;