1

i have AudioQueueBuffer's Audio Data which has void * const format. i want short array (short *) Audio data for my codec.

How to Convert void * const to Short * in Objective c??? type cast from void to short possible ? or i have to use some byte order conversion like OSReadBigInt16??? if so sample for conversion? please help me to find solution.

Vasu Ashok
  • 1,413
  • 3
  • 17
  • 37

2 Answers2

1

Yes, Objective-C is just like C for typecasting. That means if you want to interpret your void * pointer as a short * pointer, all you need to do is:

(short *)voidPointer

If the bytes aren't in the right order (ie, endianness mismatch), you'll need to deal with that too - there's no way to tell if that's the case based on the information in your question, though.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • thanks for fast reply . i need above conversion for IOS device. and one more thing i have typecast unsigned char from void *(which is 8 bit) ,it works fine. if i type cast to short(16 bit) is possible??? – Vasu Ashok Jan 13 '12 at 04:05
  • You can't dereference a `void *` at all, so who cares what 'size' the data it points to is? It's totally up to you and the specifics of your program if it's going to work or not. What is the data pointed to by the `void *` pointer? Is it 16-bit binary data? If so, a simple typecast is fine. If not, you have more work to do. – Carl Norum Jan 13 '12 at 04:06
  • Check the documentation for the API that gave you the pointer. – Carl Norum Jan 13 '12 at 04:20
  • sorry for the stupid question ? how to find void *'s pointer? whether 16 or 8 bit? presently i have AudioQueueBuffer struct ,how i can i get short from this ? i have referred AudioQueueBuffer but they doesn't specify abt void pointer ? – Vasu Ashok Jan 13 '12 at 04:25
  • http://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html – Carl Norum Jan 13 '12 at 04:29
  • Check out that link. It looks like you probably want to be using `AudioQueueGetProperty`. – Carl Norum Jan 13 '12 at 04:31
  • I'd love to +10 because you brought up the endianness issues. – Evan Jan 13 '12 at 06:26
1

void* is a convention for declaring untyped pointers. Such pointers need to be re-interpreted as pointers to some data type. Whether or not it works depends on what you or the API put at the address pointed to by the void* pointer: if it is a sequence of short values with no gaps, a simple cast to short* is going to work; otherwise, you may need to perform additional conversion.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523