summaryrefslogtreecommitdiffstats
path: root/debian/pilot-link/pilot-link-0.12.5-dfsg/src/pilot-wav.c
blob: d149c41227ba87411466944f15453bb2cdd3369e (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * $Id: pilot-wav.c,v 1.19 2006/10/12 14:21:21 desrod Exp $ 
 *
 * pilot-wav.c: Palm Voice Memo Wav Fetcher/Converter
 *
 * This is a palm conduit to fetch Memo Wav files from a Palm.  It can also
 * convert *.wav.pdb files that have already been fetched from the Palm.
 *
 * Copyright (C) 2003 by David Turner
 * Based on pilot-foto Copyright (C) 2003 by Judd Montgomery
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pi-file.h"
#include "pi-source.h"
#include "pi-userland.h"

/* Declare prototypes */
long write_header(FILE *out);
long write_data(char *buffer, int index, int size, long dataChunkSize, FILE *out);
int pdb_to_wav(char *filename);
int fetch_wavs(int sd, char *dbname);
int do_fetch(char *dbname);

#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif


/***********************************************************************
 *
 * Function:    write_header
 *
 * Summary:     Writes out header of wave file with correct parameters
 *
 * Parameters:  File out - output file pointer
 *
 * Returns:     long - size of format chunk
 *
 ***********************************************************************/
long write_header(FILE * out)
{
        const char formatChunkID[4] = "fmt ";
        long formatChunkSize;

        short wFormatTag;
        unsigned short wChannels;
        unsigned long dwSamplesPerSec;
        unsigned long dwAvgBytesPerSec;
        unsigned short wBlockAlign;
        unsigned short wBitsPerSample;
        unsigned short cbSize;
        unsigned short wSamplesPerBlock;

        const char dataChunkID[4] = "data";
        long dataChunkSize;

        long wWaveLength;

	/* In bytes, not including formatChunkID and formatChunkSize itself */
        formatChunkSize = 20;

	/* 17 = Intel IMA (DVI) ADPCM */
        wFormatTag = 17;

	/* Mono */
        wChannels = 1;
        dwSamplesPerSec = 8000;

        /* This value varies with file - need formula */
        dwAvgBytesPerSec = 4425;
        wBlockAlign = 256;
        wBitsPerSample = 4;

	/* Extended format block size including(it appears) cbSize itself */
        cbSize = 2;
        wSamplesPerBlock = 505;

	/* fill in at end of file with seek */
        dataChunkSize = 0;

        /* fill in at end of file with seek */
        wWaveLength = 0;

        /* RIFF Header */
        fwrite("RIFF", 4, 1, out);
        fwrite(&wWaveLength, 4, 1, out);
        fwrite("WAVE", 4, 1, out);

        /* Format Chunk */
        fwrite(formatChunkID, 4, 1, out);

	/* Length of Format Chunk - 4 (fmt ) - 4 (length value itself) */
        fwrite(&formatChunkSize, 4, 1, out);
        fwrite(&wFormatTag, 2, 1, out);
        fwrite(&wChannels, 2, 1, out);
        fwrite(&dwSamplesPerSec, 4, 1, out);
        fwrite(&dwAvgBytesPerSec, 4, 1, out);
        fwrite(&wBlockAlign, 2, 1, out);
        fwrite(&wBitsPerSample, 2, 1, out);

        /* Extended Format Chunk Fields */

	/* Extended format block size including(it appears) cbSize itself */
        fwrite(&cbSize, 2, 1, out);
        fwrite(&wSamplesPerBlock, 2, 1, out);

        /* Data Chunk */
        fwrite(dataChunkID, 4, 1, out);
        fwrite(&dataChunkSize, 4, 1, out);

        return formatChunkSize;
}


/***********************************************************************
 *
 * Function:    write_data
 *
 * Summary:     Writes out data from record buffer updating dataChunkSize
 *
 * Parameters:  char pointer buffer - self explanatory
 *              int size - size of data to be written
 *              FILE *out - pointer to output file
 *              long dataChunkSize - present data chunk size
 *
 * Returns:     long - updated dataChunkSize
 *
 ***********************************************************************/
long write_data(char *buffer, int index, int size, long dataChunkSize, FILE *out)
{
	if (index == 0) {
		fwrite(buffer + 122, size - 122, 1, out);
		dataChunkSize += size - 122;
	} else {
		fwrite(buffer + 8, size - 8, 1, out);
		dataChunkSize += size - 8;
	}
	return dataChunkSize;
}

/***********************************************************************
 *
 * Function:    fetch_wavs
 *
 * Summary:     Grab the voice memos matching 'Vpad' on the device
 *
 * Parameters:  None
 *
 * Returns:     0
 *
 ***********************************************************************/
int fetch_wavs(int sd, char *dbname)
{
	FILE *out;
	recordid_t id_;
	int 	index,
		db,
		attr,
		category,
		ret,
		start;

	struct DBInfo info;
	char creator[5];
	char type[5];
	pi_buffer_t *buffer;

        int booldb = TRUE;

        long wWaveLength;
        long formatChunkSize;
        long dataChunkSize;

        start = 0;

	buffer = pi_buffer_new (65536);

	while (dlp_ReadDBList(sd, 0, dlpOpenRead, start, buffer) > 0) {
		memcpy(&info, buffer->data, sizeof(struct DBInfo));
		start = info.index + 1;
		creator[0] = (info.creator & 0xFF000000) >> 24;
		creator[1] = (info.creator & 0x00FF0000) >> 16;
		creator[2] = (info.creator & 0x0000FF00) >> 8;
		creator[3] = (info.creator & 0x000000FF);
		creator[4] = '\0';
		type[0] = (info.type & 0xFF000000) >> 24;
		type[1] = (info.type & 0x00FF0000) >> 16;
		type[2] = (info.type & 0x0000FF00) >> 8;
		type[3] = (info.type & 0x000000FF);
		type[4] = '\0';

		if (!strcmp(dbname, "all")) {
			booldb = FALSE;
		} else {
			booldb = strcmp(info.name, dbname);
		}

                if (!(strcmp(creator, "Vpad") || strcmp(type, "strm") || booldb)) {
			memset (buffer->data, 0, buffer->allocated);
			if (!plu_quiet) {
				printf("Fetching '%s' (Creator ID '%s')... ",
					info.name, creator);
			}
                        ret =
                            dlp_OpenDB(sd, 0, dlpOpenRead, info.name, &db);
                        if (ret < 0) {
                                fprintf(stderr, "   WARNING: Unable to open %s\n",
                                        info.name);
                                continue;
                        }

                        out = fopen(info.name, "w");
                        if (!out) {
                                fprintf(stderr,
                                        "   WARNING: Failed, unable to create file %s\n",
                                        info.name);
				dlp_CloseDB(sd,db);
                                continue;
                        }

                        formatChunkSize = write_header(out);

                        index = 0;
                        dataChunkSize = 0;
                        ret = 1;
                        while (ret > 0) {
								ret =
                                    dlp_ReadRecordByIndex
                                    PI_ARGS((sd, db, index, buffer, &id_,
                                             &attr, &category));
				if (ret > 0) {
                                  dataChunkSize = write_data(buffer->data, index, buffer->used, dataChunkSize, out);
				}
				index++;
                        }
			wWaveLength = 4 + 4 + 4 + formatChunkSize + 4 + 4 + dataChunkSize;
                        fseek(out, 44, SEEK_SET);
                        fwrite(&dataChunkSize, 4, 1, out);
                        fseek(out, 4, SEEK_SET);
			fwrite(&wWaveLength, 4, 1, out);
                        dlp_CloseDB(sd, db);
                        fclose(out);
			if (!plu_quiet) {
                        	printf("OK\n");
			}
                }
        }
		pi_buffer_free(buffer);
        return 0;
}


/***********************************************************************
 *
 * Function:    do_fetch
 *
 * Summary:     Connect and fetch the file
 *
 * Parameters:  None
 *
 * Returns:     0
 *
 ***********************************************************************/
int do_fetch(char *dbname)
{
        int     sd              = -1,
		ret;

        sd = plu_connect();

	ret = fetch_wavs(sd, dbname);

	dlp_EndOfSync(sd, dlpEndCodeNormal);
	pi_close(sd);

	return 0;
}


/***********************************************************************
 *
 * Function:    pdb_to_wav
 *
 * Summary:     Do the bulk of the conversion here
 *
 * Parameters:  None
 *
 * Returns:     0 - Success, -1 - Failure
 *
 ***********************************************************************/
int pdb_to_wav(char *filename)
{
        struct pi_file *pi_fp;
        struct DBInfo info;
        FILE *out;
        int 	index,
		ret,
		attr,
		cat;
        void *buffer;
        recordid_t uid;

	size_t	size;

	long wWaveLength;
        long formatChunkSize;
	long dataChunkSize;

	if (!plu_quiet) {
        	printf("Converting %s... ", filename);
	}
        pi_fp = pi_file_open(filename);
        if (!pi_fp) {
                fprintf(stderr,"   FAILED: could not open %s\n", filename);
                return -1;
        }
        pi_file_get_info(pi_fp, &info);

        out = fopen(info.name, "w");
        if (!out) {
		pi_file_close(pi_fp);
                fprintf(stderr,"   FAILED: could not open %s to write\n", info.name);
                return -1;
        }

        formatChunkSize = write_header(out);

        index = 0;
        dataChunkSize = 0;
        ret = 1;
        while (ret >= 0) {
                ret =
                    pi_file_read_record(pi_fp, index, &buffer, &size, &attr,
                                        &cat, &uid);
		if (ret >= 0) {
                  dataChunkSize = write_data(buffer, index, size, dataChunkSize, out);
		}
		index++;
        }
	wWaveLength = 4 + 4 + 4 + formatChunkSize + 4 + 4 + dataChunkSize;
        fseek(out, 44, SEEK_SET);
        fwrite(&dataChunkSize, 4, 1, out);
        fseek(out, 4, SEEK_SET);
	fwrite(&wWaveLength, 4, 1, out);
        fclose(out);
        pi_file_close(pi_fp);
	if (!plu_quiet) {
        	printf("OK, wrote %ld bytes to %s\n", dataChunkSize, info.name);
	}
        return 0;
}




int main(int argc, const char *argv[])
{
        int 	c;

        char
	    	*fetch		= NULL,
	    	*convert	= NULL;

	poptContext pc;

	struct poptOption options[] = {
		USERLAND_RESERVED_OPTIONS
		{"fetch", 'f', POPT_ARG_STRING, &fetch, 0, "Fetch all wav files or specified wav file from the Palm", "[file|all]"},
		{"convert", 'c', POPT_ARG_STRING, &convert, 0, "Convert <file>.wav.pdb file to wav", "file"},
		POPT_TABLEEND
	};

	pc = poptGetContext("pilot-wav", argc, argv, options, 0);
	poptSetOtherOptionHelp(pc,"\n\n"
	"   Decodes Palm Voice Memo files to wav files you can read\n\n"
	"   Example arguments: \n"
	"      pilot-wav -p /dev/pilot -f MyVoiceMemo.wav.pdb\n"
	"      pilot-wav -c MyVoiceMemo.wav.pdb\n\n");

	if (argc < 2 ) {
		poptPrintUsage(pc,stderr,0);
		return 1;
	}


	while ((c = poptGetNextOpt(pc)) >= 0) {
		fprintf(stderr,"   ERROR: Unhandled option %d.\n",c);
		return 1;
        }

	if (c < -1) {
		plu_badoption(pc,c);
	}

	if(convert != NULL)
		pdb_to_wav(convert);

	if(fetch != NULL)
		do_fetch(fetch);

        return 0;
}

/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
/* ex: set tabstop=4 expandtab: */
/* Local Variables: */
/* indent-tabs-mode: t */
/* c-basic-offset: 8 */
/* End: */