byte array to short array and back again in java -


i'm having issues taking audio data stored in byte array, converting big-endian short array, encoding it, changing byte array. here have. original audio data stored in audiobytes2. using same format decode minus on cos function instead. unfortunately, changing byte , short data types non-negotiable.

    short[] audiodata = null;     int nlengthinsamples = audiobytes2.length / 2;     audiodata = new short[nlengthinsamples];      (int = 0; < nlengthinsamples; i++) {        short msb = (short) audiobytes2[2*i+1];        short lsb = (short) audiobytes2[2*i];        audiodata[i] = (short) (msb << 8 | (255 & lsb));     }      int = 0;     while (i < audiodata.length) {         audiodata[i] = (short)(audiodata[i] + (short)5*math.cos(2*math.pi*i/(((number)encodebox.getvalue()).intvalue())));         i++;     }      short x = 0;     = 0;     while (i < audiodata.length) {         x = audiodata[i];         audiobytes2[2*i+1] = (byte)(x >>> 0);         audiobytes2[2*i] = (byte)(x >>> 8);         i++;     } 

i have done can think of make work, closest i've come getting work every other encode/decode , have no idea why. help.

i suggest try bytebuffer.

byte[] bytes = {}; short[] shorts = new short[bytes.length/2]; // turn bytes shorts either big endian or little endian.  bytebuffer.wrap(bytes).order(byteorder.little_endian).asshortbuffer().get(shorts);  // turn shorts bytes. byte[] bytes2 = new byte[shortsa.length * 2]; bytebuffer.wrap(bytes2).order(byteorder.little_endian).asshortbuffer().put(shortsa); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -