private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     if (_writer != null)
     {
         _writer.Write(e.Buffer, 0, e.BytesRecorded);
     }
 }
예제 #2
0
        /// <summary>
        /// バッファリング
        /// </summary>
        /// <example>
        /// バイトから浮動少数に変換するのに16bitが楽なので、ビットレートは16bit固定の仕様とする.
        /// </example>
        private void OnDataAvailable(object s, NAudio.Wave.WaveInEventArgs e)
        {
            // 録音バッファ長さが0の場合は返却
            if (e.BytesRecorded == 0)
            {
                return;
            }


            // ビットレートは16bitを仕様とする.
            if (this.Bits != 16)
            {
                throw new ArgumentException($"ビットレートは16bitにしてください. {Bits}bitになっています.");
            }

            // 総サンプル数を計算
            int nSample = e.BytesRecorded / 2; // 1サンプル 2バイト

            // 整数から符号付浮動小数点に変換
            for (int i = 0; i < nSample; i++)
            {
                // 16bit用の演算
                short vShort = BitConverter.ToInt16(e.Buffer, 2 * i); // 2バイト限定
                float vFloat = (float)vShort / short.MaxValue;
                Buffer[i] = vFloat;
            }
        }
예제 #3
0
파일: Form1.cs 프로젝트: Tomazis/DiscordBot
        void wi_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            try
            {
                byte[] audioData = new byte[e.Buffer.Length];
                Buffer.BlockCopy(e.Buffer, 0, audioData, 0, e.Buffer.Length);

                // use the ipaddress as in the server program



                stm_s.Write(audioData, 0, audioData.Length);

                //byte[] bb = new byte[100];
                //int k = stm.Read(bb, 0, 100);
                //String answer = "";
                //for (int i = 0; i < k; i++)
                //    answer += Convert.ToChar(bb[i]);
            }
            catch (Exception ex)
            {
                logbox.AppendText(ex.Message, Color.Red);
                crash++;
                wavein.StopRecording();
                stm_s.Close();
                tcpclnt_sound.Close();
            }
        }
예제 #4
0
 void capture_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     if (recording)
     {
         waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
     }
 }
예제 #5
0
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            int bytesPerSample  = wvin.WaveFormat.BitsPerSample / 8;
            int samplesRecorded = args.BytesRecorded / bytesPerSample;

            if (dataPcm == null)
            {
                dataPcm = new Int16[samplesRecorded];
            }
            for (int i = 0; i < samplesRecorded; i++)
            {
                dataPcm[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
            }

            /*
             * int sampleRate = 44100;
             * short[] buffer = new short[2205];
             * double amplitude = 0.25 * short.MaxValue;
             * double frequency = 1000;
             * for (int n = 0; n < buffer.Length; n++)
             * {
             *  dataPcm[n] = (short)(amplitude * Math.Sin((2 * Math.PI * n * frequency) / sampleRate));
             * }
             */
        }
예제 #6
0
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            int bytesPerSample  = wvin.WaveFormat.BitsPerSample / 8;
            int samplesRecorded = args.BytesRecorded / bytesPerSample;

            Int16[] lastBuffer = new Int16[samplesRecorded];
            for (int i = 0; i < samplesRecorded; i++)
            {
                lastBuffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
            }
            int    lastBufferAmplitude = lastBuffer.Max() - lastBuffer.Min();
            double amplitude           = (double)lastBufferAmplitude / Math.Pow(2, wvin.WaveFormat.BitsPerSample);

            if (amplitude > peakAmplitudeSeen)
            {
                peakAmplitudeSeen = amplitude;
            }
            amplitude    = amplitude / peakAmplitudeSeen * 100;
            buffersRead += 1;

            // TODO: make this sane
            ScottPlot.PlottableAxLine axLine = (ScottPlot.PlottableAxLine)scottPlotUC1.plt.GetPlottables()[1];
            axLine.position = (buffersRead % amplitudes.Length) * 20.0 / 1000.0;

            Console.WriteLine(string.Format("Buffer {0:000} amplitude: {1:00.00}%", buffersRead, amplitude));
            PlotAddPoint(amplitude);
        }
        //what happens when the sourcestream receives data
        private void SourceStream_DataAvailuable(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            if (waveWriter == null) return; //exit method if wavewriter hasnt been set up yet

            waveWriter.Write(e.Buffer, 0, e.BytesRecorded); //write the contents to the savefile using the wavewriter
            waveWriter.Flush(); //flush the wavewriters contents after
        }
예제 #8
0
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            int bytesPerSample = wvin.WaveFormat.BitsPerSample / 8;

            float[] buffer = new float[args.BytesRecorded / bytesPerSample];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
            }
            try
            {
                if (waterfall)
                {
                    spec.AddScroll(buffer, fixedSize: pictureBox1.Height);
                }
                else
                {
                    spec.AddCircular(buffer, fixedSize: pictureBox1.Width);
                }
                renderNeeded = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("EXCEPTION: " + ex);
            }
        }
예제 #9
0
 //Event args to check if Data is available
 private void wavSource_DataAvail(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     if (wavFile != null)
     {
         wavFile.Write(e.Buffer, 0, e.BytesRecorded);
         wavFile.Flush();
     }
 }
예제 #10
0
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            // convert from a 16-bit byte array to a double array
            int bytesPerValue  = BITRATE / 8;
            int valuesInBuffer = args.BytesRecorded / bytesPerValue;

            bufferValues = new double[valuesInBuffer];
            for (int i = 0; i < valuesInBuffer; i++)
            {
                bufferValues[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerValue) * signalMultiple;
            }
            double moyennePuiss = 0;

            foreach (var item in bufferValues)
            {
                moyennePuiss += item;
            }
            // determine if a heartbeat occured

            int j = 0;

            while (j < bufferValues.Length)
            {
                if (bufferValues[j] > beatThreshold)
                {
                    int beatSampleNumber = j + buffersCaptured * valuesInBuffer;
                    beatTimeSec = (double)beatSampleNumber / SAMPLERATE;
                    BeatDetected(beatTimeSec);
                    break;
                }
                j++;
            }

            // create the values buffer if it does not exist
            if (values == null)
            {
                int idealSampleCount = STORESECONDS * SAMPLERATE;
                int bufferCount      = idealSampleCount / valuesInBuffer;
                values = new double[bufferCount * valuesInBuffer];
                times  = new double[bufferCount * valuesInBuffer];
                for (int i = 0; i < times.Length; i++)
                {
                    times[i] = (double)i / SAMPLERATE;
                }
            }

            // copy these data into the correct place of the larger buffer
            Array.Copy(bufferValues, 0, values, bufferIndex * valuesInBuffer, bufferValues.Length);
            lastPointUpdated = bufferIndex * valuesInBuffer + bufferValues.Length;

            // update counts
            buffersCaptured += 1;
            bufferIndex     += 1;
            if (bufferIndex * valuesInBuffer > values.Length - 1)
            {
                bufferIndex = 0;
            }
        }
예제 #11
0
        private void callHelper_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            string selectedContact = this.GetSelectedContact();

            if (selectedContact == string.Empty)
            {
                return;
            }
        }
예제 #12
0
파일: Recorder.cs 프로젝트: lex4all/lex4all
 /// <summary>
 /// input stream event for recording
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void waveIn_DataAvailableRecording(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     // if sound is recorded, it is written to file
     if (waveFile != null)
     {
         waveFile.Write(e.Buffer, 0, e.BytesRecorded);
         waveFile.Flush();
     }
 }
예제 #13
0
 private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     if (waveWriter == null)
     {
         return;
     }
     waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
     waveWriter.Flush();
 }
예제 #14
0
        private void SourceStreamDataAvailableEvent(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            if (m_oWaveWriter == null)
            {
                return;
            }

            m_oWaveWriter.Write(e.Buffer, 0, e.BytesRecorded);
            m_oWaveWriter.Flush();
        }
예제 #15
0
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            int bytesPerSample  = wvin.WaveFormat.BitsPerSample / 8;
            int samplesRecorded = args.BytesRecorded / bytesPerSample;

            if (lastBuffer is null || lastBuffer.Length != samplesRecorded)
            {
                lastBuffer = new double[samplesRecorded];
            }
            for (int i = 0; i < samplesRecorded; i++)
            {
                lastBuffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
            }
        }
예제 #16
0
        private static void ShowPeakMono(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            float maxValue       = 32767;
            int   peakValue      = 0;
            int   bytesPerSample = 2;

            for (int index = 0; index < args.BytesRecorded; index += bytesPerSample)
            {
                int value = BitConverter.ToInt16(args.Buffer, index);
                peakValue = Math.Max(peakValue, value);
            }

            Console.WriteLine("L=" + GetBars(peakValue / maxValue));
        }
예제 #17
0
                    void WI_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
                    {
                        float sum = 0;
                        int   g   = (int)(Math.Pow(2, Gain));

                        for (int n = 0; n < e.BytesRecorded; n += block)
                        {
                            for (int c = 0; c < Channels_in; c++)
                            {
                                short sample = (short)(BitConverter.ToInt16(e.Buffer, n + 2 * c) * g);
                                Response[c].Add(sample);
                                sum += sample;
                            }
                        }
                    }
예제 #18
0
파일: Form1.cs 프로젝트: wyb586/ScottPlot
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            int bytesPerSample  = wvin.WaveFormat.BitsPerSample / 8;
            int samplesRecorded = args.BytesRecorded / bytesPerSample;

            Int16[] lastBuffer = new Int16[samplesRecorded];
            if (dataPcm == null)
            {
                dataPcm = new double[samplesRecorded];
            }
            for (int i = 0; i < samplesRecorded; i++)
            {
                lastBuffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
                dataPcm[i]    = lastBuffer[i];
            }
            updateFFT();
        }
예제 #19
0
파일: Recorder.cs 프로젝트: lex4all/lex4all
 /// <summary>
 /// inout stream event for volume control
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void waveIn_DataAvailableVolume(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     // data is passed over to determine sound level and visualize it
     for (int index = 0; index < e.BytesRecorded; index += 2)
     {
         // transform each sample (16 bit: 2 bytes: 2 index steps) from the byte buffer
         short sample = (short)((e.Buffer[index + 1] << 8) |
                                e.Buffer[index + 0]);
         float sample32 = sample / 32768f;
         // passing over to visualization if certain amount of samples is reached
         if (aggregator.passSample(sample32) == 1)
         {
             // only pass over maximum or minimum
             ProcessSample(Math.Max(aggregator.MaxSample, Math.Abs(aggregator.MinSample)));
             aggregator.MaxSample = 0;
             aggregator.MinSample = 0;
         }
     }
 }
예제 #20
0
        private void OnNewAudioData(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            int bytesPerSample = wvin.WaveFormat.BitsPerSample / 8;
            int newSampleCount = args.BytesRecorded / bytesPerSample;

            double[] buffer = new double[newSampleCount];
            double   peak   = 0;

            for (int i = 0; i < newSampleCount; i++)
            {
                buffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
                peak      = Math.Max(peak, buffer[i]);
            }
            lock (audio)
            {
                audio.AddRange(buffer);
            }
            AmplitudeFrac = peak / (1 << 15);
            TotalSamples += newSampleCount;
        }
예제 #21
0
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            int bytesPerSample = wvin.WaveFormat.BitsPerSample / 8;

            float[] buffer = new float[args.BytesRecorded / bytesPerSample];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
            }
            lastAmplitudeFrac = (double)buffer.Max() / Int16.MaxValue;

            try
            {
                spec.AddCircular(buffer, fixedSize: pbSpec.Width);
            }
            catch (Exception ex)
            {
                Console.WriteLine("EXCEPTION: " + ex);
            }
        }
예제 #22
0
        private static void ShowPeakStereo(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            float maxValue       = 32767;
            int   peakL          = 0;
            int   peakR          = 0;
            int   bytesPerSample = 4;

            for (int index = 0; index < args.BytesRecorded; index += bytesPerSample)
            {
                int valueL = BitConverter.ToInt16(args.Buffer, index);
                peakL = Math.Max(peakL, valueL);
                int valueR = BitConverter.ToInt16(args.Buffer, index + 2);
                peakR = Math.Max(peakR, valueR);
            }

            Console.Write("L=" + GetBars(peakL / maxValue));
            Console.Write(" ");
            Console.Write("R=" + GetBars(peakR / maxValue));
            Console.Write("\n");
        }
예제 #23
0
        private void WaveIn_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            (new Thread(() =>
            {
                var sw = new Stopwatch();
                sw.Start();
                AudioHelpers.ByteArrayTo16BITInputFormat(ref receivedData, e.Buffer);
                var input = receivedData.Convert2ChannelsToFloat();

                if (processor.Model.InputSamplesCount != input.Count())
                {
                    processor.Model.InputSamplesCount = input.Count();
                }

                var result = processor.Process(input.ToArray());
                var note = NoteViewModel.GetNote(result);
                sw.Stop();
                Debug.WriteLine(string.Format("Note {0}\tTime elapsed {1}", string.Format(note.Tone.ToString(), note.Base), sw.Elapsed));
            })).Start();
        }
예제 #24
0
        private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            byte[] waveData = e.Buffer;

            int  globalMax = 0;
            int  globalMin = 0;
            long p;

            for (p = 0; p < waveData.Length; p += bytesPerChannel)
            {
                int val = BitConverter.ToInt16(waveData, (int)p);
                if (val < globalMin)
                {
                    globalMin = val;
                }
                if (val > globalMax)
                {
                    globalMax = val;
                }
            }

            globalMin = -globalMin;
            if (globalMin > globalMax)
            {
                globalMax = globalMin;
            }

            int l = (int)((globalMax * 100) / (32768 >> level));

            if (l > 100)
            {
                l = 100;
            }
            if (l < 0)
            {
                l = 0;
            }
            audioLevel.Value = l;
        }
예제 #25
0
파일: Audio.cs 프로젝트: BSalita/Woundify
        private static void WaveIn_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            if (Options.options.debugLevel >= 4)
            {
                Log.WriteLine("Received buffer: length=" + e.BytesRecorded.ToString());
            }
            else
            {
                Console.Write("*");
            }
            // can't derive waveInFile from WaveInEvent so must use class property

            // add received wave audio to waveProvider buffer
            //if (waveInProvider != null)
            //waveInProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);

            // add received wave audio to memory stream
            if (waveInFile != null)
            {
                waveInFile.Write(e.Buffer, 0, e.BytesRecorded);
                waveInFile.Flush();
            }
        }
        private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            float avg = 0;
            float sum = 0;

            NAudio.Wave.WaveBuffer buff = new NAudio.Wave.WaveBuffer(args.Buffer);
            // interpret as 32 bit floating point audio
            for (int index = 0; index < args.BytesRecorded / 4; index++)
            {
                short sample = buff.ShortBuffer[index];

                // absolute value
                if (sample < 0)
                {
                    sample = (short)-sample;
                }
                // is this the max value?
                sum += sample;
            }

            avg = sum / (args.BytesRecorded / 4);

            Amani.Update(avg, slider_sensitivity.Value);
        }
예제 #27
0
 private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     waveWriter.Write(e.Buffer, 0, e.BytesRecorded); // LAST BYTES ARE NOT RECORDED.
     waveWriter.Flush();
 }
예제 #28
0
 public void OnRecorded(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     localClient.SendVoiceMessage(e.Buffer);
 }
예제 #29
0
 private void Capture_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     _waveFileWriter?.Write(e.Buffer, 0, e.BytesRecorded);
 }
예제 #30
0
 private void Wi_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
 {
     writer.Write(e.Buffer, 0, e.BytesRecorded);
     writer.Flush();
 }