1

I'm trying to convert 728 .raw files to 16khz .wav files by using the script given in the link here but I'm receiving an error

sox FAIL formats: can't open input file `./*.wav': No such file or directory

script I'm using is

for i in ./*.raw; do sox "$i" -r 16000 "${i%%.wav}r.wav"; done

the script is in convert.sh and the raw files are in rawfiles folder. The file structure is like

/Orginal/rawfiles/.rawfiles and /Orginal/convert.sh

I have 728 files to convert .raw files to 16 bit 16khz mono wav files. What modifications do I have to do in order to remove the error and convert the files?

Kings85
  • 345
  • 2
  • 15
Saad
  • 159
  • 1
  • 2
  • 14
  • 1
    Which folder are you running this script from? `*.wav` will look for files with `wav` extension and not `.raw` files – Inian Feb 20 '18 at 11:54
  • @Inian I'm sorry. That was a mistake. I corrected it. In is in `*.raw` – Saad Feb 22 '18 at 18:49

3 Answers3

3

The SOX code line that you are using is not at all for raw file conversion, check the SOX full documentation for more info.

Next is an explanation on: what is a raw file, followed by the correct code.

First you need to know that A raw file is a headerless file.

In order to convert raw files into wav, you need to recreate the file header, that consists of:

  • Sample rate (44100, 16000, 8000, ...) [Hz]
  • Sample size [bits]
  • data encoding (floating-point, μ-law, ADPCM, signed-integer PCM)
  • channels (usually: 1 mono or 2 stereo)

So, as mentioned in this Answer, the format is:

sox -r 16000 -e unsigned -b 8 -c 1 <RAW_FILE> <TARGET_FILE>

This is for the specific case that your raw data is originated from a 16KHz sample rate with 8 bit per sample encoded with unsigned integer, and it is one channel.

Next, for the for loop, you can use any for script that works... So the answer from: tripleee, with this modification will work:

mkdir -p outputdir
for i in rawfiles/*.raw; do
    o=outputdir/${i#rawfiles/}
    sox -r 16000 -e unsigned -b 8 -c 1 "$i" "${o%.raw}.wav"
done
Kings85
  • 345
  • 2
  • 15
1

The script is looking in the current directory. The error message looks like you simply have no files matching the wildcard in the current directory.

I guess you want

mkdir -p outputdir
for i in rawfiles/*.raw; do
    o=outputdir/${i#rawfiles/}
    sox "$i" -r 16000 "${o%.raw}.wav"
done

If you want the files in the current directory, obviously don't create or write to outputdir.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I'm getting an error `sox FAIL formats: bad input format for file`.Code inside for loop is this `o = outputdir/${i}` `sox "$i" -r 16000 "${o%.raw}.wav"` – Saad Feb 22 '18 at 18:42
  • The error seems unrelated to the code you posted; check the `sox` manual page. The spaces around the equals sign would cause a syntax error (unless you have a command named `o` which likes an equals sign as its first argument; the assignment is superfluous if you don't need to trim anything off the beginning of `$i` anyway). – tripleee Feb 23 '18 at 07:03
-1

Here is the windows equvilant that may help you. I had this same problem and could not find the solution via here or any other site. So I kicked and moaned and figured it out, you can convert it for use in linux shell "$", but here's the batch convert sox for 96hz 24bit to 48hz 16bit flac to flac. Of course, you can change this to whatever combination you want (i.e. 16b 44.1k).

Create a batch file, i.e: 24b-96hz to 16b-48hz-FLAC.bat

@break off
mkdir ".\FLACOUT\"
FOR %%F IN (".\*.flac") DO sox.exe "%%F" -S -b 16 -r 48k -C 8 ".\FLACOUT\%%~NF.flac"

It will take the *.flac files from your main directory and create a sub called FLACOUT (or whatever you rename it to), and put the newly downsampled files into that subdirectory with the same name and metadata.

Jake J
  • 109
  • 1
  • 1
  • 7