• SlimDX的DirectSound模块


    网上SlimDX的资源很少,搜到了http://www.xukailun.me/article/238/这篇关于《SlimDX的DirectSound模块应用实战》的文章,备份下来以备不时之需。

    1.基本的播放功能

    void CreateDirectSound(Guid deviceId, IntPtr handle)
    {    
        // 音频设备的Id    
        _ds = new DirectSound(deviceId);                 
      // 需要使用窗口的句柄
        _ds.SetCooperativeLevel(handle, CooperativeLevel.Normal); 
    }             
    void Play(string file)
     {     
        SecondarySoundBuffer snd = null;     
        // 读取wav音频文件     
        using (WaveStream ws = new WaveStream(file))
         {         
            var desc = new SoundBufferDescription();
                desc.Format = ws.Format;         
            desc.Flags = BufferFlags.GlobalFocus | BufferFlags.ControlVolume | BufferFlags.ControlPositionNotify | BufferFlags.GetCurrentPosition2;
                desc.SizeInBytes = (int)ws.Length;                     
            // 为声音建立二级缓冲区          
            snd = new SecondarySoundBuffer(_ds, desc);         
            snd.Volume = -200;         
            byte[] buffer = new byte[desc.SizeInBytes];         
            ws.Read(buffer, 0, buffer.Length);         
            snd.Write<byte>(buffer, 0, LockFlags.None);     
        }     
        snd.Play(0, PlayFlags.None); 
    } 

    2.播放中的回调

    AutoResetEvent功能就类似于Java的wait/notify调用WaitOne方法相当于wait,使当前线程等待;调用Set方法相当于调用notify,通知线程继续执行

    class SoundUnit 
    {     
        public int Id { get; private set; }     
        public SoundBuffer Buffer { get; private set; }     
        public AutoResetEvent Event { get; private set; }                
        public SoundUnit(SoundBuffer buffer) 
        {         
                this.Id = ++i;         
                this.Buffer = buffer;         
                this.Event = new AutoResetEvent(false);     
        } 
    }            
        void Play(string file) 
        {     
            SecondarySoundBuffer snd = null;     
            SoundUnit su = null;                
            // 读取wav音频文件     
            using (WaveStream ws = new WaveStream(file)) 
            {         
                var desc = new SoundBufferDescription();         
                desc.Format = ws.Format;         
                desc.Flags = BufferFlags.GlobalFocus | BufferFlags.ControlVolume |             BufferFlags.ControlPositionNotify | BufferFlags.GetCurrentPosition2;         
                desc.SizeInBytes = (int)ws.Length;                    
                // 为声音建立二级缓冲区          
                snd = new SecondarySoundBuffer(_ds, desc);         
                snd.Volume = -200;         
                byte[] buffer = new byte[desc.SizeInBytes];         
                ws.Read(buffer, 0, buffer.Length);         
                snd.Write&amp;lt;byte&amp;gt;(buffer, 0, LockFlags.None);                    
                su = new SoundUnit(snd);         
                snd.SetNotificationPositions(new[]{             
                // 设置播放结束后回调             
                new NotificationPosition{ Event = su.Event, Offset = DSBPN_OFFSETSTOP }         });     
            }     
            snd.Play(0, PlayFlags.None);                    
            Console.WriteLine("播放开始:" + su.Id);     
            WaitForNotification(su); 
        }            
        void WaitForNotification(SoundUnit unit) 
        {     
            new Thread(() =>{         
            // 等待播放完成         
            unit.Event.WaitOne();        
             Console.WriteLine("播放结束:" + unit.Id);     }).Start(); 
        } 
  • 相关阅读:
    [BJOI2019] 删数
    [HNOI2019] 校园旅行
    [ARC093D] Dark Horse
    rpn模块
    通俗易懂谈强化学习
    np.random.choice()的用法
    深度学习模型量化
    np.array中的[::1]的一点理解
    如何增加深度学习模型的泛化能力(L1/L2正则化,dropout,数据增强等等)
    深度学习中的网络剪枝(pruning)简介
  • 原文地址:https://www.cnblogs.com/smartsensor/p/3998421.html
Copyright © 2020-2023  润新知