summaryrefslogtreecommitdiff
path: root/apps/plugins/xxx2wav.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/xxx2wav.h')
-rw-r--r--apps/plugins/xxx2wav.h240
1 files changed, 240 insertions, 0 deletions
diff --git a/apps/plugins/xxx2wav.h b/apps/plugins/xxx2wav.h
new file mode 100644
index 0000000000..96942824fd
--- /dev/null
+++ b/apps/plugins/xxx2wav.h
@@ -0,0 +1,240 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20/* Various "helper functions" common to all the xxx2wav decoder plugins
21
22 We include them as a .h because the plugin build system links
23 exactly one .c file with exactly one .rock file.
24 */
25
26
27/* the main data structure of the program */
28typedef struct {
29 int infile;
30 int outfile;
31 off_t curpos;
32 off_t filesize;
33 int samplerate;
34 int channels;
35 int frames_decoded;
36 unsigned long total_samples;
37 unsigned long current_sample;
38 unsigned long start_tick;
39} file_info_struct;
40
41file_info_struct file_info;
42
43#define MALLOC_BUFSIZE (512*1024)
44
45int mem_ptr;
46int bufsize;
47unsigned char* mp3buf; // The actual MP3 buffer from Rockbox
48unsigned char* mallocbuf; // 512K from the start of MP3 buffer
49unsigned char* filebuf; // The rest of the MP3 buffer
50
51void* malloc(size_t size) {
52 void* x;
53 char s[32];
54
55 x=&mallocbuf[mem_ptr];
56 mem_ptr+=size+(size%4); // Keep memory 32-bit aligned (if it was already?)
57
58 rb->snprintf(s,30,"Memory used: %d\r",mem_ptr);
59 rb->lcd_putsxy(0,80,s);
60 rb->lcd_update();
61 return(x);
62}
63
64void* calloc(size_t nmemb, size_t size) {
65 void* x;
66 x=malloc(nmemb*size);
67 rb->memset(x,0,nmemb*size);
68 return(x);
69}
70
71void free(void* ptr) {
72 (void)ptr;
73}
74
75void* realloc(void* ptr, size_t size) {
76 void* x;
77 (void)ptr;
78 x=malloc(size);
79 return(x);
80}
81
82void *memcpy(void *dest, const void *src, size_t n) {
83 return(rb->memcpy(dest,src,n));
84}
85
86void *memset(void *s, int c, size_t n) {
87 return(rb->memset(s,c,n));
88}
89
90int memcmp(const void *s1, const void *s2, size_t n) {
91 return(rb->memcmp(s1,s2,n));
92}
93
94void* memmove(const void *s1, const void *s2, size_t n) {
95 char* dest=(char*)s1;
96 char* src=(char*)s2;
97 size_t i;
98
99 for (i=0;i<n;i++) { dest[i]=src[i]; }
100 // while(n>0) { *(dest++)=*(src++); n--; }
101 return(dest);
102}
103
104void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) {
105 rb->qsort(base,nmemb,size,compar);
106}
107
108void display_status(file_info_struct* file_info) {
109 char s[32];
110 unsigned long ticks_taken;
111 unsigned long long speed;
112 unsigned long xspeed;
113
114 rb->snprintf(s,32,"Bytes read: %d\r",file_info->curpos);
115 rb->lcd_putsxy(0,0,s);
116 rb->snprintf(s,32,"Samples Decoded: %d\r",file_info->current_sample);
117 rb->lcd_putsxy(0,20,s);
118 rb->snprintf(s,32,"Frames Decoded: %d\r",file_info->frames_decoded);
119 rb->lcd_putsxy(0,40,s);
120
121 ticks_taken=*(rb->current_tick)-file_info->start_tick;
122
123 /* e.g.:
124 ticks_taken=500
125 sam_fmt.rate=44,100
126 samples_decoded=172,400
127 (samples_decoded/sam_fmt.rate)*100=400 (time it should have taken)
128 % Speed=(400/500)*100=80%
129 */
130
131 if (ticks_taken==0) { ticks_taken=1; } // Avoid fp exception.
132
133 speed=(100*file_info->current_sample)/file_info->samplerate;
134 xspeed=(speed*10000)/ticks_taken;
135 rb->snprintf(s,32,"Speed %ld.%02ld %% Secs: %d",(xspeed/100),(xspeed%100),ticks_taken/100);
136 rb->lcd_putsxy(0,60,s);
137
138 rb->lcd_update();
139}
140
141static unsigned char wav_header[44]={'R','I','F','F', // 0 - ChunkID
142 0,0,0,0, // 4 - ChunkSize (filesize-8)
143 'W','A','V','E', // 8 - Format
144 'f','m','t',' ', // 12 - SubChunkID
145 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
146 1,0, // 20 - AudioFormat (1=16-bit)
147 2,0, // 22 - NumChannels
148 0,0,0,0, // 24 - SampleRate in Hz
149 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
150 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
151 16,0, // 34 - BitsPerSample
152 'd','a','t','a', // 36 - Subchunk2ID
153 0,0,0,0 // 40 - Subchunk2Size
154 };
155
156
157int local_init(char* file, file_info_struct* file_info) {
158 char s[32];
159 int i,n,bytesleft;
160
161 mem_ptr=0;
162 mp3buf=rb->plugin_get_mp3_buffer(&bufsize);
163 mallocbuf=mp3buf;
164 filebuf=&mp3buf[MALLOC_BUFSIZE];
165
166 rb->snprintf(s,32,"mp3 bufsize: %d\r",bufsize);
167 rb->lcd_putsxy(0,100,s);
168 rb->lcd_update();
169
170 file_info->infile=rb->open(file,O_RDONLY);
171 file_info->outfile=rb->creat("/ac3test.wav",O_WRONLY);
172 rb->write(file_info->outfile,wav_header,sizeof(wav_header));
173 file_info->curpos=0;
174 file_info->frames_decoded=0;
175 file_info->filesize=rb->filesize(file_info->infile);
176
177 if (file_info->filesize > (bufsize-MALLOC_BUFSIZE)) {
178 rb->close(file_info->infile);
179 rb->splash(HZ*2, true, "File too large");
180 return(1);
181 }
182
183 rb->snprintf(s,32,"Loading file...");
184 rb->lcd_putsxy(0,0,s);
185 rb->lcd_update();
186
187 bytesleft=file_info->filesize;
188 i=0;
189 while (bytesleft > 0) {
190 n=rb->read(file_info->infile,&filebuf[i],bytesleft);
191 if (n < 0) {
192 rb->close(file_info->infile);
193 rb->splash(HZ*2, true, "ERROR READING FILE");
194 return(1);
195 }
196 i+=n; bytesleft-=n;
197 }
198 rb->close(file_info->infile);
199 return(0);
200}
201
202void close_wav(file_info_struct* file_info) {
203 int x;
204 int filesize=rb->filesize(file_info->outfile);
205
206 /* We assume 16-bit, Stereo */
207
208 rb->lseek(file_info->outfile,0,SEEK_SET);
209
210 // ChunkSize
211 x=filesize-8;
212 wav_header[4]=(x&0xff);
213 wav_header[5]=(x&0xff00)>>8;
214 wav_header[6]=(x&0xff0000)>>16;
215 wav_header[7]=(x&0xff000000)>>24;
216
217 // Samplerate
218 wav_header[24]=file_info->samplerate&0xff;
219 wav_header[25]=(file_info->samplerate&0xff00)>>8;
220 wav_header[26]=(file_info->samplerate&0xff0000)>>16;
221 wav_header[27]=(file_info->samplerate&0xff000000)>>24;
222
223 // ByteRate
224 x=file_info->samplerate*4;
225 wav_header[28]=(x&0xff);
226 wav_header[29]=(x&0xff00)>>8;
227 wav_header[30]=(x&0xff0000)>>16;
228 wav_header[31]=(x&0xff000000)>>24;
229
230 // Subchunk2Size
231 x=filesize-44;
232 wav_header[40]=(x&0xff);
233 wav_header[41]=(x&0xff00)>>8;
234 wav_header[42]=(x&0xff0000)>>16;
235 wav_header[43]=(x&0xff000000)>>24;
236
237 rb->write(file_info->outfile,wav_header,sizeof(wav_header));
238 rb->close(file_info->outfile);
239}
240