Esempio n. 1
0
        public MyAudio(float soundEffectVolume)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo("music");
            int           length        = 0;

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (file.Name.Split('.')[1] == "ogg")
                {
                    ++length;
                    this.musicNames.Add(file.Name);
                }
            }
            this.musiclength = new int[2, length];
            try
            {
                this.waveOut           = new WaveOut(-1, 44100, 16, 2);
                this.xaDevice          = new SlimDX.XAudio2.XAudio2();
                this.xaMaster          = new MasteringVoice(this.xaDevice);
                this.SoundEffectVolume = soundEffectVolume;
            }
            catch
            {
                this.disabled = true;
            }
            this.thread_1 = new Thread(new ThreadStart(this.Init));
            this.thread_1.Start();

            this.pianoNotePlayer = new PianoNotePlayer();
        }
Esempio n. 2
0
        public Form1()
        {
            InitializeComponent();
            var mainThread  = Thread.CurrentThread;
            var audioThread = new Thread(() =>
            {
                var xaDev = new XA.XAudio2();
                xaDev.StartEngine();

                var wf = new WaveFormat
                {
                    FormatTag             = WaveFormatTag.Pcm,
                    Channels              = 2,
                    BitsPerSample         = 16,
                    BlockAlignment        = 4,
                    SamplesPerSecond      = SampleRate,
                    AverageBytesPerSecond = SampleRate * 4
                };

                // ReSharper disable once ObjectCreationAsStatement
                new XA.MasteringVoice(xaDev, 2, SampleRate, 0);
                var xaSv  = new XA.SourceVoice(xaDev, wf, XA.VoiceFlags.None);
                var xaBuf = new XA.AudioBuffer();

                var data = new byte[Samples * 4];
                ReadWave(data);
                xaBuf.AudioData  = new MemoryStream(data, false);
                xaBuf.AudioBytes = data.Length;
                xaSv.SubmitSourceBuffer(xaBuf);

                xaSv.BufferStart += delegate
                {
                    if (!mainThread.IsAlive)
                    {
                        return;
                    }
                    ReadWave(data);
                    xaBuf.AudioData.Position = 0;
                    xaSv.SubmitSourceBuffer(xaBuf);
                };

                xaSv.Start();
                while (mainThread.IsAlive)
                {
                    lock (tones)
                    {
                        MidiUpdate();
                    }
                    Thread.Sleep(0);
                }
                xaSv.Stop();
                xaSv.Dispose();
                xaDev.Dispose();
                midi.Dispose();
            });

            audioThread.Start();
        }
Esempio n. 3
0
 static XAudio2PlaybackEngine()
 {
     device = new AudioDevice();
     voice  = new MasteringVoice(device);
     AppDomain.CurrentDomain.ProcessExit += (o, args) =>
     {
         voice.Dispose();
         device.Dispose();
     };
 }
Esempio n. 4
0
 public GlobalDataPackage()
 {
     this.TextureObjectDictionary = new Dictionary <string, TextureObject>();
     try
     {
         this.DeviceXaudio2    = new SlimDX.XAudio2.XAudio2();
         this.MasteringVoice   = new MasteringVoice(this.DeviceXaudio2);
         this.SoundInitSuccess = true;
         this.BGM_Player       = new Wave_Player(this.DeviceXaudio2);
     }
     catch
     {
         int num = (int)MessageBox.Show("音频设备故障", "Sound Initial Error");
     }
 }
Esempio n. 5
0
        private void InitInstance()
        {
            lock (mutex)
            {
                bufferEndCallback = new EventHandler <ContextEventArgs>(streaming_BufferEnd);
                streamBuffers     = new List <AudioBuffer>(NumStreamingBuffers);
                byteBuffers       = new List <byte[]>(NumStreamingBuffers);
                bufferStatus      = new System.Collections.BitArray(NumStreamingBuffers, false);
                for (var i = 0; i < NumStreamingBuffers; i++)
                {
                    byte[] byteBuff = new byte[StreamingBufferSize];
                    byteBuffers.Add(byteBuff);
                    AudioBuffer audioBuff = new AudioBuffer();
                    audioBuff.AudioData = new SlimDX.DataStream(byteBuff, true, true);
                    streamBuffers.Add(audioBuff);
                }

                xaudio         = new XAudio2();
                masteringVoice = new MasteringVoice(xaudio);
            }
        }
Esempio n. 6
0
        public void Dispose()
        {
            lock (mutex)
            {
                DisposeWave();

                if (decodeThread != null)
                {
                    decodeThread.Abort();
                    System.Threading.Monitor.Pulse(mutex);
                    decodeThread = null;
                }

                if (streamBuffers != null)
                {
                    foreach (AudioBuffer buffer in streamBuffers)
                    {
                        buffer.AudioData.Dispose();
                        buffer.Dispose();
                    }
                    byteBuffers   = null;
                    streamBuffers = null;
                }

                if (masteringVoice != null)
                {
                    masteringVoice.Dispose();
                    masteringVoice = null;
                }

                if (xaudio != null)
                {
                    xaudio.Dispose();
                    xaudio = null;
                }
            }
        }
Esempio n. 7
0
        static void Main()
        {
            var form = new RenderForm("DotRocket/SlimDX example");

            var description = new SwapChainDescription()
            {
                BufferCount = 1,
                Usage = Usage.RenderTargetOutput,
                OutputHandle = form.Handle,
                IsWindowed = true,
                ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags = SwapChainFlags.AllowModeSwitch,
                SwapEffect = SwapEffect.Discard
            };

            // Setup rendering
            Device device;
            SwapChain swapChain;
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
            RenderTargetView renderTarget;
            using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
                renderTarget = new RenderTargetView(device, resource);
            var context = device.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
            context.OutputMerger.SetTargets(renderTarget);
            context.Rasterizer.SetViewports(viewport);

            // Prevent alt+enter (broken on WinForms)
            using (var factory = swapChain.GetParent<Factory>())
                factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);

            // Setup audio-streaming
            XAudio2 xaudio2 = new XAudio2();
            stream = new XWMAStream("tune.xwma");
            MasteringVoice masteringVoice = new MasteringVoice(xaudio2);
            sourceVoice = new SourceVoice(xaudio2, stream.Format);
            audioBuffer = new AudioBuffer();
            audioBuffer.AudioData = stream;
            audioBuffer.AudioBytes = (int)stream.Length;
            audioBuffer.Flags = BufferFlags.EndOfStream;
            sourceVoice.SubmitSourceBuffer(audioBuffer, stream.DecodedPacketsInfo);
            sourceVoice.Start();

            // Setup DotRocket
#if DEBUG
            DotRocket.Device rocket = new DotRocket.ClientDevice("sync");
            rocket.OnPause += Pause;
            rocket.OnSetRow += SetRow;
            rocket.OnIsPlaying += IsPlaying;
            rocket.Connect("localhost", 1338);
#else
            DotRocket.Device rocket = new DotRocket.PlayerDevice("sync");
#endif

            // Get our belowed tracks!
            DotRocket.Track clear_r = rocket.GetTrack("clear.r");
            DotRocket.Track clear_g = rocket.GetTrack("clear.g");
            DotRocket.Track clear_b = rocket.GetTrack("clear.b");

            MessagePump.Run(form, () =>
            {
                // Hammertime.
                double row = ((double)(sourceVoice.State.SamplesPlayed - samplesBias) / stream.Format.SamplesPerSecond) * rowRate;

                // Paint some stuff.
                rocket.Update((int)System.Math.Floor(row));
                context.ClearRenderTargetView(renderTarget, new Color4(
                    clear_r.GetValue(row),
                    clear_g.GetValue(row),
                    clear_b.GetValue(row)));
                swapChain.Present(0, PresentFlags.None);
            });

            // clean up all resources
            // anything we missed will show up in the debug output
            renderTarget.Dispose();
            swapChain.Dispose();
            device.Dispose();
        }
Esempio n. 8
0
        static void Main()
        {
            var form = new RenderForm("DotRocket/SlimDX example");

            var description = new SwapChainDescription()
            {
                BufferCount       = 1,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = form.Handle,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };

            // Setup rendering
            Device    device;
            SwapChain swapChain;

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
            RenderTargetView renderTarget;

            using (var resource = Resource.FromSwapChain <Texture2D>(swapChain, 0))
                renderTarget = new RenderTargetView(device, resource);
            var context  = device.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);

            context.OutputMerger.SetTargets(renderTarget);
            context.Rasterizer.SetViewports(viewport);

            // Prevent alt+enter (broken on WinForms)
            using (var factory = swapChain.GetParent <Factory>())
                factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);

            // Setup audio-streaming
            XAudio2 xaudio2 = new XAudio2();

            stream = new XWMAStream("tune.xwma");
            MasteringVoice masteringVoice = new MasteringVoice(xaudio2);

            sourceVoice            = new SourceVoice(xaudio2, stream.Format);
            audioBuffer            = new AudioBuffer();
            audioBuffer.AudioData  = stream;
            audioBuffer.AudioBytes = (int)stream.Length;
            audioBuffer.Flags      = BufferFlags.EndOfStream;
            sourceVoice.SubmitSourceBuffer(audioBuffer, stream.DecodedPacketsInfo);
            sourceVoice.Start();

            // Setup DotRocket
#if DEBUG
            DotRocket.Device rocket = new DotRocket.ClientDevice("sync");
            rocket.OnPause     += Pause;
            rocket.OnSetRow    += SetRow;
            rocket.OnIsPlaying += IsPlaying;
            rocket.Connect("localhost", 1338);
#else
            DotRocket.Device rocket = new DotRocket.PlayerDevice("sync");
#endif

            // Get our belowed tracks!
            DotRocket.Track clear_r = rocket.GetTrack("clear.r");
            DotRocket.Track clear_g = rocket.GetTrack("clear.g");
            DotRocket.Track clear_b = rocket.GetTrack("clear.b");

            MessagePump.Run(form, () =>
            {
                // Hammertime.
                double row = ((double)(sourceVoice.State.SamplesPlayed - samplesBias) / stream.Format.SamplesPerSecond) * rowRate;

                // Paint some stuff.
                rocket.Update((int)System.Math.Floor(row));
                context.ClearRenderTargetView(renderTarget, new Color4(
                                                  clear_r.GetValue(row),
                                                  clear_g.GetValue(row),
                                                  clear_b.GetValue(row)));
                swapChain.Present(0, PresentFlags.None);
            });

            // clean up all resources
            // anything we missed will show up in the debug output
            renderTarget.Dispose();
            swapChain.Dispose();
            device.Dispose();
        }
Esempio n. 9
0
 public XAudio2_Player(string fileName, SlimDX.XAudio2.XAudio2 device)
 {
     this.device      = device;
     this.loadSuccess = this.LoadSound(fileName);
 }
Esempio n. 10
0
        public void Dispose()
        {
            lock (mutex)
            {
                DisposeWave();

                if (decodeThread != null)
                {
                    decodeThread.Abort();
                    System.Threading.Monitor.Pulse(mutex);
                    decodeThread = null;
                }

                if (streamBuffers != null)
                {
                    foreach (AudioBuffer buffer in streamBuffers)
                    {
                        buffer.AudioData.Dispose();
                        buffer.Dispose();
                    }
                    byteBuffers = null;
                    streamBuffers = null;
                }

                if (masteringVoice != null)
                {
                    masteringVoice.Dispose();
                    masteringVoice = null;
                }

                if (xaudio != null)
                {
                    xaudio.Dispose();
                    xaudio = null;
                }
            }
        }
Esempio n. 11
0
        private void InitInstance()
        {
            lock (mutex)
            {
                bufferEndCallback = new EventHandler<ContextEventArgs>(streaming_BufferEnd);
                streamBuffers = new List<AudioBuffer>(NumStreamingBuffers);
                byteBuffers = new List<byte[]>(NumStreamingBuffers);
                bufferStatus = new System.Collections.BitArray(NumStreamingBuffers, false);
                for (var i = 0; i < NumStreamingBuffers; i++)
                {
                    byte[] byteBuff = new byte[StreamingBufferSize];
                    byteBuffers.Add(byteBuff);
                    AudioBuffer audioBuff = new AudioBuffer();
                    audioBuff.AudioData = new SlimDX.DataStream(byteBuff, true, true);
                    streamBuffers.Add(audioBuff);
                }

                xaudio = new XAudio2();
                masteringVoice = new MasteringVoice(xaudio);
            }
        }