Пример #1
0
            private void OpenWaveFile(out int Sample_Freq, out double[] SignalETC)
            {
                NAudio.Wave.WaveFileReader WP = new NAudio.Wave.WaveFileReader(Signal_Status.Text);

                int BytesPerSample  = WP.WaveFormat.Channels * WP.WaveFormat.BitsPerSample / 8;
                int BytesPerChannel = WP.WaveFormat.BitsPerSample / 8;

                byte[] signalbuffer = new byte[BytesPerSample];
                int    ChannelCt    = WP.WaveFormat.Channels;

                Sample_Freq = WP.WaveFormat.SampleRate;
                double[] SignalInt = new double[WP.SampleCount];

                if (WP.WaveFormat.BitsPerSample == 8)
                {
                    System.Windows.Forms.MessageBox.Show("Selected File is an 8-Bit audio file. This program requires a minimum bit-depth of 16.");
                    SignalETC = new double[SignalInt.Length];
                    return;
                }
                byte[] temp = new byte[4];
                int    c    = (int)DryChannel.Value - 1;

                for (int i = 0; i < WP.SampleCount; i++)
                {
                    WP.Read(signalbuffer, 0, BytesPerSample);

                    if (WP.WaveFormat.BitsPerSample == 32)
                    {
                        SignalInt[i] = BitConverter.ToInt32(signalbuffer, c * 4);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 24)
                    {
                        temp[1]      = signalbuffer[c * BytesPerChannel];
                        temp[2]      = signalbuffer[c * BytesPerChannel + 1];
                        temp[3]      = signalbuffer[c * BytesPerChannel + 2];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 16)
                    {
                        temp[2]      = signalbuffer[c * BytesPerChannel];
                        temp[3]      = signalbuffer[c * BytesPerChannel + 1];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                }

                SignalETC = new double[SignalInt.Length];

                double Max = double.NegativeInfinity;

                for (int i = 0; i < SignalInt.Length; i++)
                {
                    Max = Math.Max(Max, Math.Abs(SignalInt[i]));
                }

                for (int i = 0; i < SignalInt.Length; i++)
                {
                    SignalETC[i] = SignalInt[i] / Max;
                }
                SignalETC = SignalInt;
            }
        /// <summary>
        /// Reads the wave file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        double[] ReadWaveFile(string filePath)
        {
            byte[]   buffer      = new byte[4];
            double[] dataStorage = new double[signalLength];
            long     read        = 0;
            long     position    = 0;

            NAudio.Wave.WaveFileReader waveReader = new NAudio.Wave.WaveFileReader(filePath);

            while (waveReader.Position < dataStorage.Length * 2)
            {
                read = waveReader.Read(buffer, 0, 4);

                for (int i = 0; i < read / 2; i += 1)
                {
                    dataStorage[position] = BitConverter.ToInt16(buffer, i * 2);
                    position++;
                }
            }

            return(dataStorage);
        }
Пример #3
0
        private async static void PlaySoundWav(MessageEventArgs e)
        {
            if (e.Message.Text.Length > "-play ".Length && voiceclient != null)
            {
                string file = e.Message.Text.Substring("-play ".Length);
                Console.WriteLine("Trying to play: " + file);
                try {
                    var ws = new NAudio.Wave.WaveFileReader(file);
                    byte[] buf;
                    if (ws.WaveFormat.Channels > 1)
                    {
                        var tomono = new NAudio.Wave.StereoToMonoProvider16(ws);
                        tomono.RightVolume = 0.5f;
                        tomono.LeftVolume = 0.5f;
                        buf = new byte[ws.Length];
                        while (ws.HasData(ws.WaveFormat.AverageBytesPerSecond))
                        {
                            tomono.Read(buf, 0, ws.WaveFormat.AverageBytesPerSecond);
                            voiceclient.SendVoicePCM(buf, buf.Length);
                        }
                    }
                    else
                    {
                        buf = new byte[ws.Length];
                        ws.Read(buf, 0, buf.Length);
                        voiceclient.SendVoicePCM(buf, buf.Length);
                    }
                    ws.Close();
                }
                catch(Exception)
                {
                    Console.WriteLine("File not found or incompatible.");
                }

            }
        }
Пример #4
0
            private void OpenWaveFile(out int Sample_Freq, out double[] SignalETC)
            {
                NAudio.Wave.WaveFileReader WP = new NAudio.Wave.WaveFileReader(Signal_Status.Text);

                int BytesPerSample  = WP.WaveFormat.Channels * WP.WaveFormat.BitsPerSample / 8;
                int BytesPerChannel = WP.WaveFormat.BitsPerSample / 8;

                byte[] signalbuffer = new byte[BytesPerSample];
                int    ChannelCt    = WP.WaveFormat.Channels;

                Sample_Freq = WP.WaveFormat.SampleRate;
                double[] SignalInt = new double[WP.SampleCount];

                if (WP.WaveFormat.BitsPerSample == 8)
                {
                    System.Windows.Forms.MessageBox.Show("Selected File is an 8-Bit audio file. This program requires a minimum bit-depth of 16.");
                    SignalETC = new double[SignalInt.Length];
                    return;
                }
                byte[] temp = new byte[4];
                int    c    = (int)DryChannel.Value - 1;

                //double Max;

                //switch (WP.WaveFormat.BitsPerSample)
                //{
                //    case 32:
                //        Max = Int32.MaxValue;
                //        break;
                //    case 24:
                //        Max = BitConverter.ToInt32(new byte[] { 0, byte.MaxValue, byte.MaxValue, byte.MaxValue }, 0);
                //        break;
                //    case 16:
                //        Max = Int16.MaxValue;
                //        break;
                //    case 8:
                //        Max = BitConverter.ToInt16(new byte[] { 0, byte.MaxValue }, 0);
                //        break;
                //    default:
                //        throw new Exception("Invalid bit depth variable... Where did you get this audio file again?");
                //}

                for (int i = 0; i < WP.SampleCount; i++)
                {
                    WP.Read(signalbuffer, 0, BytesPerSample);

                    if (WP.WaveFormat.BitsPerSample == 32)
                    {
                        SignalInt[i] = BitConverter.ToInt32(signalbuffer, c * 4);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 24)
                    {
                        temp[1]      = signalbuffer[c * BytesPerChannel];
                        temp[2]      = signalbuffer[c * BytesPerChannel + 1];
                        temp[3]      = signalbuffer[c * BytesPerChannel + 2];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 16)
                    {
                        temp[2]      = signalbuffer[c * BytesPerChannel];
                        temp[3]      = signalbuffer[c * BytesPerChannel + 1];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                }

                SignalETC = new double[SignalInt.Length];

                double Max = double.NegativeInfinity;

                for (int i = 0; i < SignalInt.Length; i++)
                {
                    Max = Math.Max(Max, Math.Abs(SignalInt[i]));
                }

                for (int i = 0; i < SignalInt.Length; i++)
                {
                    SignalETC[i] = SignalInt[i] / Max;
                }
                SignalETC = SignalInt;
            }
            private void OpenWaveFile(out int Sample_Freq, out double[] SignalETC)
            {
                NAudio.Wave.WaveFileReader WP = new NAudio.Wave.WaveFileReader(Signal_Status.Text);

                int BytesPerSample = WP.WaveFormat.Channels * WP.WaveFormat.BitsPerSample / 8;
                int BytesPerChannel = WP.WaveFormat.BitsPerSample / 8;
                byte[] signalbuffer = new byte[BytesPerSample];
                int ChannelCt = WP.WaveFormat.Channels;
                Sample_Freq = WP.WaveFormat.SampleRate;
                double[] SignalInt = new double[WP.SampleCount]; //[Sample]

                if (WP.WaveFormat.BitsPerSample == 8)
                {
                    System.Windows.Forms.MessageBox.Show("Selected File is an 8-Bit audio file. This program requires a minimum bit-depth of 16.");
                    SignalETC = new double[SignalInt.Length];
                    return;
                }
                byte[] temp = new byte[4];
                int c = (int)DryChannel.Value - 1;
                double Max;

                switch (WP.WaveFormat.BitsPerSample)
                {
                    case 32:
                        Max = Int32.MaxValue;
                        break;
                    case 24:
                        Max = BitConverter.ToInt32(new byte[] { 0, byte.MaxValue, byte.MaxValue, byte.MaxValue }, 0);
                        break;
                    case 16:
                        Max = Int16.MaxValue;
                        break;
                    case 8:
                        Max = BitConverter.ToInt16(new byte[] {0, byte.MaxValue}, 0);
                        break;
                    default:
                        throw new Exception("Invalid bit depth variable... Where did you get this audio file again?");
                }

                for (int i = 0; i < WP.SampleCount; i++)//Have we chosen the right property to get the number of bytes in the file?
                {
                    WP.Read(signalbuffer, 0, BytesPerSample);

                    if (WP.WaveFormat.BitsPerSample == 32)
                    {
                        SignalInt[i] = BitConverter.ToInt32(signalbuffer, c * 4);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 24)
                    {
                        temp[1] = signalbuffer[c * BytesPerChannel];
                        temp[2] = signalbuffer[c * BytesPerChannel + 1];
                        temp[3] = signalbuffer[c * BytesPerChannel + 2];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 16)
                    {
                        temp[2] = signalbuffer[c * BytesPerChannel];
                        temp[3] = signalbuffer[c * BytesPerChannel + 1];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                }

                SignalETC = SignalInt;
            }
        // Calcula FFT e plota o gráfico
        public void analise()
        {
            // Lê o arquivo .wav que está em reader e transfere para o buffer
            byte[] buffer = new byte[reader.Length];
            reader.Read(buffer, 0, buffer.Length);
            // Cria o vetor de amostras
            sample = new short[reader.Length];

            // Faz a conversão de buffer de byte para inteiro de 16bit(short) e armazena
            // no vetor de amostras.
            // Observar que grava em amostra a cada 2 saltos de posição.
            for (int n = 0; n < buffer.Length; n += 2)
            {
                sample[n] = BitConverter.ToInt16(buffer, n);
            }
            // Plota o grafico deste vetor de amostra em função do tempo
            if (cfg_dg.checkBox1.Checked)
            {
                graph_t_amostrado(sample);
            }

            // Faz a adaptação de inteiro para complexo. Ex 15 => 15 + 0i
            // E armazena em um vetor complexo que servirá de entrada
            // para a FFT.
            Complex[] complexData = new Complex[sample.Length];
            for (int i = 0; i < complexData.Length; i++)
            {
                Complex tmp = new Complex(sample[i], 0);
                complexData[i] = tmp;
            }
            //MessageBox.Show("Arquivo: " + reader.Length + "\nAmostras: " + sample.Length);
            //Pega a offset+N(cfg_dg.nzao)-1 amostras que é definida pelo usuario
            // atraves das configurações. Obrigatoriamente potencia de 2.
            tmpComplexArray = new Complex[cfg_dg.nzao];
            for (int i = cfg_dg.offset, j = 0; i < cfg_dg.nzao; i++, j++)
            {
                Complex a = new Complex(complexData[i]);
                tmpComplexArray[j] = a;
            }
            //run FFT - Verificar FFT_Aforge.cs para codigo do FFT
            FourierTransform.FFT(tmpComplexArray, FourierTransform.Direction.Forward);

            // Plota graficos de saida da FFT
            // Pega-se a magnitude do num complexo e calcula 20log10(Mag) para obter ganho
            // em dB.
            // Para a frequencia captura-se a frequencia de amostragem (Supor FA = 44100 Hz)
            // do arquivo.wav e captura o valor de N (aquele que tem que ser potencia de 2).
            // Supondo que N = 1024 (ou 2^10), então os valores da frequencia em Hz são
            // calculados da seguinte maneira:
            // i * FA/N
            // 0 * 44100/1024 = 0Hz
            // 1 * 44100/1024 = 43.1Hz
            // .
            // .
            // 511 * 44100/1024 = 22006.9 Hz
            // 512 * 44100/1024 = 22050 Hz  Particularidade: Representa a frequencia de Nyquist (FA/2)
            // .
            // 1023 * 44100/1024 = 44056,9 Hz Ultimo.
            double freq_para_hertz;
            double mag_para_dB;

            for (int i = 0; i < tmpComplexArray.Length; i++)
            {
                freq_para_hertz = ((double)i * (double)_reader_freq / (double)cfg_dg.nzao);
                mag_para_dB     = 20 * Math.Log10(tmpComplexArray[i].Magnitude);

                if (cfg_dg.rb_Bar.Checked)
                {
                    chart1.Series["Series1"].Points.AddXY(freq_para_hertz, mag_para_dB);
                }
                if (cfg_dg.rb_Pon.Checked)
                {
                    chart1.Series["Series2"].Points.AddXY(freq_para_hertz, mag_para_dB);
                }
                if (cfg_dg.rb_PR.Checked)
                {
                    chart1.Series["Series2"].Points.AddXY(freq_para_hertz, mag_para_dB);
                    chart1.Series["Series3"].Points.AddXY(freq_para_hertz, mag_para_dB);
                }
                if (cfg_dg.rb_PS.Checked)
                {
                    chart1.Series["Series2"].Points.AddXY(freq_para_hertz, mag_para_dB);
                    chart1.Series["Series4"].Points.AddXY(freq_para_hertz, mag_para_dB);
                }
            }
            //
        }
            private void OpenWaveFile(out int Sample_Freq, out double[] SignalETC)
            {
                NAudio.Wave.WaveFileReader WP = new NAudio.Wave.WaveFileReader(Signal_Status.Text);

                int BytesPerSample = WP.WaveFormat.Channels * WP.WaveFormat.BitsPerSample / 8;
                int BytesPerChannel = WP.WaveFormat.BitsPerSample / 8;
                byte[] signalbuffer = new byte[BytesPerSample];
                int ChannelCt = WP.WaveFormat.Channels;
                Sample_Freq = WP.WaveFormat.SampleRate;
                double[] SignalInt = new double[WP.SampleCount];

                if (WP.WaveFormat.BitsPerSample == 8)
                {
                    System.Windows.Forms.MessageBox.Show("Selected File is an 8-Bit audio file. This program requires a minimum bit-depth of 16.");
                    SignalETC = new double[SignalInt.Length];
                    return;
                }
                byte[] temp = new byte[4];
                int c = (int)DryChannel.Value - 1;
                //double Max;

                //switch (WP.WaveFormat.BitsPerSample)
                //{
                //    case 32:
                //        Max = Int32.MaxValue;
                //        break;
                //    case 24:
                //        Max = BitConverter.ToInt32(new byte[] { 0, byte.MaxValue, byte.MaxValue, byte.MaxValue }, 0);
                //        break;
                //    case 16:
                //        Max = Int16.MaxValue;
                //        break;
                //    case 8:
                //        Max = BitConverter.ToInt16(new byte[] { 0, byte.MaxValue }, 0);
                //        break;
                //    default:
                //        throw new Exception("Invalid bit depth variable... Where did you get this audio file again?");
                //}

                for (int i = 0; i < WP.SampleCount; i++)
                {
                    WP.Read(signalbuffer, 0, BytesPerSample);

                    if (WP.WaveFormat.BitsPerSample == 32)
                    {
                        SignalInt[i] = BitConverter.ToInt32(signalbuffer, c * 4);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 24)
                    {
                        temp[1] = signalbuffer[c * BytesPerChannel];
                        temp[2] = signalbuffer[c * BytesPerChannel + 1];
                        temp[3] = signalbuffer[c * BytesPerChannel + 2];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                    else if (WP.WaveFormat.BitsPerSample == 16)
                    {
                        temp[2] = signalbuffer[c * BytesPerChannel];
                        temp[3] = signalbuffer[c * BytesPerChannel + 1];
                        SignalInt[i] = BitConverter.ToInt32(temp, 0);
                    }
                }

                SignalETC = new double[SignalInt.Length];

                double Max = double.NegativeInfinity;
                for(int i = 0; i < SignalInt.Length; i++)
                {
                    Max = Math.Max(Max, Math.Abs(SignalInt[i]));
                }

                for(int i = 0; i < SignalInt.Length; i++)
                {
                    SignalETC[i] = SignalInt[i] / Max;
                }
                SignalETC = SignalInt;

            }