Velocity Reviews > Java > bitwise problems&dpcm

# bitwise problems&dpcm

theo
Guest
Posts: n/a

 05-16-2005
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[i]=sndpcm1[i];
}

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[i]=snd[i]-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[i]=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[i]=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[i]=(byte)(snd[i-1]+diafores[i]);
}

Wibble
Guest
Posts: n/a

 05-16-2005

+
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[i]=sndpcm1[i];
> }
>
> 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[i]=snd[i]-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[i]=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[i]=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[i]=(byte)(snd[i-1]+diafores[i]);
> }

christopher@dailycrossword.com
Guest
Posts: n/a

 05-17-2005
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?