WriteShort in small Endian: hex shows large, but opening only works in le

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:

read access

See the answer to the problem in the next article.

+4
source share
1 answer

Ok guys and everyone who is interested following this question:

The error has nothing to do with the small or large endian. Everything that I understood from LE and BE was correct. (But don't assume at the end about access to reading, which tricked me when I was looking for any explanations.)

The error was related to the signal amplitude.

If the amplitude was too low, the volume itself (which generates in amplitude) was too low. Therefore, nothing was heard. Interpreting the written file as a large endian as a small endian, each short sample in the file is larger (returns to the comment zch: 00 02 - 512, when interpreted as a small endian).

This created a higher sampling value at the time and made me hear something.

Having established, as a rule, a higher amplitude and the correct replacement of bytes with a small end, I could again hear something. So the main failure was not with regard to endianess.

The zch comment made me think again and again, the result was a little C ++ code for windows reading values, and seeing that the native interpretation of LE is the same as zch. This led me to the amplitude point, and now I have found the problem. I hope that no one else will make this stupid failure or find this post and will not know in the future :-)

+2
source

All Articles