File: raw2wav

package info (click to toggle)
sptk 3.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 8,376 kB
  • sloc: ansic: 38,342; sh: 4,834; makefile: 1,403
file content (308 lines) | stat: -rwxr-xr-x 6,030 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

cmnd=$0;
SPTK_PATH=/usr/lib/sptk/bin
PATH="$SPTK_PATH":"$PATH"

set -e;

usage()
{
    cat<<EOF


 $cmnd - raw to wav (RIFF)

  usage:
       $cmnd [ options ] [ infile(s) ]
  options:
       -swab         : change endian                [FALSE]
       -s s          : sampling frequency (kHz)     [16.0]
       -d d          : destination directory        [N/A]
       -n            : normalization with the maximum value
                       if max >= 32767              [FALSE]
       -N            : normalization with the maximum value       [FALSE]
       +<type1>      : input data type               [s]
       +<type2>      : output data type              [type1]
                       c  (char)         C  (unsigned char)
                       s  (short)        S  (unsigned short)
                       i3 (int, 3byte)   I3 (unsigned int, 3byte)
                       i  (int)          I  (unsigned int)
                       l  (long)         L  (unsigned long)
                       le (long long)    LE (unsigned long long)
                       f  (float)        d  (double)
                       de (long double)
       -h            : print this message
  infile(s):
       waveform                                     [N/A]
  output:
       $cmnd attaches RIFF header(s) to input raw file(s).

       The outfile has an extention ".wav", e.g.,
          sample.m15 ---> sample.m15.wav

       If the infile has an extention ".raw",
       the extention is removed, e.g.,
          sample.m15.raw ---> sample.m15.wav

       The outfile is stored in the same directory
       as the infile.'
       However, once a destination directory is specified,
       all wav files are stored in that directory.

 Replacement of the SPTK raw2wav command

EOF
}

directory=
flagdirectory=0
swab=0
normalization=0
normalization_all=0
frequency=16
type1=s
type2=$type1
input=0
bit=16


OPTIND=0
OPT=""
for i in "$@"
do
    OPTIND=`expr $OPTIND + 1`
    if [ "$OPT" = "" ]
    then
	OPTARG=""
	OPT="$i"
	case "$OPT" in
	    -s|-d|+type1|+type2)
		continue;
		;;
	    -swab|-n|-N|-h|+c|+s|+i3|+i|+l|+le|+C|+S|+I3|+I|+L|+LE|+f|+d|+de)
		;;
            *)
		OPT=""
		break;
	esac
    else
	OPTARG="$i"
    fi
    case "$OPT" in
	-s)
	    frequency="$OPTARG"
	    ;;
	-d)
	    directory="$OPTARG"
	    flagdirectory=1
	    ;;
	-swab)
	    swab=1
	    ;;
	-n)
	    normalization=1
	    ;;
	-N)
	    normalization=1
	    normalization_all=1
	    ;;
        -h)
            usage >&2;
            exit 0;
            ;;
	+c)
            if [ $input -ne 0 ]; then
		type2=c
		bit=8
            else
		type1=c
		input=1
            fi
            ;;
	+s)
            if [ $input -ne 0 ]; then
		type2=s
		bit=16
            else
		type1=s
		input=1
            fi
            ;;
	+i3)
            if [ $input -ne 0 ]; then
		type2=i3
		bit=24
            else
		type1=i3
		input=1
            fi
            ;;
	+i)
            if [ $input -ne 0 ]; then
		type2=i
		bit=32
            else
		type1=i
		input=1
            fi
            ;;
	+l)
            if [ $input -ne 0 ]; then
		type2=l
		bit=32
            else
		type1=l
		input=1
            fi
            ;;
	+le)
            if [ $input -ne 0 ]; then
		type2=le
		bit=64
            else
		type1=le
		input=1
            fi
            ;;
	+C)
            if [ $input -ne 0 ]; then
		type2=C
		bit=8
            else
		type1=C
		input=1
            fi
            ;;
	+S)
            if [ $input -ne 0 ]; then
		type2=S
		bit=16
            else
		type1=S
		input=1
            fi
            ;;
	+I3)
            if [ $input -ne 0 ]; then
		type2=I3
		bit=24
            else
		type1=I3
		input=1
            fi
            ;;
	+I)
            if [ $input -ne 0 ]; then
		type2=I
		bit=32
            else
		type1=I
		input=1
            fi
            ;;
	+L)
            if [ $input -ne 0 ]; then
		type2=L
		bit=32
            else
		type1=L
		input=1
            fi
            ;;
	+LE)
            if [ $input -ne 0 ]; then
		type2=LE
		bit=64
            else
		type1=LE
		input=1
            fi
            ;;
	+f)
            if [ $input -ne 0 ]; then
		type2=f
		bit=32
            else
		type1=f
		input=1
            fi
            ;;
	+d)
            if [ $input -ne 0 ]; then
		type2=d
		bit=64
            else
		type1=d
		input=1
            fi
            ;;
	+de)
            if [ $input -ne 0 ]; then
		type2=de
		bit=96
            else
		type1=de
		input=1
            fi
	    ;;
    esac
    OPT=""
done
if [ "$OPT" != "" ]
then
    usage >&2;
    exit 1;
fi

if [ $OPTIND -gt 0 ]; then
    shift $(($OPTIND - 1))
fi

if [ $frequency -gt 192 ]; then
    echo "$cmnd : sampling frequency exceeds 192 kHz!" >&2;
    if [ $frequency -gt 2147483647 ]; then
	echo "$cmnd : sampling frequency is too large!" >&2;
	exit 1
    fi
fi
if [ $input -eq 0 ]; then
    type2=$type1
fi

TMPFILE=`mktemp -t tmpraw2wav.XXXXXX`
trap "rm -f '$TMPFILE'.raw '$TMPFILE'" EXIT

for f in "$@"
do
    if [ $normalization -ne 0 ]; then
             if [ $swab -ne 0 ]; then
		 swab +"$type1" < "$f" | x2x +"$type1"f > "$TMPFILE"
	     else
		 x2x +"$type1"f < "$f" > "$TMPFILE"
	     fi
	     max=`minmax < "$TMPFILE" | sopr -ABS | minmax | bcut -s 1 | x2x +fa %.100f`
	     negativemax=`echo "$max < 0" | bc`
	     if [ $normalization_all -ne 0 ] || [ "$negativemax" -ne 0 ]; then
		sopr -m 32767 -d "$max" < "$TMPFILE" |\
		    x2x +f"$type2" > "$TMPFILE".raw
	     else
		    x2x +f"$type2" < "$TMPFILE" > "$TMPFILE".raw
	     fi
	 else
             if [ $swab -ne 0 ]; then
		 swab +"$type1" < "$f" | x2x +"$type1""$type2" > "$TMPFILE".raw
	     else
		 x2x +"$type1""$type2" < "$f" > "$TMPFILE".raw
	     fi
	 fi
         outfile=`echo "$f" | sed -e 's/.raw$\|$/.wav/'`
	 if [ $flagdirectory -ne 0 ]; then
	     outfile=`basename "$outfile"`
             outfile="$directory"/"$outfile"
	 fi
	 mkdir -p `dirname "$outfile"`
	 frequency=`echo "$frequency * 1000" | bc`
	 rawtowav $frequency $bit "$TMPFILE".raw "$TMPFILE"
	 mv "$TMPFILE" "$outfile"
done