/// <summary>
        /// Called to initialise core.
        /// </summary>
        public void Initialize()
        {
            // Create content manager
            contentManager = new ContentManager(serviceProvider, contentRootDirectory);

            // Create input
            input = new Input();
            input.ActiveDevices = Input.DeviceFlags.All;

            // Create empty scene
            scene = new Scene(this);

            // Initialise engine objects
            renderer.Initialize();

            // Get GraphicsDevice
            IGraphicsDeviceService graphicsDeviceService =
                (IGraphicsDeviceService)serviceProvider.GetService(typeof(IGraphicsDeviceService));

            this.graphicsDevice = graphicsDeviceService.GraphicsDevice;

            // Create sprite batch for rendering console and other overlays
            spriteBatch = new SpriteBatch(graphicsDevice);

            // Create Daggerfall managers.
            // MaterialManager must be created before ModelManager due to dependencies.
            this.materialManager = new MaterialManager(this);
            this.modelManager    = new ModelManager(this);
            this.blockManager    = new BlocksFile(Path.Combine(arena2Path, BlocksFile.Filename), FileUsage.UseDisk, true);
            this.mapManager      = new MapsFile(Path.Combine(arena2Path, MapsFile.Filename), FileUsage.UseDisk, true);
            this.soundManager    = new SndFile(Path.Combine(arena2Path, SndFile.Filename), FileUsage.UseDisk, true);

            // Load engine objects content
            renderer.LoadContent();
        }
Esempio n. 2
0
        private static void TranscodeInternal([NotNull] SndFile input, [NotNull] SndFile output)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            const int frames = 1024;
            var       buffer = new short[frames * input.Format.Channels];
            long      read;
            long      done = 0;

            Console.CursorVisible = false;
            do
            {
                read = input.ReadFrames(buffer, frames);

                var write = output.WriteFrames(buffer, read);
                if (write != read)
                {
                    throw new InvalidOperationException();
                }

                done += write;
                Console.WriteLine($"{(float) done / input.Frames:P}");
                Console.SetCursorPosition(0, Console.CursorTop - 1);
            } while (read == frames);
            Console.WriteLine();
        }
Esempio n. 3
0
        private static void TranscodeVirtual([NotNull] string source, [NotNull] string destination)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            using (var helper1 = new SfVirtualStreamHelper(File.OpenRead(source)))
                using (var helper2 = new SfVirtualStreamHelper(File.Create(destination)))
                    using (var input = SndFile.OpenRead(helper1.Virtual))
                        using (var output = SndFile.OpenWrite(helper2.Virtual, SfFormat.DefaultWav))
                        {
                            if (input == null)
                            {
                                throw new InvalidOperationException(SndFile.GetErrorMessage());
                            }

                            if (output == null)
                            {
                                throw new InvalidOperationException(SndFile.GetErrorMessage());
                            }

                            TranscodeInternal(input, output);
                        }
        }
Esempio n. 4
0
        public async Task <float[]> GetSamplesAsync()
        {
            if (_samples != null)
            {
                return(_samples);
            }

            await Task.Run(() => {
                using (var sf = new SndFile(FullPath)) {
                    _samples = sf.Read(sf.Info.frames);
                }
            });

            return(_samples);
        }
Esempio n. 5
0
        private bool ReadyCheck()
        {
            // Ensure we have a DaggerfallUnity reference
            if (dfUnity == null)
            {
                dfUnity = DaggerfallUnity.Instance;
            }

            // Do nothing if DaggerfallUnity not ready
            if (!dfUnity.IsReady)
            {
                DaggerfallUnity.LogMessage("SoundReader: DaggerfallUnity component is not ready. Have you set your Arena2 path?");
                return(false);
            }

            // Ensure sound reader is ready
            if (soundFile == null)
            {
                soundFile = new SndFile(Path.Combine(dfUnity.Arena2Path, SndFile.Filename), FileUsage.UseMemory, true);
            }

            return(true);
        }
Esempio n. 6
0
        private static int Main(string[] args)
        {
            var result = Parser.Default.ParseArguments <Options>(args);

            if (result.Errors.Any()) // prints usage
            {
                Console.WriteLine("Return codes:");
                Console.WriteLine("  Help         : -2");
                Console.WriteLine("  Error        : -1");
                foreach (var k in Enum.GetValues(typeof(Key)).Cast <Key>())
                {
                    Console.WriteLine($"  {k,-12} : {(int) k}");
                }

                Console.WriteLine();
                Console.WriteLine("Credits :");
                Console.WriteLine("  https://github.com/ibsh/libKeyFinder");
                Console.WriteLine("  https://github.com/aybe/libKeyFinder.NET");
                Console.WriteLine("  https://github.com/erikd/libsndfile");

                return(-2);
            }

            var options = result.Value;

            var path = options.Path;

            if (!File.Exists(path))
            {
                Console.WriteLine("File not found");
                return(-1);
            }

            var sf = SndFile.OpenRead(path);

            if (sf == null)
            {
                Console.WriteLine($"File couldn't be opened: '{SndFile.GetErrorMessage()}'.");
                return(-1);
            }

            var format       = sf.Format;
            var frames       = Math.Min(Options.DefaultBlockSize, options.BlockSize);
            var samples      = frames * format.Channels;
            var buffer       = new double[samples];
            var totalFrames  = 0L;
            var totalPercent = 0;
            var kd           = new KeyDetector(format.SampleRate, format.Channels, samples);

            while (true)
            {
                var readFrames = sf.ReadFrames(buffer, frames);
                if (readFrames <= 0)
                {
                    break;
                }

                var readSamples = readFrames * format.Channels;
                for (var i = 0; i < readSamples; i++)
                {
                    kd.SetSample((uint)i, buffer[i]);
                }
                for (var i = readSamples; i < samples; i++)
                {
                    kd.SetSample((uint)i, 0.0d);
                }

                kd.ProgressiveChromagram();
                totalFrames += readFrames;

                var percent = (int)((double)totalFrames / sf.Frames * 100.0d);
                if (percent <= totalPercent)
                {
                    continue;
                }
                var key1 = kd.KeyOfChromagram();
                Console.WriteLine($"{percent,6}% : {key1,-12} ({(int) key1})");
                totalPercent = percent;
            }

            kd.FinalChromagram();
            var key2 = kd.KeyOfChromagram();

            kd.Dispose();
            sf.Dispose();
            Console.WriteLine();
            Console.WriteLine($"Finale  : {key2,-12} ({(int) key2})");
            return((int)key2);
        }
Esempio n. 7
0
        /// <summary>
        /// Generate a new buffer by reading in an audio file
        /// </summary>
        /// <param name="fileName"></param>
        private void GenBuffer(string fileName)
        {
            // create a new file descriptor
            var wavfile = SndFile.OpenRead(fileName);

            if (wavfile.GetError() != SfError.NoError)
            {
                return;
            }

            // size of the wav file
            long bufferSize = wavfile.Format.Channels * wavfile.Frames;

            // read the sound data
            short[] soundData = new short[bufferSize];
            wavfile.ReadFrames(soundData, wavfile.Frames);

            // type of the wave file
            ALFormat subtype = 0;

            if (wavfile.Format.Subtype == SfFormatSubtype.PCM_16)
            {
                if (wavfile.Format.Channels == 1)
                {
                    subtype = ALFormat.Mono16;
                }
                else if (wavfile.Format.Channels == 2)
                {
                    subtype = ALFormat.Stereo16;
                }
            }
            else if (wavfile.Format.Subtype == SfFormatSubtype.PCM_S8)
            {
                if (wavfile.Format.Channels == 1)
                {
                    subtype = ALFormat.Mono8;
                }
                else if (wavfile.Format.Channels == 2)
                {
                    subtype = ALFormat.Stereo8;
                }
            }


            // frequency of the sound file
            int freq = wavfile.Format.SampleRate;

            // dispose file descriptor
            wavfile.Close();
            wavfile.Dispose();

            // unsupported file type, so return
            if (subtype == 0)
            {
                return;
            }

            // create a openAL buffer
            int buffer = AL.GenBuffer();

            AL.BufferData(buffer, subtype, soundData, (int)bufferSize * sizeof(short), freq);

            // assign it
            _audioBuffers[fileName] = buffer;
        }