public static bool GetDataFromExtAudioFile (ExtAudioFile ext, AudioStreamBasicDescription outputFormat, int maxBufferSize, byte[] dataBuffer, out int dataBufferSize, out ALFormat format, out double sampleRate) { uint errorStatus = 0; uint bufferSizeInFrames = 0; dataBufferSize = 0; format = ALFormat.Mono16; sampleRate = 0; /* Compute how many frames will fit into our max buffer size */ bufferSizeInFrames = (uint)(maxBufferSize / outputFormat.BytesPerFrame); if (dataBuffer != null) { var audioBufferList = new AudioBuffers(maxBufferSize); // This a hack so if there is a problem speak to kjpou1 -Kenneth // the cleanest way is to copy the buffer to the pointer already allocated // but what we are going to do is replace the pointer with our own and restore it later // GCHandle meBePinned = GCHandle.Alloc (dataBuffer, GCHandleType.Pinned); IntPtr meBePointer = meBePinned.AddrOfPinnedObject (); audioBufferList.SetData (0, meBePointer); try { // Read the data into an AudioBufferList // errorStatus here returns back the amount of information read ExtAudioFileError extAudioFileError = ExtAudioFileError.OK; errorStatus = ext.Read (bufferSizeInFrames, audioBufferList, out extAudioFileError); if (errorStatus >= 0) { /* Success */ /* Note: 0 == bufferSizeInFrames is a legitimate value meaning we are EOF. */ /* ExtAudioFile.Read returns the number of frames actually read. * Need to convert back to bytes. */ dataBufferSize = (int)bufferSizeInFrames * outputFormat.BytesPerFrame; // Now we set our format format = outputFormat.ChannelsPerFrame > 1 ? ALFormat.Stereo16 : ALFormat.Mono16; sampleRate = outputFormat.SampleRate; } else { #if DEBUG Console.WriteLine ("ExtAudioFile.Read failed, Error = " + errorStatus); #endif return false; } } catch (Exception exc) { #if DEBUG Console.WriteLine ("ExtAudioFile.Read failed: " + exc.Message); #endif return false; } finally { // Don't forget to free our dataBuffer memory pointer that was pinned above meBePinned.Free (); // and restore what was allocated to beginwith audioBufferList.SetData (0, IntPtr.Zero); } } return true; }
static AudioConverterError HandleInputData (ref int numberDataPackets, AudioBuffers data, ref AudioStreamPacketDescription[] dataPacketDescription) { int maxPackets = afio.SrcBufferSize / afio.SrcSizePerPacket; if (numberDataPackets > maxPackets) numberDataPackets = maxPackets; // read from the file int outNumBytes; var res = afio.SourceFile.ReadPackets (false, out outNumBytes, afio.PacketDescriptions, afio.SrcFilePos, ref numberDataPackets, afio.SrcBuffer); if (res != 0) { throw new ApplicationException (res.ToString ()); } // advance input file packet position afio.SrcFilePos += numberDataPackets; // put the data pointer into the buffer list data.SetData (0, afio.SrcBuffer, outNumBytes); // don't forget the packet descriptions if required if (dataPacketDescription != null) { if (afio.PacketDescriptions != null) { dataPacketDescription = afio.PacketDescriptions; } else { dataPacketDescription = null; } } return AudioConverterError.None; }