# bitwise problems&dpcm

Discussion in 'Java' started by theo, May 16, 2005.

1. ### theoGuest

Hello people,
the problem is as follows :

i am given an array of bytes called sndpcm1[] , in every nibble of each
byte there is a signed integer in the range -8 to +7 . From this array i
am supposed to make another array of bytes in which the real values are
stored.The real values are taken by adding the value of the previous
element with the value of the corresponding nibble(dpcm).So the new
array of bytes which i call snd[] will be double the length of
sndpcm1[].The values in snd[] are samples of sound which are then played
on my pc.The most significant nibble of every byte has priority over the
least significant nibble.i tried this dpcm->pcm conversion with the
following code and the question is whether this piece of code does the
conversion right,cause instead of music i hear only noise.

byte[] sndpcm2 = new byte[sndpcm1.length];

for (int i=0;i<=sndpcm2.length-1;i++)
{
sndpcm2=sndpcm1;
}

byte[] snd = new byte[2*sndpcm1.length]; //the values of the
//waveform

diafores = new byte[2*sndpcm1.length];//array that contains in
//every byte the difference among two bytes of snd[] :
//diafores=snd-snd[i-1]

for (int i=0,j=0 ; (i<=(diafores.length-2)) ; i+=2,j++ ) //get
//the most significant nibble out if a byte

{
sndpcm1[j]>>>=4;
sndpcm1[j]<<=4;

if (sndpcm1[j]<0)
{
sndpcm1[j]<<=1;
sndpcm1[j]>>>=5;
sndpcm1[j]=(byte)(sndpcm1[j]|0x80);
}
else
{
sndpcm1[j]>>>=4;
}

diafores=sndpcm1[j];
}

for (int
i=1,j=0;(i<=(2*sndpcm2.length-1))&&(j<=(sndpcm2.length-1));i+=2,j++)
//least significant nibble

{
sndpcm2[j]<<=4;

if (sndpcm2[j]<0)
{
sndpcm2[j]<<=1;
sndpcm2[j]>>>=5;
sndpcm2[j]=(byte)(sndpcm2[j]|0x80);
}

else
{
sndpcm2[j]>>>=4;
}

diafores=sndpcm2[j];
}

System.out.println("diafores size is : "+diafores.length );
System.out.println("sound size is : "+snd.length );

snd[0]=diafores[0];

for (int i=1;i<=snd.length-1;i++)
{
snd=(byte)(snd[i-1]+diafores);
}

theo, May 16, 2005

2. ### WibbleGuest

Interesting, your code is as mixed up as your functional spec.

+
theo wrote:
> Hello people,
> the problem is as follows :
>
> i am given an array of bytes called sndpcm1[] , in every nibble of each
> byte there is a signed integer in the range -8 to +7 . From this array i
> am supposed to make another array of bytes in which the real values are
> stored.The real values are taken by adding the value of the previous
> element with the value of the corresponding nibble(dpcm).So the new
> array of bytes which i call snd[] will be double the length of
> sndpcm1[].The values in snd[] are samples of sound which are then played
> on my pc.The most significant nibble of every byte has priority over the
> least significant nibble.i tried this dpcm->pcm conversion with the
> following code and the question is whether this piece of code does the
> conversion right,cause instead of music i hear only noise.
>
>
> byte[] sndpcm2 = new byte[sndpcm1.length];
>
> for (int i=0;i<=sndpcm2.length-1;i++)
> {
> sndpcm2=sndpcm1;
> }
>
> byte[] snd = new byte[2*sndpcm1.length]; //the values of the
> //waveform
>
> diafores = new byte[2*sndpcm1.length];//array that contains in
> //every byte the difference among two bytes of snd[] :
> //diafores=snd-snd[i-1]
>
> for (int i=0,j=0 ; (i<=(diafores.length-2)) ; i+=2,j++ ) //get
> //the most significant nibble out if a byte
>
> {
> sndpcm1[j]>>>=4;
> sndpcm1[j]<<=4;
>
> if (sndpcm1[j]<0)
> {
> sndpcm1[j]<<=1;
> sndpcm1[j]>>>=5;
> sndpcm1[j]=(byte)(sndpcm1[j]|0x80);
> }
> else
> {
> sndpcm1[j]>>>=4;
> }
>
> diafores=sndpcm1[j];
> }
>
> for (int
> i=1,j=0;(i<=(2*sndpcm2.length-1))&&(j<=(sndpcm2.length-1));i+=2,j++)
> //least significant nibble
>
> {
> sndpcm2[j]<<=4;
>
> if (sndpcm2[j]<0)
> {
> sndpcm2[j]<<=1;
> sndpcm2[j]>>>=5;
> sndpcm2[j]=(byte)(sndpcm2[j]|0x80);
> }
>
> else
> {
> sndpcm2[j]>>>=4;
> }
>
> diafores=sndpcm2[j];
> }
>
> System.out.println("diafores size is : "+diafores.length );
> System.out.println("sound size is : "+snd.length );
>
> snd[0]=diafores[0];
>
> for (int i=1;i<=snd.length-1;i++)
> {
> snd=(byte)(snd[i-1]+diafores);
> }

Wibble, May 16, 2005

3. ### Guest

I dream in code, too, but mine usually has a purpose, and I avoid
anything stronger than hops and alcohol . . .

"instead of music i hear only noise"

1) Why do you think you should hear music?
2) How do you know it is just noise?
3) How is a "sample of sound" played on a PC anyway?

, May 17, 2005

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.