Obtain text fields from a png file - png

Nothing seems to work so far. I got to see with pnginfo the following message:
concept_Sjet_dream6.png...
Image Width: 200 Image Length: 240
Bitdepth (Bits/Sample): 8
Channels (Samples/Pixel): 2
Pixel depth (Pixel Depth): 16
Colour Type (Photometric Interpretation): GRAYSCALE with alpha channel
Image filter: Single row per byte filter
Interlacing: No interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 11811, 11811 (pixels per meter)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 1 of 9
Comment (xTXt deflate compressed): The comment
But the remaining text strings are missing. I did also try other solutions in stackoverflow, they didn't work either. pngchunks provides this information:
Chunk: Data Length 13 (max 2147483647), Type 1380206665 [IHDR]
Critical, public, PNG 1.2 compliant, unsafe to copy
IHDR Width: 200
IHDR Height: 240
IHDR Bitdepth: 8
IHDR Colortype: 4
IHDR Compression: 0
IHDR Filter: 0
IHDR Interlace: 0
IHDR Compression algorithm is Deflate
IHDR Filter method is type zero (None, Sub, Up, Average, Paeth)
IHDR Interlacing is disabled
Chunk CRC: -277290027
Chunk: Data Length 1 (max 2147483647), Type 1111970419 [sRGB]
Ancillary, public, PNG 1.2 compliant, unsafe to copy
... Unknown chunk type
Chunk CRC: -1362223895
Chunk: Data Length 2 (max 2147483647), Type 1145523042 [bKGD]
Ancillary, public, PNG 1.2 compliant, unsafe to copy
... Unknown chunk type
Chunk CRC: -2020619073
Chunk: Data Length 9 (max 2147483647), Type 1935231088 [pHYs]
Ancillary, public, PNG 1.2 compliant, safe to copy
... Unknown chunk type
Chunk CRC: 2024095606
Chunk: Data Length 7 (max 2147483647), Type 1162692980 [tIME]
Ancillary, public, PNG 1.2 compliant, unsafe to copy
... Unknown chunk type
Chunk CRC: 292503155
Chunk: Data Length 19 (max 2147483647), Type 1951942004 [tEXt]
Ancillary, public, PNG 1.2 compliant, safe to copy
... Unknown chunk type
Chunk CRC: -528748773
Chunk: Data Length 8192 (max 2147483647), Type 1413563465 [IDAT]
Critical, public, PNG 1.2 compliant, unsafe to copy
IDAT contains image data
Chunk CRC: -309524018
Chunk: Data Length 8192 (max 2147483647), Type 1413563465 [IDAT]
Critical, public, PNG 1.2 compliant, unsafe to copy
IDAT contains image data
Chunk CRC: -1646200198
Chunk: Data Length 2301 (max 2147483647), Type 1413563465 [IDAT]
Critical, public, PNG 1.2 compliant, unsafe to copy
IDAT contains image data
Chunk CRC: -810299134
Chunk: Data Length 0 (max 2147483647), Type 1145980233 [IEND]
Critical, public, PNG 1.2 compliant, unsafe to copy
IEND contains no data
Chunk CRC: -1371381630
Sample image:
I'm very confused. Thank you anyone.
PD: apparently this happens with every image, this is not a special case but the usual case.

This is a simple Java way to extract text information from a "valid" png file. International characters may not show well on console.
import java.io.*;
import java.util.zip.InflaterInputStream;
public class PNGExtractText
{
/** PNG signature constant */
public static final long SIGNATURE = 0x89504E470D0A1A0AL;
/** PNG Chunk type constants, 4 Critical chunks */
/** Image header */
private static final int IHDR = 0x49484452; // "IHDR"
/** Image trailer */
private static final int IEND = 0x49454E44; // "IEND"
/** Palette */
/** Textual data */
private static final int tEXt = 0x74455874; // "tEXt"
/** Compressed textual data */
private static final int zTXt = 0x7A545874; // "zTXt"
/** International textual data */
private static final int iTXt = 0x69545874; // "iTXt"
/** Background color */
public static void showText(InputStream is) throws Exception
{
//Local variables for reading chunks
int data_len = 0;
int chunk_type = 0;
byte[] buf=null;
long signature = readLong(is);
if (signature != SIGNATURE)
{
System.out.println("--- NOT A PNG IMAGE ---");
return;
}
/** Read header */
/** We are expecting IHDR */
if ((readInt(is)!=13)||(readInt(is) != IHDR))
{
System.out.println("--- NOT A PNG IMAGE ---");
return;
}
buf = new byte[13+4];//13 plus 4 bytes CRC
is.read(buf,0,17);
while (true)
{
data_len = readInt(is);
chunk_type = readInt(is);
//System.out.println("chunk type: 0x"+Integer.toHexString(chunk_type));
if (chunk_type == IEND)
{
System.out.println("IEND found");
int crc = readInt(is);
break;
}
switch (chunk_type)
{
case zTXt:
{
System.out.println("zTXt chunk:");
buf = new byte[data_len];
is.read(buf);
int keyword_len = 0;
while(buf[keyword_len]!=0) keyword_len++;
System.out.print(new String(buf,0,keyword_len,"UTF-8")+": ");
InflaterInputStream ii = new InflaterInputStream(new ByteArrayInputStream(buf,keyword_len+2, data_len-keyword_len-2));
InputStreamReader ir = new InputStreamReader(ii,"UTF-8");
BufferedReader br = new BufferedReader(ir);
String read = null;
while((read=br.readLine()) != null) {
System.out.println(read);
}
System.out.println("**********************");
is.skip(4);
break;
}
case tEXt:
{
System.out.println("tEXt chunk:");
buf = new byte[data_len];
is.read(buf);
int keyword_len = 0;
while(buf[keyword_len]!=0) keyword_len++;
System.out.print(new String(buf,0,keyword_len,"UTF-8")+": ");
System.out.println(new String(buf,keyword_len+1,data_len-keyword_len-1,"UTF-8"));
System.out.println("**********************");
is.skip(4);
break;
}
case iTXt:
{
// System.setOut(new PrintStream(new File("TextChunk.txt"),"UTF-8"));
/**
* Keyword: 1-79 bytes (character string)
* Null separator: 1 byte
* Compression flag: 1 byte
* Compression method: 1 byte
* Language tag: 0 or more bytes (character string)
* Null separator: 1 byte
* Translated keyword: 0 or more bytes
* Null separator: 1 byte
* Text: 0 or more bytes
*/
System.out.println("iTXt chunk:");
buf = new byte[data_len];
is.read(buf);
int keyword_len = 0;
int trans_keyword_len = 0;
int lang_flg_len = 0;
boolean compr = false;
while(buf[keyword_len]!=0) keyword_len++;
System.out.print(new String(buf,0,keyword_len,"UTF-8"));
if(buf[++keyword_len]==1) compr = true;
keyword_len++;//Skip the compresssion method byte.
while(buf[++keyword_len]!=0) lang_flg_len++;
//////////////////////
System.out.print("(");
if(lang_flg_len>0)
System.out.print(new String(buf,keyword_len-lang_flg_len, lang_flg_len, "UTF-8"));
while(buf[++keyword_len]!=0) trans_keyword_len++;
if(trans_keyword_len>0)
System.out.print(" "+new String(buf,keyword_len-trans_keyword_len, trans_keyword_len, "UTF-8"));
System.out.print("): ");
/////////////////////// End of key.
if(compr) //Compressed text
{
InflaterInputStream ii = new InflaterInputStream(new ByteArrayInputStream(buf,keyword_len+1, data_len-keyword_len-1));
InputStreamReader ir = new InputStreamReader(ii,"UTF-8");
BufferedReader br = new BufferedReader(ir);
String read = null;
while((read=br.readLine()) != null) {
System.out.println(read);
}
}
else //Uncompressed text
{
System.out.println(new String(buf,keyword_len+1,data_len-keyword_len-1,"UTF-8"));
}
System.out.println("**********************");
is.skip(4);
break;
}
default:
{
buf = new byte[data_len+4];
is.read(buf,0, data_len+4);
break;
}
}
}
is.close();
}
private static int readInt(InputStream is) throws Exception
{
byte[] buf = new byte[4];
is.read(buf,0,4);
return (((buf[0]&0xff)<<24)|((buf[1]&0xff)<<16)|
((buf[2]&0xff)<<8)|(buf[3]&0xff));
}
private static long readLong(InputStream is) throws Exception
{
byte[] buf = new byte[8];
is.read(buf,0,8);
return (((buf[0]&0xffL)<<56)|((buf[1]&0xffL)<<48)|
((buf[2]&0xffL)<<40)|((buf[3]&0xffL)<<32)|((buf[4]&0xffL)<<24)|
((buf[5]&0xffL)<<16)|((buf[6]&0xffL)<<8)|(buf[7]&0xffL));
}
public static void main(String args[]) throws Exception
{
FileInputStream fs = new FileInputStream(args[0]);
showText(fs);
}
}
Note:: usage: java PNGExtractText image.png
This is what I got from the test image ctzn0g04.png from official png test suite :
D:\tmp>java PNGExtractText ctzn0g04.png
tEXt chunk:
Title: PngSuite
**********************
tEXt chunk:
Author: Willem A.J. van Schaik
(willem#schaik.com)
**********************
zTXt chunk:
Copyright: Copyright Willem van Schaik, Singapore 1995-96
**********************
zTXt chunk:
Description: A compilation of a set of images created to test the
various color-types of the PNG format. Included are
black&white, color, paletted, with alpha channel, with
transparency formats. All bit-depths allowed according
to the spec are present.
**********************
zTXt chunk:
Software: Created on a NeXTstation color using "pnmtopng".
**********************
zTXt chunk:
Disclaimer: Freeware.
**********************
IEND found
Edit: Just found your link and tried with the image, got this:
D:\tmp>java PNGExtractText concept_Sjet_dream6.png
tEXt chunk:
Comment: The comment
**********************
IEND found
The above example has become part of a Java image library which can be found at https://github.com/dragon66/icafe

Related

Convert two 16 Bit Registers to 32 Bit real value flutter

I am using a Modbus flutter lib, reading 2 registers I obtain:
[22136, 4660]
This means: 0x12345678 I need a function to convert it to a 32 bit real value: 305419896, in easymodbustcp library I found:
/**
* Convert two 16 Bit Registers to 32 Bit real value
* #param registers 16 Bit Registers
* #return 32 bit real value
*/
public static float ConvertRegistersToFloat(int[] registers) throws IllegalArgumentException
{
if (registers.length != 2)
throw new IllegalArgumentException("Input Array length invalid");
int highRegister = registers[1];
int lowRegister = registers[0];
byte[] highRegisterBytes = toByteArray(highRegister);
byte[] lowRegisterBytes = toByteArray(lowRegister);
byte[] floatBytes = {
highRegisterBytes[1],
highRegisterBytes[0],
lowRegisterBytes[1],
lowRegisterBytes[0]
};
return ByteBuffer.wrap(floatBytes).getFloat();
}
Any help to do it in flutter / dart?
This can be done with bit shifts and a bitwise or. You need to shift the 2 higher bytes by 16 bits, then or it to get the value you want.
void main() {
List<int> vals = [22136, 4660];
int result = vals[1] << 16 | vals[0];
print(result);//305419896
}
You could also achieve the same result in this case by replacing the bitwise or with addition.
void main() {
List<int> vals = [22136, 4660];
int result = (vals[1] << 16) + vals[0];
print(result);//305419896
}

Xilinx Echo Server Data Variable

I want to have my Zedboard return a numeric value using the Xilinx lwIP example as a base but no matter what I do I can't figure out what stores the data received or transmitted.
I have found the void type payload but I don't know what to do with it.
Snapshot of one instance of payload and a list of lwIP files
Below is the closest function to my goal:
err_t recv_callback(void *arg, struct tcp_pcb *tpcb,
struct pbuf *p, err_t err){
/* do not read the packet if we are not in ESTABLISHED state */
if (!p) {
tcp_close(tpcb);
tcp_recv(tpcb, NULL);
return ERR_OK;
}
/* indicate that the packet has been received */
tcp_recved(tpcb, p->len);
/* echo back the payload */
/* in this case, we assume that the payload is < TCP_SND_BUF */
if (tcp_sndbuf(tpcb) > p->len) {
err = tcp_write(tpcb, p->payload, p->len, 1);
//I need to change p->paylod but IDK where it is given a value.
} else
xil_printf("no space in tcp_sndbuf\n\r");
/* free the received pbuf */
pbuf_free(p);
return ERR_OK;
}
Any guidance is appreciated.
Thanks,
Turtlemii
-I cheated and just made sure that the function has access to Global_tpcb from echo.c
-tcp_write() reads in an address and displays each char it seems.
void Print_Code()
{
/* Prepare for TRANSMISSION */
char header[] = "\rSwitch: 1 2 3 4 5 6 7 8\n\r"; //header text
char data_t[] = " \n\r\r"; //area for storing the
data
unsigned char mask = 10000000; //mask to decode switches
swc_value = XGpio_DiscreteRead(&SWCInst, 1); //Save switch values
/* Write switch values to the LEDs for visual. */
XGpio_DiscreteWrite(&LEDInst, LED_CHANNEL, swc_value);
for (int i =0; i<=7; i++) //load data_t with switch values (0/1)
{
data_t[8+2*i] = '0' + ((swc_value & mask)/mask); //convert one bit to 0/1
mask = mask >> 1;//move to next bit
}
int len_header = *(&header + 1) - header; //find the length of the
header string
int len_data = *(&data_t + 1) - data_t; //find the length of the data string
tcp_write(Global_tpcb, &header, len_header, 1); //print the header
tcp_write(Global_tpcb, &data_t, len_data, 1); //print the data
}

How to convert 2 bytes to Int16 in dart?

There is a method in bitconverter class in java which has a method called toInt16
But in dart i am unable to cast short as Int16
public static short toInt16( byte[] bytes, int index )
throws Exception {
if ( bytes.length != 8 )
throw new Exception( "The length of the byte array must be at least 8 bytes long." );
return (short) ( ( 0xff & bytes[index] ) << 8 | ( 0xff & bytes[index + 1] ) << 0 );
}
can someone help me with this conversion to dart language ?
Here is the updated dart version of answer that i followed using the ByteData class suggested by emerssso and this works for me
int toInt16(Uint8List byteArray, int index)
{
ByteBuffer buffer = byteArray.buffer;
ByteData data = new ByteData.view(buffer);
int short = data.getInt16(index, Endian.little);
return short;
}
I had to specifically set Endian.little because originally getInt16 method is set to BigEndian but my byte data was in former order
I think you are looking for one of the methods on the ByteData class available in dart:typed_data. Wrap your byte array in a ByteData via ByteData.view() and then you can arbitrarily access bytes as a specified type. You could then do i.e. byteData.getInt16(index);.
https://api.dart.dev/stable/2.7.1/dart-typed_data/ByteData-class.html

Embedded fonts in RTF

According to the rtf specs, we can embed a font in an rtf file using the \fontemb and \fontfile control words. Can someone give me a working example of that? I'd like the rtf file to use the font that's located in a separate file (i.e. .ttf file)
You should use TTEmbedFont function to create embedded font data. http://msdn.microsoft.com/en-us/library/windows/desktop/dd145145(v=vs.85).aspx
Like this.
//WRITEEMBEDPROC
unsigned long WriteEmbedProc(void *lpvWriteStream, const void *lpvBuffer, const unsigned long cbBuffer)
{
BYTE *rgByte = new BYTE[cbBuffer];
memcpy(rgByte, lpvBuffer, cbBuffer);
//stream to store your font information
std::ofstream *ofs = static_cast<std::ofstream*>(lpvWriteStream);
//convert binary data to hexadeciaml, that rtf uses
std::string byte_string = BinToHex(rgByte, cbBuffer);
//Write formated data to your file (stream)
for (int i = 0; i < byte_string.size(); ++i)
{
*ofs << byte_string[i];
if((i + 1) % 128 == 0)
{
*ofs << "\n";
}
}
delete rgByte;
return cbBuffer;
}
void EmbedFontWrap(HDC hdc)
{
ULONG ulPrivStatus = 0;
ULONG ulStatus = 0;
std::ofstream *lpvWriteStream = new std::ofstream("D:\\out.txt", std::ios::binary);
USHORT *pusCharCodeSet;
USHORT usCharCodeCount;
USHORT usLanguage;
LONG ret = TTEmbedFont(
hdc,
TTEMBED_RAW | TTEMBED_EMBEDEUDC,
CHARSET_UNICODE,
&ulPrivStatus,
&ulStatus,
WriteEmbedProc,
lpvWriteStream,
nullptr,
0,
0,
nullptr);
lpvWriteStream->close();
delete lpvWriteStream;
}
Font you want to embed should be set as current for you device context by SelectObject function.

OS X / iOS - Sample rate conversion for a buffer using AudioConverterFillComplexBuffer

I'm writing a CoreAudio backend for an audio library called XAL. Input buffers can be of various sample rates. I'm using a single audio unit for output. Idea is to convert the buffers and mix them prior to sending them to the audio unit.
Everything works as long as the input buffer has the same properties (sample rate, channel count, etc) as the output audio unit. Hence, the mixing part works.
However, I'm stuck with sample rate and channel count conversion. From what I figured out, this is easiest to do with Audio Converter Services API. I've managed to construct a converter; the idea is that the output format is the same as the output unit format, but possibly adjusted for purposes of the converter.
Audio converter is successfully constructed, but upon calling AudioConverterFillComplexBuffer(), I get output status error -50.
I'd love if I could get another set of eyeballs on this code. Problem is probably somewhere below AudioConverterNew(). Variable stream contains incoming (and outgoing) buffer data, and streamSize contains byte-size of incoming (and outgoing) buffer data.
What did I do wrong?
void CoreAudio_AudioManager::_convertStream(Buffer* buffer, unsigned char** stream, int *streamSize)
{
if (buffer->getBitsPerSample() != unitDescription.mBitsPerChannel ||
buffer->getChannels() != unitDescription.mChannelsPerFrame ||
buffer->getSamplingRate() != unitDescription.mSampleRate)
{
printf("INPUT STREAM SIZE: %d\n", *streamSize);
// describe the input format's description
AudioStreamBasicDescription inputDescription;
memset(&inputDescription, 0, sizeof(inputDescription));
inputDescription.mFormatID = kAudioFormatLinearPCM;
inputDescription.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
inputDescription.mChannelsPerFrame = buffer->getChannels();
inputDescription.mSampleRate = buffer->getSamplingRate();
inputDescription.mBitsPerChannel = buffer->getBitsPerSample();
inputDescription.mBytesPerFrame = (inputDescription.mBitsPerChannel * inputDescription.mChannelsPerFrame) / 8;
inputDescription.mFramesPerPacket = 1; //*streamSize / inputDescription.mBytesPerFrame;
inputDescription.mBytesPerPacket = inputDescription.mBytesPerFrame * inputDescription.mFramesPerPacket;
printf("INPUT : %lu bytes per packet for sample rate %g, channels %d\n", inputDescription.mBytesPerPacket, inputDescription.mSampleRate, inputDescription.mChannelsPerFrame);
// copy conversion output format's description from the
// output audio unit's description.
// then adjust framesPerPacket to match the input we'll be passing.
// framecount of our input stream is based on the input bytecount.
// output stream will have same number of frames, but different
// number of bytes.
AudioStreamBasicDescription outputDescription = unitDescription;
outputDescription.mFramesPerPacket = 1; //inputDescription.mFramesPerPacket;
outputDescription.mBytesPerPacket = outputDescription.mBytesPerFrame * outputDescription.mFramesPerPacket;
printf("OUTPUT : %lu bytes per packet for sample rate %g, channels %d\n", outputDescription.mBytesPerPacket, outputDescription.mSampleRate, outputDescription.mChannelsPerFrame);
// create an audio converter
AudioConverterRef audioConverter;
OSStatus acCreationResult = AudioConverterNew(&inputDescription, &outputDescription, &audioConverter);
printf("Created audio converter %p (status: %d)\n", audioConverter, acCreationResult);
if(!audioConverter)
{
// bail out
free(*stream);
*streamSize = 0;
*stream = (unsigned char*)malloc(0);
return;
}
// calculate number of bytes required for output of input stream.
// allocate buffer of adequate size.
UInt32 outputBytes = outputDescription.mBytesPerPacket * (*streamSize / inputDescription.mBytesPerFrame); // outputDescription.mFramesPerPacket * outputDescription.mBytesPerFrame;
unsigned char *outputBuffer = (unsigned char*)malloc(outputBytes);
memset(outputBuffer, 0, outputBytes);
printf("OUTPUT BYTES : %d\n", outputBytes);
// describe input data we'll pass into converter
AudioBuffer inputBuffer;
inputBuffer.mNumberChannels = inputDescription.mChannelsPerFrame;
inputBuffer.mDataByteSize = *streamSize;
inputBuffer.mData = *stream;
// describe output data buffers into which we can receive data.
AudioBufferList outputBufferList;
outputBufferList.mNumberBuffers = 1;
outputBufferList.mBuffers[0].mNumberChannels = outputDescription.mChannelsPerFrame;
outputBufferList.mBuffers[0].mDataByteSize = outputBytes;
outputBufferList.mBuffers[0].mData = outputBuffer;
// set output data packet size
UInt32 outputDataPacketSize = outputDescription.mBytesPerPacket;
// convert
OSStatus result = AudioConverterFillComplexBuffer(audioConverter, /* AudioConverterRef inAudioConverter */
CoreAudio_AudioManager::_converterComplexInputDataProc, /* AudioConverterComplexInputDataProc inInputDataProc */
&inputBuffer, /* void *inInputDataProcUserData */
&outputDataPacketSize, /* UInt32 *ioOutputDataPacketSize */
&outputBufferList, /* AudioBufferList *outOutputData */
NULL /* AudioStreamPacketDescription *outPacketDescription */
);
printf("Result: %d wheee\n", result);
// change "stream" to describe our output buffer.
// even if error occured, we'd rather have silence than unconverted audio.
free(*stream);
*stream = outputBuffer;
*streamSize = outputBytes;
// dispose of the audio converter
AudioConverterDispose(audioConverter);
}
}
OSStatus CoreAudio_AudioManager::_converterComplexInputDataProc(AudioConverterRef inAudioConverter,
UInt32* ioNumberDataPackets,
AudioBufferList* ioData,
AudioStreamPacketDescription** ioDataPacketDescription,
void* inUserData)
{
printf("Converter\n");
if(*ioNumberDataPackets != 1)
{
xal::log("_converterComplexInputDataProc cannot provide input data; invalid number of packets requested");
*ioNumberDataPackets = 0;
ioData->mNumberBuffers = 0;
return -50;
}
*ioNumberDataPackets = 1;
ioData->mNumberBuffers = 1;
ioData->mBuffers[0] = *(AudioBuffer*)inUserData;
*ioDataPacketDescription = NULL;
return 0;
}
Working code for Core Audio sample rate conversion and channel count conversion, using Audio Converter Services (now available as a part of the BSD-licensed XAL audio library):
void CoreAudio_AudioManager::_convertStream(Buffer* buffer, unsigned char** stream, int *streamSize)
{
if (buffer->getBitsPerSample() != unitDescription.mBitsPerChannel ||
buffer->getChannels() != unitDescription.mChannelsPerFrame ||
buffer->getSamplingRate() != unitDescription.mSampleRate)
{
// describe the input format's description
AudioStreamBasicDescription inputDescription;
memset(&inputDescription, 0, sizeof(inputDescription));
inputDescription.mFormatID = kAudioFormatLinearPCM;
inputDescription.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
inputDescription.mChannelsPerFrame = buffer->getChannels();
inputDescription.mSampleRate = buffer->getSamplingRate();
inputDescription.mBitsPerChannel = buffer->getBitsPerSample();
inputDescription.mBytesPerFrame = (inputDescription.mBitsPerChannel * inputDescription.mChannelsPerFrame) / 8;
inputDescription.mFramesPerPacket = 1; //*streamSize / inputDescription.mBytesPerFrame;
inputDescription.mBytesPerPacket = inputDescription.mBytesPerFrame * inputDescription.mFramesPerPacket;
// copy conversion output format's description from the
// output audio unit's description.
// then adjust framesPerPacket to match the input we'll be passing.
// framecount of our input stream is based on the input bytecount.
// output stream will have same number of frames, but different
// number of bytes.
AudioStreamBasicDescription outputDescription = unitDescription;
outputDescription.mFramesPerPacket = 1; //inputDescription.mFramesPerPacket;
outputDescription.mBytesPerPacket = outputDescription.mBytesPerFrame * outputDescription.mFramesPerPacket;
// create an audio converter
AudioConverterRef audioConverter;
OSStatus acCreationResult = AudioConverterNew(&inputDescription, &outputDescription, &audioConverter);
if(!audioConverter)
{
// bail out
free(*stream);
*streamSize = 0;
*stream = (unsigned char*)malloc(0);
return;
}
// calculate number of bytes required for output of input stream.
// allocate buffer of adequate size.
UInt32 outputBytes = outputDescription.mBytesPerPacket * (*streamSize / inputDescription.mBytesPerPacket); // outputDescription.mFramesPerPacket * outputDescription.mBytesPerFrame;
unsigned char *outputBuffer = (unsigned char*)malloc(outputBytes);
memset(outputBuffer, 0, outputBytes);
// describe input data we'll pass into converter
AudioBuffer inputBuffer;
inputBuffer.mNumberChannels = inputDescription.mChannelsPerFrame;
inputBuffer.mDataByteSize = *streamSize;
inputBuffer.mData = *stream;
// describe output data buffers into which we can receive data.
AudioBufferList outputBufferList;
outputBufferList.mNumberBuffers = 1;
outputBufferList.mBuffers[0].mNumberChannels = outputDescription.mChannelsPerFrame;
outputBufferList.mBuffers[0].mDataByteSize = outputBytes;
outputBufferList.mBuffers[0].mData = outputBuffer;
// set output data packet size
UInt32 outputDataPacketSize = outputBytes / outputDescription.mBytesPerPacket;
// fill class members with data that we'll pass into
// the InputDataProc
_converter_currentBuffer = &inputBuffer;
_converter_currentInputDescription = inputDescription;
// convert
OSStatus result = AudioConverterFillComplexBuffer(audioConverter, /* AudioConverterRef inAudioConverter */
CoreAudio_AudioManager::_converterComplexInputDataProc, /* AudioConverterComplexInputDataProc inInputDataProc */
this, /* void *inInputDataProcUserData */
&outputDataPacketSize, /* UInt32 *ioOutputDataPacketSize */
&outputBufferList, /* AudioBufferList *outOutputData */
NULL /* AudioStreamPacketDescription *outPacketDescription */
);
// change "stream" to describe our output buffer.
// even if error occured, we'd rather have silence than unconverted audio.
free(*stream);
*stream = outputBuffer;
*streamSize = outputBytes;
// dispose of the audio converter
AudioConverterDispose(audioConverter);
}
}
OSStatus CoreAudio_AudioManager::_converterComplexInputDataProc(AudioConverterRef inAudioConverter,
UInt32* ioNumberDataPackets,
AudioBufferList* ioData,
AudioStreamPacketDescription** ioDataPacketDescription,
void* inUserData)
{
if(ioDataPacketDescription)
{
xal::log("_converterComplexInputDataProc cannot provide input data; it doesn't know how to provide packet descriptions");
*ioDataPacketDescription = NULL;
*ioNumberDataPackets = 0;
ioData->mNumberBuffers = 0;
return 501;
}
CoreAudio_AudioManager *self = (CoreAudio_AudioManager*)inUserData;
ioData->mNumberBuffers = 1;
ioData->mBuffers[0] = *(self->_converter_currentBuffer);
*ioNumberDataPackets = ioData->mBuffers[0].mDataByteSize / self->_converter_currentInputDescription.mBytesPerPacket;
return 0;
}
In the header, as part of the CoreAudio_AudioManager class, here are relevant instance variables:
AudioStreamBasicDescription unitDescription;
AudioBuffer *_converter_currentBuffer;
AudioStreamBasicDescription _converter_currentInputDescription;
A few months later, I'm looking at this and I've realized that I didn't document the changes.
If you are interested in what the changes were:
look at the callback function CoreAudio_AudioManager::_converterComplexInputDataProc
one has to properly specify the number of output packets into ioNumberDataPackets
this has required introduction of new instance variables to hold both the buffer (the previous inUserData) and the input description (used to calculate the number of packets to be fed into Core Audio's converter)
this calculation of "output" packets (those fed into the converter) is done based on amount of data that our callback received, and the number of bytes per packet that the input format contains
Hopefully this edit will help a future reader (myself included)!