Пример #1
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);
                        }
        }
Пример #2
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);
        }
Пример #3
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;
        }