Esempio n. 1
0
        public void ThreadPlaySpeech()
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();

            synth.SelectVoiceByHints(VoiceGender.Female);


            Thread[] threads = new Thread[10];


            for (int i = 0; i < 10; i++)
            {
                WaveOut waveOut = new WaveOut {
                    Device = new WaveOutDevice(0)
                };
                MemoryStream stream = new MemoryStream();
                synth.SetOutputToWaveStream(stream);

                synth.Speak("ok so based on your answers I can for example send to the client the number of bytes that I am planing to send.");


                var waveSource = new MediaFoundationDecoder(stream);
                threads[i] = new Thread(() =>
                {
                    waveOut.WaitForStopped();
                    waveOut.Initialize(waveSource);
                    waveOut.Play();
                    waveOut.WaitForStopped();
                });
                threads[i].Start();
                Thread.Sleep(100);
            }

            threads[9].Join();
        }
Esempio n. 2
0
        static void Main()
        {
            using (var stream = new MemoryStream())
                using (var speechEngine = new SpeechSynthesizer())
                {
                    Console.WriteLine("Available devices:");
                    foreach (var device in WaveOutDevice.EnumerateDevices())
                    {
                        Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
                    }
                    Console.WriteLine("\nEnter device for speech output:");
                    var deviceId = (int)char.GetNumericValue(Console.ReadKey().KeyChar);

                    speechEngine.SetOutputToWaveStream(stream);
                    speechEngine.Speak("Testing 1 2 3");

                    using (var waveOut = new WaveOut {
                        Device = new WaveOutDevice(deviceId)
                    })
                        using (var waveSource = new MediaFoundationDecoder(stream))
                        {
                            waveOut.Initialize(waveSource);
                            waveOut.Play();
                            waveOut.WaitForStopped();
                        }
                }
        }
Esempio n. 3
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource;

            if (Path.GetExtension(this.filename.ToLower()) == FileFormats.MP3)
            {
                // For MP3's, we force usage of MediaFoundationDecoder. CSCore uses DmoMp3Decoder
                // by default. DmoMp3Decoder however is very slow at playing MP3's from a NAS.
                // So we're using MediaFoundationDecoder until DmoMp3Decoder has improved.
                waveSource = new MediaFoundationDecoder(this.filename);
            }
            else
            {
                // Other file formats are using the default decoders
                waveSource = CodecFactory.Instance.GetCodec(this.filename);
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The sample rate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
Esempio n. 4
0
        private void button3_Click(object sender, EventArgs e) //requires system.management .. but it works!
        {
            SpeechSynthesizer speechEngine = new SpeechSynthesizer();
            MemoryStream      stream       = new MemoryStream();

            //--- Make the voice a woman ---//
            var genderVoices = speechEngine.GetInstalledVoices().Where(arg => arg.VoiceInfo.Gender == VoiceGender.Female).ToList();
            var firstVoice   = genderVoices.FirstOrDefault();

            firstVoice = genderVoices.FirstOrDefault();
            if (firstVoice == null)
            {
                return;
            }
            speechEngine.SelectVoice(firstVoice.VoiceInfo.Name);
            //-------------------------------

            //Enumerate devices using WaveOutDevice from CSCore library (just enumeration basics)
            foreach (var device in WaveOutDevice.EnumerateDevices())
            {
                Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
            }
            Console.WriteLine("\nEnter device for speech output:");
            var deviceId = 2;//we have to select which one

            speechEngine.SetOutputToWaveStream(stream);
            speechEngine.Speak(textBox1.Text.ToString()); //we can get the contents of the text box and play it

            using (var waveOut = new WaveOut {
                Device = new WaveOutDevice(deviceId)
            })
                using (var waveSource = new MediaFoundationDecoder(stream))
                {
                    waveOut.Initialize(waveSource);
                    waveOut.Play();
                    waveOut.WaitForStopped();
                }
            //enumerate devices using Management objects (another way to do stuff, but with more details)

            /*
             * ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
             *      "SELECT * FROM Win32_SoundDevice");
             *
             * ManagementObjectCollection objCollection = objSearcher.Get();
             *
             * ManagementObject whatever = new ManagementObject();
             *
             * foreach (ManagementObject obj in objCollection)
             * {
             *  Console.Out.WriteLine("-------------------------------------");
             *  foreach (PropertyData property in obj.Properties)
             *  {
             *      Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
             *      whatever = obj;
             *  }
             * }
             *
             */
        }
Esempio n. 5
0
 private void CleanupPlayback()
 {
     if (_soundOut != null)
     {
         _soundOut.Dispose();
         _soundOut = null;
     }
     if (_waveSource != null)
     {
         _waveSource.Dispose();
         _waveSource = null;
     }
 }
Esempio n. 6
0
        public void Open(string filename, WaveOutDevice device)
        {
            CleanupPlayback();

            _waveSource = new MediaFoundationDecoder(filename);
            _soundOut   = new WasapiOut()
            {
                Latency = 100
            };
            _soundOut.Initialize(_waveSource);
            if (PlaybackStopped != null)
            {
                _soundOut.Stopped += PlaybackStopped;
            }
        }
Esempio n. 7
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource       = null;
            bool        useFfmpegDecoder = true;

            // FfmpegDecoder doesn't support WMA lossless. If Windows Media Foundation is available,
            // we can use MediaFoundationDecoder for WMA files, which supports WMA lossless.
            if (this.supportsWindowsMediaFoundation && Path.GetExtension(filename).ToLower().Equals(FileFormats.WMA))
            {
                try
                {
                    waveSource       = new MediaFoundationDecoder(filename);
                    useFfmpegDecoder = false;
                }
                catch (Exception)
                {
                }
            }

            if (useFfmpegDecoder)
            {
                // waveSource = new FfmpegDecoder(this.filename);

                // On some systems, files with special characters (e.g. "æ", "ø") can't be opened by FfmpegDecoder.
                // This exception is thrown: avformat_open_input returned 0xfffffffe: No such file or directory.
                // StackTrace: at CSCore.Ffmpeg.FfmpegCalls.AvformatOpenInput(AVFormatContext** formatContext, String url)
                // This issue can't be reproduced for now, so we're using a stream as it works in all cases.
                // See: https://github.com/digimezzo/Dopamine/issues/746
                // And: https://github.com/filoe/cscore/issues/344
                this.audioStream = File.OpenRead(filename);
                waveSource       = new FfmpegDecoder(this.audioStream);
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The sample rate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
Esempio n. 8
0
        public void PlaySpeech()
        {
            MemoryStream      stream = new MemoryStream();
            SpeechSynthesizer synth  = new SpeechSynthesizer();

            synth.SelectVoiceByHints(VoiceGender.Female);
            WaveOut waveOut = new WaveOut {
                Device = new WaveOutDevice(0)
            };

            synth.SetOutputToWaveStream(stream);
            synth.Speak("test");
            var waveSource = new MediaFoundationDecoder(stream);

            waveOut.Initialize(waveSource);
            waveOut.Play();
            waveOut.WaitForStopped();
        }
Esempio n. 9
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource       = null;
            bool        useFfmpegDecoder = true;

            if (Path.GetExtension(filename).ToLower().Equals(FileFormats.WMA))
            {
                try
                {
                    waveSource       = new MediaFoundationDecoder(filename);
                    useFfmpegDecoder = false;
                }
                catch (Exception)
                {
                }
            }

            if (useFfmpegDecoder)
            {
                // FfmpegDecoder constructor which uses string as parameter throws exception
                // "Exception: avformat_open_input returned 0xffffffea: Invalid argument."
                // when the file name contains special characters.
                // See: https://github.com/filoe/cscore/issues/298
                // Workaround: use the constructor which uses stream as parameter.
                // IWaveSource waveSource = new FfmpegDecoder(this.filename);
                this.audioStream = File.OpenRead(filename);
                waveSource       = new FfmpegDecoder(this.audioStream);
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The sample rate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
Esempio n. 10
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource;

            if (System.IO.Path.GetExtension(this.filename) == FileFormats.MP3)
            {
                // For MP3's, we force usage of MediaFoundationDecoder. CSCore uses DmoMp3Decoder
                // by default. DmoMp3Decoder however is very slow at playing MP3's from a NAS.
                // So we're using MediaFoundationDecoder until DmoMp3Decoder has improved.
                waveSource = new MediaFoundationDecoder(this.filename);
            }
            else
            {
                // Other file formats are using the default decoders
                waveSource = CodecFactory.Instance.GetCodec(this.filename);
            }

            return(waveSource
                   .ChangeSampleRate(32000)
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
Esempio n. 11
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource       = null;
            bool        useFfmpegDecoder = true;

            // FfmpegDecoder doesn't support WMA lossless. If Windows Media Foundation is available,
            // we can use MediaFoundationDecoder for WMA files, which supports WMA lossless.
            if (this.supportsWindowsMediaFoundation && Path.GetExtension(filename).ToLower().Equals(FileFormats.WMA))
            {
                try
                {
                    waveSource       = new MediaFoundationDecoder(filename);
                    useFfmpegDecoder = false;
                }
                catch (Exception)
                {
                }
            }

            if (useFfmpegDecoder)
            {
                waveSource = new FfmpegDecoder(this.filename);
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The sample rate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
Esempio n. 12
0
 private bool Decode(string filePath)
 {
     if (!Directory.Exists("temp"))
     {
         Directory.CreateDirectory("temp");
     }
     {
         string toOut = @"temp\" + Path.GetFileNameWithoutExtension(filePath) + ".wav";
         try
         {
             using (var decoder = new MediaFoundationDecoder(filePath))
             {
                 decoder.WriteToFile(toOut);
             }
         }
         catch (MediaFoundationException e)
         {
             e.ToString();
             try
             {
                 using (var reader = new VorbisWaveReader(filePath))
                 {
                     using (var converter = new Wave32To16Stream(reader))
                     {
                         WaveFileWriter.CreateWaveFile(toOut, converter);
                     }
                 }
             }
             catch
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Esempio n. 13
0
        public static void Main()
        {
            GuiHandler gui    = new GuiHandler();
            var        handle = GetConsoleWindow();


            Console.WriteLine("Available devices:");
            foreach (WaveOutDevice device in WaveOutDevice.EnumerateDevices())
            {
                Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
            }

            Console.WriteLine("\nEnter device for speech output:");
            var deviceId = (int)char.GetNumericValue(Console.ReadKey().KeyChar);



            ShowWindow(handle, SW_HIDE);

            gui.CreateTaskBarIcon();
            gui.AddMenuOption("E&xit", (sender, e) => Environment.Exit(0));



            SpeechSynthesizer synth = new SpeechSynthesizer();

            synth.SelectVoiceByHints(VoiceGender.Female);

            foreach (InstalledVoice f in synth.GetInstalledVoices())
            {
                gui.AddMenuOption("Se&t voice to " + f.VoiceInfo.Name,
                                  (sender, e) => synth.SelectVoice(f.VoiceInfo.Name)
                                  );
            }



            new KeystrokeAPI().CreateKeyboardHook((character) =>
            {
                if ((int)character.KeyCode == 114)
                {
                    WaveOut waveOut = new WaveOut {
                        Device = new WaveOutDevice(deviceId)
                    };

                    MemoryStream stream = new MemoryStream();


                    synth.SetOutputToWaveStream(stream);

                    string text = GuiHandler.GetUserInput();

                    if (text != "")
                    {
                        synth.Speak(text);

                        var waveSource = new MediaFoundationDecoder(stream);
                        new Thread(() =>
                        {
                            waveOut.WaitForStopped();
                            waveOut.Initialize(waveSource);
                            waveOut.Play();
                            waveOut.WaitForStopped();
                        }).Start();
                    }
                }
            });

            Application.Run();
            while (true)
            {
            }
        }
Esempio n. 14
0
        float[,] LoadSong(string path)
        {
            IWaveSource reader     = new MediaFoundationDecoder(path);
            var         sampleRate = reader.WaveFormat.SampleRate;

            reader = reader.ToMono();
            MemoryStream cache = new MemoryStream();

            reader.WriteToWaveStream(cache);
            cache.Position = 0;
            reader.Dispose();
            reader = new WaveFileReader(cache);
            var r = reader.ToSampleSource();

            // Samples per key
            var fftSamplesPerKey = 10;

            // Width of the array
            var fftWidth = 10000;

            // Number of subdivisions in each sample group
            // Higher = more precise
            var precisionMultiplier = 5;

            // Wavelengths in each sample
            // Higher = more precise
            var wavelengthsPerSample = 20;

            var fftResultLen = 128 * fftSamplesPerKey;
            int len          = (int)(reader.Length / 2);
            var result       = new float[fftWidth, fftResultLen];

            var datafull = new float[(int)len];

            r.Read(datafull, 0, (int)len);
            r.Dispose();

            int       progress = 0;
            object    lck      = new object();
            Stopwatch s        = new Stopwatch();

            s.Start();
            unsafe
            {
                fixed(float *input_ptr = datafull)
                {
                    float *output_ptr = (float *)CudaFFT.process_audio((IntPtr)input_ptr, (ulong)len, (uint)sampleRate);
                    ulong  idx        = 0;

                    for (int x = 0; x < fftWidth; x++)
                    {
                        for (int y = 0; y < fftResultLen; y++)
                        {
                            result[x, y] = output_ptr[idx++];
                        }
                    }
                }
            }
            Console.WriteLine("Complete! Seconds spent: " + Math.Round(s.ElapsedMilliseconds / 1000.0, 1));

            return(result);
        }
Esempio n. 15
0
        private CodecFactory()
        {
            _codecs = new Dictionary <object, CodecFactoryEntry>();
            Register("mp3", new CodecFactoryEntry(s =>
            {
                try
                {
                    return(new DmoMp3Decoder(s));
                }
                catch (Exception)
                {
                    if (Mp3MediafoundationDecoder.IsSupported)
                    {
                        return(new Mp3MediafoundationDecoder(s));
                    }
                    throw;
                }
            },
                                                  "mp3", "mpeg3"));
            Register("wave", new CodecFactoryEntry(s =>
            {
                IWaveSource res = new WaveFileReader(s);
                if (res.WaveFormat.WaveFormatTag != AudioEncoding.Pcm &&
                    res.WaveFormat.WaveFormatTag != AudioEncoding.IeeeFloat &&
                    res.WaveFormat.WaveFormatTag != AudioEncoding.Extensible)
                {
                    res.Dispose();
                    res = new MediaFoundationDecoder(s);
                }
                return(res);
            },
                                                   "wav", "wave"));
            Register("flac", new CodecFactoryEntry(s => new FlacFile(s),
                                                   "flac", "fla"));
            Register("aiff", new CodecFactoryEntry(s => new AiffReader(s),
                                                   "aiff", "aif", "aifc"));

            if (AacDecoder.IsSupported)
            {
                Register("aac", new CodecFactoryEntry(s => new AacDecoder(s),
                                                      "aac", "adt", "adts", "m2ts", "mp2", "3g2", "3gp2", "3gp", "3gpp", "m4a", "m4v", "mp4v", "mp4",
                                                      "mov"));
            }

            if (WmaDecoder.IsSupported)
            {
                Register("wma", new CodecFactoryEntry(s => new WmaDecoder(s),
                                                      "asf", "wm", "wmv", "wma"));
            }

            if (Mp1Decoder.IsSupported)
            {
                Register("mp1", new CodecFactoryEntry(s => new Mp1Decoder(s),
                                                      "mp1", "m2ts"));
            }

            if (Mp2Decoder.IsSupported)
            {
                Register("mp2", new CodecFactoryEntry(s => new Mp2Decoder(s),
                                                      "mp2", "m2ts"));
            }

            if (DDPDecoder.IsSupported)
            {
                Register("ddp", new CodecFactoryEntry(s => new DDPDecoder(s),
                                                      "mp2", "m2ts", "m4a", "m4v", "mp4v", "mp4", "mov", "asf", "wm", "wmv", "wma", "avi", "ac3", "ec3"));
            }
        }
Esempio n. 16
0
        double scaleOffset = 1;//0.997;

        public MainWindow()
        {
            dx11 = new DXWPF.D3D11();
            dx11.SingleThreadedRender = true;
            scene = new ImgScene()
            {
                Renderer = dx11
            };

            midiOutput = new OutputDevice(0);

            InitializeComponent();
            SourceInitialized += (s, e) =>
            {
                IntPtr handle = (new WindowInteropHelper(this)).Handle;
                HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WindowProc));
            };

            for (int i = 0; i < 128; i++)
            {
                var       k   = 127 - i;
                DockPanel d   = new DockPanel();
                Grid      key = new Grid();
                key.SetBinding(Grid.WidthProperty, new Binding("ActualWidth")
                {
                    Source = keyboardBox
                });

                Storyboard storyboard = new Storyboard();

                if (isBlackNote(k))
                {
                    Panel.SetZIndex(d, 10);
                    SolidColorBrush keycolbrush = new SolidColorBrush(Colors.Black);
                    var             bord        = new Border()
                    {
                        Background = keycolbrush, Margin = new Thickness(70, 0, 0, 0)
                    };
                    key.Children.Add(bord);

                    var keyCol = new ColorAnimation(Color.FromArgb(255, 0, 0, 250), Colors.Black, new Duration(TimeSpan.FromSeconds(0.5)));
                    storyboard.Children.Add(keyCol);
                    Storyboard.SetTarget(keyCol, bord);
                    Storyboard.SetTargetProperty(keyCol, new PropertyPath("(Border.Background).(SolidColorBrush.Color)"));
                }
                else
                {
                    Panel.SetZIndex(d, 0);
                    SolidColorBrush keycolbrush = new SolidColorBrush(Colors.White);
                    var             bord        = new Border()
                    {
                        Background = keycolbrush, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1)
                    };
                    key.Children.Add(bord);

                    var keyCol = new ColorAnimation(Color.FromArgb(255, 50, 50, 255), Colors.White, new Duration(TimeSpan.FromSeconds(0.5)));
                    storyboard.Children.Add(keyCol);
                    Storyboard.SetTarget(keyCol, bord);
                    Storyboard.SetTargetProperty(keyCol, new PropertyPath("(Border.Background).(SolidColorBrush.Color)"));

                    var n = k % 12;
                    Func <double, Thickness> conv = (v) => new Thickness(0);

                    if (n == 11)
                    {
                        conv = (v) => new Thickness(0, 0, 0, -v * 3 / 4);
                    }
                    if (n == 9)
                    {
                        conv = (v) => new Thickness(0, -v / 4, 0, -v / 2);
                    }
                    if (n == 7)
                    {
                        conv = (v) => new Thickness(0, -v / 2, 0, -v / 4);
                    }
                    if (n == 5)
                    {
                        conv = (v) => new Thickness(0, -v * 3 / 4, 0, 0);
                    }

                    if (n == 4)
                    {
                        conv = (v) => new Thickness(0, 0, 0, -v * 2 / 3);
                    }
                    if (n == 2)
                    {
                        conv = (v) => new Thickness(0, -v / 3, 0, -v / 3);
                    }
                    if (n == 0)
                    {
                        conv = (v) => new Thickness(0, -v * 2 / 3, 0, 0);
                    }

                    new InplaceConverter(new[] { new Binding("ActualHeight")
                                                 {
                                                     Source = d
                                                 } }, (args) => conv((double)args[0])).Set(key, MarginProperty);
                }

                SolidColorBrush linecolbrush = new SolidColorBrush(Colors.Transparent);
                Border          b            = new Border()
                {
                    BorderBrush = new SolidColorBrush(Color.FromArgb(50, 50, 50, 255)), BorderThickness = new Thickness(0, 1, 0, 1), Background = linecolbrush
                };

                var lineCol = new ColorAnimation(Color.FromArgb(50, 50, 50, 255), Colors.Transparent, new Duration(TimeSpan.FromSeconds(0.5)));
                storyboard.Children.Add(lineCol);
                Storyboard.SetTarget(lineCol, b);
                Storyboard.SetTargetProperty(lineCol, new PropertyPath("(Border.Background).(SolidColorBrush.Color)"));

                //var h = new DoubleAnimation(0, 20, new Duration(TimeSpan.FromSeconds(0.5)));
                //storyboard.Children.Add(h);
                //Storyboard.SetTarget(h, d);
                //Storyboard.SetTargetProperty(h, new PropertyPath(HeightProperty));

                keyPressAnimations.Add(storyboard);

                d.Children.Add(key);
                d.Children.Add(b);
                d.SetBinding(HeightProperty, new Binding("LayerHeight")
                {
                    Source = this
                });

                layersContainer.Children.Add(d);
                d.Tag = k;
                d.PreviewMouseDown += (s, e) =>
                {
                    if (Keyboard.IsKeyDown(Key.LeftCtrl))
                    {
                        var pos    = e.GetPosition(contentGrid);
                        var scaled = pos.X / contentGrid.ActualWidth * (scene.HorizontalScale.Right - scene.HorizontalScale.Left) + scene.HorizontalScale.Left;
                        scaled *= scaleOffset;
                        var finalPos = (long)(scaled * (fileReader.Length / fileReader.WaveFormat.Channels)) * fileReader.WaveFormat.Channels;
                        if (finalPos > fileReader.Length)
                        {
                            finalPos = fileReader.Length;
                        }
                        if (finalPos < 0)
                        {
                            finalPos = 0;
                        }
                        fileReader.Position = finalPos;
                        return;
                    }
                    PlayKey((int)d.Tag);
                    keyPressAnimations[127 - (int)d.Tag].Begin();
                };
            }

            new InplaceConverter(new[] { new Binding("VerticalScale")
                                         {
                                             Source = this
                                         }, new Binding("ActualHeight")
                                         {
                                             Source = containerGrid
                                         } }, (b) =>
            {
                var scale       = (VScale)b[0];
                var height      = scale.Bottom - scale.Top;
                var layerHeight = (double)b[1] / height;
                return(layerHeight);
            }).Set(this, LayerHeightProperty);

            new InplaceConverter(new[] { new Binding("VerticalScale")
                                         {
                                             Source = this
                                         }, new Binding("ActualHeight")
                                         {
                                             Source = containerGrid
                                         } }, (b) =>
            {
                var scale       = (VScale)b[0];
                var height      = scale.Bottom - scale.Top;
                var layerHeight = (double)b[1] / height;
                var topMargin   = LayerHeight * scale.Top;
                return(new Thickness(0, -topMargin, 0, 0));
            }).Set(layersContainer, MarginProperty);

            imgRender.Renderer = scene;

            string path;
            var    open = new OpenFileDialog();

            open.Filter = "All Files|*.*";
            if ((bool)open.ShowDialog())
            {
                path = open.FileName;
            }
            else
            {
                throw new Exception();
            }

            scene.LoadTexture(LoadSong(path));
            var          fr    = new MediaFoundationDecoder(path);
            MemoryStream cache = new MemoryStream();

            fr.WriteToWaveStream(cache);
            cache.Position = 0;
            fileReader     = new WaveFileReader(cache);
            waveOut        = GetSoundOut();
            waveOut.Initialize(fileReader);
        }
Esempio n. 17
0
        float[,] LoadSong(string path)
        {
            IWaveSource reader     = new MediaFoundationDecoder(path);
            var         sampleRate = reader.WaveFormat.SampleRate;

            reader = reader.ToMono();
            MemoryStream cache = new MemoryStream();

            reader.WriteToWaveStream(cache);
            cache.Position = 0;
            reader.Dispose();
            reader = new WaveFileReader(cache);
            var r = reader.ToSampleSource();

            // Samples per key
            var fftSamplesPerKey = 10;

            // Width of the array
            var fftWidth = 10000;

            // Number of subdivisions in each sample group
            // Higher = more precise
            var precisionMultiplier = 5;

            // Wavelengths in each sample
            // Higher = more precise
            var wavelengthsPerSample = 20;

            var fftResultLen = 128 * fftSamplesPerKey;
            int len          = (int)(reader.Length / 2);
            var result       = new float[fftWidth, fftResultLen];

            var datafull = new float[(int)len];

            r.Read(datafull, 0, (int)len);
            r.Dispose();

            int       progress = 0;
            object    lck      = new object();
            Stopwatch s        = new Stopwatch();

            s.Start();
            Parallel.For(0, fftResultLen, i =>
            {
                float key       = i / (float)fftSamplesPerKey - 0.5f / fftSamplesPerKey;
                float freq      = (float)Math.Pow(2, (key - 69 + 9 - 7 * 3 + 12 * 1) / 12) * 440;
                int waveSize    = (int)(sampleRate / freq) * wavelengthsPerSample;
                double waveStep = (double)waveSize * fftWidth / len;
                waveStep       /= precisionMultiplier;
                if (waveStep < 1)
                {
                    waveStep = 1;
                }
                waveStep = waveStep / fftWidth * len;
                if (waveSize < 1000)
                {
                    waveSize = 1000;
                }
                for (double _l = 0; _l + waveSize < len; _l += waveStep)
                {
                    var l       = (int)_l;
                    float mult  = freq / sampleRate * (float)Math.PI * 2;
                    float sum_r = 0;
                    float sum_i = 0;
                    for (int j = 0; j < waveSize; j++)
                    {
                        float a = mult * j + (float)Math.PI;
                        sum_r  += (float)Math.Cos(a) * datafull[l + j];
                        sum_i  += (float)Math.Sin(a) * datafull[l + j];
                    }
                    var val   = (Math.Abs(sum_r) + Math.Abs(sum_i)) / waveSize;
                    int start = (int)((double)l * fftWidth / len);
                    int end   = (int)((double)(l + waveSize) * fftWidth / len);
                    for (int p = start; p <= end; p++)
                    {
                        result[p, i] = val;
                    }
                }
                lock (lck)
                {
                    Console.WriteLine("Processed frequency bands: " + (++progress));
                }
            });
            Console.WriteLine("Complete! Seconds spent: " + Math.Round(s.ElapsedMilliseconds / 1000.0, 1));

            return(result);
        }