I had reason to playback some raw g.711 audio data. I made the following script to convert the data to a .au file that is playable.
From the AU file spec.. which is way simple by the way:
You just need to add 6 32-bit header flags.
raw2au.py:
import struct
header = [ 0x2e736e64, 24, 0xffffffff, 1, 8000, 1 ]
o=open('out.au','wb')
o.write ( struct.pack ( ">IIIIII", *header ) )
raw = open('in.raw','rb').read()
o.write(raw)
o.close()
You can probably see that this would be extremely simple to use with other audio formats too. Modify as you need.
Header Bytes:
- Const AU identifier: 0x2e736e64
- Size of Header: 24.
- Size of File: use 0xffffffff for unknown, or you could replace with the actual size.
- Audio format. 1 is uLaw. From the spec choose whatever you need if it isn’t uLaw.
- Sample rate. uLaw is always 8000.
- Channels. 1 channel in this case.
Good idea. I want to convert this code to Java. Can you help me.
byte [] header*= { (byte*) 0x2e736e64}; //24, 0xffffffff, 1, (byte) 8000, 1 };
FileOutputStream out = new FileOutputStream(file);
out.write(header);