void PrepareExtAudioFile() { extAudioFile = ExtAudioFile.OpenUrl(url); CheckValue(extAudioFile, "ExtAudioFile.OpenUrl failed"); srcFormat = extAudioFile.FileDataFormat; // This is how you say,“When you convert the data, this is the format I’d like to receive.” // The client data format must be PCM. In other words, you can’t use a single ExtAudioFile to convert between two compressed formats. extAudioFile.ClientDataFormat = dstFormat; // getting total frame TotalFrames = extAudioFile.FileLengthFrames; // Allocating AudioBufferList buffer = new AudioBuffers(srcFormat.ChannelsPerFrame); for (int i = 0; i < buffer.Count; ++i) { int size = (int)(sizeof(int) * TotalFrames); buffer.SetData(i, Marshal.AllocHGlobal(size), size); } numberOfChannels = srcFormat.ChannelsPerFrame; // Reading all frame into the buffer ExtAudioFileError status; extAudioFile.Read((uint)TotalFrames, buffer, out status); if (status != ExtAudioFileError.OK) { throw new ApplicationException(); } }
void _audioUnit_RenderCallback(object sender, AudioUnitEventArgs e) { // reading buffer uint numberFrames = e.NumberFrames; numberFrames = _extAudioFile.Read(numberFrames, e.Data); // is EOF? if (numberFrames != e.NumberFrames) { // loop back to file head _extAudioFile.Seek(0); Stop(); } }
void prepareExtAudioFile() { // Opening Audio File _extAudioFile = ExtAudioFile.OpenURL(_url); // Getting file data format _srcFormat = _extAudioFile.FileDataFormat; // Setting the channel number of the output format same to the input format _dstFormat = AudioUnitUtils.AUCanonicalASBD(_sampleRate, _srcFormat.ChannelsPerFrame); // setting reading format as audio unit cannonical format _extAudioFile.ClientDataFormat = _dstFormat; // getting total frame _totalFrames = _extAudioFile.FileLengthFrames; // Aloocating AudoBufferList _buffer = new AudioBufferList((uint)_srcFormat.ChannelsPerFrame, (uint)(sizeof(uint) * _totalFrames)); _numberOfChannels = _srcFormat.ChannelsPerFrame; // Reading all frame into the buffer _extAudioFile.Read((uint)_totalFrames, _buffer); }
void prepareExtAudioFile() { // Opening Audio File _extAudioFile = ExtAudioFile.OpenUrl(_url); // Getting file data format _srcFormat = _extAudioFile.FileDataFormat; // Setting the channel number of the output format same to the input format _dstFormat = AudioStreamBasicDescription.CreateLinearPCM(channelsPerFrame: (uint)_srcFormat.ChannelsPerFrame, bitsPerChannel: 32); _dstFormat.FormatFlags |= AudioFormatFlags.IsNonInterleaved; // setting reading format as audio unit cannonical format _extAudioFile.ClientDataFormat = _dstFormat; // getting total frame _totalFrames = _extAudioFile.FileLengthFrames; // Allocating AudioBufferList _buffer = new AudioBuffers(_srcFormat.ChannelsPerFrame); for (int i = 0; i < _buffer.Count; ++i) { int size = (int)(sizeof(uint) * _totalFrames); _buffer.SetData(i, Marshal.AllocHGlobal(size), size); } _numberOfChannels = _srcFormat.ChannelsPerFrame; // Reading all frame into the buffer ExtAudioFileError status; _extAudioFile.Read((uint)_totalFrames, _buffer, out status); if (status != ExtAudioFileError.OK) { throw new ApplicationException(); } }
void PrepareExtAudioFile() { extAudioFile = ExtAudioFile.OpenUrl(url); CheckValue (extAudioFile, "ExtAudioFile.OpenUrl failed"); srcFormat = extAudioFile.FileDataFormat; // This is how you say,“When you convert the data, this is the format I’d like to receive.” // The client data format must be PCM. In other words, you can’t use a single ExtAudioFile to convert between two compressed formats. extAudioFile.ClientDataFormat = dstFormat; // getting total frame TotalFrames = extAudioFile.FileLengthFrames; // Allocating AudioBufferList buffer = new AudioBuffers(srcFormat.ChannelsPerFrame); for (int i = 0; i < buffer.Count; ++i) { int size = (int)(sizeof(int) * TotalFrames); buffer.SetData(i, Marshal.AllocHGlobal(size), size); } numberOfChannels = srcFormat.ChannelsPerFrame; // Reading all frame into the buffer ExtAudioFileError status; extAudioFile.Read((uint)TotalFrames, buffer, out status); if (status != ExtAudioFileError.OK) throw new ApplicationException(); }
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; }
public static bool GetDataFromExtAudioFile (ExtAudioFile ext, AudioStreamBasicDescription outputFormat, int maxBufferSize, byte[] dataBuffer, out int dataBufferSize, out ALFormat format, out double sampleRate) { int errorStatus = 0; int bufferSizeInFrames = 0; dataBufferSize = 0; format = ALFormat.Mono16; sampleRate = 0; /* Compute how many frames will fit into our max buffer size */ bufferSizeInFrames = maxBufferSize / outputFormat.BytesPerFrame; if (dataBuffer != null) { MutableAudioBufferList audioBufferList = new MutableAudioBufferList (1, maxBufferSize); audioBufferList.Buffers [0].DataByteSize = maxBufferSize; audioBufferList.Buffers [0].NumberChannels = outputFormat.ChannelsPerFrame; // 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 (); // Let's not use copy for right now while we test this. For very large files this // might show some stutter in the sound loading //Marshal.Copy(dataBuffer, 0, audioBufferList.Buffers[0].Data, maxBufferSize); IntPtr savedDataPtr = audioBufferList.Buffers [0].Data; audioBufferList.Buffers [0].Data = meBePointer; try { // Read the data into an AudioBufferList // errorStatus here returns back the amount of information read errorStatus = ext.Read (bufferSizeInFrames, audioBufferList); 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 = 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.Buffers[0].Data = savedDataPtr; } } return true; }
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); }
public static bool GetDataFromExtAudioFile(ExtAudioFile ext, AudioStreamBasicDescription outputFormat, int maxBufferSize, byte[] dataBuffer, out int dataBufferSize, out ALFormat format, out double sampleRate) { int errorStatus = 0; int bufferSizeInFrames = 0; dataBufferSize = 0; format = ALFormat.Mono16; sampleRate = 0; /* Compute how many frames will fit into our max buffer size */ bufferSizeInFrames = maxBufferSize / outputFormat.BytesPerFrame; if (dataBuffer != null) { MutableAudioBufferList audioBufferList = new MutableAudioBufferList(1, maxBufferSize); audioBufferList.Buffers [0].DataByteSize = maxBufferSize; audioBufferList.Buffers [0].NumberChannels = outputFormat.ChannelsPerFrame; // 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(); // Let's not use copy for right now while we test this. For very large files this // might show some stutter in the sound loading //Marshal.Copy(dataBuffer, 0, audioBufferList.Buffers[0].Data, maxBufferSize); IntPtr savedDataPtr = audioBufferList.Buffers [0].Data; audioBufferList.Buffers [0].Data = meBePointer; try { // Read the data into an AudioBufferList // errorStatus here returns back the amount of information read errorStatus = ext.Read(bufferSizeInFrames, audioBufferList); 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 = 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.Buffers[0].Data = savedDataPtr; } } return(true); }