What and how is the encoding of a raw (headerless) audio file?

22,599

Solution 1

It can vary—but at least for me, text2wave produces 1-channel, 16-bit, signed integer PCM. These are fairly normal—and it'll be very clear when you have them right (e.g., if you unsigned-integer by mistake, you'd get extremely distorted sound)

With play, that looks like:

play -r 16000 -b 16 -c 1 -e signed-integer /tmp/foo.raw
play -r 16000 -2 -s -c 1 /tmp/foo.raw # obsolete way for older versions of Sox

These parameters are configured in Festival somewhere, I suspect. Some of them may be hardcoded as well.

The only architecture-dependent thing you may encounter is big vs. little endian; on my little-endian machine Festival is writing little-endian; if I moved that file to a big-endian machine I'd likely need to add -L. If text2wav were run on a big-endian machine, I'm not sure if it'd write big- or little- endian data.

Solution 2

Use aplay instead of play to play a raw file, this way you can specify that it's a raw audio by the -t switch:

aplay -q -c 2 -t raw -f s16 test.raw

Solution 3

You can probably create your own RIFF header. A bit of bashing should do it.. and just cat the header to your other piece(s)...

This link shows the header layout: The Canonical WAVE file format

There is also a related link on SO: Converting RAW audio data to WAV with scripting, but the mplayer/mencoder answers have a zero marked-up count. However, it sems that SoX works.

SoX gets a mention in both the above links, and is available in Ubuntu's repo; I suppose it is in others also.

PS... I just tried using play (didn't know it existed) and discovered it is SoX! ...The SO link gives an example, copied here: sox -r 44100 -e unsigned -b 8 -c 1 <RAW_FILE> <TARGET_FILE>

If you can't get it working with sox, maybe mplayer/mencoder or the RIFF header will get it going for you.

Share:
22,599

Related videos on Youtube

Matthew
Author by

Matthew

Updated on September 18, 2022

Comments

  • Matthew
    Matthew almost 2 years

    I have done this:

    me@riverbrain:~/sgf$ echo "test" | text2wave -otype raw -F 16000 >> test.raw
    

    which produced a headerless audio file. The wonderful thing about this file is that it can be concatenated (using cat, like text) with another raw audio file.

    Of course, I've got a problem. The problem is that I can't play it yet.

    me@riverbrain:~/sgf$ play test.raw 
    
    play FAIL formats: bad input format for file `test.raw': sampling rate was not specified
    

    and also, when specifying sample rate

    me@riverbrain:~/sgf$ play -r 16000 test.raw 
    play FAIL formats: bad input format for file `test.raw': data encoding was not specified
    

    When I looked up some information 'encoding' I got the feeling that it had a lot to do with your processor architecture, but maybe I'm wrong. Anyway, I can't find any documentation about how to 'ask' the computer what the data encoding of the raw audio file is. And I also I know what the sample rate is, due to setting it myself, but that's as far as I'm able to get.

    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 12 years
      There is no standard raw format. You need to find out what parameters the application that produced it used. Unfortunately, that's not always well-documented.
  • Matthew
    Matthew over 12 years
    thank you. all of these responses were great, but this is the solution i ended up using.