winform 制作播放器 频谱 显示分贝

  

一 、  制作winform 播放器 频谱

 

本人百度的分享 :http://jingyan.baidu.com/article/1e5468f9706d7e484961b704.html
</pre><pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//本文链接:https://blog.csdn.net/u010919083/article/details/52574907
namespace CoreAudioSample
{
    public partial class FrmExample : Form
    {
        private const int NumLEDS = 15;
        public FrmExample()
        {
            InitializeComponent();

           // this.btnShowFalloff.Checked = this.peakMeterCtrl2.FalloffEffect;
           // this.btnShowGrid.Checked = this.peakMeterCtrl2.ShowGrid;
           // this.btnColoredGrid.Checked = this.peakMeterCtrl2.ColoredGrid;
           // this.btnColoredGrid.Enabled = this.btnShowGrid.Checked;

        }

        private void FrmExample_Load(object sender, EventArgs e)
        {

        }

        private void timerGen_Tick(object sender, EventArgs e)
        {


            int[] meters1 = new int[NumLEDS];
            int[] meters2 = new int[NumLEDS];
            Random rand = new Random();
            //for (int i = 0; i < meters1.Length; i++)
            //{
            //    meters1[i] = rand.Next(0, 100);
            //    meters2[i] = rand.Next(0, 100);
            //}

            if (masterPeak2.pkMasterVal != 0 && masterPeak2.pkMasterVal>8)
            {
                int f = 7;
                int g = 0;
                for (int i = 0; i < meters1.Length; i++)
                {
                    if (i < 8)
                    {
                        meters1[i] = rand.Next(0, masterPeak2.pkMasterVal -f);
                        f--;
                    }
                    else {
                        meters1[i] = rand.Next(0, masterPeak2.pkMasterVal - g);
                        g++;
                    }

                }
            }
            if (masterPeak2.pkMasterVal < 8)
            {
                 for (int i = 0; i < meters1.Length; i++)
                 {
                     meters1[i] = 0;
                 }
                 return;
            }
          


            resf(meters1, NumLEDS);
            this.peakMeterCtrl2.SetData(meters1, 0, meters1.Length);
        
        }


        private void resf(int[] arr, int NumEDS)
        {
            if (richTextBox1.Text.Length > 2000)
            {
                richTextBox1.Text = "";
            }
            string str = "";
            for (int i = 0; i < arr.Length; i++)
            {
                str += arr[i].ToString() + ",";
            }
            if (masterPeak2.pkLeftVal != 0)
            str += "(" + masterPeak2.pkLeftVal + ")" + "\n";
         
            richTextBox1.Text += str;
            richTextBox1.AppendText(str);
            richTextBox1.Focus();
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnShowFalloff_CheckedChanged_1(object sender, EventArgs e)
        {
            this.peakMeterCtrl2.FalloffEffect = this.btnShowFalloff.Checked;
            //    this.peakMeterCtrl2.FalloffEffect = this.btnShowFalloff.Checked;
            if (this.btnShowFalloff.Checked)
            {
                this.peakMeterCtrl2.Start(1000 / 20); // 20fps
            }
            else
            {
                this.peakMeterCtrl2.Stop();
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.peakMeterCtrl2.ShowGrid = this.btnShowGrid.Checked;
            //    this.peakMeterCtrl2.ShowGrid = this.btnShowGrid.Checked;
            this.btnColoredGrid.Enabled = this.btnShowGrid.Checked;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            this.peakMeterCtrl2.ColoredGrid = this.btnColoredGrid.Checked;
        }

        private void checkBox4_CheckedChanged_1(object sender, EventArgs e)
        {

            this.peakMeterCtrl2.LEDCount = this.btnSmooth.Checked ? 1 : NumLEDS;
        }
    }
}

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CoreAudioApi;

namespace CoreAudioSample.Contro
{
    public partial class MasterPeak : UserControl
    {
        private MMDevice device;
        public int pkMasterVal=0;
        public int pkLeftVal=0;
        public int pkRightVal=0;
        
        public MasterPeak()
        {
            InitializeComponent();
            MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
            device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
            tbMaster.Value = (int)(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
            device.AudioEndpointVolume.OnVolumeNotification += new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);
            timer1.Enabled = true;
         
        }

        private void MasterPeak_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pkMaster.Value = (int)(device.AudioMeterInformation.MasterPeakValue * 100);
            pkLeft.Value = (int)(device.AudioMeterInformation.PeakValues[0] * 100);
            pkRight.Value = (int)(device.AudioMeterInformation.PeakValues[1] * 100);


            pkMasterVal = pkMaster.Value;
            pkLeftVal=pkLeft.Value;
            pkRightVal = pkRight.Value;
        }

        private void tbMaster_Scroll(object sender, EventArgs e)
        {
            device.AudioEndpointVolume.MasterVolumeLevelScalar = ((float)tbMaster.Value / 100.0f);
        }
        void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
        {
            if (this.InvokeRequired)
            {
                object[] Params = new object[1];
                Params[0] = data;
                this.Invoke(new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification), Params);
            }
            else
            {
                tbMaster.Value = (int)(data.MasterVolume * 100);
            }
        }
    }
}

 

 

 

 

 

 

 

 

                                                        /*
 

/*
  LICENSE
  -------
  Copyright (C) 2007-2010 Ray Molenkamp

  This source code is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this source code or the software it produces.

  Permission is granted to anyone to use this source code for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this source code must not be misrepresented; you must not
     claim that you wrote the original source code.  If you use this source code
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original source code.
  3. This notice may not be removed or altered from any source distribution.
*/

using System;
using System.Collections.Generic;
using System.Text;
using CoreAudioApi.Interfaces;
using System.Runtime.InteropServices;

namespace CoreAudioApi
{

    public class AudioEndpointVolume : IDisposable
    {
        private IAudioEndpointVolume _AudioEndPointVolume;
        private AudioEndpointVolumeChannels _Channels;
        private AudioEndpointVolumeStepInformation _StepInformation;
        private AudioEndPointVolumeVolumeRange _VolumeRange;
        private EEndpointHardwareSupport _HardwareSupport;
        private AudioEndpointVolumeCallback _CallBack;
        public  event AudioEndpointVolumeNotificationDelegate OnVolumeNotification;

        public AudioEndPointVolumeVolumeRange VolumeRange
        {
            get
            {
                return _VolumeRange;
            }
        }
        public EEndpointHardwareSupport HardwareSupport
        {
            get
            {
                return _HardwareSupport;
            }
        }
        public AudioEndpointVolumeStepInformation StepInformation
        {
            get
            {
                return _StepInformation;
            }
        }
        public AudioEndpointVolumeChannels Channels
        {
            get
            {
                return _Channels;
            }
        }
        public float MasterVolumeLevel
        {
            get
            {
                float result;
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.GetMasterVolumeLevel(out result));
                return result;
            }
            set
            {
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.SetMasterVolumeLevel(value, Guid.Empty));
            }
        }
        public float MasterVolumeLevelScalar
        {
            get
            {
                float result;
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.GetMasterVolumeLevelScalar(out result));
                return result;
            }
            set
            {
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.SetMasterVolumeLevelScalar(value, Guid.Empty));
            }
        }
        public bool Mute
        {
            get
            {
                bool result;
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.GetMute(out result));
                return result;
            }
            set
            {
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.SetMute(value, Guid.Empty));
            }
        }
        public void VolumeStepUp()
        {
            Marshal.ThrowExceptionForHR(_AudioEndPointVolume.VolumeStepUp(Guid.Empty));
        }
        public void VolumeStepDown()
        {
            Marshal.ThrowExceptionForHR(_AudioEndPointVolume.VolumeStepDown(Guid.Empty));
        }
        internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
        {
            uint HardwareSupp;

            _AudioEndPointVolume = realEndpointVolume;
            _Channels = new AudioEndpointVolumeChannels(_AudioEndPointVolume);
            _StepInformation = new AudioEndpointVolumeStepInformation(_AudioEndPointVolume);
            Marshal.ThrowExceptionForHR(_AudioEndPointVolume.QueryHardwareSupport(out HardwareSupp));
            _HardwareSupport = (EEndpointHardwareSupport)HardwareSupp;
            _VolumeRange = new AudioEndPointVolumeVolumeRange(_AudioEndPointVolume);
            _CallBack = new AudioEndpointVolumeCallback(this);
            Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify( _CallBack));
        }
        internal void FireNotification(AudioVolumeNotificationData NotificationData)
        {
            AudioEndpointVolumeNotificationDelegate del = OnVolumeNotification;
            if (del != null)
            {
                del(NotificationData);
            }
        }
        #region IDisposable Members

        public void Dispose()
        {
            if (_CallBack != null)
            {
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.UnregisterControlChangeNotify( _CallBack ));
                _CallBack = null;
            }
        }

        ~AudioEndpointVolume()
        {
            Dispose();
        }

        #endregion
       
    }
}


/*
  LICENSE
  -------
  Copyright (C) 2007-2010 Ray Molenkamp


  This source code is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this source code or the software it produces.


  Permission is granted to anyone to use this source code for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:


  1. The origin of this source code must not be misrepresented; you must not
     claim that you wrote the original source code.  If you use this source code
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original source code.
  3. This notice may not be removed or altered from any source distribution.
*/


using System;
using System.Collections.Generic;
using System.Text;
using CoreAudioApi.Interfaces;
using System.Runtime.InteropServices;


namespace CoreAudioApi
{
    // This class implements the IAudioEndpointVolumeCallback interface,
    // it is implemented in this class because implementing it on AudioEndpointVolume 
    // (where the functionality is really wanted, would cause the OnNotify function 
    // to show up in the public API. 
    internal class AudioEndpointVolumeCallback : IAudioEndpointVolumeCallback    
    {
        private AudioEndpointVolume _Parent;
        
        internal AudioEndpointVolumeCallback(AudioEndpointVolume parent)
        {
            _Parent = parent;
        }
        
        [PreserveSig] public int OnNotify(IntPtr NotifyData)
        {
            //Since AUDIO_VOLUME_NOTIFICATION_DATA is dynamic in length based on the
            //number of audio channels available we cannot just call PtrToStructure 
            //to get all data, thats why it is split up into two steps, first the static
            //data is marshalled into the data structure, then with some IntPtr math the
            //remaining floats are read from memory.
            //
            AUDIO_VOLUME_NOTIFICATION_DATA data = (AUDIO_VOLUME_NOTIFICATION_DATA) Marshal.PtrToStructure(NotifyData, typeof(AUDIO_VOLUME_NOTIFICATION_DATA));
            
            //Determine offset in structure of the first float
            IntPtr Offset = Marshal.OffsetOf(typeof(AUDIO_VOLUME_NOTIFICATION_DATA),"ChannelVolume");
            //Determine offset in memory of the first float
            IntPtr FirstFloatPtr = (IntPtr)((long)NotifyData + (long)Offset);


            float[] voldata = new float[data.nChannels];
            
            //Read all floats from memory.
            for (int i = 0; i < data.nChannels; i++)
            {
                voldata[i] = (float)Marshal.PtrToStructure(FirstFloatPtr, typeof(float));
            }


            //Create combined structure and Fire Event in parent class.
            AudioVolumeNotificationData NotificationData = new AudioVolumeNotificationData(data.guidEventContext, data.bMuted, data.fMasterVolume, voldata);
            _Parent.FireNotification(NotificationData);
            return 0; //S_OK
        }
    }
}
                        

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霍先生的虚拟宇宙网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值