16 bit Ptr values

BlitzMax Forums/BlitzMax Programming/16 bit Ptr values

RexRhino(Posted 2005) [#1]
I am trying to pass a series of 16 bit values (SHORT) to what appears to be an 8 bit pointer (BYTE). I am getting the Ptr from TAudioSample.

Here is my code:
Local sample:TAudioSample = CreateAudioSample(44100, 44100, SF_STEREO16LE)
Local x:Int 
Local l_ch:Int
Local r_ch:Int
Local freq:Float = (440 * 2 * Pi) / 44100

For x = 0 To 22100
	l_ch = (x * 4)
	r_ch = (x * 4) + 2
	sample.samples[l_ch] = Short( (Sin(freq * x) * 32767) + 32767 )
	sample.samples[r_ch] = Short( (Sin(freq * x) * 32767) + 32767 )
	Print sample.samples[l_ch]
Next

Local sound:TSound = LoadSound(sample)
PlaySound sound
Delay 2000


From printing sample.samples[l_ch], and more importantly the audio that I am getting when listening to the sample, it is just putting a byte value in the Ptr array (quiet or loud, depending on if I specify Little Endian, or Big Endian).

How do I get a proper 16 bit value into memory?


Floyd(Posted 2005) [#2]
A look at the definition of TAudioSample shows that samples is a byte pointer.
So samples[something] refers to a byte.

You need a short pointer to the same memory. Here's how to cast a Byte Ptr to a Short Ptr.
Local n

Local bp:Byte Ptr = Varptr(n)

bp[0] = 256 + 1
Print n

Local sp:Short Ptr = Short Ptr bp

sp[0] = 256 + 1
Print n



ImaginaryHuman(Posted 2005) [#3]
Also, just for clarity, a pointer is pretty much always a 32-bit value (4 bytes) regardless of whether it points to a byte, a short, an int, a long, whatever. A short ptr is the same length as a byte pointer. It just points TO DATA that is of different types and sizes.