I'm a little confused by what I did. As far as I know, java and especially DataOutputStream write values ββto Big endian.
I am writing a small signal generator and should store files at the small end. While there is no problem, just change the bytes.
writeShort() also says "write high byte first."
So, for example, the decimal word "2" will be stored as:
00 02 (big endian) 02 00 (little endian) is what I need.
So I change the bytes:
public static short swap (short value) { int b1 = value & 0xff; int b2 = (value >> 8) & 0xff; return (short) (b1 << 8 | b2 << 0); }
and write a short one:
dos.writeShort(swap(x[t]));
The hex editor shows me the file in the format in which it should be: 02 00
When I try to open the created audio file, I donβt hear anything. (import of crude information and settings, such as samplerate all correct).
I removed the byte replacement, getting the file with: 00 02 , which is a great endian.
I re-opened my courage and with the same configuration, I heard a tone. Definitely, I set up a little endian!
I copied the file to windows (I work on a Mac) and opened the file in Cool Edit 2000, again selecting 16khz, 16 bit unsigned pcm and a bit endian (16-bit LSB, MSB). I heard the tone again, not choosing anything good.
Where is my failure? Something bothers me, because it should not work, as I described.
Tone Creation:
// x(t) = A*cos (2*pi * f * t + phi) // if(null != dos) { double sampPeriod = 1.0/16000; short x[] = new short[16000]; // 16k samples for 1 second for(int t=0; t < x.length; t++) { double time = t * sampPeriod; x[t] = (short) (amplitude * Math.sin(2.0*Math.PI*frequenz*time+phase)); } for(int t=0; t < x.length; t++) { try { dos.writeShort(x[t]); } catch (IOException e) { e.printStackTrace(); } } }
Edited 14:16 04/02/13: I read around, assuming that something in my understanding is a failure, and I found the following image on wikipedia:

See the answer to the problem in the next article.