| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2003 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: December 2003 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Convert binary to ascii files (for tracks) */ |
| 38 |
/*************************************************************************/ |
| 39 |
#include <stdio.h> |
| 40 |
#include "cst_wave.h" |
| 41 |
#include "cst_tokenstream.h" |
| 42 |
#include "cst_args.h" |
| 43 |
|
| 44 |
int main(int argc, char **argv) |
| 45 |
{ |
| 46 |
cst_val *files; |
| 47 |
const cst_val *f; |
| 48 |
cst_features *args; |
| 49 |
int i,n; |
| 50 |
const char *datatype; |
| 51 |
FILE *fd; |
| 52 |
int channels; |
| 53 |
int d_int; |
| 54 |
float d_flt; |
| 55 |
double d_dbl; |
| 56 |
int swap = FALSE; |
| 57 |
|
| 58 |
args = new_features(); |
| 59 |
files = |
| 60 |
cst_args(argv,argc, |
| 61 |
"usage: bin2ascii OPTIONS\n" |
| 62 |
"Convert a binary file to ascii\n" |
| 63 |
"-type <string> int, float or double\n" |
| 64 |
"-swap Swap data\n" |
| 65 |
"-c <int> Number of channels\n", |
| 66 |
args); |
| 67 |
|
| 68 |
channels = get_param_int(args,"-c",12); |
| 69 |
if (feat_present(args,"-swap")) |
| 70 |
swap = TRUE; |
| 71 |
datatype = get_param_string(args,"-type","int"); |
| 72 |
|
| 73 |
for (f=files; f; f=val_cdr(f)) |
| 74 |
{ |
| 75 |
fd = fopen(val_string(val_car(f)),"rb"); |
| 76 |
n = 0; |
| 77 |
while (! feof(fd)) |
| 78 |
{ |
| 79 |
for (i=0; i<channels; i++) |
| 80 |
{ |
| 81 |
if (cst_streq(datatype,"int")) |
| 82 |
{ |
| 83 |
n = fread(&d_int,4,1,fd); |
| 84 |
if (n != 1) break; |
| 85 |
if (swap) d_int = SWAPINT(d_int); |
| 86 |
printf("%d ",d_int); |
| 87 |
} |
| 88 |
else if (cst_streq(datatype,"float")) |
| 89 |
{ |
| 90 |
n = fread(&d_flt,4,1,fd); |
| 91 |
if (n != 1) break; |
| 92 |
if (swap) swapfloat(&d_flt); |
| 93 |
printf("%f ",d_flt); |
| 94 |
} |
| 95 |
else if (cst_streq(datatype,"double")) |
| 96 |
{ |
| 97 |
n = fread(&d_dbl,8,1,fd); |
| 98 |
if (n != 1) break; |
| 99 |
if (swap) swapdouble(&d_dbl); |
| 100 |
printf("%g ",d_dbl); |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
fprintf(stderr,"bin2ascii: unknown data type \"%s\"",datatype); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
} |
| 108 |
if (n==0 && i == 0) |
| 109 |
break; |
| 110 |
printf("\n"); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |