Exemplo n.º 1
0
        private static int[] RenderTone(SAMEAudioBit byteSpec)
        {
            if (byteSpec.length != 0.00192M)
            {
                _ = 1;
            }

            Dictionary <decimal, List <int> > headerByteCache = new Dictionary <decimal, List <int> >();
            var computedSamples = new List <int>();

            for (var i = 0; i < (44100 * byteSpec.length); i++)
            {
                for (var c = 0; c < 2; c++)
                {
                    var sample = (decimal)(byteSpec.volume * Math.Sin((2 * Math.PI) * (i / 44100d) * byteSpec.frequency));
                    if (headerByteCache.ContainsKey(sample))
                    {
                        computedSamples.AddRange(headerByteCache[sample]);
                    }
                    else
                    {
                        List <int> thisSample = PackBytes("v", sample);
                        computedSamples.AddRange(thisSample);
                        headerByteCache.Add(sample, thisSample);
                    }
                }
            }
            if (computedSamples.Count() != 340)
            {
                _ = false;
            }
            return(computedSamples.ToArray());
        }
Exemplo n.º 2
0
        static int[] GenerateBinaryHeaders(string str, int volume)
        {
            SAMEAudioBit Mark  = new SAMEAudioBit(2083, (decimal)0.00192, volume);
            SAMEAudioBit Space = new SAMEAudioBit(1563, (decimal)0.00192, volume);

            byte[] byteArray = Encoding.Default.GetBytes(str);              // return byte array representation of the string

            // converts bits in array to store information about how to render each bit
            List <SAMEAudioBit> byteSpec = new List <SAMEAudioBit>();

            SAMEAudioBit[] oneByte;
            foreach (byte thisByte in byteArray)
            {
                oneByte = new SAMEAudioBit[8];
                char c = (char)thisByte;
                for (int bit = 0; bit < 8; bit++)
                {
                    SAMEAudioBit thisBit = (thisByte & (short)Math.Pow(2, bit)) != 0 ? Mark : Space;
                    byteSpec.Add(new SAMEAudioBit(thisBit.frequency, thisBit.length, volume));
                    oneByte[bit] = thisBit;
                }
            }

            // begin rendering tones from binary data
            List <int> toneFrequencies = new List <int>();

            foreach (SAMEAudioBit currentSpec in byteSpec)
            {
                int[] returnedBytes = RenderTone(currentSpec);
                toneFrequencies.AddRange(returnedBytes);
            }

            bool IsExpected = true && (toneFrequencies[4] == 80 || toneFrequencies[4] == 182) && (toneFrequencies[5] == 5 || toneFrequencies[5] == 4);

            IsExpected &= (toneFrequencies[6] == 182 || toneFrequencies[6] == 80) && (toneFrequencies[7] == 5 || toneFrequencies[7] == 4);
            for (int i = 0; i <= 3; i++)
            {
                if (toneFrequencies[i] != 0)
                {
                    IsExpected = false;
                }
            }
            if (!IsExpected)
            {
                _ = 1;
            }

            //string output = "[";
            //foreach (int t in toneFrequencies) {
            //	output += $"{(t < 10 ? "  " : t < 100 ? " " : "")}{t},";
            //}
            //output += output.Substring(0, output.Length - 1) + $"] -> {str}\n";
            //File.AppendAllText("characters.txt", output);
            return(toneFrequencies.ToArray());
        }