Esempio n. 1
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            // Create white texture
            _white = new Texture2D(GraphicsDevice, 1, 1);
            _white.SetData(new[] { Color.White });

            // Load image data into memory
            var path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

            path = Path.Combine(path, "image.jpg");
            var buffer = File.ReadAllBytes(path);

            var image = StbImage.LoadFromMemory(buffer, StbImage.STBI_rgb_alpha);

            _image = new Texture2D(GraphicsDevice, image.Width, image.Height, false, SurfaceFormat.Color);
            _image.SetData(image.Data);

            // Load ttf
            LoadFont();

            // Load ogg
            path   = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            path   = Path.Combine(path, "Adeste_Fideles.ogg");
            buffer = File.ReadAllBytes(path);

            int chan, sampleRate;
            var audioShort = StbVorbis.decode_vorbis_from_memory(buffer, out sampleRate, out chan);

            byte[] audioData = new byte[audioShort.Length / 2 * 4];
            for (var i = 0; i < audioShort.Length; ++i)
            {
                if (i * 2 >= audioData.Length)
                {
                    break;
                }

                var b1 = (byte)(audioShort[i] >> 8);
                var b2 = (byte)(audioShort[i] & 256);

                audioData[i * 2 + 0] = b2;
                audioData[i * 2 + 1] = b1;
            }

            _effect = new DynamicSoundEffectInstance(sampleRate, AudioChannels.Stereo)
            {
                Volume = 0.5f
            };


            _effect.SubmitBuffer(audioData);

            GC.Collect();
        }
Esempio n. 2
0
        private void _buttonOpen_Click(object sender, EventArgs e)
        {
            try
            {
                using (var dlg = new OpenFileDialog())
                {
                    dlg.Filter =
                        "OGG Files (*.ogg)|*.ogg|OGA Files (*.oga)|*.oga|All Files (*.*)|*.*";
                    if (dlg.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    var bytes = File.ReadAllBytes(dlg.FileName);

                    var audioShort = StbVorbis.decode_vorbis_from_memory(bytes, out _sampleRate, out _channels);

                    _audioData = new byte[audioShort.Length / 2 * 4];
                    for (var i = 0; i < audioShort.Length; ++i)
                    {
                        if (i * 2 >= _audioData.Length)
                        {
                            break;
                        }

                        var b1 = (byte)(audioShort[i] >> 8);
                        var b2 = (byte)(audioShort[i] & 256);

                        _audioData[i * 2 + 0] = b2;
                        _audioData[i * 2 + 1] = b1;
                    }

                    Text = dlg.FileName;

                    UpdateEnabled();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error", ex.Message);
            }
        }
Esempio n. 3
0
        private static bool TestVorbis()
        {
            try
            {
                var imagesPath = "..\\..\\..\\TestOggs";

                var totalNative  = 0;
                var totalStb     = 0;
                var filesProcess = 0;

                var files = Directory.EnumerateFiles(imagesPath, "*.*", SearchOption.AllDirectories).ToArray();
                Log("Files count: {0}", files.Length);

                foreach (var f in files)
                {
                    if (!f.EndsWith(".ogg") && !f.EndsWith(".oga"))
                    {
                        continue;
                    }

                    Log("Processing file #" + (filesProcess + 1) + ": " + f);

                    var data = File.ReadAllBytes(f);

                    // Native
                    var sw = new Stopwatch();

                    int sampleRate = 0, channels = 0;

                    BeginWatch(sw);
                    var nativeResult = new short[0];

                    for (var i = 0; i < 1; ++i)
                    {
                        nativeResult = StbNative.Vorbis.decode_vorbis_from_memory(data, out sampleRate, out channels);
                    }

                    var p = EndWatch(sw);
                    totalNative += p;
                    Log("Native vorbis decode took {0} ms", p);
                    Log("Sample Rate: {0}, Channels: {1}, Size: {2}", sampleRate, channels, nativeResult.Length);

                    // StbSharp
                    BeginWatch(sw);
                    var stbResult = new short[0];
                    for (var i = 0; i < 1; ++i)
                    {
                        stbResult = StbVorbis.decode_vorbis_from_memory(data, out sampleRate, out channels);
                    }

                    p         = EndWatch(sw);
                    totalStb += p;
                    Log("Stb vorbis decode took {0} ms", p);
                    Log("Sample Rate: {0}, Channels: {1}, Size: {2}", sampleRate, channels, stbResult.Length);

                    if (nativeResult.Length != stbResult.Length)
                    {
                        throw new Exception("Inconsistent size.");
                    }

                    for (var i = 0; i < nativeResult.Length; ++i)
                    {
                        if (Math.Abs(nativeResult[i] - stbResult[i]) > 1)
                        {
                            throw new Exception(string.Format("There is difference at {0}, native={1}, stb={2}", i, nativeResult[i],
                                                              stbResult[i]));
                        }
                    }

                    ++filesProcess;

                    Log("Total Files Processed: " + filesProcess);
                    Log("Total Native Decoding Time In MS: " + totalNative);
                    Log("Total StbSharp Decoding Time In MS: " + totalStb);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Log("Error: " + ex.Message);
                return(false);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            // Create white texture
            _white = new Texture2D(GraphicsDevice, 1, 1);
            _white.SetData(new[] { Color.White });

            // Load image data into memory
            var path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

            path = Path.Combine(path, "image.jpg");
            var buffer = File.ReadAllBytes(path);

            var image = StbImage.LoadFromMemory(buffer, StbImage.STBI_rgb_alpha);

            _image = new Texture2D(GraphicsDevice, image.Width, image.Height, false, SurfaceFormat.Color);
            _image.SetData(image.Data);

            // Load ttf
            buffer = File.ReadAllBytes("Fonts/DroidSans.ttf");
            var buffer2 = File.ReadAllBytes("Fonts/DroidSansJapanese.ttf");

            var tempBitmap = new byte[FontBitmapWidth * FontBitmapHeight];

            var fontBaker = new FontBaker();

            fontBaker.Begin(tempBitmap, FontBitmapWidth, FontBitmapHeight);
            fontBaker.Add(buffer, 32, new []
            {
                FontBakerCharacterRange.BasicLatin,
                FontBakerCharacterRange.Latin1Supplement,
                FontBakerCharacterRange.LatinExtendedA,
                FontBakerCharacterRange.Cyrillic,
            });

            fontBaker.Add(buffer2, 32, new []
            {
                FontBakerCharacterRange.Hiragana,
                FontBakerCharacterRange.Katakana
            });

            _charData = fontBaker.End();

            // Offset by minimal offset
            float minimumOffsetY = 10000;

            foreach (var pair in _charData)
            {
                if (pair.Value.yoff < minimumOffsetY)
                {
                    minimumOffsetY = pair.Value.yoff;
                }
            }

            var keys = _charData.Keys.ToArray();

            foreach (var key in keys)
            {
                var pc = _charData[key];
                pc.yoff       -= minimumOffsetY;
                _charData[key] = pc;
            }


            var rgb = new Color[FontBitmapWidth * FontBitmapHeight];

            for (var i = 0; i < tempBitmap.Length; ++i)
            {
                var b = tempBitmap[i];
                rgb[i].R = b;
                rgb[i].G = b;
                rgb[i].B = b;

                rgb[i].A = b;
            }

            _fontTexture = new Texture2D(GraphicsDevice, FontBitmapWidth, FontBitmapHeight);
            _fontTexture.SetData(rgb);

            // Load ogg
            path   = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            path   = Path.Combine(path, "Adeste_Fideles.ogg");
            buffer = File.ReadAllBytes(path);

            int chan, sampleRate;
            var audioShort = StbVorbis.decode_vorbis_from_memory(buffer, out sampleRate, out chan);

            byte[] audioData = new byte[audioShort.Length / 2 * 4];
            for (var i = 0; i < audioShort.Length; ++i)
            {
                if (i * 2 >= audioData.Length)
                {
                    break;
                }

                var b1 = (byte)(audioShort[i] >> 8);
                var b2 = (byte)(audioShort[i] & 256);

                audioData[i * 2 + 0] = b2;
                audioData[i * 2 + 1] = b1;
            }

            _effect = new DynamicSoundEffectInstance(sampleRate, AudioChannels.Stereo)
            {
                Volume = 0.5f
            };


            _effect.SubmitBuffer(audioData);

            GC.Collect();
        }