Esempio n. 1
0
        public static string GetValue(this XmlNode node, string xPath, string attribute = "")
        {
            if (xPath.Contains('/'))
            {
                int    index = xPath.IndexOf('/');
                string head  = xPath.Substring(0, index);
                string tail  = xPath.Substring(index + 1);
                Sanity.Requires(node[head] != null, $"Node {head} doesn't exist.");
                return(GetValue(node[head], tail, attribute));
            }
            var subNode = node;

            if (xPath != "")
            {
                subNode = node[xPath];
                Sanity.Requires(subNode != null, $"Node {xPath} doesn't exist.");
            }
            if (attribute == "")
            {
                return(subNode.InnerText);
            }
            var attrib = subNode.Attributes[attribute];

            Sanity.Requires(attrib != null, $"Attribute {attribute} doesn't exist.");
            return(attrib.Value);
        }
        private void PostCheckDataChunkBlock(Stream fs)
        {
            fs.Seek(DataChunk.Offset + 8, SeekOrigin.Begin);
            byte[] buffer = new byte[BitsPerSample / 8 * BUFFER_SIZE];
            Func <byte[], int, long> readSquareSum = null;

            switch (BitsPerSample)
            {
            case 8:
                readSquareSum = ReadBytesSquareSum;
                break;

            case 16:
                readSquareSum = ReadShortsSquareSum;
                break;

            default:
                break;
            }
            Sanity.Requires(readSquareSum != null, $"Unsupported bits per sample: {BitsPerSample}");
            long l            = 0;
            int  totalSamples = 0;

            while (fs.Position < fs.Length)
            {
                int n = fs.Read(buffer, 0, buffer.Length);
                l            += readSquareSum(buffer, n);
                totalSamples += n;
            }
            _RMS = Math.Sqrt((double)l / totalSamples);
        }
        private void PostCheckDataChunk(Stream fs)
        {
            fs.Seek(DataChunk.Offset, SeekOrigin.Begin);
            long totalSampleEnergy            = 0;
            int  totalSamples                 = 0;
            int  divisor                      = 0;
            Func <Stream, int> GetSampleValue = null;

            switch (BitsPerSample)
            {
            case 8:
                GetSampleValue = x => fs.ReadByte();
                divisor        = 256;
                break;

            case 16:
                GetSampleValue = x => x.ReadShortFromFileStream();
                divisor        = 65536;
                break;

            default:
                break;
            }
            Sanity.Requires(GetSampleValue != null, $"Unspported wave file. Bits per sample: {BitsPerSample}");
            while (fs.Position < fs.Length)
            {
                int sampleValue = GetSampleValue(fs);
                totalSampleEnergy += sampleValue * sampleValue;
                totalSamples++;
            }
            _RMS = Math.Sqrt((double)totalSampleEnergy / totalSamples) / divisor;
        }
Esempio n. 4
0
        public static IEnumerable <string> GetXmlValues(this XmlNode rootNode, string xPath, string attribute = "")
        {
            Sanity.Requires(rootNode != null, "The root node is null.");
            var subNodes = rootNode.SelectNodes(xPath)
                           .Cast <XmlNode>();

            return(string.IsNullOrEmpty(attribute)
                ? subNodes.Select(x => x.InnerText)
                : subNodes.Select(x => x.Attributes[attribute].Value));
        }
Esempio n. 5
0
        public static string GetXmlValue(this XmlNode rootNode, string xpath, string attribute = "")
        {
            Sanity.Requires(rootNode != null, "The root node is null.");
            var subNode = string.IsNullOrEmpty(xpath) ? rootNode : rootNode.SelectSingleNode(xpath);

            Sanity.Requires(subNode != null, $"The xpath {xpath} doesn't exist.");
            return(string.IsNullOrEmpty(attribute)
                ? subNode.InnerText
                : subNode.Attributes[attribute].Value);
        }
 private void PostCheck(Stream fs)
 {
     Sanity.Requires(!string.IsNullOrEmpty(FormatChunk.Name), "Invalid wave file, missing format chunk.");
     Sanity.Requires(!string.IsNullOrEmpty(DataChunk.Name), "Invalid wave file, missing data chunk.");
     PostCheckFormatChunk(fs);
     if (IsDeep)
     {
         PostCheckDataChunk(fs);
     }
 }
        private void PostCheckFormatChunk(Stream fs)
        {
            fs.Seek(FormatChunk.Offset, SeekOrigin.Begin);
            WaveType      = fs.ReadShortFromFileStream();
            NumChannels   = fs.ReadShortFromFileStream();
            SampleRate    = fs.ReadIntFromFileStream();
            ByteRate      = fs.ReadIntFromFileStream();
            BlockAlign    = fs.ReadShortFromFileStream();
            BitsPerSample = fs.ReadShortFromFileStream();

            Sanity.Requires(ByteRate == SampleRate * BlockAlign, $"Invalid audio: ByteRate: {ByteRate}, SampleRate: {SampleRate}, BlockAlign: {BlockAlign}.");
            Sanity.Requires(BitsPerSample * NumChannels == 8 * BlockAlign, $"Invalid audio: BitsPerSample: {BitsPerSample}, NumChannels: {NumChannels}, BlockAlign: {BlockAlign}");

            AudioLength = (double)DataChunk.Length / ByteRate;
        }
Esempio n. 8
0
 public static void Run(string fileName, string arguments, bool createNewWindow, string workingDirectory)
 {
     Sanity.Requires(File.Exists(fileName), "Missing file run file.");
     using (Process proc = new Process())
     {
         proc.StartInfo = new ProcessStartInfo
         {
             FileName         = fileName,
             Arguments        = arguments,
             UseShellExecute  = createNewWindow,
             WorkingDirectory = workingDirectory,
         };
         proc.Start();
         proc.WaitForExit();
     }
 }
        private void ParseRecursively(Stream fs)
        {
            if (fs.Position == fs.Length)
            {
                return;
            }

            Sanity.Requires(fs.Position + 8 <= fs.Length, "Invalid wave file, shorter than expected.");
            string chunkName   = fs.ReadStringFromFileStream(Encoding.ASCII, 4);
            int    chunkSize   = fs.ReadIntFromFileStream();
            int    chunkOffset = (int)fs.Position;

            Sanity.Requires(chunkOffset + chunkSize <= fs.Length, $"Invalid wave file, shorter than expected in {chunkName}.");
            WaveChunk chunk = new WaveChunk
            {
                Name   = chunkName,
                Offset = chunkOffset,
                Length = chunkSize
            };

            switch (chunk.Name)
            {
            case FORMAT:
                Sanity.Requires(FormatChunk.Name == "", "Invalid wave file, more than one format chunk.");
                FormatChunk = chunk;
                break;

            case DATA:
                Sanity.Requires(DataChunk.Name == "", "Invalid wave file, more than one data chunk.");
                DataChunk = chunk;
                break;

            default:
                break;
            }
            ChunkList.Add(chunk);

            fs.Seek(chunk.Length, SeekOrigin.Current);

            ParseRecursively(fs);
        }
        private void ParseWave(Stream fs)
        {
            Sanity.Requires(fs.Length >= 44, "Invalid wave file, size too small.");
            Sanity.Requires(fs.Length <= int.MaxValue, "Invalid wave file, size too big.");

            string riff = fs.ReadStringFromFileStream(Encoding.ASCII, 4);

            Sanity.Requires(riff == RIFF, "Invalid wave file, broken RIFF header.");

            int length = fs.ReadIntFromFileStream();

            Sanity.Requires(length + fs.Position == fs.Length, "Invalid wave file, shorter than expected.");

            string wave = fs.ReadStringFromFileStream(Encoding.ASCII, 4);

            Sanity.Requires(wave == WAVE, "Invalid wave file, broken WAVE header.");

            ParseRecursively(fs);

            PostCheck(fs);
        }
        private static byte[] SetHeader(byte[] bytes, short numChannels, short audioType, int sampleRate, short bitsPerSample, int dataLength)
        {
            Sanity.Requires(bytes.Length >= 44, "Wave has to be at least 44 bytes.");
            Array.Copy(Encoding.ASCII.GetBytes("RIFF"), 0, bytes, 0, 4);
            Array.Copy(Encoding.ASCII.GetBytes("WAVE"), 0, bytes, 8, 4);
            Array.Copy(Encoding.ASCII.GetBytes("fmt "), 0, bytes, 12, 4);
            Array.Copy(Encoding.ASCII.GetBytes("data"), 0, bytes, 36, 4);
            Array.Copy(BitConverter.GetBytes(dataLength + 44 - 8), 0, bytes, 4, 4);
            Array.Copy(BitConverter.GetBytes(16), 0, bytes, 16, 4);
            Array.Copy(BitConverter.GetBytes(audioType), 0, bytes, 20, 2);
            Array.Copy(BitConverter.GetBytes(numChannels), 0, bytes, 22, 2);
            Array.Copy(BitConverter.GetBytes(sampleRate), 0, bytes, 24, 4);
            int byteRate = (sampleRate * numChannels * bitsPerSample / 8);

            Array.Copy(BitConverter.GetBytes(byteRate), 0, bytes, 28, 4);
            short blockAlign = (short)(numChannels * bitsPerSample / 8);

            Array.Copy(BitConverter.GetBytes(blockAlign), 0, bytes, 32, 2);
            Array.Copy(BitConverter.GetBytes(bitsPerSample), 0, bytes, 34, 2);
            Array.Copy(BitConverter.GetBytes(dataLength), 0, bytes, 40, 4);
            return(bytes);
        }
Esempio n. 12
0
 public TValue this[TRow r, TColumn c]
 {
     get
     {
         Sanity.Requires(RowSet.Contains(r) && ColumnSet.Contains(c), $"The row-column pair [{r}-{c}] doesn't exist.");
         Sanity.Requires(InternalDict.ContainsKey(r), "Mismatch in key sets.");
         var dict = InternalDict[r];
         return(dict.ContainsKey(c) ? dict[c] : DefaultValue);
     }
     set
     {
         if (!RowSet.Contains(r))
         {
             RowSet.Add(r);
             InternalDict.Add(r, new Dictionary <TColumn, TValue>());
         }
         if (!ColumnSet.Contains(c))
         {
             ColumnSet.Add(c);
         }
         InternalDict[r][c] = value;
     }
 }