/// <summary>
        /// Updates file header to reflect updated file information.
        /// </summary>
        /// <param name="sndfile">Audio file to update file header of.</param>
        public void UpdateHeaderNow(IntPtr sndfile)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.UpdateHeaderNow, IntPtr.Zero, 0);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.UpdateHeaderNow, retval))
            {
                throw new LibsndfileException("Unable to update header for the given file.");
            }
        }
        /// <summary>
        /// Set the GUID of a the <paramref name="sndfile"/> WAVEX file to indicate an Ambisonic format.
        /// </summary>
        /// <param name="sndfile">Audio file to set ambisonic format of.</param>
        /// <param name="mode">Ambisonic format to use.</param>
        /// <returns>Success of setting ambisonic format for the given file.</returns>
        public bool SetAmbisonic(IntPtr sndfile, LibsndfileMode mode)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.WavexSetAmbisonic, IntPtr.Zero, (int)mode);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.WavexSetAmbisonic, retval))
            {
                throw new LibsndfileException("Unable to set ambisonic format for the given file.");
            }

            return((LibsndfileMode)retval == mode);
        }
        /// <summary>
        /// Retrieves floating point to int conversion clipping state for the given <paramref name="sndfile"/> file.
        /// </summary>
        /// <param name="sndfile">Audio file to get clipping state of.</param>
        /// <returns>Current clipping state.</returns>
        public bool GetClipping(IntPtr sndfile)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.GetClipping, IntPtr.Zero, 0);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.GetClipping, retval))
            {
                throw new LibsndfileException("Unable to get clipping for the given file.");
            }

            return(Convert.ToBoolean(retval));
        }
        /// <summary>
        /// Sets file header auto update for <paramref name="sndfile"/> after every subsequent write call.
        /// </summary>
        /// <param name="sndfile">Audio file to enable automatic file header updating.</param>
        /// <param name="enable">Flag to enable or file header auto-update.</param>
        /// <returns>Current file header auto-update state.</returns>
        public bool SetUpdateHeaderAuto(IntPtr sndfile, bool enable)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.SetUpdateHeaderAuto, IntPtr.Zero, Convert.ToInt32(enable));

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.SetUpdateHeaderAuto, retval))
            {
                throw new LibsndfileException("Unable to set header auto-update for the given file.");
            }

            return(Convert.ToBoolean(retval));
        }
        /// <summary>
        /// Sets PEAK chunk in <paramref name="sndfile"/> file which contains floating point data.
        /// </summary>
        /// <param name="sndfile">Audio file to set PEAK chunk.</param>
        /// <param name="enable">Flag to enable or disable PEAK chunk.</param>
        /// <returns>True/False on whether the PEAK chunk will be written on the next write call.</returns>
        /// <remarks>
        /// This call must be made before any data is written to the file.
        /// </remarks>
        public bool SetAddPeakChunk(IntPtr sndfile, bool enable)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.SetAddPeakChunk, IntPtr.Zero, Convert.ToInt32(enable));

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.SetAddPeakChunk, retval))
            {
                throw new LibsndfileException("Unable to set PEAK chunk for the given file.");
            }

            return(Convert.ToBoolean(retval));
        }
        /// <summary>
        /// Determine if RAW data read from the given <paramref name="sndfile"/> file needs to be endian swapped.
        /// </summary>
        /// <param name="sndfile">Audio file to check for endian swapping.</param>
        /// <returns>True if bytes should be endian swapped.</returns>
        public bool RawNeedsEndianSwap(IntPtr sndfile)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.RawDataNeedsEndswap, IntPtr.Zero, 0);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.RawDataNeedsEndswap, retval))
            {
                throw new LibsndfileException("Unable to determine necessity of endian swap for the given file.");
            }

            return(Convert.ToBoolean(retval));
        }
 /// <summary>
 /// Set the Variable Bit Rate encoding quality for the given <paramref name="sndfile"/> file.
 /// </summary>
 /// <param name="sndfile">Audio file to set vbr quality for.</param>
 /// <param name="value">Vbr encoding quality.</param>
 /// <remarks>
 /// The command must be sent before any audio data is written to the file.
 /// </remarks>
 public void SetVbrEncodingQuality(IntPtr sndfile, double value)
 {
     using (var memory = m_Marshaller.Allocate(value))
     {
         var retval = m_Api.Command(sndfile, LibsndfileCommand.SetVbrEncodingQuality, memory, memory.Size);
         if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.SetVbrEncodingQuality, retval))
         {
             throw new LibsndfileException("Unable to set vbr encoding quality for the given file.");
         }
     }
 }
        /// <summary>
        /// Sets the scale factor for when integer data is written from the <paramref name="sndfile"/>
        /// as floating point data.
        /// </summary>
        /// <param name="sndfile">Audio file to set float to int scaling.</param>
        /// <param name="enable">Flag to enable or disable float to int scaling.</param>
        /// <returns>Previous scaling state.</returns>
        public bool SetScaleIntFloatWrite(IntPtr sndfile, bool enable)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.SetScaleIntFloatWrite, IntPtr.Zero, Convert.ToInt32(enable));

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.SetScaleIntFloatWrite, retval))
            {
                throw new LibsndfileException("Unable to set float to int scaling for the given file.");
            }

            return(Convert.ToBoolean(retval));
        }
        /// <summary>
        /// Gets double normalization for read and write functions on the <paramref name="sndfile"/> file.
        /// </summary>
        /// <param name="sndfile">Audio file to retrieve double normalization for.</param>
        /// <returns>Current double normalization state.</returns>
        public bool GetNormDouble(IntPtr sndfile)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.GetNormDouble, IntPtr.Zero, 0);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.GetNormDouble, retval))
            {
                throw new LibsndfileException("Unable to retrieve double normalization for the given file.");
            }

            return(Convert.ToBoolean(retval));
        }
        /// <summary>
        /// Sets float normalization for read and write functions on the <paramref name="sndfile"/> file.
        /// </summary>
        /// <param name="sndfile">Audio file to set float normalization on.</param>
        /// <param name="normalize">Flag to enable or disable float normalization.</param>
        /// <returns>Previous float normalization state.</returns>
        public bool SetNormFloat(IntPtr sndfile, bool normalize)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.SetNormFloat, IntPtr.Zero, Convert.ToInt32(normalize));

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.SetNormFloat, retval))
            {
                throw new LibsndfileException("Unable to set float normalization for the given file.");
            }

            return(Convert.ToBoolean(retval));
        }
        /// <summary>
        /// Test if the <paramref name="sndfile"/> has the GUID of a WAVEX file
        /// for any of the ambisonic formats.
        /// </summary>
        /// <param name="sndfile">Audio file to examine.</param>
        /// <returns>Returns true or false based on whether the file is ambisonic format.</returns>
        public bool GetAmbisonic(IntPtr sndfile)
        {
            var retval = m_Api.Command(sndfile, LibsndfileCommand.WavexGetAmbisonic, IntPtr.Zero, 0);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.WavexGetAmbisonic, retval))
            {
                throw new LibsndfileException("Unable to retrieve ambisonic format for the given file.");
            }

            return((LibsndfileMode)retval == LibsndfileMode.AmbisonicBFormat);
        }
 /// <summary>
 /// Change the data start offset for RAW files.
 /// </summary>
 /// <param name="sndfile">Audio file to change start offset for.</param>
 /// <param name="offset">Number of bytes offset from the beginning of the file.</param>
 public void SetRawStartOffset(IntPtr sndfile, long offset)
 {
     using (var memory = m_Marshaller.Allocate(offset))
     {
         var retval = m_Api.Command(sndfile, LibsndfileCommand.SetRawStartOffset, memory, memory.Size);
         if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.SetRawStartOffset, retval))
         {
             throw new LibsndfileException("Unable to set offset for the given file.");
         }
     }
 }
        /// <summary>
        /// Retrieve the peak value for the file as stored in the file header.
        /// </summary>
        /// <param name="sndfile">Audio file we want to examine.</param>
        /// <returns>Peak value from file header.</returns>
        public double GetSignalMax(IntPtr sndfile)
        {
            using (var memory = m_Marshaller.Allocate <double>())
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.GetSignalMax, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.CalcNormMaxAllChannels, retval))
                {
                    throw new LibsndfileException("Unable to retrieve peak value from file header.");
                }

                return(m_Marshaller.MemoryHandleTo <double>(memory));
            }
        }
        /// <summary>
        /// Truncate a file opened for write or read/write.
        /// </summary>
        /// <param name="sndfile">Audio file to truncate.</param>
        /// <param name="length">Number of frames remaining after truncation.</param>
        /// <returns>Success of file truncation.</returns>
        public bool FileTruncate(IntPtr sndfile, long length)
        {
            using (var memory = m_Marshaller.Allocate(length))
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.FileTruncate, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.FileTruncate, retval))
                {
                    throw new LibsndfileException("Unable to truncate the given file.");
                }

                return(!Convert.ToBoolean(retval));
            }
        }
        /// <summary>
        /// Returns the number of available subformats.
        /// </summary>
        /// <returns>Number of subtype formats supported.</returns>
        public int GetFormatSubtypeCount()
        {
            using (var memory = m_Marshaller.Allocate <int>())
            {
                var retval = m_Api.Command(IntPtr.Zero, LibsndfileCommand.GetFormatSubtypeCount, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(IntPtr.Zero, LibsndfileCommand.GetFormatSubtypeCount, retval))
                {
                    throw new LibsndfileException("Unable to retrieve number of supported subtype formats.");
                }

                return(m_Marshaller.MemoryHandleTo <int>(memory));
            }
        }
        /// <summary>
        /// Retrieve the peak value for each channel for the file as stored in the file header.
        /// </summary>
        /// <param name="sndfile">Audio file we want to examine.</param>
        /// <param name="channels">Number of audio channels in the audio file.</param>
        /// <returns>Peak values for each channel from file header.</returns>
        public double[] GetMaxAllChannels(IntPtr sndfile, int channels)
        {
            using (var memory = m_Marshaller.AllocateArray <double>(channels))
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.GetMaxAllChannels, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.CalcNormMaxAllChannels, retval))
                {
                    throw new LibsndfileException("Unable to retrieve peak values for all channels from file header.");
                }

                return(m_Marshaller.MemoryHandleToArray <double>(memory));
            }
        }
        /// <summary>
        /// Scan <paramref name="sndfile"/> file and return normalized peak value for each channel.
        /// </summary>
        /// <param name="sndfile">Audio file we want to scan.</param>
        /// <param name="channels">Number of audio channels in the audio file.</param>
        /// <returns>Normalized Peak values for each channel.</returns>
        public double[] CalcNormMaxAllChannels(IntPtr sndfile, int channels)
        {
            using (var memory = m_Marshaller.AllocateArray <double>(channels))
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.CalcNormMaxAllChannels, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.CalcNormMaxAllChannels, retval))
                {
                    throw new LibsndfileException("Unable to calculate normalized signal max for all channels in the given file.");
                }

                return(m_Marshaller.MemoryHandleToArray <double>(memory));
            }
        }
        /// <summary>
        /// Scan <paramref name="sndfile"/> file and return maximum calculated signal value.
        /// </summary>
        /// <param name="sndfile">Audio file we want to scan.</param>
        /// <returns>Maximum signal value.</returns>
        public double CalcSignalMax(IntPtr sndfile)
        {
            using (var memory = m_Marshaller.Allocate <double>())
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.CalcSignalMax, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.CalcSignalMax, retval))
                {
                    throw new LibsndfileException("Unable to calculate signal max for the given file.");
                }

                return(m_Marshaller.MemoryHandleTo <double>(memory));
            }
        }
        /// <summary>
        /// Sets the instrument info for the given <paramref name="sndfile"/> file.
        /// </summary>
        /// <param name="sndfile">Audio file to set instrument info on.</param>
        /// <param name="instrumentInfo">Instrument info to set.</param>
        /// <returns>True if instrument info was set, false otherwise.</returns>
        public bool SetInstrument(IntPtr sndfile, LibsndfileInstrumentInfo instrumentInfo)
        {
            using (var memory = m_Marshaller.Allocate(instrumentInfo))
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.SetInstrument, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.SetInstrument, retval))
                {
                    throw new LibsndfileException("Unable to set instrument info for the given file.");
                }

                return(Convert.ToBoolean(retval));
            }
        }
        /// <summary>
        /// Returns the internal Libsndfile log generated when loading a file.
        /// </summary>
        /// <param name="sndfile">Audio file we want the log for.</param>
        /// <returns>Libsndfile log info.</returns>
        public string GetLogInfo(IntPtr sndfile)
        {
            const int MaxLogSize = 2048;

            using (var memory = m_Marshaller.Allocate(MaxLogSize))
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.GetLogInfo, memory, MaxLogSize);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.GetLogInfo, retval))
                {
                    throw new LibsndfileException("Unable to retrieve Libsndfile log info for the given file.");
                }

                return(m_Marshaller.MemoryHandleToString(memory));
            }
        }
        /// <summary>
        /// Returns the version of the Libsndfile library.
        /// </summary>
        /// <returns>Libsndfile library version.</returns>
        public string GetLibVersion()
        {
            const int MaxVersionLength = 128;

            using (var memory = m_Marshaller.Allocate(MaxVersionLength))
            {
                var retval = m_Api.Command(IntPtr.Zero, LibsndfileCommand.GetLibVersion, memory, MaxVersionLength);
                if (!LibsndfileCommandUtilities.IsValidResult(IntPtr.Zero, LibsndfileCommand.GetLibVersion, retval))
                {
                    throw new LibsndfileException("Unable to retrieve Libsndfile library version.");
                }

                return(m_Marshaller.MemoryHandleToString(memory));
            }
        }
        /// <summary>
        /// Get the file offset and file length of a file enbedded within another larger file.
        /// </summary>
        /// <param name="sndfile">Audio file to scan for embedded files.</param>
        /// <returns></returns>
        public LibsndfileEmbedFileInfo GetEmbedFileInfo(IntPtr sndfile)
        {
            var fileInfo = new LibsndfileEmbedFileInfo();

            using (var memory = m_Marshaller.Allocate(fileInfo))
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.GetEmbedFileInfo, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.GetEmbedFileInfo, retval))
                {
                    throw new LibsndfileException("Unable to get embedded file info for the given file.");
                }

                return(m_Marshaller.MemoryHandleTo <LibsndfileEmbedFileInfo>(memory));
            }
        }
Exemple #23
0
        /// <summary>
        /// Attempts to execute the <paramref name="command"/> against the <paramref name="sndfile"/> audio
        /// file while either passing or receiving data through the <paramref name="data"/> buffer.
        /// </summary>
        /// <param name="sndfile">Audio file we run this command against.
        /// Use NULL if you wish to run a static command against the library.</param>
        /// <param name="command"><see cref="NLibsndfile.Native.LibsndfileCommand"/> to run against the given audio file.</param>
        /// <param name="data">Ref long(long*) buffer passing or receiving data based on <paramref name="command"/> specifications.</param>
        /// <param name="size">Size, in bytes, of a long.</param>
        /// <returns>Returns a specific value based on the <paramref name="command"/>.</returns>
        public int Command(IntPtr sndfile, LibsndfileCommand command, ref long data, int size)
        {
            if (sndfile == IntPtr.Zero && !LibsndfileCommandUtilities.IsStaticCommand(command))
            {
                throw new ArgumentException("File handle is invalid/closed.");
            }

            var retval = m_Api.Command(sndfile, command, ref data, size);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, command, retval))
            {
                throw new LibsndfileException(string.Format("Command {0} returned an invalid result.", command));
            }

            return(retval);
        }
        /// <summary>
        /// Retrieves instrument information for the given <paramref name="sndfile"/> file.
        /// </summary>
        /// <param name="sndfile">Audio file to example for instrument information.</param>
        /// <returns><see cref="LibsndfileInstrumentInfo"/> structure containing info about the given file.</returns>
        public LibsndfileInstrumentInfo?GetInstrument(IntPtr sndfile)
        {
            using (var memory = m_Marshaller.Allocate <LibsndfileInstrumentInfo>())
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.GetInstrument, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.GetInstrument, retval))
                {
                    throw new LibsndfileException("Unable to get instrument info for the given file.");
                }

                if (retval == 0)
                {
                    return(null);
                }
                return(m_Marshaller.MemoryHandleTo <LibsndfileInstrumentInfo>(memory));
            }
        }
        /// <summary>
        /// Retrieve the Broadcast Extension chunk from the given <paramref name="sndfile"/> file.
        /// </summary>
        /// <param name="sndfile">Audio file to examine for broadcast info chunk.</param>
        /// <returns><see cref="LibsndfileBroadcastInfo"/> structure containing broadcast info.</returns>
        public LibsndfileBroadcastInfo?GetBroadcastInfo(IntPtr sndfile)
        {
            using (var memory = m_Marshaller.Allocate <LibsndfileBroadcastInfo>())
            {
                var retval = m_Api.Command(sndfile, LibsndfileCommand.GetBroadcastInfo, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(sndfile, LibsndfileCommand.GetBroadcastInfo, retval))
                {
                    throw new LibsndfileException("Unable to retrieve broadcast info from the given file.");
                }

                if (retval == 0)
                {
                    return(null);
                }
                return(m_Marshaller.MemoryHandleTo <LibsndfileBroadcastInfo>(memory));
            }
        }
        /// <summary>
        /// Retrieves information about the given <paramref name="format"/> subtype.
        /// </summary>
        /// <param name="format">Subtype format to retrieve information about.</param>
        /// <returns><see cref="LibsndfileFormatInfo"/> object containing format information.</returns>
        public LibsndfileFormatInfo GetFormatSubtype(LibsndfileFormat format)
        {
            var formatInfo = new LibsndfileFormatInfo {
                Format = format
            };

            using (var memory = m_Marshaller.Allocate(formatInfo))
            {
                var retval = m_Api.Command(IntPtr.Zero, LibsndfileCommand.GetFormatSubtype, memory, memory.Size);
                if (!LibsndfileCommandUtilities.IsValidResult(IntPtr.Zero, LibsndfileCommand.GetFormatSubtypeCount, retval))
                {
                    throw new LibsndfileException(string.Format("Unable to retrieve format info for {0}.", format));
                }

                return(m_Marshaller.MemoryHandleTo <LibsndfileFormatInfo>(memory));
            }
        }
Exemple #27
0
        /// <summary>
        /// Attempts to execute the <paramref name="command"/> against the <paramref name="sndfile"/> audio
        /// file while either passing or receiving data through the <paramref name="data"/> buffer.
        /// </summary>
        /// <param name="sndfile">Audio file we run this command against.
        /// Use NULL if you wish to run a static command against the library.</param>
        /// <param name="command"><see cref="NLibsndfile.Native.LibsndfileCommand"/> to execute</param>
        /// <param name="data">Double[](double*) buffer passing or receiving data based on <paramref name="command"/> specifications.</param>
        /// <param name="size">Size, in bytes, of (double * buffer length)</param>
        /// <returns>Returns a specific value based on the <paramref name="command"/>.</returns>
        public int Command(IntPtr sndfile, LibsndfileCommand command, double[] data, int size)
        {
            if (sndfile == IntPtr.Zero && !LibsndfileCommandUtilities.IsStaticCommand(command))
            {
                throw new ArgumentException("File handle is invalid/closed.");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data", "Data cannot be null.");
            }
            if (data.Length == 0)
            {
                throw new ArgumentNullException("data", "Data must be initialized.");
            }

            var retval = m_Api.Command(sndfile, command, data, size);

            if (!LibsndfileCommandUtilities.IsValidResult(sndfile, command, retval))
            {
                throw new LibsndfileException(string.Format("Command {0} returned an invalid result.", command));
            }

            return(retval);
        }