4

I have an 8-Bit-WAV-file which I would like to convert into a plain TXT-file. It's important that the resulting TXT-file only contains numbers between 0 and 255, representing the value of every single sample.

Of course something like -128 to 128 would also be fine, since it should be no big deal to convert/normalize everything as needed.

I've tried exporting into different headerless RAW-formats using Audacity, but the available encodings are just not what I'm looking for.

Even though this question might be a little strange (I need that file for some kind of art project) I would appreciate any help!

lsgng
  • 465
  • 8
  • 22

1 Answers1

4

First a sample file:

sox -n -r 8k -b 8 test.wav synth 0.02 sine 400 fade t 0 0 0.02 norm

This creates a 0.02 second long 400Hz sine tone that fades linearly from beginning to end. It is stored as an 8-bit precision, 8kHz sample rate, single channel WAV file.

As for converting this into a string of sample values in the range [0, 255] you're on the right track with a raw headerless format, which can be produced with sox like this:

sox -V test.wav -e unsigned test.raw

But if you open the resulting .raw file in a text editor it just looks like bunch of nonsense. To fix this we can send if through the hexdump utility to translate the data into a more readable form:

hexdump -v -e '1/1 "%03u "' test.raw > test.txt

As far as I can tell (I only learned about hexdump yesterday, so don't take my words as gospel): -v ensures that all output is written explicitly (otherwise repetitions are represented by asterisk), -e lets you create your own format string (whats inside the single quotes): 1/1 orders the data in 'bunches' of one, one byte per bunch, "%03u " makes sure each 'bunch' is represented as a zero-padded, three digit, unsigned decimal number. A single white space is also added to separate each 'bunch'.

Alternatively, the first command can be piped directly into the second one like this:

sox -V test.wav -t raw -e unsigned - | hexdump -v -e '1/1 "%03u "' > test.txt

With the contents of test.txt looking something like this:

head -c 64 test.txt
# 132 169 205 233 251 255 250 230 203 167 128 090 057 030 014 009

enter image description here

To remove the zero padding just remove '03' from the hexdumpcommand.

I should add that hexdump is a BSD program, and as such is pretty ubiquitous on *NIX systems, but I have no idea what its equivalent would be on Windows.

AkselA
  • 8,153
  • 2
  • 21
  • 34
  • Thanks, I think that's exactly what I need! – lsgng Jul 07 '16 at 08:10
  • 1
    I was looking to do the same but my `.wav` file has 32-bit signed integer samples, and I wanted one value per line (not space-separated as in yours), so I used `'/4 %i\r\n'` as my `hexdump` format string. – cp.engr Jun 26 '18 at 16:18