File: raw2wav.in

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 (389 lines) | stat: -rwxr-xr-x 12,078 bytes parent folder | download | duplicates (3)
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
#!@csh@ -f
# ----------------------------------------------------------------- #
#             The Speech Signal Processing Toolkit (SPTK)           #
#             developed by SPTK Working Group                       #
#             http://sp-tk.sourceforge.net/                         #
# ----------------------------------------------------------------- #
#                                                                   #
#  Copyright (c) 1984-2007  Tokyo Institute of Technology           #
#                           Interdisciplinary Graduate School of    #
#                           Science and Engineering                 #
#                                                                   #
#                1996-2015  Nagoya Institute of Technology          #
#                           Department of Computer Science          #
#                                                                   #
# All rights reserved.                                              #
#                                                                   #
# Redistribution and use in source and binary forms, with or        #
# without modification, are permitted provided that the following   #
# conditions are met:                                               #
#                                                                   #
# - Redistributions of source code must retain the above copyright  #
#   notice, this list of conditions and the following disclaimer.   #
# - Redistributions in binary form must reproduce the above         #
#   copyright notice, this list of conditions and the following     #
#   disclaimer in the documentation and/or other materials provided #
#   with the distribution.                                          #
# - Neither the name of the SPTK working group nor the names of its #
#   contributors may be used to endorse or promote products derived #
#   from this software without specific prior written permission.   #
#                                                                   #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND            #
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,       #
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF          #
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS #
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          #
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED   #
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     #
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON #
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY    #
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           #
# POSSIBILITY OF SUCH DAMAGE.                                       #
# ----------------------------------------------------------------- #
#                                                                   #
#                                      Jan. 2005  K. Tokuda         #
#                                      Aug. 2009  A. Saito          #

onintr clean

set path    = ( @bindir@ $path )
set sptkver = '@package_version@'
set cvsid   = '$Id: raw2wav.in,v 1.19 2015/12/14 05:45:40 uratec Exp $'


set cmnd	= $0
set cmnd	= $cmnd:t

set file
@ flagfile    = 0

set directory
@ flagdirectory = 0


@ swab		= 0
@ normalization	= 0
@ normalization_all = 0
@ frequency   = 16
set type1	= s
set type2	= $type1
set input = 0
set bit = 16

set _argv
set white
set whitespace = 0
set index

@ i = 0
while ($i < $#argv)
    @ i++
    set whitespace = 0
    if ( "$argv[$i]" == '-n') then
        set _argv = '-n'
    else
        set _argv = `echo $argv[$i] | sed -e 's/ /_/g'`
        if ( "$_argv" != "$argv[$i]" ) then
            @ whitespace = 1
        endif
    endif
	switch ($_argv)
	case -swab:
		set swab = 1
		breaksw
	case -s:
		@ i++
		set frequency = $argv[$i]
        set cond = `echo "$frequency > 192" | bc`
        if ( $cond == 1 ) then
            echo2 "$cmnd : sampling frequency exceeds 192 kHz!"
        endif
        set cond = `echo "$frequency > 2147483647" | bc`
        if ( $cond == 1 ) then
            echo2 "$cmnd : sampling frequency is too large!"
            exit 1
        endif
		breaksw
    case +c:
        if ($input) then
            set type2 = c
            set bit = 8
        else
            set type1 = c
            set input = 1
        endif
        breaksw
    case +s:
        if ($input) then
            set type2 = s
            set bit = 16
        else
            set type1 = s
            set input = 1
        endif
        breaksw
    case +i3:
        if ($input) then
            set type2 = i3
            set bit = 24
        else
            set type1 = i3
            set input = 1
        endif
        breaksw
    case +i:
        if ($input) then
            set type2 = i
            set bit = 32
        else
            set type1 = i
            set input = 1
        endif
        breaksw
    case +l:
        if ($input) then
            set type2 = l
            set bit = 32
        else
            set type1 = l
            set input = 1
        endif
        breaksw
    case +le:
        if ($input) then
            set type2 = le
            set bit = 64
        else
            set type1 = le
            set input = 1
        endif
        breaksw
    case +C:
        if ($input) then
            set type2 = C
            set bit = 8
        else
            set type1 = C
            set input = 1
        endif
        breaksw
    case +S:
        if ($input) then
            set type2 = S
            set bit = 16
        else
            set type1 = S
            set input = 1
        endif
        breaksw
    case +I3
        if ($input) then
            set type2 = I3
            set bit = 24
        else
            set type1 = I3
            set input = 1
        endif
        breaksw
    case +I:
        if ($input) then
            set type2 = I
            set bit = 32
        else
            set type1 = I
            set input = 1
        endif
        breaksw
    case +L:
        if ($input) then
            set type2 = L
            set bit = 32
        else
            set type1 = L
            set input = 1
        endif
        breaksw
    case +LE:
        if ($input) then
            set type2 = LE
            set bit = 64
        else
            set type1 = LE
            set input = 1
        endif
        breaksw
    case +f:
        if ($input) then
            set type2 = f
            set bit = 32
        else
            set type1 = f
            set input = 1
        endif
        breaksw
    case +d:
        if ($input) then
            set type2 = d
            set bit = 64
        else
            set type1 = d
            set input = 1
        endif
        breaksw
    case +de:
        if ($input) then
            set type2 = de
            set bit = 96
        else
            set type1 = de
            set input = 1
        endif
        breaksw
	case -d:
		@ i++
		set directory = $argv[$i]
		set flagdirectory = 1
		if ( ! -d $argv[$i] ) then
			echo2 "${cmnd}: Can't find directory "'"'"$directory"'"'" \!"
			set exit_status = 1
			goto usage
		endif
		breaksw
	case -n:
		@ normalization = 1
		breaksw
	case -N:
		@ normalization = 1
		@ normalization_all = 1
		breaksw
	case -h:
		set exit_status = 0
		goto usage
		breaksw
	default:
		set file = ( $file $_argv )
		set white = ( $white $whitespace )
		set index = ( $index $i )
		set flagfile = 1
		if ( $whitespace == 1 ) then
			if ( ! -f "$argv[$i]" ) then
				echo2 "${cmnd}: Can't open file "'"'"$argv[$i]"'"'" \!"
				set exit_status = 1
			goto usage
			endif
		endif
		breaksw
	endsw
end

goto main

usage:
	echo2 ''
	echo2 " $cmnd - raw to wav (RIFF)"
	echo2 ''
	echo2 '  usage:'
	echo2 "       $cmnd [ options ] [ infile(s) ]"
	echo2 '  options:'
	echo2 '       -swab         : change endian                [FALSE]'
	echo2 '       -s s          : sampling frequency (kHz)     [16.0]'
	echo2 '       -d d          : destination directory        [N/A]'
	echo2 '       -n            : normalization                   '
        echo2 '                       with the maximum value'
        echo2 '                       if max >= 32767              [FALSE]'
	echo2 '       -N            : normalization                   '
        echo2 '                       with the maximum value       [FALSE]'
	echo2 '       +type1         : input data type               [s]'
	echo2 '       +type2         : output data type              [type1]'
        echo2 '                        c  (char)         C  (unsigned char) '
        echo2 '                        s  (short)        S  (unsigned short)'
        echo2 '                        i3 (int, 3byte)   I3 (unsigned int, 3byte)'
        echo2 '                        i  (int)          I  (unsigned int)'
        echo2 '                        l  (long)         L  (unsigned long)'
        echo2 '                        le (long long)    LE (unsigned long long)'
        echo2 '                        f  (float)        d  (double)'
        echo2 '                        de (long double)'
	echo2 '       -h            : print this message'
	echo2 '  infile(s):'
	echo2 '       waveform                                     [N/A]'
	echo2 '  output:'
	echo2 "       $cmnd attaches RIFF header(s) to input raw file(s)."
	echo2 '                                                         '
	echo2 '       The outfile has an extention ".wav", e.g.,'
	echo2 '          sample.m15 ---> sample.m15.wav'
	echo2 '                                                         '
	echo2 '       If the infile has an extention ".raw",'
	echo2 '       the extention is removed, e.g.,'
	echo2 '          sample.m15.raw ---> sample.m15.wav'
	echo2 '                                                         '
	echo2 '       The outfile is stored in the same directory'
	echo2 '       as the infile.'
	echo2 '       However, once a destination directory is specified,'
	echo2 '       all wav files are stored in that directory.'
	echo2 ''
        echo2 " SPTK: version $sptkver"
        echo2 " CVS Info: $cvsid"
        echo2 ''
exit $exit_status

main:

@ i = 1
while ( $i <= $#file )
   set f = $file[$i]
   if ( $white[$i] ) then
      /bin/ln -f -s "$argv[$index[$i]]" $f
   endif

   if ( $normalization ) then
      if ( $swab ) then
         swab +$type1 < $f |\
         x2x +{$type1}f > /tmp/sptk-raw2wav-$uid-$$.tmp
      else
         x2x +{$type1}f < $f > /tmp/sptk-raw2wav-$uid-$$.tmp
      endif
      set max = `minmax < /tmp/sptk-raw2wav-$uid-$$.tmp | sopr -ABS | minmax | bcut -s 1 | x2x +fa %.100f`
      if ( $normalization_all || `echo "$max < 0" | bc -l` ) then
         sopr -m 32767 -d $max < /tmp/sptk-raw2wav-$uid-$$.tmp |\
         x2x +f{$type2} > /tmp/sptk-raw2wav-$uid-$$.raw
      else
         x2x +f{$type2} < /tmp/sptk-raw2wav-$uid-$$.tmp > /tmp/sptk-raw2wav-$uid-$$.raw
      endif
   else
      if ( $swab ) then
         swab +$type1 < $f |\
         x2x +{$type1}{$type2} > /tmp/sptk-raw2wav-$uid-$$.raw
      else
         x2x +{$type1}{$type2} < $f > /tmp/sptk-raw2wav-$uid-$$.raw
      endif
   endif

   if ( $f:e == "raw" ) then
      if ( $flagdirectory ) then
         set outfile = $directory/"$argv[$index[$i]]:t:r".wav
      else
         set outfile = "$argv[$index[$i]]:r".wav
      endif
   else
      if ( $flagdirectory ) then
         set outfile = $directory/"$argv[$index[$i]]:t".wav
      else
         set outfile = "$argv[$index[$i]]".wav
      endif
   endif

   set frequency = `echo "$frequency * 1000" | bc`
   rawtowav $frequency $bit /tmp/sptk-raw2wav-$uid-$$.raw /tmp/raw2wav
   /bin/mv /tmp/raw2wav "$outfile"
   if ( $white[$i] ) then
      /bin/rm -f $f
   endif
   @ i++

   rm -fr /tmp/sptk-raw2wav-$uid-$$.raw
end

clean:
rm -fr /tmp/sptk-raw2wav-$uid-$$.raw /tmp/sptk-raw2wav-$uid-$$.tmp